Openlayers 从 Geojson 文件中获取值属性

Openlayers Get Value Properties from Geoson File

我有一个问题,我有一个 sqlite.geojson 文件,我想从中获取一些信息,例如,如何获取 "ORIENTATION" 值“30”?

这里是 sqlite.geojson

{
"type": "FeatureCollection",
"name": "TARGET",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "ID": 1, "COORDINATEX": "23", "COORDINATEY": "46", "ORIENTATION": "30" }, "geometry": null },
{ "type": "Feature", "properties": { "ID": 2, "COORDINATEX": "25", "COORDINATEY": "46", "ORIENTATION": "90" }, "geometry": null }
]
}

和Js代码

var vectorGeojson = new VectorSource({
  format: new GeoJSON(),
  url: 'data/sqlite.geojson'
});

var vectorGeojsonLayer = new VectorLayer({
  source: vectorGeojson,
  style: styleFunction
});

var map = new Map({
    controls: defaultControls().extend([mousePositionControl]),
    layers: [rasterLayer, vectorLayer, vectorGeojsonLayer],
    target: 'map',
    view: new View({
      center:fromLonLat([-46.68, -23.59]),
      zoom: 2
    })
});

如果您没有收到有关 Geojson 的任何加载错误并且它已正确加载,首先您需要通过以下方式从源代码中获取功能:

var features = vectorGeojson.getFeaturesCollection();

从图层中获取特征后,您现在可以通过循环迭代它们并获取您需要的属性,如下所示:

features.forEach(function(feature){
    var attribute = feature.get("attribute name");
    // Do something
});

这将为您提供所有功能的特定字段。另外,您可以添加一些 if 语句。例如,您只需要第二个 "ORIENTATION".

features.forEach(function(feature){
    if(feature.get("ID") == 2){
        var attribute = feature.get("attribute name");
        // Do something
    }
});

希望对您有所帮助。