简体   繁体   English

GeoDjango ORM查找错误地将几何转换为SQL中不需要的SRID

[英]GeoDjango ORM lookup wrongly transforms geometry to unwanted SRID in SQL

In the query below lookup with geometry variable wrongly transforms geometry to unwanted SRID in SQL. 在下面的查询中,使用geometry变量进行查找会错误地将geometry转换为SQL中不需要的SRID。

Buildings.objects.annotate(
    area_krovak=Transform('area', self.KROVAK_SRID), 
    intersection_area=RawSQL('ST_Area(ST_Intersection(ST_Transform(area, {krovak}), ST_GeomFromEWKB(\'\\x{district_geom}\')))'.format(district_geom=geometry.ewkb.hex(), krovak=5514), [])
).filter(area_krovak__intersects=geometry)

Building.area is in db a MultipolygonField() , in db there is by default SRID 4326 (WGS84). Building.area在db中是一个MultipolygonField() ,在db中,默认情况下是SRID 4326(WGS84)。 geometry is GEOSGeometry in Krovak projection (SRID 5514). geometryGEOSGeometry投影中的GEOSGeometry(SRID 5514)。 To make ST_Intersection work I need both in the same projection, so I added the field area_krovak in the corresponding projection. 为了使ST_Intersection工作,我需要两个都在同一投影中,因此我在相应的投影中添加了area_krovak字段。 But the =geometry part in lookup area_krovak__intersects=geometry results in the following SQL (when I print .query ) 但是查找area_krovak__intersects=geometry=geometry部分会导致以下SQL(当我打印.query

ST_Transform(ST_GeomFromEWKB('blabla'::bytea), 4326)

For some reason Django transforms it to 4326 and therefore it does not work. 由于某种原因,Django将其转换为4326,因此它不起作用。

Buildings.objects.annotate(
    area_krovak=Transform('area', self.KROVAK_SRID, output_field=MultiPolygonField(srid=5514)), 
    intersection_area=RawSQL('ST_Area(ST_Intersection(ST_Transform(area, {krovak}), ST_GeomFromEWKB(\'\\x{district_geom}\')))'.format(district_geom=geometry.ewkb.hex(), krovak=5514), [])
).filter(area_krovak__intersects=geometry)

Adding output_field with appropriate SRID just solved it 添加具有适当SRID的output_field刚解决了它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM