Skip to content

Instantly share code, notes, and snippets.

@scw
Last active March 12, 2018 04:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scw/5720029 to your computer and use it in GitHub Desktop.
Save scw/5720029 to your computer and use it in GitHub Desktop.
Generate projection files (.prj) from ArcGIS defaults.
import arcpy
import os
def create_path(path):
if not os.path.exists(path):
print "Creating directory {}".format(path)
os.makedirs(path)
# default application data dir; e.g. c:\Users\scw\AppData\Roaming
app_data_path = os.getenv('APPDATA')
# get current ArcGIS version
arc_version = arcpy.GetInstallInfo()['Version']
# change this path if you'd like the spatial references to be written
# out elsewhere, this is the default directory for .prj files.
output_base = '{0}\\ESRI\\Desktop{1}\\ArcMap\\Coordinate Systems'.format(app_data_path, arc_version)
create_path(output_base)
for sr_name in arcpy.ListSpatialReferences():
# spatial reference names use a single \ separator, double them up.
sr_filename = "{}.prj".format(sr_name.replace("/", "\\"))
output_file = os.path.join(output_base, sr_filename)
output_dir = os.path.dirname(output_file)
# create this parent directory, if needed
create_path(output_dir)
with open(output_file, 'w') as prj_file:
sr = arcpy.SpatialReference(sr_name)
prj_file.write(sr.exportToString())
@matejt
Copy link

matejt commented Dec 16, 2013

nice!

@nathanmooney
Copy link

Primo!

@scw
Copy link
Author

scw commented Nov 11, 2014

A user with ArcGIS 10.1 (no Service Pack) reported an issue with the output PRJ files causing a conflict within ArcGIS. The simplest solution is to not write these PRJ files to the default ArcGIS location, but instead to a new folder. For example, to create a 'projections' directory, change line 18 to this:

output_base = r'C:\projections' 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment