当前位置:   article > 正文

Openlayer对wfs服务进行增删改查_wfs是否支持增删改

wfs是否支持增删改

目录

查询数据

新增数据

删除数据

更新数据


注意:ol版本要用5.3.3,保证new GeoJson正常使用。6.1.1中不能用new GeoJSON()  

由于要修改矢量数据,涉及到权限问题,默认是不允许修改的,所以需要更改权限,操作如下: 

查询数据

查询链接:

http://120.76.197.111:8090/geoserver/csdn_data/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=csdn_data%3Asichuan&maxFeatures=50&outputFormat=application%2Fjson

方式1:

  1. <template>
  2. <div id="map" style="width: 100vw; height: 100vh"></div>
  3. </template>
  4. <script>
  5. import "ol/ol.css";
  6. import { Map, View, Feature } from "ol";
  7. import { TileWMS, OSM, Vector as VectorSource } from "ol/source";
  8. import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer";
  9. import GeoJSON from "ol/format/GeoJSON";
  10. import { Point, LineString, Polygon } from "ol/geom";
  11. export default {
  12. components: {},
  13. data() {
  14. return {
  15. map: {},
  16. vectorlayer: {},
  17. };
  18. },
  19. created() {},
  20. mounted() {
  21. this.initMap();
  22. },
  23. computed: {},
  24. methods: {
  25. initMap() {
  26. this.map = new Map({
  27. target: "map",
  28. layers: [
  29. new TileLayer({
  30. source: new OSM(),
  31. }),
  32. new VectorLayer({
  33. source: new VectorSource({
  34. //以下两个都可以,第一个是1.0.0版本,第二个是2.0.0版本
  35. url: "http://120.76.197.111:8090/geoserver/csdn_data/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=csdn_data%3Asichuan&maxFeatures=50&outputFormat=application%2Fjson",
  36. // url: "http://120.76.197.111:8090/geoserver/csdn_data/ows?service=WFS&version=2.0.0&request=GetFeature&typeName=csdn_data%3Asichuan&count=50&outputFormat=application%2Fjson",
  37. format: new GeoJSON(),
  38. }),
  39. }),
  40. ],
  41. view: new View({
  42. projection: "EPSG:4326",
  43. center: [104, 30.3],
  44. zoom: 8,
  45. }),
  46. });
  47. },
  48. },
  49. };
  50. </script>

方式2:通过writeGetFeature

  1. <template>
  2. <div id="map" style="width: 100vw; height: 100vh"></div>
  3. </template>
  4. <script>
  5. import "ol/ol.css";
  6. import { Map, View, Feature } from "ol";
  7. import { TileWMS, OSM, Vector as VectorSource } from "ol/source";
  8. import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer";
  9. import { GeoJSON, WFS, filter } from "ol/format";
  10. import { Point, LineString, Polygon } from "ol/geom";
  11. import Select from "ol/interaction/Select";
  12. import { altKeyOnly, click, pointerMove } from "ol/events/condition";
  13. export default {
  14. components: {},
  15. data() {
  16. return {
  17. map: {},
  18. vectorlayer: {},
  19. };
  20. },
  21. created() {},
  22. mounted() {
  23. this.initMap();
  24. this.addLayer();
  25. },
  26. computed: {},
  27. methods: {
  28. initMap() {
  29. this.map = new Map({
  30. target: "map",
  31. layers: [
  32. new TileLayer({
  33. source: new OSM(),
  34. }),
  35. ],
  36. view: new View({
  37. projection: "EPSG:4326",
  38. center: [104, 30.3],
  39. zoom: 7,
  40. }),
  41. });
  42. },
  43. async addLayer() {
  44. // 发送请求
  45. let json = await fetch("http://120.76.197.111:8090/geoserver/wfs", {
  46. method: "post",
  47. body: new XMLSerializer().serializeToString(
  48. new WFS().writeGetFeature({
  49. srsName: "EPSG:4326", //坐标系
  50. featureNS: "www.csdn_data.com", // 注意这个值必须为创建工作区时的命名空间URI
  51. featurePrefix: "csdn_data", //工作区的命名
  52. featureTypes: ["sichuan"], //所要访问的图层
  53. maxFeatures: 5000,
  54. outputFormat: "application/json",
  55. })
  56. ),
  57. }).then((response) => {
  58. return response.json();
  59. });
  60. let features = new GeoJSON().readFeatures(json);
  61. this.vectorlayer = new VectorLayer({
  62. source: new VectorSource(),
  63. });
  64. this.vectorlayer.getSource().addFeatures(features);
  65. this.map.addLayer(this.vectorlayer);
  66. },
  67. },
  68. };
  69. </script>

 

新增数据

目前,只能对面进行新增和编辑,不能对点和线进行新增和编辑!

ol.format.wfs的writeTransaction方法,接受4个参数,前三个参数依次分别是要插入、更新、删除操作对应的Feature对象,以数组的形式。第4个参数是一个对象,里面定义了geoserver服务的相关信息。可以在一个Transaction操作中同时执行插入、更新、和删除操作。

  1. <template>
  2. <div>
  3. <div style="position: fixed; top: 200px; left: 200px; z-index: 90000">
  4. <el-button @click="startDraw()">开始绘制</el-button>
  5. <el-button @click="saveDraw(featureDraw)">保存绘制</el-button>
  6. </div>
  7. <div id="map" style="width: 100vw; height: 100vh"></div>
  8. </div>
  9. </template>
  10. <script>
  11. import "ol/ol.css";
  12. import { Map, View, Feature } from "ol";
  13. import { TileWMS, OSM, Vector as VectorSource } from "ol/source";
  14. import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer";
  15. import { GeoJSON, WFS, filter } from "ol/format";
  16. import { Point, LineString, Polygon, MultiPolygon } from "ol/geom";
  17. import { Select, Draw, Modify } from "ol/interaction";
  18. import { altKeyOnly, click, pointerMove } from "ol/events/condition";
  19. export default {
  20. components: {},
  21. data() {
  22. return {
  23. map: {},
  24. vectorlayer: {},
  25. draw: {},
  26. featureDraw: {},
  27. };
  28. },
  29. created() {},
  30. mounted() {
  31. this.initMap();
  32. this.addLayer();
  33. },
  34. computed: {},
  35. methods: {
  36. initMap() {
  37. this.map = new Map({
  38. target: "map",
  39. layer
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/872357
推荐阅读
相关标签
  

闽ICP备14008679号