我正在尝试将地区名称和边界从geojson导出到csv。这是我的数据示例:
{
"type" : "FeatureCollection",
"features" : [{
"type":"Feature",
"properties" : {
"id" : "72639",
"alltags" : {
"name:en" : "Area1",
"admin_level" : "4"}
},
"geometry": {"type":"MultiPolygon","coordinates":[[[[36.407,45.071],[36.408,45.072],[36.406,45.071],[36.407,45.071]]],[[[35.082,45.611],[35.084,45.605],[35.082,45.611],[35.082,45.611]]]]}
},{
"type":"Feature",
"properties" : {
"id" : "71245",
"alltags" : {
"name:en" : "Area2",
"admin_level" : "4"}
},
"geometry": {"type":"MultiPolygon","coordinates":[[[[27.189,50.625],[29.196,51.607],[27.195,50.561],[27.190,50.624],[27.189,50.625]]]]}
} ]
}
我想使用'name:en'输出并像这样进行协调:
Area1
[[[[36.407,45.071],[36.408,45.072],[36.406,45.071],[36.407,45.071]]],[[[35.082,45.611],[35.084,45.605],[35.082,45.611],[35.082,45.611]]]]
Area2
[[[[27.189,50.625],[29.196,51.607],[27.195,50.561],[27.190,50.624],[27.189,50.625]]]]
我使用以下脚本:
$path = "D:\!map\!test\"
$name_in = "test.GeoJson"
$coords_pattern = '"value":(.*?),"Count"'
$inputjson = Get-Content -Raw -Path $path$name_in | ConvertFrom-Json
Foreach ($feature in $inputjson.features){
$area_name = $feature.properties.alltags.'name:en'
$geojson = $feature.geometry
$coordinates = $geojson.coordinates | ConvertTo-Json -Compress
$borders = [regex]::match($coordinates, $coords_pattern).Groups[1].Value
Write-Host $area_name
Write-Host $borders
}
对于Area2,一切正常,但是Area1由2组坐标组成,并且Area1的输出“已损坏”:
Area1
[["36.407 45.071","36.408 45.072","36.406 45.071","36.407 45.071"]]
Area2
[[[27.189,50.625],[29.196,51.607],[27.195,50.561],[27.190,50.624],[27.189,50.625]]]
在真实数据中,区域可能具有未知数量的坐标组。如何获得正确的坐标集?
答案 0 :(得分:2)
使用-replace
可能更简单:
$inputjson = $geo | ConvertFrom-Json
Foreach ($feature in $inputjson.features){
$area_name = $feature.properties.alltags.'name:en'
$geojson = $feature.geometry
$coordinates = ($geojson.coordinates | ConvertTo-Json -Compress -Depth 4) -replace '\{"value":|,"Count":1\}|"', ''
Write-Host $area_name
Write-Host $coordinates
}
输出:
Area1 [[[[36.407,45.071],[36.408,45.072],[36.406,45.071],[36.407,45.071]]],[[[35.082,45.611],[35.084,45.605],[35.082,45.611],[35.082,45.611]]]] Area2 [[[27.189,50.625],[29.196,51.607],[27.195,50.561],[27.190,50.624],[27.189,50.625]]]
我不知道坐标数组的嵌套有多深,因此您必须使用-Depth
cmdlet 上的ConvertTo-Json
参数 >