Aug 28, 2012

How to batch define projections with arcpy ArcGIS 10

ArcGIS 9.3 had a nice batch define tool in the toolbox that was removed in ArcGIS 10.  ArcGIS 10 has python 2.6.3 for the geoprocessing and well all kinds of fun stuff can be done with arcpy.

So how to batch define the projection for a folder full of shapefiles is the problem at hand. For those who can't wait I posted the code here first and the explanation comes after.

Python Code

# Name: 
# Description: Records the coordinate system information for the specified input dataset or feature class
# Author: ESRI

# import system modules
import arcpy
from arcpy import env
import os

# set workspace environment where the shapefiles are located
env.workspace = "C:\\BaseFolderName\\FolderOfShapefiles"
# creates a list of feature classes or shapefiles in the current workspace
fcs = arcpy.ListFeatureClasses('*') 

# set local variables
    for file in fcs:
        inData = file
        coordinateSystem = "PROJCS[\"MGI_Austria_GK_East\",GEOGCS[\"GCS_MGI\",DATUM[\"D_MGI\",SPHEROID[\"Bessel_1841\",6377397.155,299.1528128]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",-5000000.0],PARAMETER[\"Central_Meridian\",16.33333333333333],PARAMETER[\"Scale_Factor\",1.0],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0],AUTHORITY[\"EPSG\",31256]]"
        arcpy.DefineProjection_management(inData, coordinateSystem)

except arcpy.ExecuteError:
    print arcpy.GetMessages(2)
except Exception as e:
    print e.args[0]

First you need to of course change the input folder to your folder where you have the shapefiles you wish to define the projection for.

Second you need to specify the coordinate system you want to set for all your shapefiles.  Here you need to escape out the "  otherwise the code will crash out.  To get this definition just go to your hard drive where ArcGIS is installed and find the folder of the projections and open your projection in a text editor such as notepad++ on Windows or vi on a Linux machine and select all in the text file and copy paste into your python script.

Thats that now just go ahead and run the script.

Have fun