3

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

enter image description here

  • 1
    Print name not spatialreference – FelixIP Mar 7 '18 at 19:20
  • looks like the spatial reference is not a string type, try adding a str() around the spatial reference – Simon.y Mar 7 '18 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 Mar 8 '18 at 5:04
6

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]

  • 1
    didn't see this when I posted mine. this is a better alternative. – yanes Mar 7 '18 at 19:49
  • 1
    @yanes, no problem, just I was editing the answer to explain why .Name is important – Moh Mar 7 '18 at 19:50
2
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.

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.