农经大屏
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

862 linhas
22 KiB

  1. import { getCenterByExtent } from '@/utils/common.js';
  2. import {
  3. Map,
  4. View,
  5. Collection,
  6. Feature
  7. } from 'ol';
  8. import WFS from 'ol/format/WFS.js';
  9. import {
  10. transform,
  11. fromLonLat
  12. } from 'ol/proj';
  13. import { Point } from 'ol/geom';
  14. import TileLayer from 'ol/layer/Tile';
  15. import XYZ from 'ol/source/XYZ';
  16. import {
  17. Style,
  18. Stroke,
  19. Fill,
  20. Text,
  21. Circle,
  22. Icon
  23. } from 'ol/style';
  24. import { extend, createEmpty } from 'ol/extent';
  25. import VectorLayer from 'ol/layer/Vector';
  26. import Image from 'ol/layer/Image';
  27. import ImageWMS from 'ol/source/ImageWMS.js';
  28. import VectorSource from 'ol/source/Vector';
  29. import TileWMS from 'ol/source/TileWMS';
  30. import GeoJSON from 'ol/format/GeoJSON';
  31. import { get as getProjection } from 'ol/proj';
  32. import { getWidth, getTopLeft } from 'ol/extent';
  33. import WMTSTileGrid from 'ol/tilegrid/WMTS';
  34. import WKT from 'ol/format/WKT';
  35. import WMTS from 'ol/source/WMTS';
  36. import { Overlay } from 'ol';
  37. const pointVectorSource = new VectorSource();
  38. import PluggableMap from 'ol/PluggableMap';
  39. import { MousePosition, defaults } from 'ol/control';
  40. import { createStringXY } from 'ol/coordinate';
  41. import { Draw, Modify } from 'ol/interaction';
  42. import { get, post } from '@/api/index.js';
  43. import { MultiPolygon } from 'ol/geom';
  44. class GisUtils {
  45. map = null;
  46. overLayer = null;
  47. instance = null;
  48. constructor(domId) {
  49. this.getMap(domId)
  50. this.initView()
  51. return this
  52. }
  53. /**
  54. * 获取一个地图容器
  55. * @param {string} domId dom节点id
  56. */
  57. getMap (domId) {
  58. let view = new View({
  59. projection: 'EPSG:3857'
  60. });
  61. PluggableMap.prototype.getEventPixel = function (event) {
  62. // eslint-disable-next-line no-underscore-dangle
  63. const viewportPosition = this.viewport_.getBoundingClientRect();
  64. let size = [viewportPosition.width, viewportPosition.height];
  65. const view = this.getView();
  66. if (view) {
  67. size = view.getSizeFromViewport_();
  68. }
  69. const eventPosition =
  70. 'changedTouches' in event /** @type {TouchEvent} */ ?
  71. event.changedTouches[0] /** @type {MouseEvent} */ :
  72. event;
  73. return [
  74. (eventPosition.clientX - viewportPosition.left) * size[0] /
  75. viewportPosition.width,
  76. (eventPosition.clientY - viewportPosition.top) * size[1] /
  77. viewportPosition.height
  78. ];
  79. };
  80. let mousePositionControl = new MousePosition({
  81. coordinateFormat: createStringXY(4),
  82. projection: 'EPSG:3857',
  83. className: 'custom-mouse-position',
  84. target: document.getElementById('mouse-position'),
  85. undefinedHTML: ' '
  86. });
  87. let map = new Map({
  88. target: domId,
  89. view: view,
  90. controls: defaults({
  91. zoom: false,
  92. rotate: false,
  93. attribution: false,
  94. attributionOptions: {
  95. collapsible: false
  96. }
  97. }).extend([mousePositionControl]),
  98. layers: [
  99. ]
  100. });
  101. this.map = map;
  102. return this.map;
  103. }
  104. /**
  105. * 获取适量图层
  106. * @param {*} color
  107. * @returns
  108. */
  109. getVectorLayerByColor (color) {
  110. let layer = new VectorLayer({
  111. renderMode: 'webgl', // 使用WebGL渲染器
  112. source: new VectorSource(),
  113. zIndex: 101,
  114. style: new Style({
  115. fill: new Fill({
  116. color: color
  117. }),
  118. stroke: new Stroke({
  119. color: color,
  120. width: 2
  121. })
  122. })
  123. });
  124. this.map.addLayer(layer);
  125. return layer;
  126. }
  127. getVectorLayerByFs (features, zIndex = 1012) {
  128. const vectorSource = new VectorSource({
  129. features: features,
  130. });
  131. // 创建矢量图层
  132. let layer = new VectorLayer({
  133. zIndex,
  134. source: vectorSource
  135. });
  136. this.map.addLayer(layer)
  137. return layer
  138. }
  139. addLayer (layer) {
  140. this.map.addLayer(layer);
  141. }
  142. getPolygon (coordinates) {
  143. const polygon = new MultiPolygon([coordinates]);
  144. return polygon;
  145. }
  146. getFeature (info, icon) {
  147. let feature = new Feature({
  148. geometry: new Point(
  149. fromLonLat([info.lng, info.lat])
  150. ),
  151. deptId: info.id,
  152. deptName: info.label,
  153. orgCode: info.orgCode,
  154. lat: info.lat,
  155. lng: info.lng,
  156. deptLevel: info.deptLevel,
  157. level: 'deptPoint',
  158. })
  159. feature.setStyle(this.createFeatureStyle(feature, icon));
  160. return feature;
  161. }
  162. getFeature2 (info, icon) {
  163. let feature = new Feature({
  164. geometry: new Point(
  165. JSON.parse(info.theGeom).coordinates
  166. ),
  167. detail: info,
  168. level: 'permanent',
  169. })
  170. feature.setStyle(this.createFeatureStyle(feature, icon));
  171. return feature;
  172. }
  173. getFeature3 (item, fill, stroke) {
  174. let feature = new Feature({
  175. geometry: new MultiPolygon(JSON.parse(item.theGeom).coordinates),
  176. // name: item.centerName,
  177. // centerDeptId: item.deptId,
  178. level: 'resource',
  179. detail: item
  180. })
  181. feature.setStyle(this.createMultiPolygonFeatureStyle(fill, stroke));
  182. return feature;
  183. }
  184. createMultiPolygonFeatureStyle (fill, stroke) {
  185. return new Style({
  186. fill: new Fill({
  187. //矢量图层填充颜色,以及透明度
  188. color: fill,
  189. }),
  190. stroke: new Stroke({
  191. //边界样式
  192. color: stroke,
  193. width: 2,
  194. })
  195. });
  196. }
  197. // 设置坐标点的样式
  198. createFeatureStyle (feature, locationIcon) {
  199. return new Style({
  200. image: new Icon({
  201. //设置图标偏移
  202. anchor: [0.5, 1],
  203. //标注样式的起点位置
  204. anchorOrigin: "top-right",
  205. //X方向单位:分数
  206. anchorXUnits: "fraction",
  207. //Y方向单位:像素
  208. anchorYUnits: "pixels",
  209. //偏移起点位置的方向
  210. offsetOrigin: "top-right",
  211. //透明度
  212. opacity: 0.9,
  213. // rotation: (Math.PI / 180) * 20,
  214. //图片路径
  215. src: locationIcon,
  216. }),
  217. text: new Text({
  218. textAlign: "center", //位置
  219. textBaseline: "bottom", //基准线
  220. font: "normal 14px 微软雅黑", //文字样式
  221. text: feature.get("deptName"), //文本内容
  222. fill: new Fill({
  223. //文本填充样式(即文字颜色)
  224. color: "#ffff00",
  225. }),
  226. })
  227. });
  228. }
  229. writeFeature (fs) {
  230. return new GeoJSON().writeFeature(fs);
  231. }
  232. addModify (layer) {
  233. const modify = new Modify({
  234. features: new Collection(layer.getSource().getFeatures()),
  235. pixelTolerance: 4
  236. });
  237. return modify;
  238. }
  239. drawLineSplit (source) {
  240. let draw = new Draw({
  241. source: source,
  242. type: "LineString",
  243. style: new Style({
  244. image: new Circle({
  245. radius: 5,
  246. fill: new Fill({
  247. color: "#03a9f4"
  248. })
  249. }),
  250. stroke: new Stroke({
  251. color: "#03a9f4",
  252. width: 2
  253. }),
  254. fill: new Fill({
  255. color: "rgba(255, 255, 255, 0.7)"
  256. })
  257. })
  258. });
  259. return draw;
  260. }
  261. getMapContainer () {
  262. return this.map;
  263. }
  264. /**
  265. * 键入弹窗
  266. * @param {*} el dom
  267. * @param {*} coord 位置
  268. */
  269. addOverlay (el, coord) {
  270. let overLayer = new Overlay({
  271. element: el, // 挂载元素
  272. position: coord,
  273. className: 'overLay',
  274. positioning: 'top-center',
  275. autoPanAnimation: {
  276. duration: 250
  277. },
  278. stopEvent: true
  279. });
  280. this.map.addOverlay(overLayer);
  281. this.overLayer = overLayer;
  282. }
  283. /**
  284. * 移除弹窗
  285. */
  286. removeOverlay () {
  287. if (this.overLayer) {
  288. this.map.addOverlay(this.overLayer);
  289. }
  290. }
  291. /**
  292. * 飞入动画
  293. */
  294. flyTo (duration = 2000, position = [0, 0]) {
  295. this.map.getView().animate({
  296. center: position,
  297. duration: duration
  298. });
  299. }
  300. /**
  301. *
  302. * @param {*} evt 事件
  303. * @param {*} layerName 图层名称
  304. * @param {*} owsUrl 请求路径
  305. */
  306. async getFeatureByEvt (evt, layerName, owsUrl) {
  307. let point = new Point([evt.coordinate[0], evt.coordinate[1]]);
  308. let cql_filter = `INTERSECTS (the_geom,${new WKT().writeGeometry(point)})`;
  309. const params = this.getWFSparams(layerName, cql_filter);
  310. const feature = await this.getFeaturesByWFS(owsUrl, params);
  311. return feature;
  312. }
  313. /**
  314. * 获取一个视图
  315. * @param {array} extent 四至
  316. * @param {int} zoom 缩放级别
  317. * @param {int} minZoom 最小缩放级别
  318. * @param {int} maxZoom 最大缩放级别
  319. */
  320. initView (extent = [100, 100, 100, 100], zoom = 4, minZoom = 3, maxZoom = 18.4) {
  321. let view = new View({
  322. center: getCenterByExtent(extent),
  323. zoom,
  324. minZoom,
  325. maxZoom,
  326. projection: 'EPSG:3857'
  327. });
  328. this.map.setView(view);
  329. return view;
  330. }
  331. getView () {
  332. return this.map.getView()
  333. }
  334. /**
  335. * 获取WMS图层
  336. * @param {*} wmsurl wms路径
  337. * @param {*} layerName 图层名
  338. * @param {*} isShow 是否隐藏
  339. */
  340. getWMSLayer (wmsurl, layerName, isShow = false, zIndex = 50) {
  341. let layer = new TileLayer({
  342. visible: isShow,
  343. zIndex,
  344. source: new TileWMS({
  345. url: wmsurl,
  346. params: { 'LAYERS': layerName, 'TILED': true, SRID: 4326 },
  347. transition: 0
  348. })
  349. });
  350. this.map.addLayer(layer);
  351. return layer;
  352. }
  353. /**
  354. * 获取天地图图层
  355. * @param {string} url 最小缩放级别
  356. */
  357. addTianDiTuLayer (visible = true) {
  358. let tianDitTu = new TileLayer({
  359. visible,
  360. source: new XYZ({
  361. url: 'http://t{0-7}.tianditu.com/img_w/wmts?' + 'SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles' + '&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=cc4aba6e967096098249efa069733067'
  362. })
  363. });
  364. this.map.addLayer(tianDitTu);
  365. return tianDitTu;
  366. }
  367. /**
  368. * 获取天地图图层
  369. * @param {string} url 最小缩放级别
  370. */
  371. addAnnotationLayer (visible = true) {
  372. let tianDitTu = new TileLayer({
  373. visible,
  374. source: new XYZ({
  375. url: 'http://t{0-7}.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=cc4aba6e967096098249efa069733067'
  376. })
  377. });
  378. this.map.addLayer(tianDitTu);
  379. return tianDitTu;
  380. }
  381. addImageLayer (mapGeoServerUrl, layerName, deptIds) {
  382. let layer = new Image({
  383. source: new ImageWMS({
  384. url: mapGeoServerUrl + '/wms',
  385. params: {
  386. LAYERS: layerName,
  387. cql_filter: "dept_id in (" + deptIds + ")",
  388. SRID: 3857,
  389. }
  390. }),
  391. name: 'countyBorderLayer'
  392. });
  393. this.map.addLayer(layer)
  394. return layer
  395. }
  396. /**
  397. * 获取wfs服务请求参数
  398. * @param {string} typename 请求的图层名
  399. * @param {string} cql cql语句
  400. * @return {obj} params wfs 的请求参数
  401. */
  402. getWFSparams (typename, cql = null) {
  403. var params = {
  404. srsName: 'EPSG:4326',
  405. service: 'WFS',
  406. version: '1.0.0',
  407. request: 'GetFeature',
  408. typename,
  409. outputFormat: 'application/json'
  410. };
  411. if (cql) {
  412. params.CQL_FILTER = cql;
  413. }
  414. return params;
  415. }
  416. getVectorSource () {
  417. let source = new VectorSource();
  418. return source;
  419. }
  420. getVectorLayer () {
  421. let layer = new VectorLayer({
  422. editable: true, // 确保图层是可编辑的
  423. source: new VectorSource(),
  424. zIndex: 101,
  425. style: new Style({
  426. fill: new Fill({
  427. color: [255, 80, 80, 0.5]
  428. }),
  429. stroke: new Stroke({
  430. color: [255, 80, 80, 1],
  431. width: 2
  432. })
  433. })
  434. });
  435. this.map.addLayer(layer);
  436. return layer;
  437. }
  438. /**
  439. * 根据描边颜色获取新的要素集合
  440. * @param {string} strokeColor 描边颜色
  441. * @param {array} fsList 要素数组
  442. */
  443. getStrokeStyle (fsList, strokeColor = '#00FEFA', textName = 'NAME') {
  444. const arr = fsList.map(item => {
  445. item.setStyle(
  446. new Style({
  447. stroke: new Stroke({
  448. color: strokeColor,
  449. width: 1
  450. }),
  451. text: new Text({
  452. text: item.values_[textName],
  453. overflow: true,
  454. font: 'bold 11px serif',
  455. fill: new Fill({
  456. color: strokeColor
  457. })
  458. })
  459. })
  460. );
  461. return item;
  462. });
  463. return arr;
  464. }
  465. /**
  466. * 根据要素集合设置填充样式
  467. * @param {array} fsList 要素集合
  468. * @param {*} fillColor 填充色
  469. * @returns 返回矢量图层
  470. */
  471. getFillStyle (fsList, fillColor = '#00FEFA', testName = 'XZQHMC') {
  472. fsList.forEach(item => {
  473. item.setStyle(
  474. new Style({
  475. stroke: new Stroke({
  476. color: fillColor,
  477. width: 2
  478. }),
  479. text: new Text({
  480. text: item.values_[testName],
  481. overflow: true,
  482. font: 'bold 11px serif',
  483. fill: new Fill({
  484. color: '#00FEFA'
  485. })
  486. })
  487. })
  488. );
  489. });
  490. return fsList;
  491. }
  492. /**
  493. * 通过wfs服务拿到要素
  494. * @param {string} url 请求的路径
  495. * @param {obj} params 参数
  496. * @returns
  497. */
  498. async getFeaturesByWFS (url, params = {}) {
  499. const res = await get(url, params);
  500. const fs = new GeoJSON().readFeatures(res);
  501. return fs;
  502. }
  503. getStyle (strokeColor = [255, 0, 0, 1], fillColor = [255, 0, 0, 0.1]) {
  504. let style = new Style({
  505. stroke: new Stroke({
  506. color: strokeColor,
  507. width: 2
  508. }),
  509. fill: new Stroke({
  510. color: fillColor
  511. })
  512. });
  513. return style;
  514. }
  515. changeTextName (arr, layerName) {
  516. this.textName[layerName] = arr;
  517. }
  518. getStyleWithText (strokeColor = [180, 180, 0, 1], fillColor = [180, 180, 0, 0.1]) {
  519. let style = new Style({
  520. stroke: new Stroke({
  521. color: strokeColor,
  522. width: 2
  523. }),
  524. fill: new Stroke({
  525. color: fillColor
  526. }),
  527. text: new Text({
  528. text: '333',
  529. overflow: true,
  530. font: 'bold 11px serif',
  531. fill: new Fill({
  532. color: '#00FEFA'
  533. })
  534. })
  535. });
  536. return style;
  537. }
  538. /**
  539. *根据要素集合获得矢量图层
  540. * @param {array} fs 要素集合
  541. */
  542. getVectorLayerByFsWidthText (fs, zIndex = 200, opacity = 1, strokeColor = [0, 180, 241, 1], fillColor = [0, 180, 241, 0.1], layerName) {
  543. const source = new VectorSource({
  544. });
  545. if (fs.length) {
  546. source.addFeatures(fs);
  547. }
  548. const layer = new VectorLayer({
  549. renderMode: 'webgl',
  550. style: (feature) => {
  551. let arr = '';
  552. this.textName[layerName].forEach(item => {
  553. arr += feature.getProperties()[item] + '\n';
  554. });
  555. return new Style({
  556. fill: new Fill({
  557. color: fillColor
  558. }),
  559. text: new Text({
  560. overflow: false,
  561. text: arr,
  562. font: 'bold 12px serif',
  563. fill: new Fill({
  564. color: '#ffffff'
  565. })
  566. }),
  567. stroke: new Stroke({
  568. color: strokeColor,
  569. width: 1
  570. })
  571. });
  572. },
  573. opacity,
  574. source: source,
  575. zIndex: zIndex
  576. });
  577. this.map.addLayer(layer);
  578. return layer;
  579. }
  580. /**
  581. * 根据图层名获取瓦片图层
  582. */
  583. getTileLayerByLayerName (url, layerName, visible = true, zIndex = 50) {
  584. const layer = new TileLayer({
  585. visible,
  586. zIndex: zIndex,
  587. source: new TileWMS({
  588. url,
  589. params: { 'LAYERS': layerName, 'TILED': true, SRID: 3857 },
  590. transition: 0
  591. })
  592. });
  593. return layer;
  594. }
  595. /**
  596. * 键入弹窗
  597. * @param {*} el dom
  598. * @param {*} coord 位置
  599. */
  600. addOverlay (el, coord) {
  601. let overLayer = new Overlay({
  602. element: el, // 挂载元素
  603. position: coord,
  604. autoPanAnimation: {
  605. duration: 250
  606. },
  607. stopEvent: true
  608. });
  609. this.map.addOverlay(overLayer);
  610. this.overLayer = overLayer;
  611. }
  612. /**
  613. * 根据要素集合适配视图
  614. * @param {map} 地图容器
  615. * @param {*} featureList 要素集合
  616. */
  617. mapSetFit (featureList, padding = [100, 20, 20, 20], duration = 2000) {
  618. let options = {
  619. padding,
  620. duration
  621. };
  622. let extent = createEmpty();
  623. featureList.forEach(item => {
  624. const itemExtent = item.getGeometry().getExtent();
  625. extent = extend(extent, itemExtent);
  626. });
  627. if (featureList.length) {
  628. this.map.getView().fit(extent, options);
  629. }
  630. }
  631. /**
  632. * 坐标转换
  633. * @param {coordinate } openlayer 输出坐标值
  634. * Amap public index.html 导入
  635. */
  636. transform (coordinate) {
  637. return transform(coordinate, 'EPSG:3857', 'EPSG:4326');
  638. }
  639. // 在地图上添加点, 并添加点的样式
  640. setPoint (map, pointList) {
  641. pointVectorSource.clear();
  642. if (pointList.length === 0) return;
  643. const pointStyle = new Style({
  644. image: new Icon({
  645. src: locationIcon,
  646. scale: 1,
  647. anchor: [0.5, 0.5],
  648. rotateWithView: false,
  649. rotation: 45
  650. })
  651. });
  652. let pointFeature;
  653. pointList.forEach(item => {
  654. pointFeature = new Feature({
  655. geometry: new Point(new fromLonLat([item.longitude, item.latitude])),
  656. url: item.url,
  657. showText: true,
  658. showPicture: item.url ? true : false
  659. });
  660. pointFeature.setStyle(pointStyle);
  661. pointVectorSource.addFeature(pointFeature);
  662. });
  663. const vectorLayer = new VectorLayer({
  664. // zIndex: 999,
  665. source: pointVectorSource
  666. });
  667. map.addLayer(vectorLayer);
  668. // 每隔1秒旋转10度
  669. let rotation = 0;
  670. setInterval(() => {
  671. rotation += 5 * Math.PI / 180;
  672. pointStyle.getImage().setRotation(rotation);
  673. pointFeature.setStyle(pointStyle);
  674. }, 100);
  675. }
  676. // 初始化地块图层
  677. initDKlayer (layerName, extent) {
  678. // 地块图层
  679. let projection = getProjection('EPSG:3857');
  680. let projectionExtent = projection.getExtent();
  681. var size = getWidth(projectionExtent) / 256;
  682. var resolutions = [];
  683. for (var z = 0; z < 20; ++z) {
  684. resolutions[z] = size / Math.pow(2, z);
  685. }
  686. const layer = new TileLayer({
  687. zIndex: 51,
  688. visible: true,
  689. source: new WMTS({
  690. url: '/geoserver/gwc/service/wmts',
  691. layer: layerName,
  692. matrixSet: 'EPSG:3857',
  693. format: 'image/png',
  694. projection: projection,
  695. tileGrid: new WMTSTileGrid({
  696. tileSize: [256, 256],
  697. resolutions: resolutions,
  698. origin: getTopLeft(projectionExtent),
  699. matrixIds: ['EPSG:3857:0', 'EPSG:3857:1', 'EPSG:3857:2', 'EPSG:3857:3', 'EPSG:3857:4', 'EPSG:3857:5',
  700. 'EPSG:3857:6', 'EPSG:3857:7', 'EPSG:3857:8', 'EPSG:3857:9', 'EPSG:3857:10', 'EPSG:3857:11', 'EPSG:3857:12',
  701. 'EPSG:3857:13', 'EPSG:3857:14', 'EPSG:3857:15', 'EPSG:3857:16', 'EPSG:3857:17', 'EPSG:3857:18', 'EPSG:3857:19',
  702. 'EPSG:3857:20', 'EPSG:3857:21', 'EPSG:3857:22', 'EPSG:3857:23', 'EPSG:3857:24', 'EPSG:3857:25', 'EPSG:3857:26',
  703. 'EPSG:3857:27', 'EPSG:3857:28', 'EPSG:3857:29', 'EPSG:3857:30'],
  704. extent: extent
  705. }),
  706. style: '',
  707. wrapX: false
  708. })
  709. });
  710. this.map.addLayer(layer);
  711. return layer;
  712. }
  713. /**
  714. * 根据一个带有经纬度,返回一个矢量图层
  715. * @param {*} dataList 带有经纬度的数组
  716. * { type: type,
  717. geometry: [经度, 维度]
  718. }
  719. * @param {*} icon 图标
  720. */
  721. getVectorLayerByGeometrylist (dataList, icon, zIndex = 100) {
  722. let featureList = [];
  723. const style = new Style({
  724. image: new Icon({
  725. anchor: [0.5, 28],
  726. anchorXUnits: 'fraction',
  727. anchorYUnits: 'pixels',
  728. src: icon
  729. })
  730. });
  731. dataList.forEach(item => {
  732. const iconFeature = new Feature({
  733. _geometry: item.geometry,
  734. geometry: new Point(fromLonLat(item.geometry))
  735. // geometry: new Point(item.geometry)
  736. });
  737. iconFeature.setProperties(item.data);
  738. iconFeature.setStyle(style);
  739. featureList.push(iconFeature);
  740. });
  741. const source = new VectorSource({
  742. features: featureList
  743. });
  744. const iconLayer = new VectorLayer({
  745. zIndex,
  746. source: source
  747. });
  748. this.map.addLayer(iconLayer);
  749. return iconLayer;
  750. }
  751. /**
  752. * 加载geojson数据
  753. */
  754. loadGeojsonData (geoData) {
  755. const fs = new GeoJSON().readFeatures(geoData);
  756. this.mapSetFit(fs);
  757. let layer = this.getVectorLayerByFs(fs);
  758. return layer;
  759. }
  760. removeLayer (layer) {
  761. this.map.removeLayer(layer);
  762. }
  763. /**
  764. * 增删改查要素
  765. * @param {*} inserts 要插入的要素集合
  766. * @param {*} updates 要更新的要素集合
  767. * @param {*} deletes 要删除的要素集合
  768. * @param {*} layer 要操作的图层
  769. * @param {*} fs
  770. */
  771. async crudFs (inserts = null, updates = null, deletes = null, layer = 'cwd2') {
  772. const geoserverData = {
  773. workSpaceName: 'qianguo',
  774. uri: 'http://124.235.241.109:8080/geoserver/qianguo',
  775. wfsURL: 'http://124.235.241.109:8080/geoserver/qianguo/ows?',
  776. layer: layer
  777. };
  778. let wfs = new WFS();
  779. let transact_xml = wfs.writeTransaction(
  780. inserts, updates, deletes,
  781. {
  782. version: '1.1.0',
  783. srsName: 'EPSG:3857',
  784. featureNS: geoserverData.uri,
  785. featurePrefix: geoserverData.workSpaceName,
  786. featureType: [geoserverData.layer],
  787. }
  788. );
  789. let transact_str = (new XMLSerializer()).serializeToString(transact_xml);
  790. let updStr = transact_str.replace(/geometry/g, 'the_geom');
  791. let res = await post('/geoserver/wfs', updStr);
  792. let transactRes = wfs.readTransactionResponse(res);
  793. let str = transactRes.transactionSummary.totalInserted +
  794. " totalInserted!, insertIds: " + transactRes.insertIds + "\n";
  795. str += transactRes.transactionSummary.totalUpdated + " totalUpdated!\n";
  796. str += transactRes.transactionSummary.totalDeleted + " totalDeleted!";
  797. }
  798. }
  799. export default GisUtils;