OpenLayers按属性从GeoJSON创建图层 [英] OpenLayers create Layers from GeoJSON by property
问题描述
我想使用OpenLayers v5.3.0创建一个Web应用程序.
I want to use OpenLayers v5.3.0 to create a Web application.
我能够将外部GeoJSON文件中的所有功能显示为OSM层上方的矢量层,但是我的GeoJSON文件包含成千上万个功能,每个功能都具有许多特性.
I’m able to show all features from a external GeoJSON File as a Vector Layer above a OSM Layer, but my GeoJSON File holds thousands of features, each enriched by a lot of properties.
我正在查看的是一种能够解析GeoJSON文件,按属性(例如,具有属性"gender": "f",
或"ethnic_gro": "Latvian",
的所有特征)对其进行过滤并仅将其显示为上方的矢量层的方法我的OSM基本层.
What I’m looking at is a way to be able to parse the GeoJSON File, filter it by properties (e.g. all features with the property "gender": "f",
or "ethnic_gro": "Latvian",
) and display only those as an additional Vector Layer above my OSM Base Layer.
这是我的GeoJSON文件的一个示例(已附加,只有一个功能):
This is an example of my GeoJSON File (abbriviated, with only one Feature):
{
"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对象:
You could use a javascript array filter on the original features array to build a new GeoJSON object:
var filteredGeoJSON = {
"type": "FeatureCollection",
"features": fullGeoJSON.features.filter(function(feature){
return (feature.properties.ethnic_gro == "Latvian" && feature.properties.gender == "f");
})
};
或使用forEach循环来构建过滤后的数组:
or use a forEach loop to build a filtered array:
var filteredGeoJSON = {
"type": "FeatureCollection",
"features": []
};
fullGeoJSON.features.forEach(function(feature){
if (feature.properties.ethnic_gro == "Latvian" && feature.properties.gender == "f") {
filteredGeoJSON.features.push(feature);
}
});
这篇关于OpenLayers按属性从GeoJSON创建图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!