O'Reilly logo

Python Geospatial Analysis Essentials by Erik Westra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exporting spatial data

Our introduction to spatial databases is almost complete; the only thing left to examine is how to get spatial data out of PostGIS again, for example to save it back into a shapefile. To extract a spatial value from a GEOGRAPHY field, use the ST_AsText() function. For example:

cursor.execute("SELECT name,ST_AsText(outline) FROM borders")
for name,wkt in cursor:
    geometry = osgeo.ogr.CreateGeometryFromWkt(wkt)
    ...

You can then use the OGR geometry to write the spatial data into a shapefile, or do anything else you wish to do with it.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required