0

I am working on Ubuntu, when I tried to read spatial reference of shapefile it give me this error.

spatialRef = layer.GetSpatialRef().ExportToProj4()

Traceback (most recent call last):

File "< stdin>", line 1, in < module>

AttributeError: 'NoneType' object has no attribute 'GetSpatialRef'

Help me to remove it

nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Oct 29, 2017 at 8:46
7
  • Maybe an upper case issue.... getSpatialRef instead of GetSpatialRef??? Commented Oct 29, 2017 at 9:09
  • still getting same error spatialRef = layer.getSpatialRef() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'getSpatialRef' Commented Oct 29, 2017 at 9:20
  • What is layer?? Commented Oct 29, 2017 at 9:21
  • 1
    www2.census.gov/geo/tiger/TIGER2009/tl_2009_us_state.zip (state polygon layer) Commented Oct 29, 2017 at 9:22
  • 1
    So layer variable contain a None type object. Try to get a reference to a layer before and it should work Commented Oct 29, 2017 at 9:32

3 Answers 3

2

I don't understand your problem. You need a layer or a geometry or an EPSG code

With the www2.census.gov/geo/tiger//TIGER2009/tl_2009_us_state.zip shapefile

from osgeo import ogr
file = ogr.Open('tl_2009_us_state.shp') # from www2.census.gov/geo/tiger/TIGER2009/tl_2009_us_state.zip 
layer = file.GetLayer()
spatialRef = layer.GetSpatialRef()
spatialRef.ExportToProj4()
'+proj=longlat +datum=NAD83 +no_defs '
spatialRef.AutoIdentifyEPSG()
spatialRef.GetAuthorityCode(None)
'4269'

With a geometry

feature = layer.GetNextFeature()
geometry = feature.GetGeometryRef()
spatialRef = geometry.GetSpatialReference()
spatialRef.ExportToProj4()
'+proj=longlat +datum=NAD83 +no_defs '

With an EPSG code (EPSG:4269)

spatialRef = osr.SpatialReference()
spatialRef.ImportFromEPSG(4269)
spatialRef.ExportToProj4()
'+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs '
answered Oct 29, 2017 at 15:03
1

This is how I read the spatial reference of a shapefile.

from osgeo import ogr
file = ogr.Open("C:\file_path\file_name.shp")
layer = file.GetLayer()
spatial_ref= layer.GetSpatialRef()
print spatial_ref

To get the individual spatial reference attributes:

spatial_attr = spatial_ref.GetAttrValue('projcs')
print spatial_attr

This will output the projection coordination system, e.g. "WGS_1984_UTM_Zone_38N".

answered Dec 15, 2017 at 12:32
0
AttributeError: 'NoneType' object has no attribute 'GetSpatialRef'

The error message means, that your layer variable is empty. Check the way you obtain it.

answered Dec 12, 2018 at 12:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.