[GIS] How to create a list and append objects to it through a loop

arcpypython

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

Best Answer

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))
Related Question