GeoDjango - 保存PointField的语法?

时间:2016-01-22 20:51:35

标签: geodjango

如果我使用Google Maps API对经度和纬度进行地理编码,保存PointField的语法是什么?

GeoDjango文档缺少这类信息(感觉他们只是为专业人士编写了足够的详细资料才能有效地使用它),而我所发现的一切都没有被接受的答案:How to assign to a Django PointField model attribute?

就上下文而言,这就是我试图做的事情:

class Point(models.Model):
    geography = models.PointField(geography=True)
    geometry= models.PointField(srid=3348) #Statistics Canada Lambert projection

我的用户的大多数查询都属于"从这一点开始显示大约20公里内的所有点,并从最近到最远的顺序排序#34;。即使数据在加拿大各地都有分数,但用户只对任何给定查询的小地点内的点感兴趣,因此geometry PointField足以满足99%的使用率。

如果需要以米为单位的距离测量(而不是简单地从最近到最远的顺序排序),那么将使用geography PointField。

1 个答案:

答案 0 :(得分:3)

From looking at the source code in https://github.com/django/django/blob/master/django/contrib/gis/forms/fields.py :

if not isinstance(value, GEOSGeometry):
            try:
                value = GEOSGeometry(value)
            except (GEOSException, ValueError, TypeError):
                raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')

It shows that any input that can be converted to a GEOSGeomtery will do. So you can use any widget(for example a TextField/HiddenField) but the text value you return should have the syntax of WKT, HEX, WKB, or GeoJSON.(https://docs.djangoproject.com/ja/1.9/ref/contrib/gis/geos/#creating-a-geometry) The example you linked to (How to assign to a Django PointField model attribute?) is using the WKT format.