3

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"  
5
  • nevermind...I think I just figured it out. Don't know how to remove a post (or if I even can)
    – Adrienne
    Commented Feb 5, 2012 at 15:41
  • 1
    I don't think you can remove a post, but in any case, you shouldn't be removing posts. Mark your question as answered. It's likely someone will benefit from this.
    – dchaboya
    Commented Feb 5, 2012 at 16:41
  • ok thanks guys. Too many characters to repost the whole script so here the part I was struggling with else: # Determine the new output feature class path and name outCS = currentFC[:-4] +"_projected.shp" arcpy.Project_management(currentFC, outCS, targetSR)
    – Adrienne
    Commented Feb 6, 2012 at 5:16
  • sorry...i can't get it to post in the correct format :-(
    – Adrienne
    Commented Feb 6, 2012 at 5:24
  • It sounds like your professor wants you to create a script tool that takes parameters. For info on this see Understanding script tool parameters
    – blah238
    Commented Feb 6, 2012 at 5:50

1 Answer 1

2

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)
1
  • 1
    This could be improved by using os.path.splitext(currentFC)[0] instead of currentFC[:-4]. I would also change the variable name outCS to outFC since "CS" probably stands for Coordinate System, but what you are really working with is a Feature Class (FC).
    – blah238
    Commented Feb 6, 2012 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.