openlayer + turf 面截取线长度

今天需要计算无人机航线的有效里程,需要用到截取线的长度,在网上没有搜索到方法,就只好自己写一个,实际情况可能没有考虑全面,请勿喷!

        //创建展示图层
 function createTmpVector() {
            taskInfoManageVectorSource = new ol.source.Vector({
                format: new ol.format.GeoJSON()
            });
            taskInfoManageVectorLayer = new ol.layer.Vector({
                id: 'taskInfoManageVectorLayer',
                type: 'mainVector',
                source: taskInfoManageVectorSource,
                style: function (feat) {
                    var style = feat.get('info');
                    if (style.style == "1") {
                        return style1(feat);
                    } else {
                        return style2(feat);
                    }

                },
                zIndex: 99
            })
            map.addLayer(taskInfoManageVectorLayer)
        }
//原样式
   var style1 = function () {
            return new ol.style.Style({
                fill: new ol.style.Fill({
                    color: 'rgba(255, 255, 255, 1)'
                }),
                stroke: new ol.style.Stroke({
                    color: '#ffcc33',
                    width: 1
                })
            })
        }

//有效航线样式
        var style2 = function (feature) {
            var geometry = feature.getGeometry();
            var styles = [
                new ol.style.Style({
                    fill: new ol.style.Fill({
                        color: '#0044CC'
                    }),
                    stroke: new ol.style.Stroke({
                        color: '#fff',
                        width: 4
                    })
                })
            ];

            return styles;
        };

 //查询数据
        function searchData() {
            var line1 = turf.lineString([[105.3171, 33.3152], [105.4188, 33.0291], [105.4866, 33.0653], [105.4630, 32.9656], [105.3171, 32.8129]]);
            var feature = new ol.format.GeoJSON().readFeatures(line1);
            feature[0].set('info', { style: '1' });
            taskInfoManageVectorSource.addFeature(feature[0]);//地图上画出整条

//我用的wfs直接从服务上获取所需的面域
            var data = {
                "service": "wfs",
                "version": "1.1.0",
                "request": "GetFeature",
                "typeName": 'layer',//图层
                "outputFormat": "application/json",
                "cql_filter": "1=1"
            };
            $.ajax({
                url: 'wfsurl',//wfs服务地址
                data: data,
                type: "GET",
                contentType: "text/plain;charset=UTF-8",
                success: function (data) {
                    var features = new ol.format.GeoJSON().readFeatures(data);
                    var countLong = 0;
                    if (features.length > 0) {
//遍历处理面区域,将面转为线
                        features.forEach(element => {
                            var polygon = new ol.format.GeoJSON().writeFeatureObject(element, { rightHanded: false });
                            var splitter = turf.polygonToLine(polygon);
                            countLong += getInterSectLong(line1, splitter, polygon);
                        });
                        console.log(countLong);
                    } else {
                        alert('没有查询到功能区划数据!');
                    }
                },
                error: function (data) {
                    alert('获取功能区划数据失败!');
                }
            });
            return;
        }

//处理面,判断面转为线后是否为多线
 function getInterSectLong(line, splitter, polygon) {
            var split;
            var longSum = 0;
            if (splitter.type == 'Feature') {
                split = turf.lineSplit(line, splitter);
                longSum += getLineLong(split, polygon, true);
            } else if (splitter.type == 'FeatureCollection') {
                splitter.features.forEach(element => {
                    split = turf.lineSplit(line, element);
                    longSum += getLineLong(split, polygon, false);
                })
            } else {
                longSum += 0;
            }

            return longSum;
        }

//获取长度
        function getLineLong(split, polygon, flag) {
            var features = split.features;
            var longSum = 0;
            var addfeatures = [];
            if (features.length > 0) {
                for (var i = 0; i < features.length - 1; i++) {
                    if (flag) {//不是多线
                        if (turf.booleanContains(polygon, features[i])) {
                            longSum += turf.length(features[i], { units: 'kilometers' });
                            var feature = new ol.format.GeoJSON().readFeatures(features[i]);
                            feature[0].set('info', { style: '2' });
                            addfeatures.push(feature[0]);
                        }
                    } else {//多线
                        var center = turf.center(features[i]);//计算每段线的绝对中心,判断是否在面内部,是则计算此线段长度
                        if (turf.booleanPointInPolygon(center, polygon)) {
                            longSum += turf.length(features[i], { units: 'kilometers' });
                            var feature = new ol.format.GeoJSON().readFeatures(features[i]);
                            feature[0].set('info', { style: '2' });
                            addfeatures.push(feature[0]);

                        }
                    }
                }
            }
            taskInfoManageVectorSource.addFeatures(addfeatures);
            return longSum;
        }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值