Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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:

share|improve this answer
2  
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 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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.