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– YoLecomte2017年10月29日 09:09:50 +00:00Commented 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'PADAMNABH ELECTRONICS– PADAMNABH ELECTRONICS2017年10月29日 09:20:42 +00:00Commented Oct 29, 2017 at 9:20
-
What is layer??YoLecomte– YoLecomte2017年10月29日 09:21:30 +00:00Commented Oct 29, 2017 at 9:21
-
1www2.census.gov/geo/tiger/TIGER2009/tl_2009_us_state.zip (state polygon layer)PADAMNABH ELECTRONICS– PADAMNABH ELECTRONICS2017年10月29日 09:22:36 +00:00Commented Oct 29, 2017 at 9:22
-
1So layer variable contain a None type object. Try to get a reference to a layer before and it should workYoLecomte– YoLecomte2017年10月29日 09:32:26 +00:00Commented Oct 29, 2017 at 9:32
3 Answers 3
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 '
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".
AttributeError: 'NoneType' object has no attribute 'GetSpatialRef'
The error message means, that your layer
variable is empty. Check the way you obtain it.