Archive for the 'Geekery' Category

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.

Re-projecting vectors in JavaScript

I know, it eventually all boils down to maths. But it still blows my mind that you can re-project geographic features on-the-fly with a few lines of JavaScript in a web browser.

How?

There is this great library PROJ.4, that does everything you’d ever want in terms of cartographic projections. A few smart people have ported PROJ.4 to JavaScript, called Proj4js then.

Proj4js works great in combination with OpenLayers, a popular JavaScript web mapping framework, and allows on-the-fly projections between any spatial reference systems browser applications.

<script src="proj4js-compressed.js"></script>
<script src="http://openlayers.org/api/OpenLayers.js"></script>

Define the spatial reference you’re planning to use. Check Spatial Reference for the exact projection parameters and include them in your code.

Proj4js.defs["EPSG:26986"] = "+title=Massachusetts Mainland NAD83 +proj=lcc +lat_1=42.68333333333333 +lat_2=41.71666666666667 +lat_0=41 +lon_0=-71.5 +x_0=200000 +y_0=750000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs";

Adding all desired projections to the OpenLayers script…

projOSM = new OpenLayers.Projection("EPSG:900913");
projWGS84 = new OpenLayers.Projection("EPSG:4326");
projMassGIS = new OpenLayers.Projection("EPSG:26986");

map = new OpenLayers.Map ("map", {
	maxExtent: new OpenLayers.Bounds( -20037508.34, -20037508.34, 20037508.34, 20037508.34),
	maxResolution: 156543.0399,
	units: 'm',
	projection: projOSM,
	displayProjection: projWGS84,
	allOverlays: false
});

osm = new OpenLayers.Layer.OSM(
	"OpenStreetMap",
       "http://tile.openstreetmap.org/${z}/${x}/${y}.png"
);

openspace = new OpenLayers.Layer.WFS("Open space", "http://giswebservices.massgis.state.ma.us/geoserver/wfs", {
	typename: "massgis:GISDATA.OPENSPACE_POLY"
}, {
	projection: projMassGIS
	attribution: "<a href='http://www.mass.gov/mgis/'>MassGIS</a>"
});

…results in an interactive map with MassGIS Open Space WFS vector features overlayed on an OpenStreetMap base layer, using WGS84 lat/lon as display coordinates.

On a sidenote: OpenLayers comes with a Python proxy to retrieve information from remote servers via an XMLHttpRequest. Here is a good how-to get Python play well with IIS 6 on Windows Server 2003, which was quite useful.

Don’t forget to add the domains you’re trying to access to the Python proxy. For MassGIS you would add following string for instance:

allowedHosts = ['giswebservices.massgis.state.ma.us']

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…

cu @ bcv08

BarCamp Vienna 2008

Next weekend a BarCamp is taking place again in Vienna, organized by Dieter, Max and Michaela (thanks all!) at HP’s office space.

Alex and I would like to take the opportunity and introduce, talk about and discuss our project timatio. And I would be interested in doing a session about OpenStreetMap – advantages, use case scenarios or licensing issues compared to other map sources for instance.

Great experiences at former BarCamps let me look forward to an interesting event next weekend. Be there!

SVG on the iPhone

Mr Timoney pointed my attention to possible SVG support brought with the latest iPhone OS 2.1 update. Unfortunately I sold my iPod touch on eBay last week – got a brand new Nokia E71 instead and am totally happy with it. So I couldn’t verify or test SVG on the iPhone Safari myself and had to ask somebody for help. Richard was kind enough to quickly try and access a SVG site on his iPod touch and send me a screen shot.

Guess what, it works!

SVG is supposed to be the flash killer since its first appearance, and never really succeeded as we know very well. SVG is still a good choice for mapping applications in my opinion, for light-weighted thematic web mapping applications to be more precise. Vector graphics handled by an AJAX front-end, used to visualize statistical attribute data provide a user-friendly interface and are usually easy to develop. Mapping APIs like Google Maps or Open Layers support and use SVG. Web browsers like Firefox and Safari for instance natively support SVG elements, no “but you need an extra plugin discussion” anymore. There is good portion of potential users for SVG based mapping applications.

