当前位置:   article > 正文

openlayer中加载geojson的几种方式_openlayers加载geojson

openlayers加载geojson

目录

一,加载本地geojson文件

二,加载geoserver发布的geojson数据

1、wms方式

2、wfs方式

三、点击查看属性

wms方式

wfs方式

加载4326

加载3857


推荐使用openlayer5,不要使用openlayer6,否则不能使用矢量切片和new GeoJSON()

cnpm i -S ol@5.3.3

先贴一个经常需要导入的模块 

  1. import "ol/ol.css";
  2. import { Map, View, Overlay } from "ol";
  3. import { TileWMS, OSM, Vector as VectorSource } from "ol/source";
  4. import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
  5. import {altKeyOnly, click, pointerMove,platformModifierKeyOnly} from 'ol/events/condition';
  6. import { Fill, Stroke, Style, Text , Circle, Icon} from "ol/style";
  7. import { Select,DragBox } from "ol/interaction";
  8. import { transform } from "ol/proj";
  9. import {
  10. defaults as defaultControls,
  11. OverviewMap,
  12. FullScreen,
  13. ScaleLine,
  14. ZoomSlider,
  15. MousePosition,
  16. ZoomToExtent
  17. } from "ol/control";
  18. import GeoJSON from "ol/format/GeoJSON";

一,加载本地geojson文件

1、

  1. new VectorSource({
  2. features:new GeoJSON().readFeatures(this.geojsonData)
  3. })

2、推荐

  1. import sichuan from "@/assets/sichuan.json";
  2. source: new VectorSource({
  3. features: new GeoJSON().readFeatures(sichuan),
  4. }),

3、public中的文件

注意:使用new GeoJSON()需要ol@5.3.3,而ol@6.1.1不能用

  1. new VectorSource({
  2. url: "sichuan_shp.json",
  3. format: new GeoJSON()
  4. });

  1. <template>
  2. <div id="map"></div>
  3. </template>
  4. <script>
  5. import "ol/ol.css";
  6. import { Map, View } from "ol";
  7. import { OSM, Vector as VectorSource } from "ol/source";
  8. import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
  9. import GeoJSON from "ol/format/GeoJSON";
  10. export default {
  11. components: {},
  12. data() {
  13. return {
  14. map: {}
  15. };
  16. },
  17. created() {},
  18. mounted() {
  19. this.initMap();
  20. },
  21. computed: {},
  22. methods: {
  23. initMap() {
  24. var layers = [
  25. new TileLayer({
  26. source: new OSM()
  27. }),
  28. new VectorLayer({
  29. source: new VectorSource({
  30. url: "sichuan.json",
  31. format: new GeoJSON()
  32. })
  33. })
  34. ];
  35. this.map = new Map({
  36. layers: layers,
  37. target: "map",
  38. view: new View({
  39. projection: "EPSG:4326",
  40. center: [115, 39],
  41. zoom: 4
  42. })
  43. });
  44. }
  45. }
  46. };
  47. </script>
  48. <style lang="scss" scoped>
  49. #map {
  50. height: 700px;
  51. width: 100%;
  52. }
  53. </style>

这里千万要注意: osm地图放在图层数组的第一个位置!!!否则其他图层会被遮住!!

或者给图层设置zIndex属性

 

二,加载geoserver发布的geojson数据

