当前位置:   article > 正文

openlayers操控图斑的crud增删查改的整套代码_利用openlayers实现图斑的切割 合并 整形

利用openlayers实现图斑的切割 合并 整形

目录

1 增加图斑

2 更新图斑

3 删除图斑

4 执行的共同方法


1 增加图斑

测试代码发现 插入图斑其图形经纬度是正常的经纬度插入 图形更新他妈就变成需要纬经度,所以涉及一个applyTransform 调换经纬度坐标,以符合wfs协议中经纬度的位置

  1. <template>
  2. <!--地图挂载dom-->
  3. <div id="map">
  4. </div>
  5. </template>
  6. <script>
  7. //引入依赖
  8. import { ImageWMS, TileWMS } from 'ol/source'
  9. import MultiPolygon from 'ol/geom/MultiPolygon.js';
  10. import { Feature, Map, View, geom } from 'ol';
  11. import XYZ from 'ol/source/XYZ';
  12. import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';
  13. import { Vector as VectorSource } from 'ol/source'
  14. import { Image as ImageLayer, Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
  15. import { Polygon } from 'ol/geom'
  16. import * as turf from '@turf/turf';
  17. import { GeoJSON, WFS, GML } from 'ol/format';
  18. import turfData from '../data/turfIntersectData';
  19. import { Draw, Select, Modify } from 'ol/interaction';
  20. export default {
  21. data() {
  22. return {
  23. map: null
  24. }
  25. },
  26. mounted() {
  27. let _this = this;
  28. //初始化地图
  29. this.map = new Map({
  30. target: 'map',//指定挂载dom,注意必须是id
  31. layers: [
  32. new ImageLayer({
  33. opacity: 0.1, //图层透明度
  34. source: new ImageWMS({
  35. ratio: 1,
  36. url: 'http://192.168.10.17:8080/geoserver/custom/wms',
  37. params: {
  38. 'FORMAT': 'image/png',
  39. 'VERSION': '1.1.1',
  40. "STYLES": '',
  41. "LAYERS": 'custom:JGSQGW_Geo',
  42. "exceptions": 'application/vnd.ogc.se_inimage',
  43. }
  44. })
  45. }),
  46. ],
  47. //配置视图
  48. view: new View({
  49. //--start-#视图中心位置#-->
  50. center: [115.794692, 28.621057],
  51. //--start-#指定坐标系,4326为wgs84地理坐标系统#-->4490
  52. projection: "EPSG:4326",
  53. zoom: 12
  54. })
  55. });
  56. this.map.getView().fit([115.77521, 28.18268,
  57. 115.79453, 28.20572], this.map.getSize());
  58. // var url1 = "http://localhost:8090/geoserver/HT/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=HT%3Acrud_zhen&maxFeatures=50&outputFormat=application%2Fjson"
  59. var polygonSource = new VectorSource({
  60. url:
  61. "http://192.168.10.162:8080/geoserver/wfs?" +
  62. "service=WFS&" +
  63. "version=1.1.0&" +
  64. "request=GetFeature&" +
  65. "typeNames= custom:JGSQGW_Geo&" +
  66. "outputFormat=json&" +
  67. "srsname=EPSG:4490",
  68. format: new GeoJSON({
  69. geometryName: "geom" // postgis的空间存储字段名
  70. })
  71. });
  72. var polygonLayer = new VectorLayer({
  73. source: polygonSource,
  74. style: new Style({
  75. stroke: new Stroke({
  76. color: '#0000ff',
  77. width: 2
  78. })
  79. })
  80. });
  81. this.map.addLayer(polygonLayer);
  82. // 绘制控件
  83. let drawedFeatures = [];
  84. let draw = new Draw({
  85. source: polygonLayer.getSource(),
  86. type: 'MultiPolygon',
  87. geometryName: 'geom' // 注意:这里图形信息字段一定要和后端服务器一致
  88. });
  89. draw.on('drawend', (event) => {
  90. console.log("drawend", event.feature.getGeometry().getCoordinates());
  91. let ft = new Feature();
  92. // let properties = polygonLayer.getSource().getFeatures()[0].getProperties();
  93. //delete properties.geom;
  94. let properties = {};
  95. properties.gridone = '大哥向东12';
  96. properties.gridtwo = '388向东12';
  97. ft.setProperties(properties);
  98. let geom = event.feature.getGeometry().clone();
  99. // 调换经纬度坐标,以符合wfs协议中经纬度的位置
  100. // geom.applyTransform((flatCoordinates, flatCoordinates2, stride) => {
  101. // for (var j = 0; j < flatCoordinates.length; j += stride) {
  102. // var y = flatCoordinates[j]
  103. // var x = flatCoordinates[j + 1]
  104. // flatCoordinates[j] = x
  105. // flatCoordinates[j + 1] = y
  106. // }
  107. // })
  108. console.log("geom", geom.getCoordinates());
  109. ft.setGeometryName('geom');// 一定要放在setGemetry之前
  110. ft.setGeometry(geom);
  111. _this.transact_crud("insert", ft, "www.custom.com", "custom", ["JGSQGW_Geo"], "EPSG:4490");
  112. // let ft = new Feature();
  113. // let properties = polygonLayer.getSource().getFeatures()[0].getProperties();
  114. // delete properties.geom;
  115. // properties.gridone = '大哥向东';
  116. // properties.gridtwo = '388向东';
  117. // ft.setProperties(properties);
  118. // let geom = event.feature.getGeometry().clone()
  119. // let feature2 = event.feature;
  120. // // console.log("drawend",feature.getGeometry().getCoordinates());
  121. // console.log("drawend", feature);
  122. // // var feature1 = new Feature({
  123. // // geom: new ol.geom.MultiPolygon([antitone])
  124. // // });
  125. // console.log(event.feature.getGeometry().getCoordinates());
  126. // var points = event.feature.getGeometry().getCoordinates();
  127. // var antitone = [[]];
  128. // points[0].map(function (val, index) {
  129. // //var point = new proj.toLonLat(val);
  130. // // antitone[0].push([point[1], point[0]]);
  131. // antitone[0].push([val[1], val[0]]);
  132. // });
  133. // // let ft = new Feature()
  134. // // ft.setGeometryName('geom')
  135. // // ft.setGeometry(new MultiPolygon([antitone]));
  136. // // ft.set("gridone", "大哥向东"); //这里可以设置其他属性
  137. // var feature = new Feature({
  138. // // geometry: new MultiPolygon([antitone]),
  139. // geometry: new MultiPolygon(points),
  140. // //geometry: new Polygon([antitone])
  141. // });
  142. // let properties = polygonLayer.getSource().getFeatures()[0].getProperties()
  143. // console.log("properties", properties);
  144. // delete properties.geom;
  145. // console.log("properties2", properties);
  146. // properties.gridone = '大哥向东';
  147. // properties.gridtwo = '小弟向下';
  148. // ft.setProperties(properties)
  149. // let geom = feature.getGeometry().clone()
  150. // ft.setGeometryName('geom') // 一定要放在setGemetry之前
  151. // ft.setGeometry(geom)
  152. // //为要素添加属性,我这里随便设置属性
  153. // feature.set('gridone', '大哥向东');
  154. // // // let ft = new ol.Feature();
  155. // // // let ft = new Feature();
  156. // feature.set('gridtwo', '小弟向下');
  157. // console.log("ft_feature", feature);
  158. // let properties = polygonLayer.getSource().getFeatures()[0].getProperties();
  159. // //let geom = feature.getGeometry()
  160. // properties.type = 'road'
  161. // properties.type1 = 'road1'
  162. // delete properties.type1
  163. // console.log("properties", properties);
  164. // console.log("properties222", polygonLayer.getSource().getFeatures()[0]);
  165. // drawedFeatures.push(event.feature);
  166. // console.log("event.feature", event.feature);
  167. // _this.addFeatureWFS(drawedFeatures);
  168. // _this.transact_crud("insert", event.feature, "www.custom.com", "custom", ["JGSQGW_Geo"], "EPSG:4490");
  169. // _this.transact_crud("insert", ft, "www.custom.com", "custom", ["JGSQGW_Geo"], "EPSG:4490");
  170. });
  171. this.map.addInteraction(draw);
  172. draw.setActive(true);
  173. },
  174. computed: {
  175. },
  176. methods: {
  177. // transType为增删改类型
  178. // feat为要增删改的要素
  179. transact_crud(transType, feat, featureNS, featurePrefix, featureType, srsName) {
  180. if (featureNS == "" || featurePrefix == "" || featureType == "" || srsName == "") {
  181. return;
  182. }
  183. var formatWFS = new WFS();
  184. var formatGML = new GML({
  185. featureNS,
  186. featurePrefix,
  187. featureType,
  188. gmlOptions: { srsName }
  189. });
  190. let transact_xml = null;
  191. switch (transType) {
  192. case "insert":
  193. transact_xml = formatWFS.writeTransaction([feat], null, null, formatGML);
  194. break;
  195. case "update":
  196. transact_xml = formatWFS.writeTransaction(null, [feat], null, formatGML);
  197. break;
  198. case "delete":
  199. transact_xml = formatWFS.writeTransaction(null, null, [feat], formatGML);
  200. break;
  201. }
  202. let transact_str = (new XMLSerializer()).serializeToString(transact_xml);
  203. // 使用Fetch将请求发送到后端
  204. fetch('http://192.168.10.162:8080/geoserver/wfs', {
  205. method: 'POST',
  206. body: transact_str,
  207. headers: {
  208. 'Content-Type': 'text/xml'
  209. }
  210. }).then(res => res.text()).then(res => {
  211. // let transactRes = WFS.readTransactionResponse(res);
  212. // let str = transactRes.transactionSummary.totalInserted +
  213. // " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  214. // str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  215. // str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  216. // alert(str);
  217. });
  218. },
  219. addFeatureWFS(features) {
  220. let WFSGML = new WFS();
  221. // 生成XML格式的WFS请求信息
  222. let transact_xml = WFSGML.writeTransaction(features, null, null, {
  223. srcName: 'EPSG:4490',
  224. featureNS: "www.custom.com",
  225. featurePrefix: "custom",
  226. featureType: ["JGSQGW_Geo"],
  227. }
  228. );
  229. // 将XML格式请求信息序列化为字符串格式
  230. let transact_str = (new XMLSerializer()).serializeToString(transact_xml);
  231. // 使用Fetch将请求发送到后端
  232. fetch('http://192.168.10.162:8080/geoserver/wfs', {
  233. method: 'POST',
  234. body: transact_str,
  235. headers: {
  236. 'Content-Type': 'text/xml'
  237. }
  238. }).then(res => res.text()).then(res => {
  239. let transactRes = WFS.readTransactionResponse(res);
  240. let str = transactRes.transactionSummary.totalInserted +
  241. " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  242. str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  243. str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  244. alert(str);
  245. });
  246. },
  247. // feat为要增删改的要素
  248. // layerName为要进行操作的目标图层名称
  249. transact(feature, layerName) {
  250. if (layerName == "") {
  251. return;
  252. }
  253. // 将选中的要素序列化为字符串格式
  254. var formatWFS = new WFS();
  255. var formatGML = new GML({
  256. featureNS: "www.custom.com",
  257. featurePrefix: "custom",
  258. featureType: ["JGSQGW_Geo"],
  259. gmlOptions: { srsName: "EPSG:4490" }
  260. });
  261. let transact_str = formatWFS.writeTransaction(null, null, [feature], formatGML);
  262. fetch('http://192.168.10.162:8080/geoserver/wfs', {
  263. method: 'POST',
  264. body: new XMLSerializer().serializeToString(featureRequest),
  265. headers: {
  266. 'Content-Type': 'text/xml'
  267. }
  268. }).then((response) => {
  269. console.log("response", response);
  270. return response.text();
  271. })
  272. .then((res) => {
  273. let transactRes = formatWFS.readTransactionResponse(res);
  274. let str = transactRes.transactionSummary.totalInserted +
  275. " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  276. str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  277. str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  278. alert(str);
  279. });
  280. },
  281. },
  282. }
  283. </script>
  284. <style scoped>
  285. #map {
  286. width: 100%;
  287. height: 100%;
  288. }
  289. </style>

