[GIS] Automate Projection of multiple shp files using ArcPy

arcgis-10.0arcpycoordinate systempython

I have been trying to create a Python script that will convert all shape files within a folder from GCS WGS 84 to Web Mercator. Below is the code I have so far but what I'm struggling with is how to iterate thru each of the files while creating a new name for the output (the new projected file in Web Mercator). Any ideas on how I could change this code to make this work? I know I don't have the output parameter right but I think everything else is right.

import arcpy
import glob
inFCFolder = "C:\Data"
def projectToWebM():
    for f in glob.glob(r""+ inFCFolder + "\*.shp"):
        try:
            arcpy.Project_management(f, f + r"\Project\*.shp","PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]]","#","GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
        except:
            print 'Error: Unable to project ' + f

if __name__== '__main__':
    projectToWebM()

Best Answer

## Your code:
##
## import arcpy
## import glob
##
##
## inFCFolder = "C:\Data"
## def projectToWebM():
##     for f in glob.glob(r""+ inFCFolder + "\*.shp"):
##         try:
##             arcpy.Project_management(f, f + r"\Project\*.shp","PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]]","#","GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
##         except:
##             print 'Error: Unable to project ' + f
##
## if __name__== '__main__':
##     projectToWebM()

# I would suggest something like this:

import arcpy
import glob
import os

# Normally the '\' character is an escape character, so you will need to use the
# r'C:\Data' raw string format, or use 'C:\\Data' to get a "backslash" in your string
# literal.
#
# See: http://docs.python.org/reference/lexical_analysis.html#literals
inFCFolder = r'C:\Data'

def projectToWebM():
    for f in glob.glob(inFCFolder + r'\*.shp'):
        try:
            # Let arcpy create a scratch name, and use os.path.basename to transfer part
            # of the original file name to the new file name.
            scratch_name = arcpy.CreateScratchName('wm_' + os.path.basename(f).replace('.shp', '') + '_', \
                                                   '', \
                                                   'Shapefile', \
                                                   inFCFolder)
            # To make it simple, I'm outputting the new projected shapefiles to the same
            # directory that they came from (guaranteed to exist). It looks like you will
            # want to put them in the "Project" folder from looking at your code. Also,
            # if your data is already projected (as in has a defined spatial reference),
            # you don't need to specify a fifth "in_coor_system" parameter.
            arcpy.Project_management(f, scratch_name, \
                                     r"PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere'," + \
                                     r"GEOGCS['GCS_WGS_1984'," + \
                                     r"DATUM['D_WGS_1984'," + \
                                     r"SPHEROID['WGS_1984',6378137.0,298.257223563]]," + \
                                     r"PRIMEM['Greenwich',0.0]," + \
                                     r"UNIT['Degree',0.0174532925199433]]," + \
                                     r"PROJECTION['Mercator_Auxiliary_Sphere']," + \
                                     r"PARAMETER['False_Easting',0.0]," + \
                                     r"PARAMETER['False_Northing',0.0]," + \
                                     r"PARAMETER['Central_Meridian',0.0]," + \
                                     r"PARAMETER['Standard_Parallel_1',0.0]," + \
                                     r"PARAMETER['Auxiliary_Sphere_Type',0.0],"+ \
                                     r"UNIT['Meter',1.0]]")
        except:
            print 'Error: Unable to project ' + f

if __name__== '__main__':
    projectToWebM()
Related Question