Select to view content in your preferred language

Batch project using Loop Script - Beginner needs Help!

1849
1
10-08-2011 02:25 PM
MatthewBelsky
Emerging Contributor
Hello,

I am a beginniner scripter who is having some problems working on a script.  I am sure you guys will be able to help me out 🙂 Here is what the script must do:

The user opens a GUI window and enters two input parameters:
1) A target folder
2) A target projection dataset

The script then runs through all the feature classes in the target folder using a loop and re-projects them to the projection of the specified target projection dataset.

I also must:

1) Append "_projected" to the end of each projected dataset name
2) Skip projecting datasets that equal the target projection
3) Report a geoprocessing message saying what datasets were projected

Here are the problems I'm having (All because I'm a beginner and I'm quite sick right now - I have lyme disease and I'm having trouble with my cognition.  Ugh.  Please help!):

1) I'm confused about how to specify the target folder, target projectoin system, and output coordinate system as relative and not hard coded.  Do I use the arcpy.GetParameterAsText method, or is there an alternative method?  I'm especially confused about how to not hard code the output .prj coordinate system.

2) I'm not sure how to append "_projected.shp" to the end of the projected shapefiles.  I know there's the endswith method whereby you can remove the end characters of a file name, but I'm not sure how to apply that correctly.

3) I'm not sure how to correctly construct the output path name

4) I'm not sure how to check the input feature class and NOT project it if it has the same projectoin as the target feature class.  I know this involves creating a spatial reference object and comparing the two spatial reference objects against one another, but I don't believe my syntax is correct.

Again, any help is much appreciated.  I'm quite sick and this is the last class I need to finish in order to graduate.  Thank you very much ahead of time!

Here's my current code:

# Description: Changes coordinate systems of several datasets based on the input of one feature class dataset

import arcpy
from arcpy import env

#Create path variables
targetFolder = arcpy.GetParameterAsText(0) #"C:\\GEOG485\\Lesson2\\"
targetProjection = arcpy.GetParameterAsText(1) #"C:\\GEOG485\\Lesson2\\StateRoutes.shp"
outCoordinateSystem = "C:\\GEOG485\\Lesson2\\StateRoutes.prj"

#get list of all feature classes in targetFolder
arcpy.env.workspace = targetFolder
featureClassList = arcpy.ListFeatureClasses()

try:
    #Loop through all feature classes
    for featureClass in featureClassList

        #temporarily remove .SHP suffix
        rootName = " "
        if featureClass.endswith(".shp"):
            rootName = featureClass[:-4]

        #Construct output path and added _projected suffix
        outProjectFeatureClass = targetFolder + featureClass + "_projected.shp"

        #check if the input feature class has the same projection as the target feature class; if they do, do not project - i'm not sure how to do this
        spatialRef = arcpy.SpatialReference(featureClass)
        spatialRef.createFromFile("C:\\GEOG485\\Lesson2\\StateRoutes.prj") #but I need to change this so the path isn't hard-coded
        if spatialRef.Name != targetProjection.Name:
            #Perform the project and report what happened
            arcpy.Project_management(featureClass, outProjectFeatureClass, outCoordinateSystem)
            arcpy.AddMessage(arcpy.GetMessages())
          
        else:
            arcpy.AddMessage("Did not project " + outProjectFeatureClass + "because project is the same as target.")

except:
    # Report if there was an error
    arcpy.AddError("Could not project feature classe")
    #print "Could not project feature classe"
    #print arcpy.GetMessages()
Tags (2)
0 Kudos
1 Reply
chriss_
Emerging Contributor
Hi

just some hints for you. I didn't try it. Just wrote from memory.

1) Leave it hard coded and replace it when your code works
e.g. TargetProject= "Coordinate Systems/Projected Coordinate Systems/UTM/WGS 1984/Northern Hemisphere/WGS 1984 UTM Zone 18N.prj"
later you replace it by:
TargetProject=arcpy.GetParameterAsText(0)

2/3) try this: 
#for" in_data" use the describe command to ge the name of the current projection
desc = arcpy.Describe("C:/Data/example.gdb/yourshape")
in_data=desc.name
#for the new shape use the old + some string. I often make mistakes in combining so counter check
out_data=str(str(in_data) + "_projected.shp")
Rename_management (in_data, out_data)

4) Try this:
desc = arcpy.Describe("C:/Data/example.gdb/yourshape")
sr = desc.spatialReference
if str(sr) ==TargetProject:
#then do your stuff etc.
else:
   print: "Already correct coordinate System"


good luck
chris
0 Kudos