I’m still very new to GeoDjango and the Django world in general, but I have to say that, based on what I’ve seen so far, I’m very impressed by how easy, fast and clean you can build powerful geographic web applications with it.
Django 1.2 is the current version and one thing that caught my eye in the release notes was Model validation. Among other benefits, model validation allows you to “perform custom validation on your Model” everytime a user wants to save data through a
ModelForm. Translated to GeoDjango, that basically means that one can access the unleashed power of PostGIS (or the functionality of your supported geo-database of choice) to validate geometries and provide feedback for users through a nice web interface. Best part of it: it’s all done only by a few lines of additional code in your data model.
I’m not going into much detail how Django’s model validation works, you should check out the documentation if you’re interested, but here’s a quick example of creating something like a simple topology rule with GeoDjango:
Let’s say I have users entering and modifying point data and need to make sure that each point falls inside a polygon from another layer.
# models.py from django.contrib.gis.db import models from django.core.exceptions import ValidationError class Poly(models.Model): geometry = models.PolygonField() objects = models.GeoManager() class Point(models.Model): geometry = models.PointField() objects = models.GeoManager() # simple topology rule def clean(self): # find Polys that contain the Point pq = Poly.objects.filter(geometry__contains=self.geometry) if not pq: # no Poly has been found raise ValidationError('Point is not within a Poly.')
That’s it. You only need to add a custom
clean() method and put your validation rules there. Those few lines of additional code check if the new point falls inside a polygon and tell the user if it’s not through the Django admin interface.
I’ve posted the project on bitbucket if you want to give it a shot. Add a little more PostGIS extravaganza and you can have a pretty sophisticated set of topology rules in your web application, bundled with the great capabilities Django apps offer.