Dirt-Simple PostGIS HTTP API

One of the first projects I ever open sourced was a PHP web service framework I created for Mecklenburg back in 2010. It got a little coding love after its initial release (mostly from that mad genius Jason Sanford), but it really didn’t change much over time. It generally just worked, and I still get emails from people who use it today.

Q: What’s the worst code you’ve seen recently?
A: My own.

A little while back I did a screencast on the Ubersearch autocomplete thing, and when I dug into my old code I did a lot of frowning. People describe bad code as having a smell, but 3 cats have burned out my olfactory sense. Bad code gives me visual hallucinations. When I see bad code, I see this.

My bad code totem animal is an anteater.

I think if you can always say the worst code you’ve ever seen is the code you wrote 6 months ago, you’re a good programmer. I wrote this code years ago. I saw a giant anteater when I looked at this code.

Part of me wanted to just point people to CartoDB, which is like the Cadillac of this kind of stuff, but people still seem to be interested in this project, and there is something to be said for a really simple solution like this (the dependencies for CartoDB would choke a pony). So, I scrubbed it and tossed the new release on GitHub. Among the changes are:

  • I renamed it. The old title had REST in it, and REST it ain’t. At the time I kinda thought it was, at least in the common-language sense. Thanks to the many folks that helped educate me on this point.
  • Much, much, much cleaner code. The code reduction I’d guess is ~40%.
  • JSON or JSONP. That’s it. I can count the number of times I’ve used the myriad other return formats on no hands. You can still return JSON or GeoJSON as a field from a geometry column ala fields=st_asgeojson(the_geom) as geojson and then interpret that as JSON via jQuery.parseJSON(the_json_value) or something similar.
  • It uses PHP’s json_encode rather than my fugly recordset-to-JSON loop. It’s faster and does a perfect job of escaping junk in the return. You can get a record count via the_json_return.length.
  • Some sensible defaults so you don’t have to pass a bunch of empty arguments all the time. Curse my stupid brains.
  • Put in ST_ for the geometry functions so you don’t have to do the legacy SQL thing in PostGIS 2.0.
  • More friendly to different projections and geometry column names. I had 2264 and the_geom hard coded in a lot of places.
  • Much, much, much better documentation. I wasn’t doing anybody any favors with my old documentation, which probably should have come with gratis Tequila. I’m doing MarkDown conversion in JavaScript, so all the service docs are in MarkDown and the initial index.html content area is actually just GitHub’s README.md. I thought this was clever. It also is using a much prettier Bootstrap layout.

Enjoy, and patches are always welcome.