Monday, May 16, 2011

Spatial Reference Class

The other day, I was using the new data driven page tools found in the cartography section of ArcToolbox, and I noticed that the 'Grid Index Feature' tool will generate the incorrect sized grid cells if the spatial reference is not set when run outside of ArcMap. To fix this problem, use the spatial reference object and set the output spatial reference parameters to whatever coordinate system you are working with.

Before starting, you need to know what geographic or projected coordinate system needed to use for the tool.  Let's assume that WGS-1984 will be used which has a WKID of 4326.  It's a very common geographic coordinate system, so I won't go into any details about WGS-1984.

Usings the geographic coordinate systems references from the above provided links, create a spatial reference object based on the WKID.
Example: Using Factory Codes

import arcpy
from arcpy import env
WKID = 4326 # WGS-1984
sr = arcpy.SpatialReference()
sr.factoryCode = WKID
sr.create()
env.outputCoordinateSystem = sr


Now the default output coordinate system in WGS-1984.

There are many other ways to create a spatial reference obeject and they can be found here.

Enjoy

5 comments:

Jahfet said...

Finally! I never worked out how to do this. EPSG codes are pretty much the standard way of setting a CRS in all the open source GIS packages, but in ArcGIS they are really hard to find and use... Thanks for that.

Andrew said...

You're very welcome. Glad to help.

Anonymous said...

This is very helpful. The canonical way to create a GCS in ArcGIS:

arcpy.SpatialReference(os.path.join(arcpy.GetInstallInfo()["InstallDir"],"Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj"))

is bizarre and stupid. Mind-bogglingly easy to break, too.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Thank you, Thank you, Thank you, Thank you. You saved me from a headache. I was about to get mad before coming here.