Before we move on to querying and displaying, let’s look at how to get data into the database from “bare metal” – as nice as LayerMapping is not everything is in a shapefile, and using other formats has a few snares that are easy to avoid but you need to know them.
Our data source is going to be comma separated from www.uscampgrounds.info – a nice dataset of public places available for primitive camping.
We built the model for a campground back on November 2, and it’s available in full form from my library https://github.com/adamfast/geodjango-uscampgrounds
So let’s pick up in load.py https://github.com/adamfast/geodjango-uscampgrounds/blob/master/uscampgrounds/load.py I’m only going to post relevant code here, I’m assuming you know how to build a CSV importer.
try:camp = Campground.objects.get(campground_code=row[CAMPGROUND_CODE].lower(), campground_type=row[TYPE])except Campground.DoesNotExist:camp = Campground(campground_code=row[CAMPGROUND_CODE].lower(), campground_type=row[TYPE])
camp.name = scrub_chars(row[CAMPGROUND_NAME])camp.phone = scrub_chars(row[PHONE])camp.comments = row[COMMENTS]camp.sites = row[SITES]camp.elevation = row[ELEVATION]camp.hookups = row[HOOKUPS]camp.point = Point((Decimal(row[LON]), Decimal(row[LAT])))camp.save()
Snare #1: the order is not what you expect. I’ve been using Google Earth, GPSes and all this stuff for a long time and points are always referred to as latitude, longitude. Not here. Make sure you put longitude first or your maps are going to look very funny.
Snare #2: The extra tuple is needed, at least in this case when I’m passing in Decimals. If you pass in floats, the tuple isn’t necessary but I’m in the habit of always using Decimal.
Now we have polygon data and point data available, so we can start to combine the two.