Tag Archive for 'Development'

Topology rules with GeoDjango

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.

GeoDjango Model Validation

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.

GeoDjango on a Windows server

Deploying GeoDjango projects on a Windows server is obviously possible, but not necessarily the most seamless process one can think of. To get started, there are some great tutorials – at the django project site and here, with some more information about the correct PATH settings – available, that guide you step-by-step through a GeoDjango installation.

Below are some things that I did in addition, did differently, or simply couldn’t do because I never got them to work.

The basics: Django and IIS

IIS is was the web server of choice on the Windows machine in question. Serving Django through IIS is of course doable, there is even a dedicated page on the Django project about it. However, I totally failed in getting Django to work well with IIS. It seemed painfull and eventually made me switch the entire web server to Apache. Django integration with WSGI under Apache is a piece of cake compared to PyISAPIe and IIS if you ask me, up and running in less than 10 minutes.

Virtual Environments

Virtual Environments are great when developing with Python. They give you separated Python installations for every project you work on and avoid interferences with dependencies and installed packages across your projects. Virtual environments is something I’ll really miss on Windows, because I never got it to work correctly. Virtualenv basically creates the correct folder structure and activates a Virtual Environment to work in, but for some reason dependencies in my projects were attempting to access the main Python installation. Which obviously defeats the purpose of Virtual Environments.
What I ended up doing was, using one Django installation and being extremely careful about what goes there. Basically I have to manually validate, check and test that new packages for new projects don’t mess with existing once. Painful, I tell ya.

win-psycopg headache

I ran into serious troubles with psycopg 2.2.1 (the PostgreSQL adapter for Python). My project wouldn’t connect to the PostgreSQL database server at all. I found this entry at stackoverflow, which describes exactly the same issue. The posted solution worked for me too: use psycopg 2.0.14 instead of 2.2.1, no problems so far.

Multiple Django projects in one Apache/WSGI instance

This was the most annoying issue: turned out that only one project at a time was working on the server. That might be an option for the iPhone, but it clearly was unacceptable in my case. The problem caused by the “Python for Windows extensions” is documented in this thread, and my solution, similar to the one posted at the end of the thread, was to install pywin32-212 instead of pywin32-214.

Lessons learned

If you have a choice, get a Linux box to deploy GeoDjango. Anything but .NET seems painful to serve on Windows.

Heating up SVG

Last week I came over Raphaël, a great JavaScript library for vector graphics visualizations, and I started playing around with maps and SVG again. Long time no see!

To bring some map content from ArcMap to Raphaël I used the VBA Macro I wrote 4 years ago in ArcMap. It still does the job and gives me clean vector graphics the way I want them. I couldn’t find a decent SVG export option for QGIS, although there are some efforts to improve that kind of functionality.

AsSVG, a Python geoprocessing script for ArcGIS is pretty good too. It provides some nice export options, such as pick style and data attribute fields, and I actually ended up using it a lot.

However, it’s 2009 and there are other ways available for sharing code then just providing a plain text file. So I ended up wrapping a bitbucket repository around it. Just in case if somebody is interested in working on or improving the script…

Early spring cleaning

TextMateI never thought I would license a text editor for 39 Euros. After reading quite fantastic reviews of TextMate I decided to give this supposedly divine editor a try. Turned out to be a terrible mistake as I know now.

The first impression was like “OK, it’s a good editor, but 39 Euros while TextWrangler is still free, no way!”.

10 days later I already was knee-deep in TextMate projects, accustomed to its keyboard short-cuts, had the blog-editor configured, let alone all the the bundle-features (discovered the secrets behind FIXME, CHANGED and TODO!!) and was overall very impressed by TextMate’s slick and fast usability. To make a long story short, I couldn’t imagine switching back to another text editor again.

However, TextMate was very helpful today when I changed my blog-theme from my badly hacked and messed up Unsleepable to a clean K2 setup, doing search&replace orgies in my database dumps and WordPress files. K2 is surely one of the best available WordPress themes. It offers an excellent reading experience and nifty features like archive pages sliders or sidebar modules.

Main reason for my clean-up was the migration from UTW to the Simple Tagging Plugin, which I couldn’t get to work very well on my former theme. On K2 it basically works, but for full support (e.g. archive pages) some of the K2 files have to be edited too, not a big problem though. I put the changed files in a zip-file, so if you didn’t modify your K2 installation, just download it and overwrite your existing K2 files.

Simple Tagging Plugin performs better than UTW, comes with built-in features like type-ahead tagging, tag suggestions, related posts and is, unlike UTW, still under ongoing development.

Update
Changed files for K2 v0.9.6 are available here.

Web development tools

As I had lately to do more, let’s call it, web development as usual I really appreciated that I have found (and tried) Eclipse. Before I was just happy using a text editor with syntax coloring, sufficient for my projects, since they never went too big.

Once you’ve started using AJAX libraries like MochiKit (as I did) you’ll notice the efficiency boost when Eclipse and the fantastic JavaScript IDE Aptana enter the game and replace your beloved text editor. I simply can’t imagine how extensive JavaScript development is possible without an environment like Aptana.

Besides syntax coloring, code validation, HTML/CSS support, it finds all objects, functions, variables, methods etc. in your JavaScript libraries and provides you with overviews, documentations and auto-completion. Considering all the functionality a large library like MochiKit offers, it either takes you a week to go through the documentation or you let Aptana do the work for you and start coding right away. I never have been a big documentation reader, always preferred the trial-and-error method, so Aptana made my life a lot easier.

Everyone who ever had to debug JavaScript knows that this task can drive you mad. Firefox and its Web Developer Toolbar are very helpful in that case. But the extension Firebug is just priceless when it comes to tracking down errors in your JavaScript code.

Still the best of it: all open source software.

PHP development on Mac OS X

A new version of MAMP, containing the latest updates of its components, is available for download.

MAMP (Macintosh, Apache, Mysql and PHP) is an excelent environment if you’re planning to do some PHP development on your Mac. As they state on the MAMP website, it should therefore not be used as Live Webserver for the Internet.

Included software & libraries

  • Apache Version: 2.0.55
  • MySQL Version: 4.1.12
  • PHP Version: 4.4.2 & 5.1.2
  • eAccelerator: 0.9.4-rc1
  • Zend Optimizer: 2.6.0
  • phpMyAdmin 2.7.0-pl2
  • SQLite Version: 2.8.16
  • SQLiteManager Version: 1.1.3
  • Freetype Version: 2.1.9
  • t1lib Version: 5.1.0
  • curl Version: 7.14.0
  • jpeg Version: 6b
  • libpng Version: 1.2.5
  • gd Version: 2.0.28

I use MAMP primarily to test or modify WordPress upgrades, themes or plugins on my local machine before installing them on my webserver. The installation, administration and removal of MAMP is pretty straightforward – a highly recommended package!