python - 将geopandas shapely 多边形转换为geojson
问题描述
我使用 geopandas 创建了一个圆圈,它返回了一个匀称的多边形:
POLYGON: ((...))
我想要这个与 geojson 对象相同的多边形。我遇到了这个:
shapely.geometry.mapping(shapelyObject)
它返回这个:
{'type': 'Polygon', 'coordinates': (((570909.9247264927, 125477.71811034005)...}
但是当我尝试在 mapbox 中映射它时,它没有显示任何内容。我想也许它不完全是一个 geojson 对象。
解决方案
如果你不想手动创建这个字典,你也可以依赖geopandas
创建它:
In [1]: import shapely.geometry
In [2]: import geopandas
In [3]: shapely_polygon = shapely.geometry.Polygon([(0, 0), (0, 1), (1, 0)])
In [4]: geopandas.GeoSeries([shapely_polygon]).__geo_interface__
Out[4]:
{'bbox': (0.0, 0.0, 1.0, 1.0),
'features': [{'bbox': (0.0, 0.0, 1.0, 1.0),
'geometry': {'coordinates': (((0.0, 0.0),
(0.0, 1.0),
(1.0, 0.0),
(0.0, 0.0)),),
'type': 'Polygon'},
'id': '0',
'properties': {},
'type': 'Feature'}],
'type': 'FeatureCollection'}
(请注意,这给出了 FeatureCollection 而不是单个特征。)
或字符串(或文件):
In [4]: geopandas.GeoSeries([shapely_polygon]).to_json()
Out[4]: '{"features": [{"bbox": [0.0, 0.0, 1.0, 1.0], "geometry": {"coordinates": [[[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 0.0]]], "type": "Polygon"}, "properties": {}, "id": "0", "type": "Feature"}], "bbox": [0.0, 0.0, 1.0, 1.0], "type": "FeatureCollection"}'
推荐阅读
- mysql - 从 TIMESTAMPDIFF (time, Current_Time()) 中提取 hh:mm:ss
- java - mysql-binlog-connector-java - 在二进制日志索引文件中找不到第一个日志文件名
- java - IBM AppScan 的验证要求问题
- openssl - 根 CA 证书和中间 CA 证书
- docker - 使用 docker 和 docker-compose 构建的工作流程是什么?
- javascript - 在 webpack + react 中使用 sass-loader 出现多个错误
- java - Reactor 调度程序实施注意事项
- javascript - 将 HTML 对象添加到事件 javascript/meteor
- c# - 找不到方法 Microsoft.Owin.Security.Notifications.MessageReceivedNotification
- c++ - C++调用另一个子类的公共方法