Created
June 29, 2010 02:13
-
-
Save palewire/456696 to your computer and use it in GitHub Desktop.
Generate a viewport bias from a GeoDjango model. Useful when geocoding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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