Is there a kind of wrapper/library written in python which would enable me to programatically create exports from postgis database into different file formats (most common being, shapefile, dxf, dgn,...). If not i ll probably build one myself using psycopg2, gdal and ogr, these are probably components to start with, yeah?

I need this because i ll be making some kind of web service for data exports.

3 Answers 3


i think you dont need lots of search for your work about exporting from postgis. everything is clear gdal, psycopg2 and python can make lots of things for your request.

i have been using them with geodjango for same request from user for a long time. you can get more info about Using psycopg2 with PostgreSQL here and a document which name is Reading and Writing Vector Data with OGR highly recommended for you to read...

the basic connection with postgis:

res = []
con = psycopg2.connect("dbname='mydb' user='reid' host='' password='reid'")
cur = con.cursor()
cur.execute("SELECT ST_Area(the_geom) FROM mypoly")
rows = cur.fetchall()

for row in rows:
    respre = row[2] # find your result.


i hope it helps you....

  • Thats exactly what i ll in the end, i was just wondering if someone did the dirty work for me already ;)
    – U2ros
    Nov 29, 2012 at 13:23
  • @U2ros the code i have shared with you is not all the code for your purpose, you have to work on it for reaching the end point. Good Luck :)
    – urcm
    Nov 29, 2012 at 13:31
  • I know i know, still, i appreciate the "outline"
    – U2ros
    Nov 29, 2012 at 13:50
  • Could anyone kindly share a full example, or a link for how to do it? Jan 27, 2016 at 21:19

Tidiest approach that I have seen is to use python to trigger the pgsql2shp command line utility which is part of the postgis package.

Not very pythonic, but very simple and robust.

e.g. See How can I get a shapefile from a postgis query?

Once you have a shapefile it is straight forward to convert to most common formats using ogr2ogr, also over the command line. http://www.gdal.org/ogr2ogr.html

Both DGN and DXF seem to be supported: http://www.gdal.org/ogr_formats.html


import os #of course you should use subprocess instead...

os.system("/path/to/postgis/bin/pgsql2shp -f ./output_shapefile.shp ... database_name \"SELECT * FROM schema.tablename LIMIT 5;\"")

os.system("/path/to/ogr2ogr -f DXF ./output_converted.dxf ./output_shapefile.shp")

os.system("/path/to/ogr2ogr -f DGN ./output_converted.dgn ./output_shapefile.shp")

You mention dgn and dxf and open source code then answer is probably No. To my knowledge there is no good free or open source solutions to work with dgn (v8, v7 may even work) (not sure about dxf) files

If you dont have to touch dgn (v8, v7 may even work) then GDAL supports kml (use st_ASKML and XML to get full kml). QGIS has option to "Save as..." for vector layers and probably has somekind python library for it.

I use FME for my dgn to shp/db/dgn/csv/raster conversion. Also FME has support for PostGIS and Postgresql databases (and with some strange lisence you get support for Esri etc..). Works great and costs a lot, isn't open source.

  • I don't have any problems exporting when i have working needs to do so ;) I love FME as well hehe. However, i was thinking about a tool or a bunch of scripts, that i could add into a web service context for the consumers environment who won't ne using advanced tools but rather s simple webgis. Thanks anyway
    – U2ros
    Nov 29, 2012 at 13:09
  • FME server or FME SDK? prbably can handle that service part. Biggest problem are dgn and dxf fileformats. Nov 29, 2012 at 13:14
  • And i forgot GeoServer. It can export published layer in several formats WFS (docs.geoserver.org/latest/en/user/services/wfs/…) and WMS (docs.geoserver.org/stable/en/user/services/wms/…) Nov 29, 2012 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.