Any object that provides the GeoJSON-like Python geo interface can be converted to a Shapely geometry using the shapely.geometry.shape()
function.
任何提供类似GeoJSON的Python地理接口的对象都可以通过shapely.geometry.shape()函数转换为Shapely几何对象。
Returns a new, independent geometry with coordinates copied from the context.
返回一个新的、独立的几何对象,其坐标是从context中复制的。
For example, a dictionary:
例如,对一个字典对象进行转换:
>>> from shapely.geometry import shape
>>> data = {"type": "Point", "coordinates": (0.0, 0.0)}
>>> geom = shape(data)
>>> geom.geom_type
'Point'
>>> list(geom.coords)
[(0.0, 0.0)]
Or a simple placemark-type object:
先建立一个GeoThing类,这个类接收字典参数。
>>> class GeoThing:
... def __init__(self, d):
... self.__geo_interface__ = d
>>> thing = GeoThing({"type": "Point", "coordinates": (0.0, 0.0)})
>>> geom = shape(thing)
>>> geom.geom_type
'Point'
>>> list(geom.coords)
[(0.0, 0.0)]
The GeoJSON-like mapping of a geometric object can be obtained using shapely.geometry.mapping()
.
可以使用shapely.geometry.mapping()获得几何对象的类GeoJSON映射。
Returns a GeoJSON-like mapping from a Geometry or any object which implements __geo_interface__
.
从一个几何体或任何实现__geo_interface__的对象中返回一个类似GeoJSON的映射。
New in version 1.2.3.
For example, using the same GeoThing class:
例如,使用相同的GeoThing类:
>>> from shapely.geometry import mapping
>>> thing = GeoThing({"type": "Point", "coordinates": (0.0, 0.0)})
>>> m = mapping(thing)
>>> m['type']
'Point'
>>> m['coordinates']
(0.0, 0.0)