Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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:
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):
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