from shapely.geometry import Point
from numpy import array
array(Point(0, 0))
array(<POINT (0 0)>, dtype=object)
from shapely.geometry import LineString
from numpy import array
array(LineString([(0, 0), (1, 1)]))
array(<LINESTRING (0 0, 1 1)>, dtype=object)
numpy.asarray()
功能不复制坐标值,代价是对 Shapely 对象坐标的 numpy 访问较慢。
注意: Numpy数组接口对Numpy本身并不依赖。
通过xy
属性,同种类型的几何对象坐标能够有 x
和 y
的标准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 \times 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数组代表。
from shapely.geometry import shape
d = {"type": "Point", "coordinates": (0.0, 0.0)}
shape = shape(d)
shape.geom_type
'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)})
thing
<__main__.GeoThing at 0x7f18803ce210>
# shape = shape(thing)
shape.geom_type
'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)
from shapely.geometry import Point
Point(0, 0).wkt
'POINT (0 0)'
Point(0, 0).wkb
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
shapely.wkt
和shapely.wkb
模块提供dumps()
和
loads()
功能,这些功能几乎和它们的 pickle
和simplejson
模块副本的机制一模一样。
用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'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
loads(wkt).wkt
'POINT (0 0)'
from shapely.wkt import loads
two_blocks = loads('POLYGON((0 0, 0 1, 8 1, 8 2, 4 2, 4 0, 0 0))')
two_blocks