2

I need help changing this code to do:

1) Within a specified directory, reproject all .tif and/or .shp files to: NAD_1983_UTM_Zone_16N without needing to input what the old projection is.

2) If already projected correctly, skip.

I attempted to search and manipulate existing code already out there, and found this: How do I use python to batch process the reprojection of a group of shapefiles?

import sys, arcpy, os

mypath = r"C:\Users\Robby\Desktop\Test"
shapefiles = []
arcpy.env.workspace = mypath

for ThisFile in os.listdir(mypath):
    thisName,thisExt = os.path.splitext(ThisFile)
    if thisExt.upper() == ".TIF": # or thisExt.upper() == ".SHP": # include your shape files as well
        shapefiles.append(ThisFile)

out_workspace = r"C:\Users\Robby\Desktop\Test" 
out_cs = ''
template = "C:\\"
transformation = "NAD_1983_UTM_Zone_16N" # that's not a transform, but I'm not an  expert on North American projections
OutSR = arcpy.SpatialReference(26916)    # http://spatialreference.org/ref/epsg/nad83-utm-zone-16n/, EPSG:26916

for ThisFile in shapefiles:
    desc = arcpy.Describe(ThisFile)
    SR = desc.spatialReference
    if SR.factoryCode != 26916: # http://spatialreference.org/ref/epsg/nad83-utm-zone-16n/, EPSG:26916
        arcpy.Project_management(ThisFile,out_workspace + "\\" ThisFile,OutSR)
3
  • there's an inconsistent indent on if filename.endswith.. It does matter what the input coordinate system is, if the datum is incompatible or spatial reference is not set there will be a problem. Try avoid using batch project and iteratively project singularly. Mar 23 '15 at 2:08
  • @MichaelMiles-Stimson I'll do that then. New to this so I very much appreciate the advice.
    – rnr0004
    Mar 23 '15 at 2:11
  • also template = "c:\" should be either template = r"c:\" or template = "c:\\" as \ is the escape character, it's an unterminated string. Mar 23 '15 at 2:15
1

Try something like this:

import sys, arcpy, os

mypath = "C:\input"
Images = []
arcpy.env.workspace = mypath

for ThisFile in os.listdir(mypath):
    thisName,thisExt = os.path.splitext(ThisFile)
    if thisExt.upper() == ".TIF":
        Images.append(ThisFile)

out_workspace = r"C:\Users\Robby\Desktop\Test" 
OutSR = arcpy.SpatialReference(26916)    # http://spatialreference.org/ref/epsg/nad83-utm-zone-16n/, EPSG:26916

for ThisFile in Images:
    desc = arcpy.Describe(ThisFile)
    SR = desc.spatialReference
    if SR.factoryCode != 26916: # http://spatialreference.org/ref/epsg/nad83-utm-zone-16n/, EPSG:26916
        arcpy.ProjectRaster_management(ThisFile,out_workspace + "\\" + ThisFile,OutSR)

ShapeFiles = []
for ThisFile in os.listdir(mypath):
    thisName,thisExt = os.path.splitext(ThisFile)
    if thisExt.upper() == ".SHP":
        ShapeFiles.append(ThisFile)

for ThisFile in ShapeFiles:
    desc = arcpy.Describe(ThisFile)
    SR = desc.spatialReference
    if SR.factoryCode != 26916: # http://spatialreference.org/ref/epsg/nad83-utm-zone-16n/, EPSG:26916
        arcpy.Project_management(ThisFile,out_workspace + "\\" + ThisFile,OutSR)

You need to determine what transformation to apply based on what the input spatial reference is, most of the time you shouldn't need to supply anything; you can use the factory code to decide:

# these are example numbers DON'T USE THESE VALUES
TransFactCodes = [11111,22222,33333]

if SR.factoryCode in TransFactCodes:
    arcpy.Project_management(ThisFile,out_workspace + "\\" ThisFile,OutSR,transformation)
else:
    arcpy.Project_management(ThisFile,out_workspace + "\\" ThisFile,OutSR)
12
  • Thanks @Michael for the help. I've edited my original post with exactly what I'm putting into ArcMap. Unfortunately, I'm receiving errors. I don't know what a TransFactCode is, but I know the raster images I (currently) want to reproject have a Spatial Reference of Albers_Conic_Equal_Area and a Datum of D_North_American_1983. If I'm missing anything in the code I copied from yours and pasted above, then its because I don't understand what to put where. Sorry, just starting to learn GIS/RS and first attempt at Python.
    – rnr0004
    Mar 23 '15 at 3:20
  • That's no problem. with the TransFactCode are the SRIDs of the spatial references that need a transformation applied. If the datum is the same but you're projecting between zones or geographic then you can skip that parameter. Esri uses the term 'factory code' for EPSG or SRID - all three terms mean the same thing! a number that defines a spatial reference (the easiest way to pass/check is as a number). As I said before, I'm not an expert on North American spatial references so I don't know if D_North_American_1983 to NAD83 / UTM zone 16N needs a transformation, they look similar to me. Mar 23 '15 at 3:25
  • Try using the project tool from ArcCatalog and see if anything comes up in the 'geographic transformations' box, if there's nothing there then it's fairly safe to say you don't need that parameter and can just supply parameters: In Features, Out features, Out Spatial Reference Mar 23 '15 at 3:27
  • I did what you said in ArcCatalog without any problem. I'm not clear on what part of the code to remove. Not sure why, but I get a syntax error here: arcpy.Project_management(ThisFile,out_workspace + "\\" ThisFile,OutSR,transformation)
    – rnr0004
    Mar 23 '15 at 3:33
  • remove ,transformation from arcpy.Project_management(ThisFile,out_workspace + "\\" ThisFile,OutSR,transformation) if the transformation isn't needed, providing a blank, wrong or empty variable is causing an error. Mar 23 '15 at 3:48
1

Changed .upper == to .upper() == and it works.

1
  • Thanks, I wrote that in a hurry. The brackets are the difference between a property and a function... it should have returned an error saying 'string has no property upper'. Mar 23 '15 at 21:19

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.