December 2008


Geobase: Free Canadian Geodata for all!

William Lachance, 19 December 2008

Up until today, I was under the impression that the options for someone wanting raw Canadian road data were quite limited. Yahoo and Google maps are great, but their terms of use prevent creative and novel use (it’s against their terms of use create a derived work). OpenStreetMap is free of these restrictions, but its coverage can be kind of spotty in outlying areas. What’s a geohacker to do?

Little did I know that the Canadian government makes a complete survey of this information available under an extremely non-restrictive license (basically all you need to do is provide attribution). One better, they’ve come to an arrangement with the OpenStreetMap project that allows them to import all this data (under the creative commons attribution license), which will (as long as some care is taken) bring OpenStreetMap up to the level of Yahoo or Google maps. I’m positively giddy about the novel applications this should make possible.

That being said, integration of the data into OpenStreetMap will probably take some time. However, if you’re writing an application which consumes OSM data (like I am), there’s no need to wait. All I had to do to get an extremely accurate and complete OSM file for the region of the Halifax Regional Municipality was download the Nova Scotia road network in KML and use gpsbabel to simplify and trim said network down to my region of interest, outputting the result in OpenStreetMap format:

gpsbabel -i kml -f RoadSegment.kml -x simplify,error=0.01k \
      -x polygon,file=restrictpoly.arc -o osm -F hrm.osm

The result? Pure city road network win. Here’s a quick visualization courtesy of a cheesy 100 line script I knocked up using PyGame:

The empty space in the middle is the Bedford basin. The dense structure towards the center is the Halifax peninsula, connected via two bridges to the city of Dartmouth. The cluster to the north is Bedford.

I’d like to emphasize that, internally, this is a complete map, with street names and everything. Apparently GeoBase also provides a data set with address information which could be useful for writing a free geocoder, but I haven’t had the chance to look at that yet.

If all that was nerd speak to you, just wait. I’ll be bringing this topic down to earth in a way that any conscious being can appreciate in a few weeks…

Python’s class variables

Mark Côté, 15 December 2008

For some reason, perhaps some badly written tutorials, many people new to Python assume that class variables are a form of C++’s initializer list. In other words, they might think that these two are functionally equivalent:

class Toaster(object):
    lever_depressed = False

class Toaster
{
    Toaster() : lever_depressed(false) {}

    bool lever_depressed;
};

And that, therefore, you could do this

t1 = Toaster()
t1.lever_depressed = True
t2 = Toaster()
print t2.lever_depressed

and expect the output to be “False”, which indeed it would be. So where’s the problem?

Well, try this:

class Lever(object):
    def __init__(self):
        self.depressed = False

class Toaster(object):
    lever = Lever()

t1 = Toaster()
t1.lever.depressed = True
t2 = Toaster()
print t2.lever.depressed

Looks about the same right? We’re just wrapping the bool in a class in case we want to add other attributes to the lever. So what happens when you run this program? You see “True”!

There are actually two things at work here:

  • Class variables are not like C++ initializer lists; they are like C++ static member variables.
  • Simple native Python types are immutable and are copied when passed, whereas more complex types are mutable and passed by reference.

So in the first example, when t2 is created a copy of lever_depressed is made, since it is a simple boolean. But when t2 is created in the second example, a reference to the class variable ‘lever’ is made, since the latter is no longer a simple built-in type (even if it is an extremely simple user-defined class). This latter point isn’t strongly emphasized in introductory tutorials to Python, but it is rather important, especially when you think you have a function that modifies its arguments but it fails to work when you pass in a simple integer.

Thanks to wlach for helping me discover this.

The Fundamental Rule of Insurance

Peter McCurdy, 15 December 2008

You know, I spent 5 years getting a math degree, and somehow they never thought to mention The Fundamental Rule of Insurance. But since it needs wider distribution, here it is:

Only insure what you can’t afford to replace.

That’s it. It’s incredibly simple, and, if you think about it, it’s obviously correct – insurance companies are better at math than you are. They’ve worked out the odds. If you pay them to insure something, they’ll generally make money from you, unless you know something that they don’t (which generally falls under the heading of insurance fraud).  The other way to look at it is that you become your own insurance company, though you still need to get insurance to keep from going under when something out of the ordinary does happen – real insurance companies have this too, they call it reinsurance.  So you need insurance on the things you can’t afford to replace, such as your house, your job, in the form of disability insurance, if you’re not yet able to retire, etc.  Of course, even if you can afford to replace your car, you still need liability insurance, both for legal reasons, and because cars can cause damage well beyond their own value.

This also gives some interesting corollaries: as you get wealthier, you need to carry less insurance, and as you get less wealthy, you probably need to get more insurance.  Of course, once you lose too much wealth, you can’t afford the insurance either, which, er, sucks.  And as you get wealthier, or even if you just keep going along with the same income, you probably pick up more stuff you’d have to replace, which might tip things back over into needing insurance again.  Stupid math.