>>> from env_helper import info; info()
页面更新时间: 2024-04-04 20:47:26
运行环境:
    Linux发行版本: Debian GNU/Linux 12 (bookworm)
    操作系统内核: Linux-6.1.0-18-amd64-x86_64-with-glibc2.36
    Python版本: 3.11.2

7.7. Shapely互操作接口

Shapely 定义的几何对象的格式,同时也提供了与其他工具或类库的接口,方便使用。

7.7.1. Numpy与Python列表

所有有坐标序列的几何对象, 如 点、线 线环提供Numpy数组接口, 都能被转化或接纳为Numpy数组。

>>> from shapely.geometry import Point
>>> from numpy import array
>>> array(Point(0, 0))
/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py:3378: ShapelyDeprecationWarning: The array interface is deprecated and will no longer work in Shapely 2.0. Convert the '.coords' to a numpy array instead.
  exec(code_obj, self.user_global_ns, self.user_ns)
array([0., 0.])
>>> from shapely.geometry import LineString
>>> from numpy import array
>>> array(LineString([(0, 0), (1, 1)]))
array([[0., 0.],
       [1., 1.]])

numpy.asarray()功能不复制坐标值,代价是对Shapely对象坐标的numpy访问较慢。

注意:

Numpy数组接口对Numpy本身并不依赖。

通过xy属性,同种类型的几何对象坐标能够有 xy的标准Python数组。

>>> Point(0, 0).xy
(array('d', [0.0]), array('d', [0.0]))
>>> LineString([(0, 0), (1, 1)]).xy
(array('d', [0.0, 1.0]), array('d', [0.0, 1.0]))

shapely.geometry.asShape()函数能够被用于封装numpy坐标数组,因此 它们能通过使用Shapely被用于分析,同时保持原始存储。 1 × 2矩阵可以被采纳为一个点。例如:

>>> from shapely.geometry import Point
>>> pa = Point(array([0.0, 0.0]))
>>> pa.wkt
'POINT (0 0)'

N × 2矩阵可以被采纳为一条线。例如:

>>> from shapely.geometry import LineString
>>> la = LineString(array([[1.0, 2.0], [3.0, 4.0]]))
>>> la.wkt
'LINESTRING (1 2, 3 4)'

多边形没有Numpy数组代表。

7.7.2. Python的 Geo 接口

任何提供类似GeoJSON对象的 Python 都通过 shapely.geometry.asShape() 或者 shapely.geometry.shape() 功能能够被采纳并用作Shapely几何体。

shapely.geometry.asShape(context)

将上下文转化为几何接口。 坐标仍然存在于上下文中。

shapely.geometry.shape(context)

返回一个新的、独立的几何体,其坐标是从上下文复制的。

例如,一个字典搜索:

>>> from shapely.geometry import asShape
>>> d = {"type": "Point", "coordinates": (0.0, 0.0)}
>>> shape = asShape(d)
>>> shape.geom_type
/tmp/ipykernel_45415/1497834305.py:3: ShapelyDeprecationWarning: The proxy geometries (through the 'asShape()', 'asPoint()' or 'PointAdapter()' constructors) are deprecated and will be removed in Shapely 2.0. Use the 'shape()' function or the standard 'Point()' constructor instead.
  shape = asShape(d)
'Point'
>>> tuple(shape.coords)
((0.0, 0.0),)
>>> list(shape.coords)
[(0.0, 0.0)]

或者一个简单的标-型对象,例如:

>>> class GeoThing(object):
>>>     def __init__(self, d):
>>>         self.__geo_interface__ = d
>>> thing = GeoThing({"type": "Point", "coordinates": (0.0, 0.0)})
>>> shape = asShape(thing)
>>> shape.geom_type
/tmp/ipykernel_45415/3621694412.py:5: ShapelyDeprecationWarning: The proxy geometries (through the 'asShape()', 'asPoint()' or 'PointAdapter()' constructors) are deprecated and will be removed in Shapely 2.0. Use the 'shape()' function or the standard 'Point()' constructor instead.
  shape = asShape(thing)
'Point'
>>> tuple(shape.coords)
((0.0, 0.0),)
>>> list(shape.coords)
[(0.0, 0.0)]

可以通过shapely.geometry.mapping()功能得到一个类似GeoJSON的地图的几何对象。 http://toblerity.github.com/shapely/manual.html#shapely.geometry.mapping.

shapely.geometry.mapping(ob)

返回一个新的、独立的坐标是从上下文复制的几何体。 新版本为1.2.3

例如,用相同的类型:

>>> 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)

7.7.3. 知名文本(WKT、WKB)格式

任何几何对象的知名文本(WKT)或知名二进制(WKB) 代表能够通过它的wkt或者wkb属性获得。 这些表征允许GIS程序的互换。 例如,PostGIS可以与16进制的WKB进行交换。

>>> from shapely.geometry import Point
>>> Point(0, 0).wkt
'POINT (0 0)'
>>> Point(0, 0).wkb
b'x01x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00'

shapely.wktshapely.wkb 模块提供dumps()loads()功能,这些功能几乎和它们的 picklesimplejson 模块副本的机制一模一样。

dumps()来序列化一个几何对象为一个二进制或者文本字符串。 为了反序列化一个字符串来得到一个相似类型的新的几何对象,用 loads()。

shapely.wkb.dumps(ob) 返回ob的WKB表示。

shapely.wkb.loads(wkb)

从一个WKB表示wkb返回一个几何对象。

>>> from shapely.wkb import dumps, loads
>>> wkb = dumps(Point(0, 0))
>>> loads(wkb).wkt
'POINT (0 0)'

这些功能支持Shapely的所有几何类型。

shapely.wkt.dumps}(ob)

返回一个ob的WKT表示。

shapely.wkt.loads}(wkt)

从WKT表示中返回一个几何对象。

>>> wkt = dumps(Point(0, 0))
>>> wkt
b'x01x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00'
>>> loads(wkt).wkt
'POINT (0 0)'
>>> from shapely.wkt import loads
>>> uu = loads('POLYGON((0 0, 0 1, 8 1, 8 2, 4 2, 4 0, 0 0))')
>>> uu
_images/sec7_interop_34_0.svg