openlayers结合turf.js 做空间分析

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

1 具体思路

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

2 效果和代码如下

import * as ol from 'ol';
import * as layer from 'ol/layer';
import * as source from 'ol/source';
import * as style from 'ol/style';
import * as geom from 'ol/geom';
import 'ol/ol.css';
import turf from "turf";
var map = new ol.Map({
  layers:[
    new layer.Tile({
        source:new source.OSM()
    })
  ],
  target: "map",
  view: new ol.View({
    center: [122.520217, 45.535693],
    zoom: 9,
    projection: "EPSG:4326"
  })
});
// 创建source对象
var VectorSource = new source.Vector({
  features: []            // 值是一个feature数组   source:features  1:N
});
// 创建layer对象
var VectorLayer = new layer.Vector({
  source: VectorSource    // layer-source  1:1
});
//将layer添加到map
map.addLayer(VectorLayer);
var pt1 = [
  [
    [122.801742, 45.48565],
    [122.801742, 45.60491],
    [122.584762, 45.60491],
    [122.584762, 45.48565],
    [122.801742, 45.48565]
  ]
];
var pt2 = [
  [
    [122.520217, 45.535693],
    [122.64038, 45.553967],
    [122.720031, 45.526554],
    [122.669906, 45.507309],
    [122.723464, 45.446643],
    [122.532577, 45.408574],
    [122.487258, 45.477466],
    [122.520217, 45.535693]
  ]
];

var poly1 = turf.polygon(
  [
    pt1[0]
  ],
  {
    name: "大头1",
    age: "280"
  }
);
var poly2 = turf.polygon(
  [
    pt2[0]
  ],
  {
    name: "大头",
    age: "28"
  }
);
/**
 * 创建两个多边形到地图
 */
function createPolygon(ptArr) {
  var feature = new ol.Feature({
    geometry: new geom.Polygon(ptArr)
  });
  VectorSource.addFeature(feature);
}
createPolygon(pt1);
createPolygon(pt2);

// 获取相交几何坐标
var intersection = turf.intersect(poly1, poly2);
var geometry1 = intersection.geometry.coordinates;
// 创建相交图层
var IntersectLayer = new layer.Vector({
  source: new source.Vector({
    features: [
      new ol.Feature({
        geometry: new geom.Polygon(geometry1)
      })
    ] 
  })
});
// 定义相交样式
var IntersectStyle= new style.Style({
  stroke: new style.Stroke({
    color: "blue",
    width: 3
  }),
  fill: new style.Fill({
    color: "rgba(202, 12, 22, 0.5)"
  })
});
IntersectLayer.setStyle(IntersectStyle);
// 添加相交图层到地图
map.addLayer(IntersectLayer);

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨大大28

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值