(注意:如果想要发布成geojson,那么需要保证geoserver中这以下这四个jar包 。

这四个包下载地址:geoserver中发布geojson服务需要的包.zip_geoserver发布geojson-互联网文档类资源-CSDN下载

查看是否有这几个包,随便点击一个图层,在Tile Caching中看是否有“application/json;type=geojson”这个选项即可

将这四个jar包复制到geoserver安装路径下即可,如下路径:

参考:Geoserver配置以及发布geojson服务教程_迷茫的小猿的博客-CSDN博客

1、wms方式

  1. source: new VectorSource({
  2. url:
  3. "http://localhost:8090/geoserver/NKYYGS/wms?service=WMS&version=1.1.0&request=GetMap&layers=NKYYGS%3Asichuan_shp&bbox=97.350096%2C26.045865%2C108.546488%2C34.312446&width=768&height=567&srs=EPSG%3A4326&format=application%2Fjson%3Btype%3Dgeojson",
  4. format: new GeoJSON()
  5. })

(注意:如果有时候用这种方式加载不出图层,那么可以用另一种方式:Vue中访问Geoserver发布的openlayer中wms地图服务_李疆的博客-CSDN博客) 

 

2、wfs方式

new GeoJSON()方式:

  1. source: new VectorSource({
  2. url:
  3. "http://localhost:8090/geoserver/NKYYGS/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=NKYYGS%3Asichuan_shp&maxFeatures=50&outputFormat=application%2Fjson",
  4. format: new GeoJSON()
  5. })
  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>

new GeoJSON().readFeatures(json)方式: 

  1. let json = await fetch(
  2. "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"
  3. ).then((response) => {
  4. return response.json();
  5. });
  6. this.features = new GeoJSON().readFeatures(json);

完整示例 

  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. this.addLayer();
  23. },
  24. computed: {},
  25. methods: {
  26. initMap() {
  27. this.map = new Map({
  28. target: "map",
  29. layers: [
  30. new TileLayer({
  31. source: new OSM(),
  32. }),
  33. ],
  34. view: new View({
  35. projection: "EPSG:4326",
  36. center: [104, 30.3],
  37. zoom: 8,
  38. }),
  39. });
  40. },
  41. async addLayer() {
  42. let json = await fetch(
  43. // "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"
  44. "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"
  45. ).then((response) => {
  46. return response.json();
  47. });
  48. let features = new GeoJSON().readFeatures(json);
  49. this.vectorlayer = new VectorLayer({
  50. source: new VectorSource(),
  51. });
  52. this.vectorlayer.getSource().addFeatures(features);
  53. this.map.addLayer(this.vectorlayer);
  54. },
  55. },
  56. };
  57. </script>

如果还加载不出来的话,可能是中文编码问题,在工作空间中修改默认编码为utf8即可

三、点击查看属性

wms方式

见:https://blog.csdn.net/qq_40323256/article/details/109315337

wfs方式

加载4326

openlayer上加载了geojson图层后,就可以通过点击事件查看图层的要素属性了,代码如下:

  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. 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. select: {},
  20. };
  21. },
  22. created() {},
  23. mounted() {
  24. this.initMap();
  25. this.selectFeature();
  26. },
  27. computed: {},
  28. methods: {
  29. initMap() {
  30. this.map = new Map({
  31. target: "map",
  32. layers: [
  33. new TileLayer({
  34. source: new OSM(),
  35. }),
  36. new VectorLayer({
  37. source: new VectorSource({
  38. //以下两个都可以,第一个是1.0.0版本,第二个是2.0.0版本
  39. // 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",
  40. 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",
  41. format: new GeoJSON(),
  42. }),
  43. }),
  44. ],
  45. view: new View({
  46. projection: "EPSG:4326",
  47. center: [104, 30.3],
  48. zoom: 8,
  49. }),
  50. });
  51. },
  52. selectFeature() {
  53. this.select = new Select({
  54. condition: click,
  55. });
  56. this.map.addInteraction(this.select);
  57. this.select.on("select", (e) => {
  58. console.log("e", e);
  59. });
  60. },
  61. },
  62. };
  63. </script>

加载3857

  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 Select from "ol/interaction/Select";
  11. import { altKeyOnly, click, pointerMove } from "ol/events/condition";
  12. export default {
  13. components: {},
  14. data() {
  15. return {
  16. map: {},
  17. vectorlayer: {},
  18. select: {},
  19. };
  20. },
  21. created() {},
  22. mounted() {
  23. this.initMap();
  24. this.selectFeature();
  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. new VectorLayer({
  36. source: new VectorSource({
  37. url: "http://120.76.197.111:8090/geoserver/keshan/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=keshan%3Asichuan&maxFeatures=50&outputFormat=application%2Fjson",
  38. format: new GeoJSON(),
  39. }),
  40. }),
  41. ],
  42. view: new View({
  43. projection: "EPSG:3857", //默认
  44. center: [11577227.04, 3542170.63],
  45. zoom: 8,
  46. }),
  47. });
  48. },
  49. selectFeature() {
  50. this.select = new Select({
  51. condition: click,
  52. });
  53. this.map.addInteraction(this.select);
  54. this.select.on("select", (e) => {
  55. console.log("e", e);
  56. });
  57. },
  58. },
  59. };
  60. </script>

接下来还可以在图层上进行框选、多选等操作,见:vue+openlayer实现地图框选、多选

地图框选功能 在线预览:WebGIS之家WebGIS之家,openlayers示例源码,cesium示例源码,openlayers在线调试预览,cesium在线调试预览,webgis,数字地球,数字孪生icon-default.png?t=N7T8https://www.webgishome.com/preview?id=90&example_name=dragBox_feature&title=dragBox%E6%A1%86%E9%80%89%E8%A6%81%E7%B4%A0

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

闽ICP备14008679号