Stay Connected with the Boundless Blog

PostGIS ST_GeomFromGeoJSON New Addition To the OGC Formats

One of the things that makes PostGIS powerful and useful for web developers is that it can handle multiple input formats and multiple output formats. That means that a simple script binding a web service to the database can be a powerful processing and query tool. But, in order for those queries to work, the database has to speak the same language as the web service.

So, for output we have in addition to the OGC standard formats, ST_AsGML, ST_AsKML, ST_AsGeoJSON, and ST_AsSVG. And for input we have in addition to the OGC formats (WKB, WKT),  ST_GeomFromGML and ST_GeomFromKML. But web developers don’t work in GML or KML, they work in JSON, and our favourite web developers, Vizzuality wanted to have a ST_GeomFromGeoJSON function, and backed up their desire with funding.

It turns out that most of the work had already been done, I just needed to clean up an existing patch and integrate with the build system a little more. Like the from-GML and from-KML functions, this iteration of the from-JSON function probably needs to be moved into a more central part of the code tree, but that’s a programmer-level clean-up that can wait. For now, thanks Vizzuality for supporting PostGIS and thanks Kashif Rasul for the patch!

To prove to yourself that it works:

select st_astext(st_geomfromgeojson(st_asgeojson('POLYGON((0 0,1 1,1 0,0 0))')));

To actually see the JSON:

select st_astext(st_geomfromgeojson('{"type":"Point","coordinates":[1,1]}'));