javascript - Openlayers 从 Geojson 文件获取值属性

标签 javascript openlayers geojson

我有一个问题,我有一个 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
    }
});

希望有帮助。

关于javascript - Openlayers 从 Geojson 文件获取值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58311051/

相关文章:

javascript - 从表单输入创建多维关联数组

javascript - 你如何在Javascript中的对象数组中找到属性的最小值和最大值

html - OpenLayer.Popup.FramedCloud 中无响应的 onclick 事件

php - 解析巨大的GeoJSON文件并获取特定属性的多边形坐标

javascript - 键盘快捷键 ctrl+r 对我不起作用

for循环内的Javascript递归函数

php - 在 openlayers 3 中单击标记时显示弹出窗口

javascript - 添加特征之前进行 OpenLayers 拟合

python - Apache Beam Pipeline 与 DirectRunner 一起运行,但在初始读取步骤期间因 DataflowRunner(SDK 线束 sdk-0-0 断开连接)而失败

java - 使用java将java字符串转换为geojson