2 更新图斑

  1. <template>
  2. <!--地图挂载dom-->
  3. <div id="map">
  4. <div class="btn">
  5. <el-button type="success" @click="updateFeature">111</el-button>
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. //引入依赖
  11. import { ImageWMS, TileWMS } from 'ol/source'
  12. import { Feature, Map, View } from 'ol';
  13. import XYZ from 'ol/source/XYZ';
  14. import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';
  15. import { Vector as VectorSource } from 'ol/source'
  16. import { Image as ImageLayer, Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
  17. import { Polygon } from 'ol/geom'
  18. import * as turf from '@turf/turf';
  19. import { GeoJSON, WFS, GML } from 'ol/format';
  20. import turfData from '../data/turfIntersectData';
  21. import { Draw, Select, Modify } from 'ol/interaction';
  22. export default {
  23. data() {
  24. return {
  25. map: null,
  26. modifiedFeatures: []
  27. }
  28. },
  29. mounted() {
  30. let _this = this;
  31. //初始化地图
  32. this.map = new Map({
  33. target: 'map',//指定挂载dom,注意必须是id
  34. layers: [
  35. new ImageLayer({
  36. opacity: 0.1, //图层透明度
  37. source: new ImageWMS({
  38. ratio: 1,
  39. url: 'http://192.168.10.162:8080/geoserver/custom/wms',
  40. params: {
  41. 'FORMAT': 'image/png',
  42. 'VERSION': '1.1.1',
  43. "STYLES": '',
  44. "LAYERS": 'custom:JGSQGW_Geo',
  45. "exceptions": 'application/vnd.ogc.se_inimage',
  46. }
  47. })
  48. }),
  49. ],
  50. //配置视图
  51. view: new View({
  52. //--start-#视图中心位置#-->
  53. center: [115.794692, 28.621057],
  54. //--start-#指定坐标系,4326为wgs84地理坐标系统#-->4490
  55. projection: "EPSG:4326",
  56. zoom: 12
  57. })
  58. });
  59. this.map.getView().fit([115.77521, 28.18268,
  60. 115.79453, 28.20572], this.map.getSize());
  61. // var url1 = "http://localhost:8090/geoserver/HT/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=HT%3Acrud_zhen&maxFeatures=50&outputFormat=application%2Fjson"
  62. var polygonSource = new VectorSource({
  63. url:
  64. "http://192.168.10.162:8080/geoserver/wfs?" +
  65. "service=WFS&" +
  66. "version=1.1.0&" +
  67. "request=GetFeature&" +
  68. "typeNames= custom:JGSQGW_Geo&" +
  69. "outputFormat=json&" +
  70. "srsname=EPSG:4490",
  71. format: new GeoJSON({
  72. geometryName: "geom" // postgis的空间存储字段名
  73. })
  74. });
  75. var polygonLayer = new VectorLayer({
  76. source: polygonSource,
  77. style: new Style({
  78. stroke: new Stroke({
  79. color: '#0000ff',
  80. width: 2
  81. })
  82. })
  83. });
  84. this.map.addLayer(polygonLayer);
  85. // 选择要素控件
  86. let select = new Select({
  87. layers: [polygonLayer],
  88. hitTolerance: 10,
  89. style: new Style({
  90. stroke: new Stroke({
  91. color: 'red',
  92. width: 2
  93. })
  94. })
  95. });
  96. this.map.addInteraction(select);
  97. // 修改要素控件
  98. let modify = new Modify({
  99. features: select.getFeatures(),
  100. style: new Style({
  101. stroke: new Stroke({
  102. color: 'black',
  103. width: 2
  104. })
  105. }),
  106. });
  107. this.map.addInteraction(modify);
  108. modify.on("modifyend", function (e) {
  109. console.log("modifyend");
  110. var feature = e.features.item(0).clone();
  111. feature.setId(e.features.item(0).getId());
  112. var geomType = feature.getGeometry().getType().toLowerCase(); //openlayers绘制类型
  113. let geom = feature.getGeometry().getCoordinates();
  114. console.log("getCoordinates", geom);
  115. // 调换经纬度坐标,以符合wfs协议中经纬度的位置
  116. feature
  117. .getGeometry()
  118. .applyTransform(function (flatCoordinates, flatCoordinates2, stride) {
  119. for (var j = 0; j < flatCoordinates.length; j += stride) {
  120. var y = flatCoordinates[j];
  121. var x = flatCoordinates[j + 1];
  122. flatCoordinates[j] = x;
  123. flatCoordinates[j + 1] = y;
  124. }
  125. });
  126. console.log("getCoordinates2", feature.getGeometry().getCoordinates());
  127. _this.transact_crud("update", feature, "www.custom.com", "custom", "JGSQGW_Geo", "EPSG:4490");
  128. });
  129. },
  130. computed: {
  131. },
  132. methods: {
  133. updateFeature() {
  134. let _this = this;
  135. let features = [];
  136. this.modifiedFeatures.forEach(function (item, index, array) {
  137. console.log("item", item);
  138. features.push(item);
  139. //_this.updateWFS(features);
  140. _this.transact_crud("update", item, "www.custom.com", "custom", "JGSQGW_Geo", "EPSG:4490");
  141. });
  142. },
  143. updateWFS(features) {
  144. let WFSs = new WFS();
  145. // 生成XML格式的WFS请求信息
  146. let transact_xml = WFSs.writeTransaction(
  147. null, features, null,
  148. {
  149. srcName: 'EPSG:4490',
  150. featureNS: "www.custom.com",
  151. // featurePrefix: "custom",
  152. featureType: "JGSQGW_Geo",
  153. }
  154. )
  155. // 将XML格式请求信息序列化为字符串格式
  156. let transact_str = (new XMLSerializer()).serializeToString(transact_xml);
  157. // 使用Fetch将请求发送到后端
  158. fetch('http://192.168.10.162:8080/geoserver/wfs', {
  159. method: 'POST',
  160. body: transact_str,
  161. headers: {
  162. 'Content-Type': 'text/xml'
  163. }
  164. }).then(res => res.text()).then(res => {
  165. console.log(res);
  166. });
  167. }
  168. ,
  169. // transType为增删改类型
  170. // feat为要增删改的要素
  171. transact_crud(transType, feat, featureNS, featurePrefix, featureType, srsName) {
  172. if (featureNS == "" || featurePrefix == "" || srsName == "") {
  173. return;
  174. }
  175. var formatWFS = new WFS();
  176. var formatGML = new GML({
  177. featureNS,
  178. featurePrefix,
  179. featureType,
  180. gmlOptions: { srsName }
  181. });
  182. let transact_xml = null;
  183. switch (transType) {
  184. case "insert":
  185. transact_xml = formatWFS.writeTransaction([feat], null, null, formatGML);
  186. break;
  187. case "update":
  188. transact_xml = formatWFS.writeTransaction(null, [feat], null, formatGML);
  189. break;
  190. case "delete":
  191. transact_xml = formatWFS.writeTransaction(null, null, [feat], formatGML);
  192. break;
  193. }
  194. let transact_str = (new XMLSerializer()).serializeToString(transact_xml);
  195. // 使用Fetch将请求发送到后端
  196. fetch('http://192.168.10.162:8080/geoserver/wfs', {
  197. method: 'POST',
  198. body: transact_str,
  199. headers: {
  200. 'Content-Type': 'text/xml'
  201. }
  202. }).then(res => res.text()).then(res => {
  203. // let transactRes = WFS.readTransactionResponse(res);
  204. // let str = transactRes.transactionSummary.totalInserted +
  205. // " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  206. // str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  207. // str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  208. // alert(str);
  209. });
  210. },
  211. addFeatureWFS(features) {
  212. let WFSGML = new WFS();
  213. // 生成XML格式的WFS请求信息
  214. let transact_xml = WFSGML.writeTransaction(features, null, null, {
  215. srcName: 'EPSG:4490',
  216. featureNS: "www.custom.com",
  217. featurePrefix: "custom",
  218. featureType: ["JGSQGW_Geo"],
  219. }
  220. );
  221. // 将XML格式请求信息序列化为字符串格式
  222. let transact_str = (new XMLSerializer()).serializeToString(transact_xml);
  223. // 使用Fetch将请求发送到后端
  224. fetch('http://192.168.10.162:8080/geoserver/wfs', {
  225. method: 'POST',
  226. body: transact_str,
  227. headers: {
  228. 'Content-Type': 'text/xml'
  229. }
  230. }).then(res => res.text()).then(res => {
  231. let transactRes = WFS.readTransactionResponse(res);
  232. let str = transactRes.transactionSummary.totalInserted +
  233. " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  234. str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  235. str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  236. alert(str);
  237. });
  238. },
  239. // feat为要增删改的要素
  240. // layerName为要进行操作的目标图层名称
  241. transact(feature, layerName) {
  242. if (layerName == "") {
  243. return;
  244. }
  245. // 将选中的要素序列化为字符串格式
  246. var formatWFS = new WFS();
  247. var formatGML = new GML({
  248. featureNS: "www.custom.com",
  249. featurePrefix: "custom",
  250. featureType: ["JGSQGW_Geo"],
  251. gmlOptions: { srsName: "EPSG:4490" }
  252. });
  253. let transact_str = formatWFS.writeTransaction(null, null, [feature], formatGML);
  254. fetch('http://192.168.10.162:8080/geoserver/wfs', {
  255. method: 'POST',
  256. body: new XMLSerializer().serializeToString(featureRequest),
  257. headers: {
  258. 'Content-Type': 'text/xml'
  259. }
  260. }).then((response) => {
  261. console.log("response", response);
  262. return response.text();
  263. })
  264. .then((res) => {
  265. let transactRes = formatWFS.readTransactionResponse(res);
  266. let str = transactRes.transactionSummary.totalInserted +
  267. " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  268. str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  269. str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  270. alert(str);
  271. });
  272. },
  273. },
  274. }
  275. </script>
  276. <style scoped>
  277. #map {
  278. width: 100%;
  279. height: 100%;
  280. .btn {}
  281. }
  282. </style>

