Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Renamed `reverse` to `reverse_geom` because of clash with existing `Q…

…uerySet` method; added `GeoQuerySet.geohash` support for producing GeoHash reprsentations.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12350 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 72c39410c80ee842bf3de22418086d102cb2b768 1 parent 5b21033
@jbronn jbronn authored
View
7 django/contrib/gis/db/models/manager.py
@@ -39,6 +39,9 @@ def extent3d(self, *args, **kwargs):
def force_rhr(self, *args, **kwargs):
return self.get_query_set().force_rhr(*args, **kwargs)
+ def geohash(self, *args, **kwargs):
+ return self.get_query_set().geohash(*args, **kwargs)
+
def geojson(self, *args, **kwargs):
return self.get_query_set().geojson(*args, **kwargs)
@@ -72,8 +75,8 @@ def perimeter(self, *args, **kwargs):
def point_on_surface(self, *args, **kwargs):
return self.get_query_set().point_on_surface(*args, **kwargs)
- def reverse(self, *args, **kwargs):
- return self.get_query_set().reverse(*args, **kwargs)
+ def reverse_geom(self, *args, **kwargs):
+ return self.get_query_set().reverse_geom(*args, **kwargs)
def scale(self, *args, **kwargs):
return self.get_query_set().scale(*args, **kwargs)
View
17 django/contrib/gis/db/models/query.py
@@ -162,6 +162,20 @@ def geojson(self, precision=8, crs=False, bbox=False, **kwargs):
}
return self._spatial_attribute('geojson', s, **kwargs)
+ def geohash(self, precision=20, **kwargs):
+ """
+ Returns a GeoHash representation of the given field in a `geohash`
+ attribute on each element of the GeoQuerySet.
+
+ The `precision` keyword may be used to custom the number of
+ _characters_ used in the output GeoHash, the default is 20.
+ """
+ s = {'desc' : 'GeoHash',
+ 'procedure_args': {'precision': precision},
+ 'procedure_fmt': '%(geo_col)s,%(precision)s',
+ }
+ return self._spatial_attribute('geohash', s, **kwargs)
+
def gml(self, precision=8, version=2, **kwargs):
"""
Returns GML representation of the given field in a `gml` attribute
@@ -253,12 +267,13 @@ def point_on_surface(self, **kwargs):
"""
return self._geom_attribute('point_on_surface', **kwargs)
- def reverse(self, **kwargs):
+ def reverse_geom(self, **kwargs):
"""
Reverses the coordinate order of the geometry, and attaches as a
`reverse` attribute on each element of this GeoQuerySet.
"""
s = {'select_field' : GeomField(),}
+ kwargs.setdefault('model_att', 'reverse_geom')
if connections[self.db].ops.oracle:
s['geo_field_type'] = LineStringField
return self._spatial_attribute('reverse', s, **kwargs)
View
23 django/contrib/gis/tests/geoapp/tests.py
@@ -690,14 +690,14 @@ def test27_snap_to_grid(self):
@no_mysql
@no_spatialite
def test28_reverse(self):
- "Testing GeoQuerySet.reverse()."
+ "Testing GeoQuerySet.reverse_geom()."
coords = [ (-95.363151, 29.763374), (-95.448601, 29.713803) ]
Track.objects.create(name='Foo', line=LineString(coords))
- t = Track.objects.reverse().get(name='Foo')
+ t = Track.objects.reverse_geom().get(name='Foo')
coords.reverse()
- self.assertEqual(tuple(coords), t.reverse.coords)
+ self.assertEqual(tuple(coords), t.reverse_geom.coords)
if oracle:
- self.assertRaises(TypeError, State.objects.reverse)
+ self.assertRaises(TypeError, State.objects.reverse_geom)
@no_mysql
@no_oracle
@@ -714,6 +714,21 @@ def test29_force_rhr(self):
s = State.objects.force_rhr().get(name='Foo')
self.assertEqual(rhr_rings, s.force_rhr.coords)
+ @no_mysql
+ @no_oracle
+ @no_spatialite
+ def test29_force_rhr(self):
+ "Testing GeoQuerySet.geohash()."
+ if not connection.ops.geohash: return
+ # Reference query:
+ # SELECT ST_GeoHash(point) FROM geoapp_city WHERE name='Houston';
+ # SELECT ST_GeoHash(point, 5) FROM geoapp_city WHERE name='Houston';
+ ref_hash = '9vk1mfq8jx0c8e0386z6'
+ h1 = City.objects.geohash().get(name='Houston')
+ h2 = City.objects.geohash(precision=5).get(name='Houston')
+ self.assertEqual(ref_hash, h1.geohash)
+ self.assertEqual(ref_hash[:5], h2.geohash)
+
from test_feeds import GeoFeedTest
from test_regress import GeoRegressionTests
from test_sitemaps import GeoSitemapTest
Please sign in to comment.
Something went wrong with that request. Please try again.