2

I currently have code that re-projects all my shapefiles in a folder based off an input shapefile that I select. I will attach the code below. The code runs fine, but I would like the message on the geoprocessor window to say "Projected feature1, feature2, feature3, etc"

I currently can only get the script to list: "Projected feature1" "Projected feature2" "Projected feature3"

It's basically printing the message for each iteration of the loop but I need it to print each feature class selected.

Attached is my example script below.

import arcpy
folderLocation = arcpy.GetParameterAsText(0)
targetShapefile = arcpy.GetParameterAsText(1)

try:

    targetDescribe = arcpy.Describe(targetShapefile)
    targetSpatialRef= targetDescribe.spatialReference
    targetSpatialName = targetSpatialRef.Name
    arcpy.env.workspace = folderLocation
    featureList = arcpy.ListFeatureClasses()
    for fc in featureList:
        desc = arcpy.Describe(fc)
        spatialRef = desc.spatialReference
        fcName = spatialRef.Name


        if fcName != targetSpatialName:
            rootName = fc.replace(".shp", "")
            outfc = folderLocation + '\\' + rootName + "_Projected"
            outCS = targetSpatialRef
            arcpy.Project_management(fc, outfc, outCS)
            arcpy.AddMessage("Projected "+ fc)


except:
    print arcpy.GetMessages()
3
  • 1
    You could build a list and then AddMessage after they're done. Unfortunately AddMessage adds a carriage return on the end of the line and cannot be stopped. Start with PjList = [] # new blank list then PjList.append(fc) inside the loop then when done arcpy.AddMessage('Projected {}'.format(','.join(PjList))); ','.join converts the entire list to a string comma delim - quite handy! Commented Jun 14, 2017 at 23:40
  • Thanks Michael, this helped me a bunch and was exactly what I needed. Didn't think to use the .append option. The join is handy!
    – DEB
    Commented Jun 14, 2017 at 23:51
  • 1
    After you've worked that into your code please post, with code, as an answer to your own question. Commented Jun 14, 2017 at 23:52

1 Answer 1

2
import arcpy


folderLocation = arcpy.GetParameterAsText(0)
targetShapefile = arcpy.GetParameterAsText(1)

newList = []

try:

    targetDescribe = arcpy.Describe(targetShapefile)
    targetSpatialRef= targetDescribe.spatialReference
    targetSpatialName = targetSpatialRef.Name
    arcpy.env.workspace = folderLocation
    featureList = arcpy.ListFeatureClasses()


    for fc in featureList:
        desc = arcpy.Describe(fc)
        spatialRef = desc.spatialReference
        fcName = spatialRef.Name


        if fcName != targetSpatialName:
            rootName = fc.replace(".shp", "")
            outfc = folderLocation + '\\' + rootName + "_Projected"
            outCS = targetSpatialRef
            arcpy.Project_management(fc, outfc, outCS)
            newList.append(fc)


    arcpy.AddMessage("Projected {}".format(','.join(newList))); ','

except:
    print arcpy.GetMessages(2)

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.