3 删除图斑

  1. <template>
  2. <!--地图挂载dom-->
  3. <div id="map">
  4. </div>
  5. </template>
  6. <script>
  7. //引入依赖
  8. import { ImageWMS, TileWMS } from 'ol/source'
  9. import { Feature, Map, View } from 'ol';
  10. import XYZ from 'ol/source/XYZ';
  11. import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';
  12. import { Vector as VectorSource } from 'ol/source'
  13. import { Image as ImageLayer, Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
  14. import { Polygon } from 'ol/geom'
  15. import * as turf from '@turf/turf';
  16. import { GeoJSON, WFS, GML } from 'ol/format';
  17. import turfData from '../data/turfIntersectData';
  18. import { Draw, Select } from 'ol/interaction';
  19. export default {
  20. data() {
  21. return {
  22. map: null
  23. }
  24. },
  25. mounted() {
  26. let _this = this;
  27. //初始化地图
  28. this.map = new Map({
  29. target: 'map',//指定挂载dom,注意必须是id
  30. layers: [
  31. new ImageLayer({
  32. opacity: 0.1, //图层透明度
  33. source: new ImageWMS({
  34. ratio: 1,
  35. url: 'http://192.168.10.162:8080/geoserver/custom/wms',
  36. params: {
  37. 'FORMAT': 'image/png',
  38. 'VERSION': '1.1.1',
  39. "STYLES": '',
  40. "LAYERS": 'custom:JGSQGW_Geo',
  41. "exceptions": 'application/vnd.ogc.se_inimage',
  42. }
  43. })
  44. }),
  45. ],
  46. //配置视图
  47. view: new View({
  48. //--start-#视图中心位置#-->
  49. center: [115.794692, 28.621057],
  50. //--start-#指定坐标系,4326为wgs84地理坐标系统#-->4490
  51. projection: "EPSG:4326",
  52. zoom: 12
  53. })
  54. });
  55. this.map.getView().fit([115.77521, 28.18268,
  56. 115.79453, 28.20572], this.map.getSize());
  57. // var url1 = "http://localhost:8090/geoserver/HT/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=HT%3Acrud_zhen&maxFeatures=50&outputFormat=application%2Fjson"
  58. var polygonSource = new VectorSource({
  59. url:
  60. "http://192.168.10.162:8080/geoserver/wfs?" +
  61. "service=WFS&" +
  62. "version=1.1.0&" +
  63. "request=GetFeature&" +
  64. "typeNames= custom:JGSQGW_Geo&" +
  65. "outputFormat=json&" +
  66. "srsname=EPSG:4490",
  67. format: new GeoJSON({
  68. geometryName: "geom" // postgis的空间存储字段名
  69. })
  70. });
  71. var polygonLayer = new VectorLayer({
  72. source: polygonSource
  73. });
  74. this.map.addLayer(polygonLayer);
  75. var select = new Select();
  76. this.map.addInteraction(select);
  77. // 选中要素后执行删除操作
  78. select.on("select", function (e) {
  79. // console.log("null11");
  80. if (select.getFeatures().getLength() == 0) {
  81. // console.log("null");
  82. } else {
  83. let featureId = e.target
  84. .getFeatures()
  85. .getArray()[0]
  86. .getId();
  87. var feature;
  88. // console.log("null", e.target.getFeatures());
  89. feature = polygonSource.getFeatureById(featureId);
  90. // 从地图删除
  91. polygonSource.removeFeature(feature);
  92. e.target.getFeatures().clear();
  93. // 从数据库删除
  94. _this.transact(feature, "JGSQGW_Geo");
  95. }
  96. });
  97. },
  98. computed: {
  99. },
  100. methods: {
  101. // feat为要增删改的要素
  102. // layerName为要进行操作的目标图层名称
  103. transact(feature, layerName) {
  104. if (layerName == "") {
  105. return;
  106. }
  107. // 将选中的要素序列化为字符串格式
  108. var formatWFS = new WFS();
  109. var formatGML = new GML({
  110. featureNS: "www.custom.com",
  111. featurePrefix: "custom",
  112. featureType: layerName,
  113. gmlOptions: { srsName: "EPSG:4490" }
  114. });
  115. var featureRequest = formatWFS.writeTransaction(null, null, [feature], formatGML);
  116. // var s = new XMLSerializer();
  117. // str = s.serializeToString(featureRequest);
  118. // 发送post请求获取图层要素
  119. // var aaa = '<?xml version="1.0" encoding="UTF-8"?><wfs:TransactionResponse xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wfs="http://www.opengis.net/wfs" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://192.168.10.17:8080/geoserver/schemas/wfs/1.1.0/wfs.xsd"><wfs:TransactionSummary><wfs:totalInserted>0</wfs:totalInserted><wfs:totalUpdated>0</wfs:totalUpdated><wfs:totalDeleted>1</wfs:totalDeleted></wfs:TransactionSummary><wfs:TransactionResults/><wfs:InsertResults><wfs:Feature><ogc:FeatureId fid="none"/></wfs:Feature></wfs:InsertResults></wfs:TransactionResponse>';
  120. // console.log("aaa", new XMLSerializer().serializeToString(aaa));
  121. fetch('http://192.168.10.17:8080/geoserver/wfs', {
  122. method: 'POST',
  123. body: new XMLSerializer().serializeToString(featureRequest),
  124. headers: {
  125. 'Content-Type': 'text/xml'
  126. }
  127. }).then((response) => {
  128. console.log("response", response);
  129. return response.text();
  130. })
  131. .then((res) => {
  132. let transactRes = formatWFS.readTransactionResponse(res);
  133. let str = transactRes.transactionSummary.totalInserted +
  134. " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  135. str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  136. str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  137. alert(str);
  138. const layers = this.map.getLayers().getArray();
  139. if (layers.length > 0) {
  140. layers.forEach((item) => {
  141. item.getSource().refresh(); //这句代码
  142. });
  143. }
  144. });
  145. },
  146. },
  147. }
  148. </script>
  149. <style scoped>
  150. #map {
  151. width: 100%;
  152. height: 100%;
  153. }
  154. </style>

