[Gdal-dev] OGR and Python. Accessing spatial data from non-spatial tables

Frank Warmerdam warmerdam at pobox.com
Fri Feb 16 19:07:30 EST 2007

Sampson, David wrote:
> Hey Folks,
> I'm looking for help using OGR and python. I am pretty novice with 
> python at this stage so I think I am loosing sight of what objects need 
> to be made. Some guidance would be great.
> I am trying to create polygons (rectangles) from an ODBC connection to a 
> non-spatial table.
> In all I need to access 8 collumns (x and y for four points)
> Each polygon is defined by a single row
> Groups of rows exist based on a SEGMENT collumn. Each group could have 
> upwards of 100 polygons.
> This is some of the code I have been working on


This script will read a table from an OGR datasource and fetch the
fields you name.  In your case instead of test.csv you will want
something like:

   "ODBC:" + UID + "/" + PW + "@" + DSN + "," + TBL

But you don't need to open each column seperately.

import ogr

ds = ogr.Open('test.csv')

lyr = ds.GetLayerByName('test')

feat = lyr.GetNextFeature()

while feat is not None:

     nw = (float(feat.X_NO),float(feat.Y_NO))
     ne = (float(feat.X_NE),float(feat.Y_NE))
     sw = (float(feat.X_SO),float(feat.Y_SO))
     se = (float(feat.X_SE),float(feat.Y_SE))

     print nw, ne, sw, se

     feat = lyr.GetNextFeature()

Best regards,
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo, http://osgeo.org

More information about the Gdal-dev mailing list