I'm writing a python script that uses the CreateFeatureclass_management tool, without a template. One of the required parameters is a spatial reference. I used to be able to point to the system *.prj file in 9.3, but that's no longer an option, and I don't want to point to an existing shapefile's *.prj because that will cause issues when I share the script.

Now that Esri doesn't install all the *.prj files with ArcGIS, how can I set the projection in a script without referencing an existing shapefile?


As stated in the help simply choose your spatial reference by name or factory code.

# Using spatial reference name
sr = arcpy.SpatialReference("Hawaii Albers Equal Area Conic")

# Using factory code
sr = arcpy.SpatialReference(32145)

You can find the list of spatial reference factory code here:

  • 4
    Also: not documented, but if you refer to a .prj file that has the same name as that of a factory code and the file does not exist, it will still work. So your 10.0 arcpy code that reads sr = arcpy.SpatialReference("c:\program files\esri\etc\Hawaii Albers Equal Area Conic.prj") will silently do the right thing even though the file isn't there. – Jason Scheirer Jun 27 '14 at 21:23

The spatial reference class is very adaptable. Building a little on what Eric said, here's something perhaps more immediately helpful for you, creating the SR object from one of your fcs, then getting the factory code...or likewise you could get it from your 'favorites' if you have it saved. In short, do something like this:

# this is prelim code, not to include in your script, to get the factory code
dataset = r"pathname to dataset with the appropriate spatial reference"
spatial_ref = arcpy.Describe(dataset).spatialReference
factory_code = spatial_ref.factoryCode
print factory_code
# note the above printed number; this is your desired factory code

# then in your code, set your spatial ref obj using the noted factory code
spatial_ref = arcpy.SpatialReference(factory_code)

All that info is in the help docs here:

SpatialReference (arcpy) Desktop » Geoprocessing » ArcPy » ArcPy classes http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000000v000000

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.