当前位置:   article > 正文

OpenLayers结合Turf实现空间运算_openlayers turf

openlayers turf

1. 引言

空间运算利用几何函数来接收输入的空间数据,对其进行分析,然后生成输出数据,输出数据为针对输入数据执行分析的派生结果。

可从空间运算中获得的派生数据包括:

  • 作为输入要素周围缓冲区的面
  • 作为对几何集合执行分析的结果的单个要素
  • 作为比较结果以确定不与其他要素位于同一物理空间的要素部分的单个要素
  • 作为比较结果以查找与其他要素的物理空间相交的要素部分的单个要素
  • 由彼此不位于同一物理空间的输入要素部分组成的多部分 (multipart) 要素
  • 作为两个几何的并集的要素

参考文档:空间运算—ArcMap | 文档 (arcgis.com)

Turf.js是MapBox公司研发的基于浏览器端的空间分析库,它使用JavaScript进行编写,通过npm进行包管理。值得一提的是,良好的模块化设计使其不仅能够作用于浏览器端、还可通过Node.js在服务端使用。Turf 原生支持 GeoJSON 矢量数据。GeoJSON 的优点是结构简单,并且得到了所有网页地图API的支持;但 GeoJSON 不支持空间索引,这个缺点可能会限制 Turf 处理大型文件的能力效率。其适用于轻量级(数据轻量而非功能轻量)的WebGIS应用

参考文献:Turf.js—让你在浏览器上实现地理分析 - 掘金 (juejin.cn)

turf.js官网:Turf.js | Advanced Geospatial Analysis (turfjs.org)

中文站点:Turf.js中文网 (fenxianglu.cn)

Turf.js Github地址:Turfjs/turf: A modular geospatial engine written in JavaScript (github.com)

空间运算可谓是空间分析的基础,Turf.js提供了大量的空间分析功能,包含了空间运算功能,本文参考OpenLayers与Turf集成的官方示例,使用示例数据和原生JavaScript,进行求交运算与缓冲区运算,并进行可视化

OpenLayers与Turf集成示例:turf.js (openlayers.org)

2.初始化地图

引入OpenLayers和Turf.js的CDN:

  1. <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
  2. <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
  3. <script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>

注意:

  • OpenLayers与Turf集成示例使用的Turf版本是2.0.0,版本偏老,与目前版本(6.5.0)部分API不兼容,不建议使用

构建网页基本内容,添加地图容器:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet"
  9. href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
  10. <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
  11. <script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
  12. <style>
  13. html,
  14. body,
  15. #map {
  16. height: 100%;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="map"></div>
  22. </body>
  23. </html>

添加地图并加载示例矢量数据:

  1. <script>
  2. fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
  3. .then(response => response.json())
  4. .then(function (json){
  5. var vectorSource = new ol.source.Vector({
  6. features: (new ol.format.GeoJSON()).readFeatures(json, {
  7. featureProjection: 'EPSG:3857'
  8. })
  9. });
  10. var vectorLayer = new ol.layer.Vector({
  11. source: vectorSource
  12. });
  13. var map = new ol.Map({
  14. layers: [
  15. new ol.layer.Tile({
  16. source: new ol.source.OSM()
  17. }),
  18. vectorLayer
  19. ],
  20. target: 'map',
  21. view: new ol.View({
  22. center: ol.proj.fromLonLat([126.980366, 37.52654]),
  23. zoom: 15
  24. })
  25. });
  26. })
  27. </script>

