Wednesday, 7 October 2015

postgis - Accessing non-spatial PostgreSQL tables using OGR Python bindings?


When connecting to PostgreSQL/PostGIS using the OGR Python bindings is it possible to get non-spatial table names (i.e. OGR layers) from the connection?


Currently, doing:


conn = ogr.Open("PG: #params")
for layer in conn:
print layer.GetName()

Will only print the names of spatial tables, however I can access non-spatial tables by (for example) directly specifying the table name, e.g.:



layer = conn.GetLayerByName('non_spatial_table_name')

The OGR docs refer to the "PG_LIST_ALL_TABLES=YES" parameter, but I can't find how this links to API (let alone the Python SWIG bindings!).



Answer



For reference, it is possible to set GDAL configuration options using gdal.SetConfigOption().


To list all tables:


import osgeo.gdal as gdal
import osgeo.ogr as ogr

gdal.SetConfigOption("PG_LIST_ALL_TABLES", "YES")


conn = ogr.Open("PG: #params")
for layer in conn:
print layer.GetName()
# Will print all tables including non-spatial ones.

You can see further examples of gdal.SetConfigOption in the GDAL/OGR test suite PostGIS module: http://svn.osgeo.org/gdal/trunk/autotest/ogr/ogr_pg.py


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...