Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working thru "Python Geospatial Programming" and I have a problem in a program in Chapter 6 that I cannot seem to solve.

The small program:

import osgeo.ogr

shapefile = osgeo.ogr.Open("TM_WORLD_BORDERS-0.3.shp")

layer = shapefile.GetLayer(0)
numberFeatures = layer.GetFeatureCount()

countries = []      # List of (code, name, minLat, maxLat,
                    # minLong, maxLong) tuples.

for i in range(numberFeatures):
    feature = layer.GetFeature(i)
    countryCode = feature.GetField("ISO3")
    countryName = feature.GetField("NAME")
    geometry = feature.GetGeometryRef()
    minLong, maxLong, minLat, maxLat = geometry.GetEnvelope()
    countries.append((countryName, countryCode, minLat, maxLat, minLong, maxLong))

for name, code, minLat, maxLat, minLong, maxLong in countries:
    print "%s (%s) lat=%0.4f..%0.4f, long=%0.4f..%0.4f" % (name, code, minLat, maxLat, minLong, maxLong)

It works perfect if I do not use the "NAME" attribute (field), for example AREA or POP15 which all come as attributes of the world borders shp file. NAME is a string field of 50 characters and all looks good in the dbf file.

The program does not issue any error codes and it comes back as successful but it does not go beyond that line.

Really perplexing!!

share|improve this question
sometimes osgeo.ogr.UseExceptions() exposes more details – Mike T Apr 27 '13 at 22:14
Well, I tried on your shp - all fine! What you could do, is print out all Field names to check if it is really what it is :) 'n=0 while n < feature.GetFieldCount(): print feature.GetFieldDefnRef(n).GetName() n+=1' – najuste Apr 27 '13 at 22:40
no problem for me with the original program, not in chapter 6 but in chapter 5, p.103 – gene Apr 28 '13 at 10:54
Thanks to all! I ended up uninstalling & reinstalling libgdal1 with synaptic in Ubuntu and now all works perfect!! – Carlos Apr 29 '13 at 20:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.