2

Still struggling with this script. Now I need to create an arpy.AddMessage that says "Projected: with the list of feature classes that were projected in the loop"

i.e. Projected: filename, filename, filename etc.

I've been at this for 3 days and know I am missing something stupid. Any help would be appreciated. Here is my code again:

# Get the spatial reference of the Feature Class from folder A
# Examine all Feature Classes in folder B and report if their
# spatial references match the Feature Class in folder A

import arcpy
arcpy.env.overwriteOutput = True

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

targetFC = arcpy.GetParameterAsText(0)
#print "target FC path is "+targetFC
folderToExamine = arcpy.GetParameterAsText(1)
#print "folder to examine"+folderToExamine

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

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

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

    if currentFCSRName != targetSRName:
        print "Spatial references don't match"
    else:
        print "Spatial references do match"   
    if currentFCSRName == targetSRName:
        #skip
       continue
    else:
       # Determine the new output feature class path and name
        outCS = currentFC[:-4] +"_projected.shp"
        #Reproject datasets that are different from target file
        arcpy.Project_management(currentFC, outCS, targetSR)

        #Create a message stating which files were projected
        projFCs = outCS[:-14] +".shp"
        #print projFCs
        projFCList = arcpy.ListFeatureClasses(projFCs)
        x = ", ".join(projFCList) + ", "
        arcpy.AddMessage("Projected " + str(x))
2
  • 1
    you can get your feature list easily with this link and then append features...[gis.stackexchange.com/a/19834/3071]
    – urcm
    Commented Feb 7, 2012 at 14:46
  • What is the purpose of the line projFCs = outCS[:-14] +".shp", and have you seen my comments to your previous question?
    – blah238
    Commented Feb 7, 2012 at 20:45

1 Answer 1

2

I think this is what you need, if I understand your problem correctly. Create a list to hold the FC names, add them to the list, then once you are done, add the list to message:

# Create a empty list to store projFCs names in
projFCs_list = []
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 = currentFC[:-4] +"_projected.shp"
        # Reproject datasets that are different from target file
        arcpy.Project_management(currentFC, outCS, targetSR)
        # Create a message stating which files were projected
        projFCs = outCS[:-14] +".shp"
        # Add reprojected FC to list
        projFCc_list.append(projFCs)
# Now that  you are done iterating thru listOfFCs, print out your message        
x = ", ".join(projFCList) + ", "
arcpy.AddMessage("Projected " + str(x))
1
  • 1
    I do not believe this will work unless you move the list initialization outside of the loop.
    – blah238
    Commented Feb 8, 2012 at 0: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.