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()