has*_*bal 7 python geoalchemy2
为了通过GPS位置扩展我的restfull api,我决定尝试geoalchemy.我已经有了一个数据库,我认为它已经将点数保存到了我的数据库中.但是,每当我尝试打印一个我保存的点(例如返回给用户)时,我会得到一个内存地址或类似的东西:
<WKBElement at 0x7ffad5310110; '0101000000fe47a643a7f3494049f4328ae5d61140'>
Run Code Online (Sandbox Code Playgroud)
对于非启动程序员和用户来说,这都是无用的.
我有一个简单的问题.如何以人类可读的形式表示Geometry对象,如:
POINT(40.5563 30.5567)
Run Code Online (Sandbox Code Playgroud)
Ant*_*ala 13
这将是在W¯¯ ell- ķ nown 乙 inary格式; 您可以使用geoalchemy2.functions.ST_AsText它将它们转换为WKT文本格式.
这将在数据库本身中起作用,因此您将此应用于您的查询以在WKT而不是WKB中请求结果; 在您选择的SQLAlchemy中
Model.column.ST_AsText()
Run Code Online (Sandbox Code Playgroud)
要么
ST_AsText(Model.column)
Run Code Online (Sandbox Code Playgroud)
对于WKT和WKB之间的数据库外转换,您可以使用该shapely模块.注意,wkb函数需要二进制,而不是十六进制:
from shapely import wkb, wkt
from binascii import unhexlify
>>> binary = unhexlify(b'0101000000fe47a643a7f3494049f4328ae5d61140')
>>> binary
b'\x01\x01\x00\x00\x00\xfeG\xa6C\xa7\xf3I@I\xf42\x8a\xe5\xd6\x11@'
>>> point = wkb.loads(binary)
>>> point.x, point.y
(51.903542, 4.45986)
>>> wkt.dumps(point)
'POINT (51.9035420000000016 4.4598599999999999)'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1446 次 |
| 最近记录: |