4 执行的共同方法

  1. transact_crud(transType, feat, featureNS, featurePrefix, featureType, srsName) {
  2. if (featureNS == "" || featurePrefix == "" || featureType == "" || srsName == "") {
  3. return;
  4. }
  5. var formatWFS = new WFS();
  6. var formatGML = new GML({
  7. featureNS,
  8. featurePrefix,
  9. featureType,
  10. gmlOptions: { srsName }
  11. });
  12. let transact_xml = null;
  13. switch (transType) {
  14. case "insert":
  15. transact_xml = formatWFS.writeTransaction([feat], null, null, formatGML);
  16. break;
  17. case "update":
  18. transact_xml = formatWFS.writeTransaction(null, [feat], null, formatGML);
  19. break;
  20. case "delete":
  21. transact_xml = formatWFS.writeTransaction(null, null, [feat], formatGML);
  22. break;
  23. }
  24. let transact_str = (new XMLSerializer()).serializeToString(transact_xml);
  25. // 使用Fetch将请求发送到后端
  26. fetch('http://192.168.10.162:8080/geoserver/wfs', {
  27. method: 'POST',
  28. body: transact_str,
  29. headers: {
  30. 'Content-Type': 'text/xml'
  31. }
  32. }).then(res => res.text()).then(res => {
  33. // let transactRes = WFS.readTransactionResponse(res);
  34. // let str = transactRes.transactionSummary.totalInserted +
  35. // " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  36. // str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  37. // str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  38. // alert(str);
  39. });
  40. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/717125
推荐阅读
相关标签
  

闽ICP备14008679号