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

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 7, 2018 at 18:42
3
  • 1
    Print name not spatialreference Commented Mar 7, 2018 at 19:20
  • looks like the spatial reference is not a string type, try adding a str() around the spatial reference Commented 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. Commented Mar 8, 2018 at 5:04

2 Answers 2

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]

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

answered Mar 7, 2018 at 19:46

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.