MapBox获取点位高程的三种方式

贴个群号

WebGIS学习交流群461555818,欢迎大家。

以下提供了三种方法和思路

1,通过mapbox全球dem数据获取高程

这里我们利用了mapbox的tilequery
官网地址在这里

https://docs.mapbox.com/api/maps/tilequery/

以下是示例代码,这个方式是简单快捷,缺点就是精度不高,大概是以10m为一个跨度,而且不带建筑、树木等的高度

        this.map.on('click',e=>{
          var lngLat = e.lngLat.toArray();
          var url = 'https://api.mapbox.com/v4/mapbox.mapbox-terrain-v2/tilequery/' + lngLat[0] + ',' + lngLat[1]+'.json?layers=contour&access_token='+mapboxgl.accessToken;
          var xhr = new XMLHttpRequest();
          xhr.open('GET', url);
          xhr.setRequestHeader('Content-Type','application/json');
          xhr.onload = function (){
          if (xhr.status === 200) {
            var data = JSON.parse(xhr.responseText);
            var elevation = data.features[0].properties.ele;
            console.log('当前位置高度为:' + elevation);
            }
          }
          xhr.send()
        })

2、加载dem模型

这里我们是加载dem。mapbox如果想要加载dem的话,需要mapbox自己特定的一种格式
在这里插入图片描述
加载成功后然后通过这个方法来获取高程

在这里插入图片描述
具体示例代码,有这个官方地址

https://docs.mapbox.com/mapbox-gl-js/example/query-terrain-elevation/

3、通过geoserver发布dem服务,使用geoserver自带的GetFeatureInfo服务获取高程

首先这里放一下4D数据是什么意思
在这里插入图片描述
可以看到我们这里其实最好是使用dsm,因为这个还会附带建筑、树木的高程,这样我们在地图上向下点的时候,如果是点击的有建筑或者树木的话,仍然可以获取比较精确的高程数据。

然后这里的dsm数据源,我们取自于倾斜摄影,mapbox现在无法从倾斜摄影上获取高程,只能采取这样的办法曲线救国,先从倾斜摄影转为dsm,别的软件不知道,大势智慧可以做这一步,然后dsm的话,用geoserver发布出去,GetFeatureInfo拿不到高程,应该是只支持dem,所以我们还需要将dsm转化为dem。
转化为dem之后,我们就用geoserver将其发布成服务。预览之后在这里可以点击地图上的要素获取高程,查看网络那里就可以拿到请求的接口,值的注意的是他这个接口的格式是html的,我们需要将其转化为json的。在这里插入图片描述
具体参数在geoserver的官网上面都有,不过直接从这里截取一下请求路径,改一下也是可以的。
然后在代码里面,就可以获取到高程啦

map.on('click',e=>{
                let point = e.lngLat; //鼠标单击点的坐标
                let offset = 0.00001;
                let minx = point.lng - offset;
                let miny = point.lat - offset;
                let maxx = point.lng + offset;
                let maxy = point.lat + offset;
                var url = geoserverUrl+'/services/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&FORMAT=image%2Fjpeg&TRANSPARENT=true&QUERY_LAYERS=services%3A'+layerId+'&STYLES&LAYERS=services%3A'+layerId+'&exceptions=application%2Fvnd.ogc.se_inimage&INFO_FORMAT=application%2Fjson&FEATURE_COUNT=50&X=50&Y=50&SRS=EPSG%3A4326&WIDTH=101&HEIGHT=101&BBOX=' + encodeURIComponent(minx + "," + miny + "," + maxx + "," + maxy)
                let res = await fetch(url).then(res=>res.json())
                console.log('当前位置高度为:' + res.features[0].properties.GRAY_INDEX.toFixed(2));
})
要根据point获取点位信息,可以使用Mapbox的Feature State功能。具体步骤如下: 1.Mapbox官网注册账户并创建一个项目。 2. 在项目中添加地图数据源和图层。 3. 在数据源中添加点位信息,并为每个点位设置一个唯一的ID。 4. 在图层中使用数据源中的点位信息,并将点位ID设置为图层的id属性。 5. 在前端代码中监听地图的点击事件,获取点击点的坐标。 6. 使用Mapbox的queryRenderedFeatures方法查询当前点击点下面的点位信息,并获取点位的ID。 7. 使用Mapbox的setFeatureState方法设置该点位的状态为选中状态。 8. 在前端页面中根据该点位的状态展示对应的信息。 以下是示例代码: ```javascript // 添加地图数据源和图层,其中点位数据源名为 'points',点位图层名为 'points-layer' map.addSource('points', { type: 'geojson', data: { type: 'FeatureCollection', features: [ { type: 'Feature', properties: { name: 'Point 1' }, geometry: { type: 'Point', coordinates: [100, 0] }, id: 'point-1' }, { type: 'Feature', properties: { name: 'Point 2' }, geometry: { type: 'Point', coordinates: [101, 1] }, id: 'point-2' } ] } }); map.addLayer({ id: 'points-layer', type: 'circle', source: 'points', paint: { 'circle-radius': 10, 'circle-color': '#007cbf' } }); // 监听地图点击事件 map.on('click', function(e) { // 获取点击点的坐标 var point = e.point; // 查询当前点击点下面的点位信息,并获取点位的ID var features = map.queryRenderedFeatures(point, { layers: ['points-layer'] }); var feature = features[0]; var id = feature.properties.id; // 设置该点位的状态为选中状态 map.setFeatureState({ source: 'points', id: id }, { selected: true }); }); // 监听地图移动事件,恢复所有点位的状态为未选中状态 map.on('move', function() { var features = map.queryRenderedFeatures({ layers: ['points-layer'] }); features.forEach(function(feature) { var id = feature.properties.id; map.setFeatureState({ source: 'points', id: id }, { selected: false }); }); }); // 在前端页面中根据点位状态展示对应信息 map.on('click', 'points-layer', function(e) { var name = e.features[0].properties.name; var selected = e.features[0].state.selected; if (selected) { // 展示选中状态的信息 console.log(name + ' is selected'); } else { // 展示未选中状态的信息 console.log(name + ' is not selected'); } }); ``` 需要注意的是,使用Feature State功能需要在代码中设置Mapbox的版本为1.6.0及以上,并且需要在图层中设置state属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值