完整代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet"
  9. href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
  10. <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
  11. <script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
  12. <style>
  13. html,
  14. body,
  15. #map {
  16. height: 100%;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="map"></div>
  22. <script>
  23. fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
  24. .then(response => response.json())
  25. .then(function (json){
  26. var vectorSource = new ol.source.Vector({
  27. features: (new ol.format.GeoJSON()).readFeatures(json, {
  28. featureProjection: 'EPSG:3857'
  29. })
  30. });
  31. var vectorLayer = new ol.layer.Vector({
  32. source: vectorSource
  33. });
  34. var map = new ol.Map({
  35. layers: [
  36. new ol.layer.Tile({
  37. source: new ol.source.OSM()
  38. }),
  39. vectorLayer
  40. ],
  41. target: 'map',
  42. view: new ol.View({
  43. center: ol.proj.fromLonLat([126.980366, 37.52654]),
  44. zoom: 15
  45. })
  46. });
  47. })
  48. </script>
  49. </body>
  50. </html>

初始化的地图:

3. 缓冲区计算

参考官方文档:Turf.js | Advanced geospatial analysis (turfjs.org)

步骤实质就是:

  • 将Openlayers的Features对象转换为Turf支持的GeoJSON对象
  • 调用Turf.buff()函数得到运算结果
  • OpenLayers读取运算后的GeoJSON数据加载到图层中

缓存区计算的核心代码如下:

  1. const features = vectorSource.getFeatures();
  2. const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
  3. featureProjection: 'EPSG:3857'
  4. });
  5. var buffered = turf.buffer(turfLines, 25, { units: 'meters' });
  6. const bufferedLayer = new ol.layer.Vector({
  7. source: new ol.source.Vector({
  8. features: (new ol.format.GeoJSON()).readFeatures(buffered, {
  9. featureProjection: 'EPSG:3857'
  10. })
  11. })
  12. })
  13. map.addLayer(bufferedLayer);

注意:

  • 从OpenLayers的Feature对象转换为Turf支持的GeoJSON对象时务必指定坐标系

这一步的完成代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet"
  9. href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
  10. <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
  11. <script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
  12. <!-- <script src="https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script> -->
  13. <style>
  14. html,
  15. body,
  16. #map {
  17. height: 100%;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="map"></div>
  23. <script>
  24. var map = new ol.Map({
  25. layers: [
  26. new ol.layer.Tile({
  27. source: new ol.source.OSM()
  28. })
  29. ],
  30. target: 'map',
  31. view: new ol.View({
  32. center: ol.proj.fromLonLat([126.980366, 37.52654]),
  33. zoom: 15
  34. })
  35. });
  36. fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
  37. .then(response => response.json())
  38. .then(function (json) {
  39. var vectorSource = new ol.source.Vector({
  40. features: (new ol.format.GeoJSON()).readFeatures(json, {
  41. featureProjection: 'EPSG:3857'
  42. })
  43. });
  44. var vectorLayer = new ol.layer.Vector({
  45. source: vectorSource,
  46. style: new ol.style.Style({
  47. stroke: new ol.style.Stroke({
  48. color: '#0000ff',
  49. width: 2
  50. })
  51. })
  52. });
  53. map.addLayer(vectorLayer);
  54. const features = vectorSource.getFeatures();
  55. const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
  56. featureProjection: 'EPSG:3857'
  57. });
  58. var buffered = turf.buffer(turfLines, 25, { units: 'meters' });
  59. const bufferedLayer = new ol.layer.Vector({
  60. source: new ol.source.Vector({
  61. features: (new ol.format.GeoJSON()).readFeatures(buffered, {
  62. featureProjection: 'EPSG:3857'
  63. })
  64. })
  65. })
  66. map.addLayer(bufferedLayer);
  67. })
  68. </script>
  69. </body>
  70. </html>

结果图如下:

4. 相交计算

参考官方文档:Turf.js | Advanced geospatial analysis (turfjs.org)

步骤与缓冲区计算类似:

  • 将Openlayers的Features对象转换为Turf支持的GeoJSON对象
  • 调用Turf.lineIntersect()函数得到运算结果
  • OpenLayers读取运算后的GeoJSON数据加载到图层中

本文所使用的数据是LineString,所以此处的相交计算的是线段相交

