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.

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))
share|improve this question
    
you can get your feature list easily with this link and then append features...[gis.stackexchange.com/a/19834/3071] –  Aragon Feb 7 '12 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 Feb 7 '12 at 20:45
add comment

1 Answer

up vote 2 down vote accepted

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))
share|improve this answer
1  
I do not believe this will work unless you move the list initialization outside of the loop. –  blah238 Feb 8 '12 at 0:00
    
@blah238 - You are correct! Fixed, thanks. –  Chad Cooper Feb 8 '12 at 1:37
    
thank you!! This helped me get it to work!! –  niklas24 Feb 11 '12 at 18:22
add comment

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.