一个用于在GeoJSON中强制执行多边形环绕行顺序的Python库
项目描述
geojson-rewind
一个用于在GeoJSON中强制执行多边形环绕行顺序的Python库
线性环必须遵循相对于其包围区域的右手定则,即外部环为逆时针,孔为顺时针。
这有助于您生成符合规定的多边形和MultiPolygon几何形状。
注意:输入数据中的坐标假定是WGS84,(lon, lat)顺序,如RFC 7946中所述。使用任何其他CRS的坐标可能导致意外结果。
安装
pip install geojson-rewind
用法
作为库
强制执行RFC 7946环绕行顺序(输入/输出为GeoJSON字符串)
>>> from geojson_rewind import rewind
>>> input = """{
... "geometry": { "coordinates": [ [ [100, 0],
... [100, 1],
... [101, 1],
... [101, 0],
... [100, 0]]],
... "type": "Polygon"},
... "properties": {"foo": "bar"},
... "type": "Feature"}"""
>>> output = rewind(input)
>>> output
'{"geometry": {"coordinates": [[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]], "type": "Polygon"}, "properties": {"foo": "bar"}, "type": "Feature"}'
>>> type(output)
<class 'str'>
强制执行RFC 7946环绕行顺序(输入/输出为python字典)
>>> from geojson_rewind import rewind
>>> input = {
... 'geometry': { 'coordinates': [ [ [100, 0],
... [100, 1],
... [101, 1],
... [101, 0],
... [100, 0]]],
... 'type': 'Polygon'},
... 'properties': {'foo': 'bar'},
... 'type': 'Feature'}
>>> output = rewind(input)
>>> output
{'geometry': {'coordinates': [[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]], 'type': 'Polygon'}, 'properties': {'foo': 'bar'}, 'type': 'Feature'}
>>> type(output)
<class 'dict'>
在控制台中
# Enforce ring winding order on a GeoJSON file
$ rewind in.geojson > out.geojson
# fetch GeoJSON from the web and enforce ring winding order
$ curl "https://myserver.com/in.geojson" | rewind
版本
geojson-rewind遵循语义版本控制。对于此项目,“API”还包括
- CLI标志和选项
- CLI退出代码
遵循Python社区中的常见做法,geojson-rewind将不会在不增加主版本的情况下与不支持Python版本兼容。
致谢
geojson-rewind
是Mapbox的javascript geojson-rewind软件包的Python端口。感谢Tom MacWright和贡献者。