shape字段保存着geometry对象,它是以十六进制形式的二进制wkb数据,常规思路是把数据读到取出来,然后把它转回二进制,例如:
十六进制字符串:
2A0000000200000004000C00E9930400120300000100000080C7A0EAA50393959DD2FA05C3F8018AE805
转成二进制:
1010100000000000000000000000000000001000000000000000000000000000000100000000000000110000000000111010011001001100000100000000000001001000000011000000000000000000000001000000000000000000000000100000001100011110100000111010101010010100000011100100111001010110011101110100101111101000000101110000111111100000000001100010101110100000000101
然后就可以解析成wkt了————解析方法需要安装环境
——————————————————————————————————————————
其实,有另外一种更方便的方法:
如果数据保存在postgis空间数据库中,可以使用postgis的st_astext()函数来直接获取
todo:了解PostGIS常用函数
python示例:
`import psycopg2
"""
postgis 方法
st_astext
"""
def get_data():
"""从空间数据库获取shape字段(Geometry)
在sql语句中使用postgis方法st_astext()将shape转为
"""
conn = psycopg2.connect(host='192.168.1.88', user='sde', password='123456', dbname='sde', port=5432)
cursor = conn.cursor()
# shape_sql = "select shape from bridge" # 获取shape字段的原始内容的sql
# cursor.execute(shape_sql)
shape_astext_sql = "select st_astext(shape) from bridge" # 获取shape字段的转换后的内容的sql
cursor.execute(shape_astext_sql)
res = cursor.fetchall()
conn.close()
return res
if name == 'main':
res = get_data()
for i in res[3]:
print(i)`