[GIS] How to write a script that re-projects vector datasets in a folder

arcpycoordinate system

This is probably a novice question, but I am a novice with python. I am trying to write a script that re-projects vector datasets in a folder. However there are rules in which to do this. They are:

  • Must re-project shapefile vector datasets in the folder to match the
    target dataset's projection.
  • Must append "_projected" to the end of each projected dataset name. For example:
    CityBoundaries_projected.shp.
  • Must skip projecting any datasets that are already in the target projection.
  • Must report a geoprocessing message of which datasets were projected. In this message, the dataset names can be separated by spaces. In the message, do not include datasets that were skipped because they were already in the target projection.
  • Must not contain any hard-coded values such as dataset names, path names, or projection names.
  • Must be made available as a script tool that can be easily run from ArcToolbox by
    someone with no knowledge of scripting.

The part I am struggling with is the bolded requirement b/c I cannot hard code the projection I want all of the files to have. I need it to assign the projection of the remaining shapefiles from that of the targetFC. So I don't know what to assign to my "outCS" variable. Oh…and I cannot use the batch projection tool. I have to make a variation of a batch project tool by running the Project tool inside a loop. Here is a sample of my script. Any help would be greatly appreciated.

import arcpy
import os

#set up the paths
targetFC = r"C:\MGIS\geog485\Lesson2\Lesson2\CountyLines.shp"
folderToExamine = r"C:\MGIS\geog485\Lesson2\Lesson2"


#get spatial reference for the target feature class
targetDescribe = arcpy.Describe(targetFC)
targetSR = targetDescribe.SpatialReference
targetSRName = targetSR.Name

# Get a list of my feature classes
arcpy.env.workspace = folderToExamine
listOfFCs = arcpy.ListFeatureClasses()

#Loop through the list of FCs
for currentFC in listOfFCs:
#Read the spatial reference of the current one
currentFCDescribe = arcpy.Describe(currentFC)
currentFCSR = currentFCDescribe.SpatialReference
currentFCSRName = currentFCSR.Name

if currentFCSRName != targetSRName:
    print "Spatial references don't match"
else:
    print "Spatial references do match"

if currentFCSRName == targetSRName:
    continue
else:
    # Determine the new output feature class path and name
    **outCS =** 
    arcpy.Project_management(currentFC, outCS)
    rootName = currentFC[:-4]
    print rootName
    print rootName +"_projected.shp"  

Best Answer

OK I logged in so now maybe this will display correctly (again only the part that was troublesome):

if currentFCSRName == targetSRName:
    #skip
    continue
else:
    # Determine the new output feature class path and name
    outCS = currentFC[:-4] +"_projected.shp"
    arcpy.Project_management(currentFC, outCS, targetSR)