Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.