相交运算的核心代码如下:

  1. const features = vectorSource.getFeatures();
  2. const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
  3. featureProjection: 'EPSG:3857'
  4. });
  5. const intersectionSource = new ol.source.Vector()
  6. for (let i = 0; i < turfLines.features.length; i++) {
  7. for (let j = i + 1; j < turfLines.features.length; j++) {
  8. const intersections = turf.lineIntersect(turfLines.features[i], turfLines.features[j]);
  9. if (intersections) {
  10. if (intersections.features.length > 0) {
  11. intersectionSource.addFeatures(new ol.format.GeoJSON().readFeatures(intersections, {
  12. featureProjection: 'EPSG:3857'
  13. }))
  14. }
  15. }
  16. }
  17. }
  18. const intersectionLayer = new ol.layer.Vector({
  19. source: intersectionSource,
  20. style: new ol.style.Style({
  21. stroke: new ol.style.Stroke({
  22. color: '#ff0000',
  23. width: 2
  24. }),
  25. fill: new ol.style.Fill({
  26. color: '#ff0000'
  27. }),
  28. image: new ol.style.Circle({
  29. radius: 5,
  30. fill: new ol.style.Fill({
  31. color: '#ff0000'
  32. })
  33. })
  34. })
  35. })
  36. map.addLayer(intersectionLayer);

注意:

  • 计算结果为点集合(Point),最好设置一定的显示样式,不然难以看见

这一步的完成代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet"
  9. href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
  10. <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
  11. <script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
  12. <!-- <script src="https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script> -->
  13. <style>
  14. html,
  15. body,
  16. #map {
  17. height: 100%;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="map"></div>
  23. <script>
  24. var map = new ol.Map({
  25. layers: [
  26. new ol.layer.Tile({
  27. source: new ol.source.OSM()
  28. })
  29. ],
  30. target: 'map',
  31. view: new ol.View({
  32. center: ol.proj.fromLonLat([126.980366, 37.52654]),
  33. zoom: 15
  34. })
  35. });
  36. fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
  37. .then(response => response.json())
  38. .then(function (json) {
  39. var vectorSource = new ol.source.Vector({
  40. features: (new ol.format.GeoJSON()).readFeatures(json, {
  41. featureProjection: 'EPSG:3857'
  42. })
  43. });
  44. var vectorLayer = new ol.layer.Vector({
  45. source: vectorSource,
  46. style: new ol.style.Style({
  47. stroke: new ol.style.Stroke({
  48. color: '#0000ff',
  49. width: 2
  50. })
  51. })
  52. });
  53. map.addLayer(vectorLayer);
  54. const features = vectorSource.getFeatures();
  55. const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
  56. featureProjection: 'EPSG:3857'
  57. });
  58. const intersectionSource = new ol.source.Vector()
  59. for (let i = 0; i < turfLines.features.length; i++) {
  60. for (let j = i + 1; j < turfLines.features.length; j++) {
  61. const intersections = turf.lineIntersect(turfLines.features[i], turfLines.features[j]);
  62. if (intersections) {
  63. // console.log(intersections)
  64. if (intersections.features.length > 0) {
  65. intersectionSource.addFeatures(new ol.format.GeoJSON().readFeatures(intersections, {
  66. featureProjection: 'EPSG:3857'
  67. }))
  68. // console.log(intersectionSource.getFeatures())
  69. }
  70. }
  71. }
  72. }
  73. // console.log(intersectionSource)
  74. const intersectionLayer = new ol.layer.Vector({
  75. source: intersectionSource,
  76. style: new ol.style.Style({
  77. stroke: new ol.style.Stroke({
  78. color: '#ff0000',
  79. width: 2
  80. }),
  81. fill: new ol.style.Fill({
  82. color: '#ff0000'
  83. }),
  84. image: new ol.style.Circle({
  85. radius: 5,
  86. fill: new ol.style.Fill({
  87. color: '#ff0000'
  88. })
  89. })
  90. })
  91. })
  92. map.addLayer(intersectionLayer);
  93. })
  94. </script>
  95. </body>
  96. </html>

结果图如下:

5. 整合

