[GIS] Programmatically creating exports from PostGIS database into different file formats (e.g. shapefile, dxf, dgn,…)

gdalogrpostgispython

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.

Best Answer

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='127.0.0.1' 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.
    res.append(respre)     

con.commit()
cur.close()
con.close()

i hope it helps you....

Related Question