We have e python script, using osgeo, that accesses a PostgreSQL database for GIS data. The script have unexpected stopped working, and I have been able to boil it down to: .GetFieldCount() returns 0, though fields do exist.
I can reproduce the error via this script, which is a modified version of the online-help.
from osgeo import ogr import sys databaseServer = "postgrestest1" port = "5433" # <-- I know default is 5432, bu this server uses 5433 databaseName = "pgv_2017" databaseUser = "myname" databasePW = "mypassw" connString = "PG: host=%s port=%s dbname=%s user=%s password=%s" %(databaseServer,port,databaseName,databaseUser,databasePW) lyr_name = "k.jis" def GetPGLayerFields( lyr_name ): conn = ogr.Open(connString) print "conn:", conn lyr = conn.GetLayer( lyr_name ) print "lyr:", lyr lyrDefn = lyr.GetLayerDefn() print "lyrDefn:", lyrDefn num_flds = lyrDefn.GetFieldCount() print "num_flds:", num_flds for i in range(num_flds): print lyrDefn.GetFieldDefn( i ).GetName() conn.Destroy() if __name__ == '__main__': GetPGLayerFields( lyr_name )
The output looks like this:
C:\Martin\Work>python test_pg_connect.py conn: <osgeo.ogr.DataSource; proxy of <Swig Object of type 'OGRDataSourceShadow *' at 0x00000000021C0720> > lyr: <osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at 0x00000000021C0660> > lyrDefn: <osgeo.ogr.FeatureDefn; proxy of <Swig Object of type 'OGRFeatureDefnShadow *' at 0x0000000004541030> > num_flds: 0 C:\Martin\Work>
So the program can see the table, but thinks it has no fields?
But as seen below, the same table seen in the db (with the same user credentials) has 2 fields, 'jis_id' and 'geom'.
Can anybody suggest a way to access these fields from python/osgeo?