Search code examples
pythonspyderarcgisprojection

How to append "_projected.shp" at the end of each dataset name


I have a code that projects a number of shapefiles in a folder to another coordinate system and the projected shapefiles are placed in another folder. For the projected shapefiles, I want to append "_projected" at the end each shapefile name.

What I have so far works for the projection and setting the output files into a specific folder, but the new output files are not showing the "_projected" at the end.

Here is my code

import arcpy
import os

arcpy.env.workspace = "inputdatafolder"
arcpy.env.overwriteOutput = True
outWorkspace = "outputdatafolder"

for infc in arcpy.ListFeatureClasses():
   dsc = arcpy.Describe(infc)
   if dsc.spatialReference.Name == "Unknown":
        print ("skipped this fc due to undefined coordinate system: "+ infc)

    else:
        outfc = os.path.join(outWorkspace, infc)

        outCS = arcpy.SpatialReference('NAD 1983 UTM Zone 10N')

        arcpy.Project_management(infc, outfc, outCS)

        infc = infc.replace(".shp","_projected.shp")

Since the code works, I am not getting any errors. The file name just isn't replaced with the ending I want it to.


Solution

  • Your code is replacing the text of the filepath of infc, but not actually renaming the file.

    Furthermore, outfc is the path to the new projected shapefile you are creating, while infc is the path to the original file. Don't you want outfc to have the "_projected.shp"suffix?

    The code below changes the text of the output file path to include "_projected.shp" before calling arcpy.Project_management to create the new file.

    import arcpy
    import os
    
    arcpy.env.workspace = "inputdatafolder"
    arcpy.env.overwriteOutput = True
    outWorkspace = "outputdatafolder"
    
    for infc in arcpy.ListFeatureClasses():
       dsc = arcpy.Describe(infc)
       if dsc.spatialReference.Name == "Unknown":
            print ("skipped this fc due to undefined coordinate system: "+ infc)
        else:
            outfc = os.path.join(outWorkspace, infc).replace(".shp","_projected.shp")
            outCS = arcpy.SpatialReference('NAD 1983 UTM Zone 10N')
            arcpy.Project_management(infc, outfc, outCS)
    

    I'm also not sure if you're using Describe correctly. You may need to use infc.name when constructing the file paths.