我有一个问题,我有一个 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/