我创建了不规则几何表,其中包含点数组作为几何类型列,我试图根据测量点id检索点。
但是我有一个错误:
ProgrammingError: (psycopg2.errors.CannotCoerce)不能将类型geometry[]强制转换为geometry第1行:选择ST_X(CAST) (db.不规则)。
数据库:
column_name | data_type | numeric_scale || udt_schema | udt_name |
----------------------+-----------+---------------+-------------+------------+
id | integer | 0 | | pg_catalog | int4 |
measurement_point_id | integer | 0 | | pg_catalog | int4 |
axises | ARRAY | | | public | _geometry |
这是我的不规则表类:
#%% Irregular Class
class Irregular (object):
measurement_point_id = relationship("measurement_points", back_populates="id")
def __init__(self,measurement_point_id,axises=None,id= None):
self.id = id
self.measurement_point_id = measurement_point_id
self.axises = axises
#self.is_xy = xy
#Irregular Object
__tablename__ = 'irregular'
irregular = Table(
__tablename__,meta,
Column ('id', Integer, primary_key = True),
Column ( 'measurement_point_id',Integer,ForeignKey('measurement_points.id')),
Column ( 'axises', ARRAY(Geometry('POINT'))),
#Column ( 'is_xy', Boolean),
)
mapper(Irregular, irregular)
这是我试图获得数据的方式:
session.query(fns.ST_X(cast(tb.Irregular.axises, geoalchemy2.types.Geometry)),
fns.ST_Y(cast(tb.Irregular.axises, geoalchemy2.types.Geometry)).filter(tb.measurement_point_id == id).all()
我删除了cast:ProgrammingError:(psycopg2.errors.UndefinedFunction)函数st_x(geometry[])不存在第一行:SELECT ST_X(db. regular.axises) AS "ST_X_1", ST_Y(db…
我想我需要作为元组数组检索,但我找不到如何从python方面转换,我应该使用哪个函数。
我用
解决了这个问题session.query(fns.ST_AsGeoJSON(fns.ST_Collect(tb.Irregular.axises)))
.filter(tb.Irregular.measurement_point_id == mpoint_id)
.first()[0]