【问题标题】:SQL alchemy geoalchemy selecting geometry arraySQL alchemy geoalchemy 选择几何数组
【发布时间】:2023-01-03 23:43:09
【问题描述】:

我创建了不规则几何表,其中包含点数组作为几何类型列,并且我正在尝试根据测量点 ID 检索点。

但我有一个错误:

ProgrammingError: (psycopg2.errors.CannotCoerce) 无法将类型 geometry[] 转换为 geometry 第 1 行:选择 ST_X(CAST(db.irregular.axeses AS geometry(GEOMETR...

这是它在数据库中的样子:

     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()

我删除了铸造: ProgrammingError: (psycopg2.errors.UndefinedFunction) 函数 st_x(geometry[]) 不存在 第 1 行:选择 ST_X(db.irregular.axises)作为“ST_X_1”,ST_Y(db ...

我想我需要检索为元组数组,但我找不到如何从 python 端进行转换以及我应该使用哪个函数。

【问题讨论】:

    标签: python postgresql geoalchemy2


    【解决方案1】:

    我已经解决了这个问题 session.query(fns.ST_AsGeoJSON(**fns.ST_Collect(**tb.Irregular.axises))).filter(tb.Irregular.measurement_point_id == mpoint_id).first()[0]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2016-09-07
      • 2018-02-03
      • 2011-09-14
      • 1970-01-01
      相关资源
      最近更新 更多