Shapely互操作接口

Numpy与Python列表

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

In [1]:
from shapely.geometry import Point
from numpy import array
array(Point(0, 0))
Out[1]:
array([0., 0.])
In [2]:
from shapely.geometry import LineString
from numpy import array
array(LineString([(0, 0), (1, 1)]))
Out[2]:
array([[0., 0.],
       [1., 1.]])

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

注意:

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

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

In [3]:
Point(0, 0).xy
Out[3]:
(array('d', [0.0]), array('d', [0.0]))
In [4]:
LineString([(0, 0), (1, 1)]).xy
Out[4]:
(array('d', [0.0, 1.0]), array('d', [0.0, 1.0]))

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

In [5]:
from shapely.geometry import asPoint
pa = asPoint(array([0.0, 0.0]))
pa.wkt
Out[5]:
'POINT (0 0)'

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

In [6]:
from shapely.geometry import asLineString
la = asLineString(array([[1.0, 2.0], [3.0, 4.0]]))
la.wkt
Out[6]:
'LINESTRING (1 2, 3 4)'

多边形没有Numpy数组代表。

Python的 Geo 接口

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

shapely.geometry.asShape(context)

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

shapely.geometry.shape(context)

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

例如,一个字典搜索:

In [7]:
from shapely.geometry import asShape
d = {"type": "Point", "coordinates": (0.0, 0.0)}
shape = asShape(d)
shape.geom_type
Out[7]:
'Point'
In [8]:
tuple(shape.coords)
Out[8]:
((0.0, 0.0),)
In [9]:
list(shape.coords)
Out[9]:
[(0.0, 0.0)]

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

In [10]:
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
Out[10]:
'Point'
In [11]:
tuple(shape.coords)
Out[11]:
((0.0, 0.0),)
In [12]:
list(shape.coords)
Out[12]:
[(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

例如,用相同的类型:

In [13]:
from shapely.geometry import mapping
thing = GeoThing({"type": "Point", "coordinates": (0.0, 0.0)})
m = mapping(thing)
m['type']
Out[13]:
'Point'
In [14]:
m['coordinates']
Out[14]:
(0.0, 0.0)

知名文本格式

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

In [15]:
from shapely.geometry import Point
Point(0, 0).wkt
# Point(0, 0).wkb.encode('hex')
Out[15]:
'POINT (0 0)'

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

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

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

shapely.wkb.loads(wkb)

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

In [16]:
from shapely.wkb import dumps, loads
wkb = dumps(Point(0, 0))
# wkb.encode('hex')
In [17]:
loads(wkb).wkt
Out[17]:
'POINT (0 0)'

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

shapely.wkt.dumps}(ob)

返回一个ob的WKT表示。

shapely.wkt.loads}(wkt)

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

In [18]:
wkt = dumps(Point(0, 0))
wkt
Out[18]:
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
In [19]:
loads(wkt).wkt
Out[19]:
'POINT (0 0)'

In [20]:
import helper; helper.info()
页面更新时间: 2019-02-28 18:44:39
操作系统/OS: Linux-4.9.0-8-amd64-x86_64-with-debian-9.8
Python: 3.5.3