10

I am new to python geospatial programming. i ran the following script and got the corresponding error message

>>> import osgeo
>>> import osgeo.ogr
>>> shapefile = osgeo.ogr.Open("tl_2009_us_state.shp")
>>> numLayers = shapefile.GetLayerCount()

Traceback (most recent call last):   
    File "<pyshell#5>", line 1, in <module>
    numLayers = shapefile.GetLayerCount() AttributeError: 'NoneType' object has no attribute 'GetLayerCount'
  • Tried your code locally and it works. So, which version of GDAL do you have installed? – Crischan May 19 '12 at 17:14
  • The script could not access your shapefile data. Kindly save the python script within the folder containing your data i.e the tl_2009_us_state.shp file. – CalebJ Aug 23 '18 at 19:26
16

So basically, what this is saying, in Python speak, is that your attempt to open the shapefile failed. When something like osgeo.ogr.Open() fails, it usually returns None, which, in your case, gets assigned to your variable "shapefile". When you try to then access shapefile later, it tells you that shapefile is "NoneType" (rather than the type of object that osgeo would have created) and that NoneType objects don't have the method GetLayerCount.

How do you fix this? First, test for errors in your code - it'll give you better messages. Something like:

import osgeo
import osgeo.ogr
try:
    shapefile = osgeo.ogr.Open("tl_2009_us_state.shp")

    if shapefile: # checks to see if shapefile was successfully defined
        numLayers = shapefile.GetLayerCount()
    else: # if it's not successfully defined
        print "Couldn't load shapefile"
except: # Seems redundant, but if an exception is raised in the Open() call,
    #   # you get a message
    print "Exception raised during shapefile loading"

    # if you want to see the full stacktrace - like you are currently getting,
    # then you can add the following:
    raise

So, now we need to answer the question of why your shapefile isn't loading. My guess is that you need to provide the fully qualified path (ie, "C:\Users...\tl_2009_us_state.shp") because osgeo can't find your shapefile with the path currently provided. That's a hunch though.

  • 1
    No, that's not "Python speak" at all. As Mike says below, instead of returning None, this is what osgeo.ogr ought to do: "IOError [brief description]". – sgillies May 21 '12 at 14:18
  • Sorry, what I was attempting to say was that "'NoneType' object has no attribute 'GetLayerCount'" is a pretty standard Python error message that often occurs when you expect an object to have been assigned that, for whatever reason, didn't get assigned. Sorry that wasn't clear. – nicksan May 21 '12 at 15:10
7

@Nick's answer is correct: "NoneType" means that the data source could not be opened. OGR (and GDAL) don't raise exceptions where they normally should, and unfortunately ogr.UseExceptions() doesn't seem to do anything useful. Here is my usual code block to actually raise the proper exceptions:

from osgeo import ogr

# Change this to your OGR data source
ds_fname = r'C:\temp\tl_2009_us_state.shp'

ds = ogr.Open(ds_fname)
if not ds:
    raise IOError("Could not open '%s'"%ds_fname)

numLayers = ds.GetLayerCount()
...
1

I have encountered this error before and has been stuck with it for so long. I got it to work by using a different shapefile. The US Tiger shapefile must have been corrupted or something. I'm using gdal1.6 here.

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.