简体   繁体   English

OpenLayers按属性从GeoJSON创建图层

[英]OpenLayers create Layers from GeoJSON by property

I want to use OpenLayers v5.3.0 to create a Web application. 我想使用OpenLayers v5.3.0创建一个Web应用程序。

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文件中的所有功能显示为OSM层上方的矢量层,但是我的GeoJSON文件包含成千上万个功能,每个功能都具有许多特性。

What I'm looking at is a way to be able to parse the GeoJSON File, filter it by properties (eg 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文件,按属性过滤的方法(例如,所有具有属性"gender": "f","ethnic_gro": "Latvian", )仅显示这些作为我的OSM基本层之上的附加向量层。

This is an example of my GeoJSON File (abbriviated, with only one Feature): 这是我的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",
      }
    }
  ]
}

Thanks for any help! 谢谢你的帮助!

You could use a javascript array filter on the original features array to build a new GeoJSON object: 您可以在原始要素数组上使用javascript数组过滤器来构建新的GeoJSON对象:

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

or use a forEach loop to build a filtered array: 或使用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);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM