"Everything is related to everything else, but near things are more related than distant things"
Batch re-projection tool
The Python script re-projects the vector datasets in a folder based on spatial reference of a file.
# Ehsan Momeni
from arcpy import env, ListFeatureClasses, Describe, Project_management, DefineProjection_management, \
GetParameterAsText, AddMessage, CopyFeatures_management, ListFiles, FeatureClassToShapefile_conversion, \
CreateFileGDB_management, Exists
from os import path
Input_Path = GetParameterAsText(0) # enter the folder that contains all files
AddMessage("\nInput folder is:\n\t%s" % Input_Path)
Target_CS_shapefile = GetParameterAsText(1) # enter the shapefile its coordinat system is target
AddMessage("\nTarget shapefile is:\n\t%s" % Target_CS_shapefile)
Output_Path = GetParameterAsText(2) # enter the output path
AddMessage("\nOutput path is:\n\t%s" % Output_Path)
Output_Path = Output_Path + "/"
env.overwriteOutput = True # to overnight results
env.workspace = Input_Path
AddMessage('\n---------------------------------------------------------')
Target_CS = Describe(Target_CS_shapefile).spatialReference
AddMessage('\nTarget CS is:\n\t%s' % Target_CS.Name)
Shapefiles = ListFeatureClasses() # "h*"
AddMessage('\n---------------------------------------------------------')
AddMessage('\nThere are %d shapefiles in that folder' % len(Shapefiles))
if len(Shapefiles) > 0:
for Shapefile in Shapefiles:
Shapefile_Name = Describe(Shapefile).Name
# print Shapefile_Name
spatial_ref = Describe(Shapefile).spatialReference.Name
# print ('Original CS is %s\n'%spatial_ref)
AddMessage("\n%s has CS of %s." % (Shapefile_Name, spatial_ref))
Output_name = Shapefile_Name[:-4] + "_projected.shp"
Output = Output_Path + Output_name
if spatial_ref == Target_CS.Name:
# print ("%s had the same spatial reference." %Shapefile_Name)
CopyFeatures_management(Shapefile, Output)
AddMessage("\t=> %s had the same spatial reference and ..." % Shapefile_Name)
AddMessage("\t... copied to the target folder.")
elif spatial_ref == "Unknown":
CopyFeatures_management(Shapefile, Output)
DefineProjection_management(Output, Target_CS)
# print "\n\tA new CS for shapefile has defined."
AddMessage("\t=> A new CS for %s has defined and ..." % Shapefile_Name)
AddMessage("\t... copied to the target folder.")
else:
Project_management(Shapefile, Output, Target_CS)
AddMessage("\t=> %s has projected to the target coordinate system." % Shapefile)
else:
AddMessage("\n=> No shapefile is available.")
# if there is any geodatabase in the input folder:
Geodatabases = ListFiles("*.gdb") # listing geodatabases
AddMessage('\n---------------------------------------------------------')
AddMessage('\nThere are %d geodatabases in that folder' % len(Geodatabases))
if len(Geodatabases) > 0:
for geodatabase in Geodatabases:
if Exists(Output_Path + geodatabase):
AddMessage("\n%s is exist.\n" % (geodatabase))
else:
CreateFileGDB_management(Output_Path, geodatabase) # creating new geodatabese at output folder
AddMessage("\n%s is created." % (geodatabase))
Temp_Input_Path = Input_Path + "/" + geodatabase
env.workspace = Temp_Input_Path
for infc in ListFeatureClasses():
if Describe(infc).spatialReference.Name == Target_CS.Name:
CopyFeatures_management(infc, path.join(Output_Path + geodatabase, infc.strip(".shp")))
AddMessage("\t%s had the same CS." % infc)
elif Describe(infc).spatialReference.Name == "Unknown":
CopyFeatures_management(infc, path.join(Output_Path + geodatabase, infc.strip(".shp")))
DefineProjection_management(Output_Path + geodatabase + infc.strip(".shp"), Target_CS)
AddMessage("\tA new projection for %s is defined." % infc)
else:
outfc = path.join(Output_Path + geodatabase, infc)
Project_management(infc, outfc, Target_CS)
AddMessage("\t%s is projected." % infc)
else:
AddMessage("\n=> No geodatabase is available.")
AddMessage("\n------------- Done ---------------\n")