当前位置:   article > 正文

openlayers结合turf.js 做空间分析_基于turf.js的空间分析

基于turf.js的空间分析

turf.js是一个空间分析的gis库,封装了很多常用的空间分析算法,比如相交,缓冲,点在面内等等;在做gis开发的时候,gis渲染引擎结合该库非常方便,不需要自己动手做空间分析的相关计算;本文用openlayers结合turf.js做个简单的相交分析

1 具体思路

 使用openlayers创建两个多边形,然后利用turf.js的turf.intersect方法做空间相交分析,得到两个多边形相交的几何部分,然后用openlayers绘制高亮凸显出来。

2 效果和代码如下

  1. import * as ol from 'ol';
  2. import * as layer from 'ol/layer';
  3. import * as source from 'ol/source';
  4. import * as style from 'ol/style';
  5. import * as geom from 'ol/geom';
  6. import 'ol/ol.css';
  7. import turf from "turf";
  8. var map = new ol.Map({
  9. layers:[
  10. new layer.Tile({
  11. source:new source.OSM()
  12. })
  13. ],
  14. target: "map",
  15. view: new ol.View({
  16. center: [122.520217, 45.535693],
  17. zoom: 9,
  18. projection: "EPSG:4326"
  19. })
  20. });
  21. // 创建source对象
  22. var VectorSource = new source.Vector({
  23. features: [] // 值是一个feature数组   source:features  1:N
  24. });
  25. // 创建layer对象
  26. var VectorLayer = new layer.Vector({
  27. source: VectorSource // layer-source  1:1
  28. });
  29. //将layer添加到map
  30. map.addLayer(VectorLayer);
  31. var pt1 = [
  32. [
  33. [122.801742, 45.48565],
  34. [122.801742, 45.60491],
  35. [122.584762, 45.60491],
  36. [122.584762, 45.48565],
  37. [122.801742, 45.48565]
  38. ]
  39. ];
  40. var pt2 = [
  41. [
  42. [122.520217, 45.535693],
  43. [122.64038, 45.553967],
  44. [122.720031, 45.526554],
  45. [122.669906, 45.507309],
  46. [122.723464, 45.446643],
  47. [122.532577, 45.408574],
  48. [122.487258, 45.477466],
  49. [122.520217, 45.535693]
  50. ]
  51. ];
  52. var poly1 = turf.polygon(
  53. [
  54. pt1[0]
  55. ],
  56. {
  57. name: "大头1",
  58. age: "280"
  59. }
  60. );
  61. var poly2 = turf.polygon(
  62. [
  63. pt2[0]
  64. ],
  65. {
  66. name: "大头",
  67. age: "28"
  68. }
  69. );
  70. /**
  71. * 创建两个多边形到地图
  72. */
  73. function createPolygon(ptArr) {
  74. var feature = new ol.Feature({
  75. geometry: new geom.Polygon(ptArr)
  76. });
  77. VectorSource.addFeature(feature);
  78. }
  79. createPolygon(pt1);
  80. createPolygon(pt2);
  81. // 获取相交几何坐标
  82. var intersection = turf.intersect(poly1, poly2);
  83. var geometry1 = intersection.geometry.coordinates;
  84. // 创建相交图层
  85. var IntersectLayer = new layer.Vector({
  86. source: new source.Vector({
  87. features: [
  88. new ol.Feature({
  89. geometry: new geom.Polygon(geometry1)
  90. })
  91. ]
  92. })
  93. });
  94. // 定义相交样式
  95. var IntersectStyle= new style.Style({
  96. stroke: new style.Stroke({
  97. color: "blue",
  98. width: 3
  99. }),
  100. fill: new style.Fill({
  101. color: "rgba(202, 12, 22, 0.5)"
  102. })
  103. });
  104. IntersectLayer.setStyle(IntersectStyle);
  105. // 添加相交图层到地图
  106. map.addLayer(IntersectLayer);

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

闽ICP备14008679号