Openlayer 计算多个feature的外接矩形,并且缩放到合适的视角显示

5 篇文章 0 订阅
1 篇文章 0 订阅

开发gis系统的时候需要点击一个工程然后打开openlayers地图并且将该工程的线条缩放到合适的区域,对这个问题的解决方案:

1.旋转卡壳法求点集的最小覆盖矩形面积以及周长

https://www.cnblogs.com/mypsq/p/4348243.html

2.计算最小最大经纬度获取外接矩形

https://blog.csdn.net/lihefei_coder/article/details/104997663

本文介绍一种另类的思路来解决这一方式,openlayer提供了feature的getExtent()来获取一个,但是如何获取多个呢?

(多亏公司的前辈给了提示,还是经验最重要!)方法是将多个feature当作一个feature来计算

代码:

// 计算外接矩形
        let str = "http://192.168.0.188:9100/geoserver/szygis/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=szygis:pm_spatial_design_pipeline&outputFormat=application%2Fjson&PROPERTYNAME=shape,tenantid,linename,devicemodel,pointlength,depth,projectid,sectionid,jobid,createtime,createuid,color&CQL_FILTER=tenantid=1 and projectid='09f2d433-7772-499b-a7ca-ae9e002471c6'";
        this.$http.get(str).then(res=>{
          let vectorSource = new VectorSource({
            format: new GeoJSON()
          })
          let readFeatures = vectorSource.getFormat().readFeatures(res)
          let cords = [];
          for(let i=0;i<readFeatures.length;i++) {
            let coordinates = readFeatures[i].getGeometry().getCoordinates()
            cords = cords.concat(coordinates);
          }
          let testFeature = new Feature(
            {
              geometry: new LineString(cords)
            }
          );
          let extent = testFeature.getGeometry().getExtent()
          let ex1 = [extent[0],extent[1]]
          let ex2 = [extent[2],extent[3]]

          // 将4326转换为3857
          ex1 = transform(ex1, 'EPSG:4326', 'EPSG:3857')
          ex2 = transform(ex2, 'EPSG:4326', 'EPSG:3857')
          extent = [ex1[0],ex1[1],ex2[0],ex2[1]]

          store.commit('setExtent',extent)
          this.$router.push('/openLayersMap')
        })

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值