2

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?

/Martin

Same data viewed from pgadmin

  • I believe that layerDefinition.GetFieldCount() returns the number of user defined fields and because your table has only fid and geometry the number is zero which is correct. – user30184 Sep 11 '17 at 14:35
  • Thanks for your instructive replies, they are valuable help. Can any body suggest a way to list and/or access the other fields, including the two I have in my table? – Martin Sep 11 '17 at 17:33
  • See the answer by @Pablo. – user30184 Sep 11 '17 at 18:37
  • the 'jis_id' is a user defined field, we made it on purpose with an SQL script. It's a foreign key to another db table. – Martin Sep 12 '17 at 7:31
  • Add SQL for CREATE TABLE... and one INSER INTO... and maybe somebody will make a test with their own PostGIS. – user30184 Sep 12 '17 at 7:50
2

The feature ID and the geometry are "special" fields and you need to use specific methods to access them.

In order to access the feature ID (jis_id) use feature.GetFID(), for example (Python 2.7):

layer.ResetReading()
for feature in layer:
    print feature.GetFID()

To access the geometry (geom), use feature.GetGeometryRef(), it returns a Geometry object. Then you can use for example geometry.ExportToWkt() or any other geometry method.

  • Hi @Pablo. Your script runs fine, but the FID that it returns is not the same number as the 'jis_id' I'm looking for. Apparently it is some (internal?) integer id number in the db. – Martin Sep 12 '17 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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