私は、点と矩形が実世界の経度と緯度に基づいている矩形内のすべての点を返すクエリを実行したいと思います。Geoalchemy2&ST_Within - ポイントとポリゴンの間の型の不一致?
これが失敗したクエリです:
results = session.query(Store.id).filter(func.ST_Within(Store.location, func.ST_GeomFromEWKT('SRID=4326;POLYGON((150 -33, 152 -33, 152 -31, 150 -31, 150 -33))')))
それは文句なしに実行されますが、()をresults.first呼び出すときに、私は以下のエラーと警告を参照してください。
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) function st_within(geography, geometry) does not exist LINE 3: WHERE ST_Within(store.location, ST_GeomFromEWKT('SRID=4326;P... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. [SQL: 'SELECT store.id AS store_id \nFROM store \nWHERE ST_Within(store.location, ST_GeomFromEWKT(%(ST_GeomFromEWKT_1)s )) \n LIMIT %(param_1)s'] [parameters: {'ST_GeomFromEWKT_1': 'SRID=4326;POLYGON((150 -33, 152 -33, 152 -31, 150 -31, 150 -33))', 'param_1': 1}]
私が作ることができますクエリにダミーポイントを作成すると(クエリはすべてのストアが一致するようになります)、次のようにクエリを処理できます。
これは、問題が私のStore.locationフィールドであることを示していますが、[type_coerce(Store.location、Geoography)を含む]は何も試みていませんでした。
これは、位置列の私のSQLAlchemyの定義である:
location = Column(Geography(geometry_type='POINT', srid=4326))
これは私が位置に経度&緯度をオンに走った(私も強制するfunc.ST_GeomFromEWKT()を使用して試したコードでありますタイプ):
stores = session.query(Store)
for store in stores:
store.location = 'SRID=4326;POINT({} {})'.format(store.longitude, store.latitude)
session.commit()
PythonはStore.locationの種類は、私が文書から期待するものである「geoalchemy2.elements.WKBElement」であることを私に伝えます。
誰でもクエリの修正方法に関する提案はありますか?
FYI私が実行している:
- のPostgreSQL 9.6.1
- psycopg2 2.6.2
- SQLAlchemyの1.1.4、および
- Geoalchemy2 0.4.0