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

  • Maybe an upper case issue.... getSpatialRef instead of GetSpatialRef??? – YoLecomte Oct 29 '17 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' – PADAMNABH ELECTRONICS Oct 29 '17 at 9:20
  • What is layer?? – YoLecomte Oct 29 '17 at 9:21
  • 1
  • 1
    So layer variable contain a None type object. Try to get a reference to a layer before and it should work – YoLecomte Oct 29 '17 at 9:32
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 '
|improve this answer|||||
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".

|improve this answer|||||
0
AttributeError: 'NoneType' object has no attribute 'GetSpatialRef'

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

|improve this answer|||||

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.