I am trying to read and print out the coordinate system of a shapefile like this
import arcpy
dataset = r"E:\GIS\Centers.shp"
desc = arcpy.Describe(dataset)
extent = desc.extent
spatialReference = desc.spatialRe
name = desc.name
dataType = desc.dataType
print " File Name => " +name +" / File Type => " + dataType + " / Coordinate System => " + spatialReference;
but I am getting this error on printing spatialReference
part
Message File Name Line Position Traceback
9 TypeError: coercing to Unicode: need string or buffer, geoprocessing spatial reference object found
-
1Print name not spatialreferenceFelixIP– FelixIP2018年03月07日 19:20:10 +00:00Commented Mar 7, 2018 at 19:20
-
looks like the spatial reference is not a string type, try adding a str() around the spatial referenceSimon.y– Simon.y2018年03月07日 19:30:12 +00:00Commented Mar 7, 2018 at 19:30
-
A SpatialReference is an object that has a number of properties. The name is certainly not unique, since there are an infinite number of spatial references possible, just by choosing different XY origins, while still using a single coordinate system.Vince– Vince2018年03月08日 05:04:06 +00:00Commented Mar 8, 2018 at 5:04
2 Answers 2
Your code should be like this
import arcpy
dataset = r"E:\GIS\Centers.shp"
desc = arcpy.Describe(dataset)
extent = desc.extent
spatialReference = desc.spatialReference.Name
name = desc.name
dataType = desc.dataType
print " File Name => " +name +" / File Type => " + dataType + " / Coordinate System => " + spatialReference
I changed (desc.spatialReference.Name) by adding .Name and the result will be the name of the reference system (e.g WGS-84) as stated in [ArcPy SptialReference]
import arcpy
dataset = r"E:\GIS\Centers.shp"
desc = arcpy.Describe(dataset)
extent = desc.extent
print extent # check value on screen
spatialReference = desc.spatialReference
print spatialReference.name # Check value on screen
print " File Name => " +name +" / File Type => " + dataType + " / Coordinate System => " + spatialReference.name;
Make sure the variables Name and DataType exist in your code. Also note that on your code you truncated spatialReference into spatialRe. It might be when you just pasted it here.
Explore related questions
See similar questions with these tags.