python-geojson、wkt和uuu-geo_u接口都很简单。
geodais的Python项目详细描述
大地测量学:python geojson、wkt和geo接口使之变得简单
GeoDaisy可帮助您转换为GeoJSON、知名文本(WKT)和支持Geo接口标准的Python对象。
Geodaisy与Python2和Python3一起工作。
这是干什么的?
如果您:
- 拥有geojson并需要wkt
- 拥有WKT并需要GeoJSON
- 使用geojson或wkt,需要python字典或数组
- 读取shapefile后有一个pyshp shape对象,需要geojson或wkt
- 有一个形状良好的对象,需要geojson或wkt
- 有任何其他带有geo接口的python对象,并且需要geojson或wkt
- 想要一个正确且一致地实现specification
大地测量学提供了一个可以从上述任何格式或对象创建的地理对象。geoobject方法输出对象的geojson、wkt等表示。大地测量还包括单独的转换器,如果你不需要一个对象,可以单独使用。
示例
将wkt转换为geojson
>>>importgeodaisy.convertersasconvert>>>wkt='POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'>>>convert.wkt_to_geojson(wkt)'{"type": "Polygon", "coordinates": [[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]]}'
将geojson转换为wkt
>>>importgeodaisy.convertersasconvert>>>geojson='{"type": "Polygon", "coordinates": [[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]]}'>>>convert.geojson_to_wkt(geojson)'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'
从任何带有geo_接口的python对象创建geo object
>>>fromshapely.geometryimportPolygon>>>polygon=Polygon([(30,10),(40,40),(20,40),(10,20),(30,10)])>>>fromgeodaisyimportGeoObject>>>geo_obj=GeoObject(polygon)>>>geo_obj{'type':'Polygon','coordinates':[[(30.0,10.0),(40.0,40.0),(20.0,40.0),(10.0,20.0),(30.0,10.0)]]}>>>geo_obj.type'Polygon'>>>geo_obj.coordinates[[(30.0,10.0),(40.0,40.0),(20.0,40.0),(10.0,20.0),(30.0,10.0)]]>>>geo_obj.geojson()'{"type": "Polygon", "coordinates": [[[30.0, 10.0], [40.0, 40.0], [20.0, 40.0], [10.0, 20.0], [30.0, 10.0]]]}'>>>geo_obj.wkt()'POLYGON ((30.0 10.0, 40.0 40.0, 20.0 40.0, 10.0 20.0, 30.0 10.0))'
为什么要使用shapely或geojson等?
>>>fromshapely.geometryimportPolygon>>>polygon=Polygon([(30,10),(40,40),(20,40),(10,20),(30,10)])>>>fromgeodaisyimportGeoObject>>>geo_obj=GeoObject(polygon)>>>geo_obj{'type':'Polygon','coordinates':[[(30.0,10.0),(40.0,40.0),(20.0,40.0),(10.0,20.0),(30.0,10.0)]]}>>>geo_obj.type'Polygon'>>>geo_obj.coordinates[[(30.0,10.0),(40.0,40.0),(20.0,40.0),(10.0,20.0),(30.0,10.0)]]>>>geo_obj.geojson()'{"type": "Polygon", "coordinates": [[[30.0, 10.0], [40.0, 40.0], [20.0, 40.0], [10.0, 20.0], [30.0, 10.0]]]}'>>>geo_obj.wkt()'POLYGON ((30.0 10.0, 40.0 40.0, 20.0 40.0, 10.0 20.0, 30.0 10.0))'
为什么要使用shapely或geojson等?
其他库只提供特定格式的翻译。例如,shapely可以往返于wkt和geo接口,但不能访问geojson。GeoJSON可以往返于GeoJSON和Geo界面,但不能往返于WKT。如果需要多种翻译,则需要使用多个库。大地测量可以转换成多种格式。
此外,其他一些库,比如shapely,要重得多。这是有道理的,因为他们也做了很多,但它也使安装他们更多的家务。geodaisy是轻量级的,除了python核心之外没有依赖性。
我什么时候该用别的东西?
如果需要验证坐标
大地测量学不验证坐标。但是。如果需要验证坐标,您可能会发现像Shapely或geojson这样的库很有用,这取决于预期的输入和输出是什么。请注意,在创建形状对象时,它们都不能完全验证坐标,您需要先创建对象,然后验证它,这意味着无论您使用大地测量还是其他库之一,都可能创建无效的形状。
将来,大地测量学将在创建对象时验证坐标。(如果您想帮助实现这一点,contributions are welcome!)
请注意,您仍然可以将geodaisy与其他geo库一起使用,也可以将其用作其他库或对象类型之间的中间或中间。
如果需要对几何对象进行几何谓词、关系、变换和其他操作
如果你需要以上任何一个,你可能需要Shapely。
如何安装
pip install geodaisy
运行测试
(以下命令假定您位于根目录中。)
geodaisy使用pytest测试框架以及shapely和geojson库进行测试。您需要首先通过执行pip install -r dev-requirements.txt
来安装它们。
如果您使用的是Python2或Python3.5之前的版本,则需要确保安装了typing
模块:pip install -r py2-requirements.txt
或pip install typing
要运行测试,只需执行pytest
。
Contributing
Contributions are very welcome! In addition to the tests mentioned above, Geodaisy also uses flake8 for linting and Mypy for type checking. Pull requests will need to pass the tests as well as flake8 and Mypy checks (unless you're developing with Python 2, in which case you'll have to skip Mypy, since it requires Python 3.
(The following commands assume you're in the Geodaisy root directory and have already pip-installed the dev requirements (see above).)
To run flake8: ^{
To run Mypy (if you're using Python 3): ^{
License
Geodaisy is licensed under the MIT License免费供商业和私人使用。
版权所有
版权所有©2018 Kevin Brochet Nguyen