码迷,mamicode.com
首页 > Web开发 > 详细

openlayers+turf.js绘制线缓冲区

时间:2020-04-22 16:57:06      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:article   读取   文档   this   point   cto   class   task   实现   

最近再做缓冲区的绘制,然后在网上各种查资料,最后决定用turf来实现;
刚开始这样写的,然后不出现缓冲区
      var line = turf.lineString([[8940117.74537665,5936319.233970245],[10407708.688452033,5740640.441560194],[10466412.326175049,5359066.796360594]]);
      var buffered = turf.buffer(line , val, {units: ‘kilometers‘});
      //创建数据geojson对象和数据源对象
      var format = new GeoJSON();
      var source = new VectorSource({ wrapX: false });
      // //读取geojson数据
      var lineFeature  = format.readFeature(line);
      var bufferFeature = format.readFeature(buffered);
      // //将数据添加数据源的
      source.addFeature(lineFeature);
      source.addFeature(bufferFeature);
      var test = new VectorLayer({ source: source })
      this.map.addLayer(test)
并且还如下得错误
技术图片
然后开始在网上查资料后,知道turf缓冲区不能用EPSG:3857的坐标系,需要转成EPSG:4326(关于坐标系不清楚得,可以看这里)
然后我转过后再试验
      let coordinates = ‘[[8940117.74537665,5936319.233970245],[10407708.688452033,5740640.441560194],[10466412.326175049,5359066.796360594]]‘;
      let bufferCoordinates = [],pointTransform=[];
      for (var i = 0; i < coordinates.length; i++) {
          pointTransform = transform(coordinates[i], "EPSG:3857", "EPSG:4326");
          bufferCoordinates.push(pointTransform);
        }
        var line = turf.lineString(bufferCoordinates);
        var buffered = turf.buffer(line , val, {units: ‘kilometers‘});
      //创建数据geojson对象和数据源对象
      var format = new GeoJSON();
      var source = new VectorSource({ wrapX: false });
      // //读取geojson数据
      var lineFeature = format.readFeature(line);
      var bufferFeature = format.readFeature(buffered);
      // //将数据添加数据源的
      source.addFeature(lineFeature);
      source.addFeature(bufferFeature);
      var test = new VectorLayer({ source: source })
      this.map.addLayer(test)
到了这里发现虽然不报错了,但是结果还是不出来,然后这样看来代码没问题,我这个时候就开始写demo,然后在demo中查看发现运行是没问题得;
那就奇了怪了,到底是哪里还有问题,最后又看了一遍官方文档,然后加上了以下两行代码
lineFeature.getGeometry().transform(‘EPSG:4326‘, ‘EPSG:3857‘);
bufferFeature.getGeometry().transform(‘EPSG:4326‘, ‘EPSG:3857‘);

再次运行,成功出现缓冲区:

技术图片

 

 参考:openlayers+turf.js实现缓冲区得绘制

 

openlayers+turf.js绘制线缓冲区

标签:article   读取   文档   this   point   cto   class   task   实现   

原文地址:https://www.cnblogs.com/KoKoLi/p/12745554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!