Anyways, the odd thing with the iPhone and SVG is now, that a quite popular and hyped platform supports SVG but doesn’t play Flash. That’s maybe the time SVG developers have waited for.

I haven’t had the chance yet to play with SVG on the iPhone by myself. But I’m curious how far SVG support goes, what functionality is possible and how the iPhone’s multi-touch gestures can be used in mapping applications. Maybe somebody else can offer more insights on that. I’m not expecting our clients moving to the iPhone, but I would like to see if our simple mapping applications work on the iPhone or can be easily ported to suit the iPhone dimensions – should be easy with *Scalable* Vector Graphics though. However, accessing interactive maps and dig into some regional data while being in meetings or on the way could be a valuable option sometimes.

Again

1Password for iPhone/iPod was released today. It’s a life-saver on the desktop and I was waiting for the native mobile application.

Meanwhile I really believe it must be my karma or something, maybe I killed a fly by accident recently, I’m sorry!

Again, I downloaded the app, installed it, finished the initial setup and it kept crashing each time I tried to access my passwords. Not quite what I expect from software which holds a good part of my confidential information.

At the support forums I found the thread dealing with exactly the same problem. The developers already released version 1.1 and 1.2, both of them are sitting at Apple and waiting for approval:

Version 1.1/1.2 is a lot more stable and we’re just waiting for it to be approved in the App Store.

That’s good, basically, but we remember, the last time it took Apple 23 days to approve a 1.0 to 1.1 update containing a few bugfixes. Let’s see if they have speed up approval processes and will release an updated 1Password before summer ends…

Bootleneck

I had the brilliant idea to use my iPod touch as eBook reader, since there are all those wonderful applications floating around after the firmware 2.0 update. So I decided to pick an eBook reader at the iTunes App Store, paid for it, downloaded it, and it crashed every time I tried to open an eBook. Sweet.

I emailed the developer about the bug. Within a few hours I received an answer telling me the bug is already fixed in version 1.1, but it’s stuck at Apple’s approval process and he has no information when Apple is going to release his application.

Today, a week later, still no luck reading eBooks. Instead I found a note at the developer’s website.

Just a quick update as many have asked. Version 1.1 of BookShelf was submitted to Apple on the morning of July 15th. It’s been listed as “In review” status since then. My emails to Apple have received either a tracking number or no response at all. I have no idea when 1.1 will be released by Apple.

Sorry that I don’t have more useful information on this. The AppStore experience has been incredibly frustrating so far.

This is indeed frustrating, for me as customer, who bought an unusable product, too. After MobileMess, Apple seems to create a bottleneck at the App Store. I’d recommend developers to release less and release major updates. Expect some time waiting at Apple’s App Store gates, the gate keeper is busy.

Less is more

or Twitter is the reason for not filling up this space here with more content recently.

Seriously, when Twitter was released I never would have thought that the 140 character messages will catch my attention. As I used to say LOST is not my thing and now I’m into the 4th season.

Guess I was wrong.

Twitter is fascinating, it’s simple and quick – no title, no abstract, body, trackback, category or comment, basically just a line. It makes you post micro-fragments of your day and read other’s. Mostly unimportant and meaningless, sometimes resulting in short conversations.

It’s not the single message stuffed into 140 characters which draws so much attention. It’s the stream, the ongoing sum of tweets what makes it so interesting.

Ask content providers first

Flickr Resistance Against Microsoft Takeover

User generated content means users care what happens to their content. They even might act like shareholders and want a vote or at least share their opinion. It’s a totally interesting aspect in business decision making processes – asking the crowd before the board.

I personally don’t care if Yahoo! is taken over by Microsoft or not. If the service is good and works for me, I use it, no matter what label is on it. [via Max]