gpt4 book ai didi

javascript - OpenLayers 通过属性从 GeoJSON 创建图层

转载 作者:行者123 更新时间:2023-12-01 01:31:16 24 4
gpt4 key购买 nike

我想使用 OpenLayers v5.3.0 创建一个 Web 应用程序。

我能够将外部 GeoJSON 文件中的所有功能显示为 OSM 层上方的向量层,但我的 GeoJSON 文件包含数千个功能,每个功能都包含许多属性。

我正在寻找一种能够解析 GeoJSON 文件、按属性过滤它的方法(例如,具有属性 "gender": "f", 的所有功能>"ethnic_gro": "Latvian",) 并仅将其显示为 OSM 基础层上方的附加矢量图层。

这是我的 GeoJSON 文件的示例(缩写,只有一个功能):

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[23.114870000000053, 56.845980000000134],
[19.131050000000164, 50.65641000000013]
]
},
"properties": {
"OID_": "0",
"ethnic_gro": "Latvian",
"religion": "protestant",
"marital_st": "widow",
"status_in_": "NULL",
"gender": "f",
}
}
]
}

感谢您的帮助!

最佳答案

您可以在原始特征数组上使用 JavaScript 数组过滤器来构建新的 GeoJSON 对象:

var filteredGeoJSON = {
"type": "FeatureCollection",
"features": fullGeoJSON.features.filter(function(feature){
return (feature.properties.ethnic_gro == "Latvian" && feature.properties.gender == "f");
})
};

或使用 forEach 循环构建过滤数组:

var filteredGeoJSON = { 
"type": "FeatureCollection",
"features": []
};
fullGeoJSON.features.forEach(function(feature){
if (feature.properties.ethnic_gro == "Latvian" && feature.properties.gender == "f") {
filteredGeoJSON.features.push(feature);
}
});

关于javascript - OpenLayers 通过属性从 GeoJSON 创建图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com