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()
  • 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! – Michael Stimson Jun 14 '17 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! – Dom B Jun 14 '17 at 23:51
  • 1
    After you've worked that into your code please post, with code, as an answer to your own question. – Michael Stimson Jun 14 '17 at 23:52
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)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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