1

I'm trying to create a relatively basic batch reprojection tool with Python by projecting all shapefiles in a folder to NAD83 UTM Zone 11N.

I want them to go to a specified folder that I have defined as a variable. I want the outputs to maintain the same name but have "_projected" appended to the final outputs. I also have defined a variable that is the file path to a vector dataset whose spatial reference will be used in the reprojection. I want to do all this with a For loop.

At this point, the outputs go to this file path below and the wrong name, which is not what I want:

"C:\\WCGIS"

How to make it go this file path below and to have the string, "_projected" appended to each newly projected shapefile?:

"C:\\WCGIS\\Project2Output"

Right now I'm trying this part of the code I'm posting just below to have the outputs go to the folder called Project2Output and it keeps on giving me names like Project2OutputCityBoundaries, Project2OutputFerries, Project2OutputStateRoads, etc, and to "C:\WCGIS".

        outPath = outLocation + sFile + "_projected"

        arcpy.Project_management(sFile, outPath, outProjection)

I will eventually be turning the variable paths targetData and out projection as arcpy.GetParameterAsText variables for my Python tool but just want to see this work in my IDE first. Here is all my code below:

import arcpy    
import os

arcpy.env.overwriteOutput = True    
targetData = "C:\\WCGIS\\Project2Data" # Shapefiles that need to be projected
outLocation = "C:\\WCGIS\\Project2Output" # Where the projected shapefiles will be stored
outProjection = "C:\\WCGIS\\Project2Data\\CountyLines.shp" # Shapefiles with correct target projection: NAD 1983 UTM Zone 10

# Get a list of all feature classes in the Project2Data folder
arcpy.env.workspace = targetData

try:
    for sFile in arcpy.ListFeatureClasses():
        dsc = arcpy.Describe(sFile)
        if dsc.spatialReference.Name == "NAD_1983_UTM_Zone_10N":
            print "Skip this shapefile because it already has the correct projection:" + sFile
        else:    
            outPath = outLocation + sFile + "_projected"            
            arcpy.Project_management(sFile, outPath, outProjection)
            print arcpy.GetMessages()

except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

except Exception as ex:
    print (ex.args[0])

1 Answer 1

0

Firstly, when assigning a value to outPath, you haven't included a path separator between outLocation and sFile resulting in the last path component of outLocation and the entirety of sFile becoming one long filename at the directory level above outLocation.

There are a few different ways to correct this, but to do it in a consistent, reliable, operating-system-agnostic way, use os.path.join().

Secondly, you should remove the ".shp" filename extension (and re-add it at the end of the new filename). You can get the rest of the filename (before the ".") using os.path.splitext(sFile)[0].

So replace this:

outPath = outLocation + sFile + "_projected"

which would result in something like C:\WCGIS\Project2OutputInFileName.shp_projected

with this:

outPath = os.path.join(outLocation, os.path.splitext(sFile)[0] + "_projected.shp")

which should result in something like C:\WCGIS\Project2Output\InFileName_projected.shp

(Note that I'm not sure if the ".shp" is required at the end... arcpy may sort this out for you automatically. I normally work with geodatabases, so my shapefile experience is very limited.)

4
  • Thanks for the help so far! I did realize that I was missing file separators after outLocation = "C:\\WCGIS\\Project2Output\\
    – Tommy JH
    Commented Sep 11, 2018 at 22:23
  • Thanks! I tried to replace: outPath = outLocation + sFile + "_projected" with outPath = os.path.join(outLocation, sFile + "_projected") and I'm still not able to append the _projected to the original shapefile names. I am able to get the outputs to go to the right folder, but just not _projected appended correctly. Any other thoughts on how to make the final append? Thanks so much in advance!
    – Tommy JH
    Commented Sep 11, 2018 at 22:40
  • That might be the case! I think I need to remove the .shp. Any help with that would be great!
    – Tommy JH
    Commented Sep 11, 2018 at 22:50
  • You are amazing! Thanks so much! It worked perfectly. Now to just study this last part to understand it better!
    – Tommy JH
    Commented Sep 11, 2018 at 23:04

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.