public

Generate a viewport bias from a GeoDjango model. Useful when geocoding.

  • Download Gist
GeoDjango model with viewport bias.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.