Any object that provides the GeoJSON-like Python geo interface can be adapted and used as a Shapely geometry using the shapely.geometry.asShape()
or shapely.geometry.shape()
functions.
任何提供类似GeoJSON的Python地理接口的对象都可以通过shapely.geometry.asShape()或shapely.geometry.shape()函数转换为Shapely几何对象。
- shapely.geometry.asShape(context)
Adapts the context to a geometry interface. The coordinates remain stored in the context.
将context 转换为几何接口,坐标仍然保存在context 中。
- shapely.geometry.shape(context)
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映射。
- shapely.geometry.mapping(ob)
Returns a new, independent geometry with coordinates copied from the context.
返回一个新的、独立的几何对象,其坐标是从context中复制的。
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)}