Last active
March 12, 2018 04:28
-
-
Save scw/5720029 to your computer and use it in GitHub Desktop.
Generate projection files (.prj) from ArcGIS defaults.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
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
Primo!