问题描述
我正在使用GeoDjango并拥有一个带有PointField的Django模型:
class ArchivrItem(models.Model):
...
coordinate = models.PointField(srid = 4326)
我尝试插入一个新的ArchivrItem,使用纬度和经度,我得到这个错误:
错误:新行关系archivr_archivritem 违反检查约束enforce_srid_coordinate
我可以避免Django,并通过尝试直接在postgresql中得到相同的错误这样做:
INSERT INTO archivr_archivritem(coordinate)VALUES('POINT(51.520667 -0.094833)');
我可能在SRID和点系统周围天真无知,我错过了什么?谢谢。
更新:我应该添加约束是什么。表的限制是:
enforce_dims_coordinateCHECK(st_ndims(coordinate)= 2)
enforce_geotype_coordinate CHECK(几何类型(坐标)='POINT'::文本或坐标IS NULL)
enforce_srid_coordinateCHECK(st_srid(coordinate)= 4326)
这听起来像是要添加一个新的ArchivrItem:
item = ArchivrItem(coordinate ='POINT(51.520667 -0.094833)')
pre>
item.save()
这是因为某些原因我不确定而得不到正确的默认SRID。但是,明确指定它应该工作,例如:
from django.contrib.gis.geos import Point
item = ArchivrItem(coordinate = Point(-0.094833,51.520667,srid = 4326))
item.save()
我会说srid是可选的,如果它将匹配模型定义,但没有指定它的危害,你可以看到,只是简单地使用对象方式修复它。 还有一些例子。
[另请注意,POINT()是X,然后Y,即len然后lat,而不是lat / lon。如果扩展了WKTSRID = 4326; POINT(-0.094833 51.520667),则可以放入SRID]]
I'm using GeoDjango and have a Django model with a PointField:
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:
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:
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? 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:
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. However, specifying it explicitly should work, e.g.:
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. https://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#creating-and-saving-geographic-models has some more examples.
[Aside, note that POINT() is X then Y, i.e. lon then lat, not lat/lon. You can put a SRID in if it's extended WKT with "SRID=4326;POINT(-0.094833 51.520667)"]
这篇关于如何满足“enforce_srid_coordinate”约束与GeoDjango / PostGIS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!