【发布时间】:2020-07-30 18:16:50
【问题描述】:
我想在 Python 中以有限的坐标精度序列化 GeoJSON FeatureCollections。
例如,这是一个 FeatureCollection(在 Python 中表示为 dicts 和 lists):
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Liberty Island",
"area_sqm": 24950.40123456,
"established": 1875,
"height_ft": 305
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[ -74.04715418815613, 40.690994683044906 ],
[ -74.04499769210815, 40.68873311507798 ],
[ -74.04354929924011, 40.689676800252016 ],
[ -74.04715418815613, 40.690994683044906 ]
]
]
}
}
]
}
我可以使用json.dumps对其进行序列化:
print(json.dumps(fc))
这会打印出 JSON:
{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"name": "Liberty Island", "area_sqm": 24950.40123456, "established": 1875, "height_ft": 305}, "geometry": {"type": "Polygon", "coordinates": [[[-74.04715418815613, 40.690994683044906], [-74.04499769210815, 40.68873311507798], [-74.04354929924011, 40.689676800252016], [-74.04715418815613, 40.690994683044906]]]}}]}
那些坐标太精确了。根据 Wikipedia 的说法,7 digits is ~cm precision 应该已经足够好了。我得到的是~纳米精度。
我想序列化 GeoJSON FeatureCollection,坐标只有七位精度。请注意,我想对 properties 中的所有内容使用 Python 的默认序列化:因为这些值可能是任何东西,所以我不能就多少精度就足够了。
我想要的输出是这样的:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Liberty Island",
"area_sqm": 24950.40123456,
"established": 1875,
"height_ft": 305
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[ -74.0471541, 40.6909946 ],
[ -74.0449976, 40.6887331 ],
[ -74.0435492, 40.6896768 ],
[ -74.0471541, 40.6909946 ]
]
]
}
}
]
}
【问题讨论】:
标签: python json python-3.x geojson