Skip to content

Instantly share code, notes, and snippets.

@palewire
Created June 29, 2010 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palewire/456696 to your computer and use it in GitHub Desktop.
Save palewire/456696 to your computer and use it in GitHub Desktop.
Generate a viewport bias from a GeoDjango model. Useful when geocoding.
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
class Multipolygon(models.Model):
"""
Try using it with this: http://github.com/palewire/latimes-mappingla-geopy
"""
name = models.CharField(max_length=200, null=True, blank=True)
multipolygon = models.MultiPolygonField(srid=2229, null=True, blank=True)
objects = models.GeoManager()
def __unicode__(self):
return self.name
class Meta:
ordering = ('name',)
def get_viewport(self):
if not self.multipolygon:
return None
xmin, ymin, xmax, ymax = self.multipolygon.extent
x, y = self.multipolygon.centroid.tuple
min_radius = Point(x, y).distance(Point(xmin, ymin))
max_radius = Point(x, y).distance(Point(xmax, ymax))
return {
'centroid': { 'y': y, 'x': x },
'span': { 'min': min_radius, 'max': max_radius }
}
viewport = property(get_viewport)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment