Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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"  
share|improve this question
    
nevermind...I think I just figured it out. Don't know how to remove a post (or if I even can) –  Adrienne Feb 5 '12 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 Feb 5 '12 at 16:41
1  
Yes, as dchaboya says, please post your answer, and mark it as accepted. –  Chad Cooper Feb 5 '12 at 20:02
    
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 Feb 6 '12 at 5:16
    
sorry...i can't get it to post in the correct format :-( –  Adrienne Feb 6 '12 at 5:24

1 Answer 1

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)
share|improve this answer
    
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 Feb 6 '12 at 6:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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