Python 2.7 Psycopg ppygis选择查询
我正在尝试使用python ppygis包设置一个基本的postgis工作设置Python 2.7 Psycopg ppygis选择查询,python-2.7,postgis,psycopg2,postgresql-9.3,Python 2.7,Postgis,Psycopg2,Postgresql 9.3,我正在尝试使用python ppygis包设置一个基本的postgis工作设置 >>> import psycopg2 >>> import ppygis >>> connection = psycopg2.connect(database='spre', user='postgres') >>> cursor = connection.cursor() >>> cursor.execute('CREATE
>>> import psycopg2
>>> import ppygis
>>> connection = psycopg2.connect(database='spre', user='postgres')
>>> cursor = connection.cursor()
>>> cursor.execute('CREATE TABLE test (geometry GEOMETRY)')
>>> cursor.execute('INSERT INTO test VALUES(%s)', (ppygis.Point(1.0, 2.0),))
>>> cursor.execute('SELECT * from test')
>>> point = cursor.fetchone()[0]
>>> print point
0101000000000000000000F03F0000000000000040
>>>
我应该有一个单独的X和Y坐标的python对象。差不多
>>> Point(X: 1.0, Y: 2.0)
我做错了什么?你没有做错什么。接下来,我得到了问题010100中所示的相同结果…,这通常是预期的。也许这与旧版本的PPyGIS/Psycopg一起工作?今天没有 该包似乎没有正确注册为PostGIS类型的适配器,因此我的建议是不要使用该包。此外,从Psycopg2使用PostGIS不需要额外的包 以下是读取/写入点的正常方法,无需任何额外的软件包:
# Assuming PostGIS 2.x, use a typmod
cursor.execute('CREATE TEMP TABLE test (geom geometry(PointZ,4326));')
# Longyearbyen, 78.22°N 15.65°E, altitude 10 m
cursor.execute('''\
INSERT INTO test (geom)
VALUES(ST_SetSRID(ST_MakePoint(%s, %s, %s), 4326));
''', (15.65, 78.22, 10.0))
cursor.execute('''\
SELECT ST_Y(geom) AS latitude, ST_X(geom) AS longitude, ST_Z(geom) AS altitude
FROM test;
''')
print(cursor.fetchone()) # (78.22, 15.65, 10.0)
cursor.execute('SELECT ST_AsText(geom) FROM test;')
print(cursor.fetchone()[0]) # POINT Z (15.65 78.22 10)
cursor.execute('SELECT ST_AsLatLonText(geom) FROM test;')
print(cursor.fetchone()[0]) # 78°13'12.000"N 15°39'0.000"E
如果您希望客户端上的几何对象与实际的几何体做更多的工作,请考虑使用SabeLy,它可以使用WKB数据进行接口:
from shapely.wkb import loads
cursor.execute('SELECT geom FROM test;')
pt = loads(cursor.fetchone()[0], hex=True)
print(pt) # POINT Z (15.65 78.22 10)
你没有做错什么。接下来,我得到了问题010100中所示的相同结果…,这通常是预期的。也许这与旧版本的PPyGIS/Psycopg一起工作?今天没有 该包似乎没有正确注册为PostGIS类型的适配器,因此我的建议是不要使用该包。此外,从Psycopg2使用PostGIS不需要额外的包 以下是读取/写入点的正常方法,无需任何额外的软件包:
# Assuming PostGIS 2.x, use a typmod
cursor.execute('CREATE TEMP TABLE test (geom geometry(PointZ,4326));')
# Longyearbyen, 78.22°N 15.65°E, altitude 10 m
cursor.execute('''\
INSERT INTO test (geom)
VALUES(ST_SetSRID(ST_MakePoint(%s, %s, %s), 4326));
''', (15.65, 78.22, 10.0))
cursor.execute('''\
SELECT ST_Y(geom) AS latitude, ST_X(geom) AS longitude, ST_Z(geom) AS altitude
FROM test;
''')
print(cursor.fetchone()) # (78.22, 15.65, 10.0)
cursor.execute('SELECT ST_AsText(geom) FROM test;')
print(cursor.fetchone()[0]) # POINT Z (15.65 78.22 10)
cursor.execute('SELECT ST_AsLatLonText(geom) FROM test;')
print(cursor.fetchone()[0]) # 78°13'12.000"N 15°39'0.000"E
如果您希望客户端上的几何对象与实际的几何体做更多的工作,请考虑使用SabeLy,它可以使用WKB数据进行接口:
from shapely.wkb import loads
cursor.execute('SELECT geom FROM test;')
pt = loads(cursor.fetchone()[0], hex=True)
print(pt) # POINT Z (15.65 78.22 10)
您得到的是众所周知的点的二进制WKB表示。尝试执行游标。执行“从测试中选择ST_Xgeometry”以查看差异或游标。执行“从测试中选择ST_Xgeometry”您得到的是众所周知的点的二进制WKB表示。尝试执行游标。执行“从测试中选择ST_Xgeometry”以查看差异或游标。执行“从测试中选择ST_Xgeometry”感谢您的回答@Mike T。有一件事,是否可以对3D点使用Shapely?好了,Shapely适用于二维点。算是吧。它被称为2.5维,因为Z维通常只是存储的,但偶尔为线性参考函数计算,而不是距离函数。这真的取决于你可能在做什么。谢谢你的回答@Mike T。有一件事,可以使用Shapely来处理3D点吗?好了,Shapely适用于二维点。算是吧。它被称为2.5维,因为Z维通常只是存储的,但偶尔为线性参考函数计算,而不是距离函数。这真的取决于你可能在做什么。