Jin*_*inD 5 javascript json turfjs
如何将坐标的 JSON 数据提供给 turf.polygon?
示例: turf.polygon();
var polygon = turf.polygon([[
[-2.275543, 53.464547],
[-2.275543, 53.489271],
[-2.215118, 53.489271],
[-2.215118, 53.464547],
[-2.275543, 53.464547]
]], { name: 'poly1', population: 400});
Run Code Online (Sandbox Code Playgroud)
示例: JSON 形式
var json =
{
"type": "geojson",
"data":
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[
[1.275543, 54.464547], // I want to feed these coordinates
[1.275543, 54.489271],
[1.215118, 54.489271],
[1.215118, 54.464547],
[1.275543, 54.464547]
]]
}
}]
}
}
Run Code Online (Sandbox Code Playgroud)
我的伪代码:这不起作用并返回错误消息"LinearRing of a Polygon must have 4 or more Positions."
var polygon = turf.polygon([[ json.data.features[0].geometry.coordinates ]], { name: 'poly1', population: 400});
Run Code Online (Sandbox Code Playgroud)
谢谢。
该coordinates属性已经是一个深度嵌套的数组:
"coordinates": [[
[1.275543, 54.464547], // I want to feed these coordinates
[1.275543, 54.489271],
[1.215118, 54.489271],
[1.215118, 54.464547],
[1.275543, 54.464547]
]]
Run Code Online (Sandbox Code Playgroud)
当你这样做时:
var polygon = turf.polygon([[
json.data.features[0].geometry.coordinates
]], ...
Run Code Online (Sandbox Code Playgroud)
它将决定:
var polygon = turf.polygon([[
[[
[1.275543, 54.464547], // I want to feed these coordinates
[1.275543, 54.489271],
[1.215118, 54.489271],
[1.215118, 54.464547],
[1.275543, 54.464547]
]]
]], ...
Run Code Online (Sandbox Code Playgroud)
您想要提取并使用原始嵌套数组本身,而不进行修改。尝试这个:
var polygon = turf.polygon(json.data.features[0].geometry.coordinates, { name: 'poly1', population: 400});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6156 次 |
| 最近记录: |