Getting the data in, part 2 – CSV and “manual” importing

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])

These 4 lines may look odd to you, so let me explain what’s happening. I didn’t forget about get_or_create(), in this case I want to match ONLY on code and type. If I find one, I’m going to replace every other field on it with the latest data. Otherwise I create it.

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()

Now it’s time to set the only geospatial piece of the model – the point.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *