SRIDs, GeoDjango, PostGIS, and me

Now that I have GeoDjango up and running I was able to import a bunch of airport data using Adam’s code. Back to PostGIS to play!

select facility_name,
    ST_GeomFromText('POINT(-122.4522 37.7555)')))
    as distance
  from airports_airport
  where control_tower = true
  order by distance
  limit 10;

ERROR:  geometry_distance_spheroid: Operation on two GEOMETRIES with different SRIDs

Frowny. Face. What’s that mean?

Long story short, an SRID identifies which Spatial Reference System the coordinates are with reference to. It’s a bit like units: it’s not enough to say something “weighs 100”, you have to specifiy if you mean 100 lbs, or 100 kgs, or whatever. For more theoretical background, see Geodesy. There’s a giant database of SRIDs at (Related keyword: EPSG). Greatest hits:

  • 4326: WGS 84, the reference most GPSes use. I think many people assume this is the SRID by default.
  • 4269: NAD83, a popular North American standard
  • 54004: World Mercator
  • 900913: Google Maps’ modified Mercator. (It spells “google” in l33t).

So the problem is I’m trying to do arithmetic with points in two different SRIDs.

select ST_SRID(point) from airports_airport limit 1;

select ST_SRID(ST_GeomFromText('POINT(-122.4522 37.7555)'));

select ST_SRID(ST_GeomFromEWKT('SRID=4326; POINT(-122.4522 37.7555)'));

GeoDjango defaults to SRID 4326; you can use others, details here. PostGIS ST_GeomFromText defaults to an SRID of -1, which I guess means unknown. But if you’re explicit then it works fine. You can also use PostGIS to transform from one SRID to another, I believe underneath it uses PROJ.4

    ST_GeomFromText('POINT(743238 2967416)', 2249),
SRID=4326;POINT(-71.1776848522251 42.3902896512902)

Personally I find this all frustrating; can’t we just use WGS 84 everywhere and be done with it? Sure, you still need to project when rendering a flat map, but consider that a separate thing entirely.

Fun fact: the reference points for WGS 84 move. And are remeasured roughly yearly. Apparently Greenwich is not at 0 longitude in WGS 84.