【问题标题】:Openlayers Get Value Properties from Geoson FileOpenlayers 从 Geojson 文件中获取值属性
【发布时间】:2019-10-09 19:30:47
【问题描述】:

我有一个问题,我有一个 sqlite.geojson 文件,我想从中获取一些信息,例如,我怎样才能获得“30”的“ORIENTATION”值?

这里是 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
    })
});

【问题讨论】:

  • 如果要素已加载到图层源中,请使用feature.get("ORIENTATION")

标签: javascript openlayers geojson


【解决方案1】:

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

var features = vectorGeojson.getFeaturesCollection();

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

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

这将为您提供所有功能的特定字段。此外,您可以添加一些 if 语句。例如,您只需要第二个“方向”。

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 2017-07-15
    • 2013-02-08
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多