14

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'
2
  • Tried your code locally and it works. So, which version of GDAL do you have installed?
    – Crischan
    May 19, 2012 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, 2018 at 19:26

4 Answers 4

17

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.

2
  • 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, 2012 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, 2012 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.

1

I'd say make sure all the files that are associated with that shapefile (.cpg, .dbf, .prj, .sbn, .sbx, .shp, .shx) are all found in the same file folder.

1
  • This answer saved a lot of my time. I was only copying shapefile and expecting the software to work. It always gave the NoneType error. After copying all the files to a single directory and opening the file from that directory I was able to run the software. Thank you. Sep 26 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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