Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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))

countries.sort()
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
1  
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

 
discard

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

Browse other questions tagged or ask your own question.