我想用Python序列化GeoJSON FeatureCollections,但其坐标精度有限
例如,这里有一个FeatureCollection(在Python中表示为dict
和list
):
{
"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]]]}}]}
那些坐标太精确了。根据维基百科,7 digits is ~cm precision,这应该足够好了。我得到的是~纳米精度
我想序列化GeoJSON FeatureCollection,其坐标精度仅为7位。请注意,我想对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 ]
]
]
}
}
]
}
我不确定您想要的是什么样的序列化,但是当涉及到
FeatureCollection
简化时,这应该让您开始:其中
shorten_arr
是一个平凡的递归:相关问题 更多 >
编程相关推荐