在mapbox-gl-js版本:2.10.0中,我单击地图上的一个点,并从2个矢量图层(application/vnd.mapbox-vector-tile)和一个geojson层数据获取所有功能:'http:/ /localhost:8080/geoserver/mydata/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=mydata%3Acontexts&outputFormat=application%2Fjson' , ,全部由 GeoServer 提供服务
在 Geoserver 的商店中,我启用了“公开主键”,因此在功能属性中,我有来自数据库的 ID
我尝试删除mapbox可能根据其properties.id创建的重复项(有时mapbox会在矢量和geojson中创建相同特征的重复项,具体取决于缩放级别)
然后我想检查第一个向量层中是否有多个特征。如果这样做,我会创建一些对象,包括来自其他图层的数据。要查看第一层中的内容属于哪个功能,我会根据它们的properties.id进行操作,但有时两个功能都是相同的,而功能id不同。我认为properties.id始终保持不变,并且在多个功能上不会重复。
这是我的代码
map.current.on('click', (e) => {
//get point
const point = e.point;
//get all features from all layers from that point
let features = map.current.queryRenderedFeatures(point);
//separate features by layer
let excavations = features.filter(f => f.layer.id === 'excavations-layer');
let parts = features.filter(f => f.layer.id === 'excavation_parts-layer');
let contexts = features.filter(f => f.layer.id === 'contexts-layer');
//removing duplicates
excavations = excavations.filter((value, index, self) =>
index === self.findIndex((et) => (
et.properties.id === value.properties.id
))
)
parts = parts.filter((value, index, self) =>
index === self.findIndex((pt) => (
pt.properties.id === value.properties.id
))
)
contexts = contexts.filter((value, index, self) =>
index === self.findIndex((ct) => (
ct.properties.id === value.properties.id
))
)
//organize data per excavation, based on ids
excavations.forEach(e => {
ex_excavations = excavations.filter((value) =>
e.properties.id == value.properties.id
)
ex_parts = parts.filter((value) =>
e.properties.id = value.properties.excavation_id
)
ex_contexts = contexts.filter((value) =>
turf.booleanIntersects(value,e)
)
exgroupArray.push(
{
name:e.properties.name,
excavations:ex_excavations,
parts:ex_parts,
contexts:ex_contexts
}
)
console.log('e id loop ', e.id);
console.log('exgroupArray ', exgroupArray);
console.log('====================');
});
我执行第一个 forEach excavations.forEach(e => { 的原始挖掘数组具有不同的特征 id (12,13) ,但具有相同的properties.id (12,12)
其他时间具有相同的功能 id (12,12) ,但不同的properties.id (12,13)
查看图片
我认为properties.id来自Geoserver,并且对于每个功能应该始终是不同的。
我尝试将我的代码基于此,但因为它发生了变化,它弄乱了我的最终 exgroupArray 并且无法在 HTML 中正确循环它。
有什么帮助可以解决这个问题吗?
谢谢
编辑:看起来如果我完全缩小,我会得到相同的功能ID,不同的properties.id,检查第二张图片
我遇到了类似的问题。我相信会发生什么,直到明确指示 Mapbox 为特定图块中的每个要素生成唯一的 ID。
因此,不同图块中的功能将获得相同的数字 ID。
但是有一种方法可以解决它。您可以通过here浏览文档。 PromotionId 配置明确告诉 Mapbox 不要生成新 ID,而是使用属性部分中的特定字段。
这是示例代码:
map.addSource('earthquakes', {
"type": "vector",
tiles: ["http://localhost:8081/api/asset/tile/{z}/{x}/{y}.pbf"],
"promoteId": {"LAYER_NAME": "id"}
});
在此示例代码中,我仅覆盖一个集群层 LAYER_NAME 的 ID,并且我从属性中使用的字段名为 id
希望这会对某人有所帮助^^
快乐编码!!