我使用Django和PostGIS搜索距离某一点有一定距离的用户。抽象代码在这里:
class User(AbstractUser):
geo = PointField(null=True, srid=4326)
objects = GeoManager()
然后在shell中运行:
u = User.objects.first()
p = Point(39.9167,116.3833, srid=4326)
u.geo = p
u.save()
User.objects.filter(geo_address__distance_lte=(p, D(km=5)))
但是得到这样的错误:
...
/usr/local/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.pyc in geodetic(self, connection)
170 system that uses non-projected units (e.g., latitude/longitude).
171 """
--> 172 return self.units_name(connection).lower() in self.geodetic_units
173
174 def get_distance(self, value, lookup_type, connection):
/usr/local/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.pyc in units_name(self, connection)
161 def units_name(self, connection):
162 if not hasattr(self, '_units_name'):
--> 163 self._get_srid_info(connection)
164 return self._units_name
165
/usr/local/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.pyc in _get_srid_info(self, connection)
147 def _get_srid_info(self, connection):
148 # Get attributes from `get_srid_info`.
--> 149 self._units, self._units_name, self._spheroid = get_srid_info(self.srid, connection)
150
151 def spheroid(self, connection):
/usr/local/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.pyc in get_srid_info(srid, connection)
35 if srid not in _srid_cache[connection.alias]:
36 # Use `SpatialRefSys` model to query for spatial reference info.
---> 37 sr = SpatialRefSys.objects.using(connection.alias).get(srid=srid)
38 units, units_name = sr.units
39 spheroid = SpatialRefSys.get_spheroid(sr.wkt)
/usr/local/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
332 raise self.model.DoesNotExist(
333 "%s matching query does not exist." %
--> 334 self.model._meta.object_name
335 )
336 raise self.model.MultipleObjectsReturned(
DoesNotExist: PostGISSpatialRefSys matching query does not exist.
答案 0 :(得分:0)
最后我找到了它,因为我没有按照我的意图从一个创建良好的空间数据库模板创建我的数据库。所以为了解决这个问题,我用spatial_ref_sys.sql更新了模板db:
psql -d template_postgis -f /path/to/spatial_ref_sys.sql
然后从template_postgis
:
createdb my_db -T template_postgis
现在一切正常。