简体   繁体   English

如何使用GeoDjango / PostGIS满足“ enforce_srid_coordinate”约束?

[英]How do I satisfy “enforce_srid_coordinate” constraint with GeoDjango/PostGIS?

I'm using GeoDjango and have a Django model with a PointField: 我正在使用GeoDjango,并具有一个带有PointField的Django模型:

class ArchivrItem(models.Model):
    ...
    coordinate = models.PointField(srid=4326)

When I try to insert a new ArchivrItem, using latitude and longitude, I get this error: 当我尝试使用纬度和经度插入新的ArchivrItem时,出现以下错误:

ERROR:  new row for relation "archivr_archivritem" violates check constraint "enforce_srid_coordinate"

I can avoid Django and get the same error in postgresql directly by trying to do this: 我可以避免Django,并通过尝试这样做直接在postgresql中得到相同的错误:

INSERT INTO archivr_archivritem (coordinate) VALUES ('POINT(51.520667 -0.094833)');

I'm probably being naive and ignorant around SRID and point systems... what am I missing? 我可能对SRID和点系统过于幼稚和无知...我想念什么? Thanks. 谢谢。

UPDATE: I should add what the constraint is. 更新:我应该添加约束。 The constraints on the table are: 该表上的约束是:

"enforce_dims_coordinate" CHECK (st_ndims(coordinate) = 2)
"enforce_geotype_coordinate" CHECK (geometrytype(coordinate) = 'POINT'::text OR coordinate IS NULL)
"enforce_srid_coordinate" CHECK (st_srid(coordinate) = 4326)

It sounds like you're trying to add a new ArchivrItem by doing this: 听起来您正在尝试通过执行以下操作添加新的ArchivrItem:

item = ArchivrItem(coordinate='POINT(51.520667 -0.094833)')
item.save()

And this is not getting the right default SRID for some reason I'm not sure about. 由于某些不确定的原因,这没有获得正确的默认SRID。 However, specifying it explicitly should work, eg: 但是,明确指定它应该起作用,例如:

from django.contrib.gis.geos import Point
item = ArchivrItem(coordinate=Point(-0.094833, 51.520667, srid=4326))
item.save()

I'd say the srid is optional if it's going to match the model definition, but no harm in specifying it, and you can see if simply using the object way fixes it anyway. 我想说srid如果要与模型定义匹配,则是可选的,但是在指定它时没有害处,您可以看到是否仅使用对象方法即可对其进行修复。 https://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#creating-and-saving-geographic-models has some more examples. https://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#creating-and-saving-geographic-models包含更多示例。

[Aside, note that POINT() is X then Y, ie lon then lat, not lat/lon. [另外,请注意POINT()是X然后是Y,即lon然后是lat,不是lat / lon。 You can put a SRID in if it's extended WKT with "SRID=4326;POINT(-0.094833 51.520667)"] 如果扩展了WKT,则可以将SRID放入“ SRID = 4326; POINT(-0.094833 51.520667)”]

After a colleague coming across the same issue, we appear to have tracked this down to an issue with the version of GEOS installed. 一位同事遇到相同的问题后,我们似乎已将其归结为安装了GEOS版本的问题。 Using a version with this problem, if p is a django.contrib.gis.geos.Point, p.wkb and p.ewkb return the same data - the EWKB lacks the SRID information. 使用存在此问题的版本,如果p是django.contrib.gis.geos.Point,则p.wkb和p.ewkb返回相同的数据-EWKB缺少SRID信息。 As Django uses EWKB to construct the SQL for import into PostGIS, this is why it fails, no matter the construction of the creation. 由于Django使用EWKB构造要导入到PostGIS中的SQL,因此无论创建的构造如何,它都会失败。

My previous comments were using Debian's 3.2.0 which does not show this issue at all. 我以前的评论使用的是Debian 3.2.0,根本没有显示此问题。 3.2.3 is the version that is showing this issue here. 3.2.3是在此处显示此问题的版本。 Upgrading to 3.3.6 fixed the issue (and also tested 3.3.3 which is fine). 升级到3.3.6可以解决此问题(并测试了3.3.3很好)。 I'll try other versions later today when I have more time to try and track it down further. 今天晚些时候,我将有更多时间尝试进一步跟踪它时,我将尝试其他版本。

You can see your version of GEOS with: 您可以通过以下方式查看您的GEOS版本:

from django.contrib.gis.geos.libgeos import geos_version
geos_version()

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

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