A Well Known Text (WKT) or Well Known Binary (WKB) representation[1] of any geometric object can be had via its wkt
or wkb
attribute. These representations allow interchange with many GIS programs. PostGIS, for example, trades in hex-encoded WKB.
任何几何对象的文本字符串(WKT)或二进制(WKB)表达可以通过其wkt或wkb属性获得。该数据格式可以与许多GIS程序相互转换。例如,PostGIS就使用十六进制编码的WKB。
>>> Point(0, 0).wkt
'POINT (0.0000000000000000 0.0000000000000000)'
>>> Point(0, 0).wkb.encode('hex')
'010100000000000000000000000000000000000000'
The shapely.wkt and shapely.wkb modules provide dumps() and loads() functions that work almost exactly as their pickle and simplejson module counterparts. To serialize a geometric object to a binary or text string, use dumps()
. To deserialize a string and get a new geometric object of the appropriate type, use loads()
.
shapely.wkt和shapely.wkb模块提供了dumps()和loads()函数,它们的工作原理与pickle和simplejson模块的对应函数一样。要将一个几何对象序列化为二进制或文本字符串,请使用dumps()。要反序列化一个字符串并获得一个相同的新几何对象,使用load()。
The default settings for the wkt attribute and shapely.wkt.dumps() function are different. By default, the attribute’s value is trimmed of excess decimals, while this is not the case for dumps(), though it can be replicated by setting trim=True.
wkt属性和shapely.wkt.dumps()函数的默认设置是不同的。默认情况下,属性的值会被修剪掉多余的小数,而dumps()则不是这种情况,不过可以通过设置trim=True来复制。
- shapely.wkb.dumps(ob)
Returns a WKB representation of ob.
返回几何对象的WKB表达。
- shapely.wkb.loads(wkb)
Returns a geometric object from a WKB representation wkb.
从WKB表达返回一个几何对象。
>>> from shapely import wkb
>>> pt = Point(0, 0)
>>> wkb.dumps(pt)
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> pt.wkb
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> pt.wkb_hex
'010100000000000000000000000000000000000000'
>>> wkb.loads(pt.wkb).wkt
'POINT (0 0)'
All of Shapely’s geometry types are supported by these functions.
函数支持Shapely的所有几何对象类型。
- shapely.wkt.dumps(ob)
Returns a WKT representation of ob. Several keyword arguments are available to alter the WKT which is returned; see the docstrings for more details.
返回几何对象的WKT表达。有几个关键字参数可以用来改变返回的WKT,更多细节请参见文档说明。
- shapely.wkt.loads(wkt)
Returns a geometric object from a WKT representation wkt.
从WKT表达返回一个几何对象
>>> from shapely import wkt
>>> pt = Point(0, 0)
>>> thewkt = wkt.dumps(pt)
>>> thewkt
'POINT (0.0000000000000000 0.0000000000000000)'
>>> pt.wkt
'POINT (0 0)'
>>> wkt.dumps(pt, trim=True)
'POINT (0 0)'