将两个运算结果结合在一起,完整代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet"
  9. href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
  10. <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
  11. <script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
  12. <!-- <script src="https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script> -->
  13. <style>
  14. html,
  15. body,
  16. #map {
  17. height: 100%;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="map"></div>
  23. <script>
  24. var map = new ol.Map({
  25. layers: [
  26. new ol.layer.Tile({
  27. source: new ol.source.OSM()
  28. })
  29. ],
  30. target: 'map',
  31. view: new ol.View({
  32. center: ol.proj.fromLonLat([126.980366, 37.52654]),
  33. zoom: 15
  34. })
  35. });
  36. fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
  37. .then(response => response.json())
  38. .then(function (json) {
  39. var vectorSource = new ol.source.Vector({
  40. features: (new ol.format.GeoJSON()).readFeatures(json, {
  41. featureProjection: 'EPSG:3857'
  42. })
  43. });
  44. var vectorLayer = new ol.layer.Vector({
  45. source: vectorSource,
  46. style: new ol.style.Style({
  47. stroke: new ol.style.Stroke({
  48. color: '#0000ff',
  49. width: 2
  50. })
  51. })
  52. });
  53. map.addLayer(vectorLayer);
  54. const features = vectorSource.getFeatures();
  55. const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
  56. featureProjection: 'EPSG:3857'
  57. });
  58. // console.log(turfLines)
  59. // const buffered = turf.buffer(turfLines, 25);
  60. var buffered = turf.buffer(turfLines, 25, { units: 'meters' });
  61. const bufferedLayer = new ol.layer.Vector({
  62. source: new ol.source.Vector({
  63. features: (new ol.format.GeoJSON()).readFeatures(buffered, {
  64. featureProjection: 'EPSG:3857'
  65. })
  66. })
  67. })
  68. map.addLayer(bufferedLayer);
  69. const intersectionSource = new ol.source.Vector()
  70. for (let i = 0; i < turfLines.features.length; i++) {
  71. for (let j = i + 1; j < turfLines.features.length; j++) {
  72. const intersections = turf.lineIntersect(turfLines.features[i], turfLines.features[j]);
  73. if (intersections) {
  74. // console.log(intersections)
  75. if (intersections.features.length > 0) {
  76. intersectionSource.addFeatures(new ol.format.GeoJSON().readFeatures(intersections, {
  77. featureProjection: 'EPSG:3857'
  78. }))
  79. // console.log(intersectionSource.getFeatures())
  80. }
  81. }
  82. }
  83. }
  84. // console.log(intersectionSource)
  85. const intersectionLayer = new ol.layer.Vector({
  86. source: intersectionSource,
  87. style: new ol.style.Style({
  88. stroke: new ol.style.Stroke({
  89. color: '#ff0000',
  90. width: 2
  91. }),
  92. fill: new ol.style.Fill({
  93. color: '#ff0000'
  94. }),
  95. image: new ol.style.Circle({
  96. radius: 5,
  97. fill: new ol.style.Fill({
  98. color: '#ff0000'
  99. })
  100. })
  101. })
  102. })
  103. map.addLayer(intersectionLayer);
  104. })
  105. </script>
  106. </body>
  107. </html>

结果图如下:

6. 参考资料

[1]空间运算—ArcMap | 文档 (arcgis.com)

[2]Turf.js—让你在浏览器上实现地理分析 - 掘金 (juejin.cn)

[3]Turf.js | Advanced Geospatial Analysis (turfjs.org)

[4]Turf.js中文网 (fenxianglu.cn)

[5]Turfjs/turf: A modular geospatial engine written in JavaScript (github.com)

[6]使用 Fetch - Web API 接口参考 | MDN (mozilla.org)

[7]turf.js (openlayers.org)

[8]openlayers+turf.js实现缓冲区的绘制_gis_SSS的博客-CSDN博客_openlayers缓冲区分析

[9]OpenLayers v6.14.1 API - Class: EsriJSON

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