说明:本示例代码使用最简单的python语法,简要介绍如何使用Python做ArcGIS二次开发。适合初学者阅读。

# -*- coding: utf-8 -*-
 
import arcpy
import os
 
input_folder = "H:\\Original\\qhnu\\wangxch20170318\\MAP"
fileExtent = [".shp",".tif",".tiff",".img"]
 
for files in os.walk(input_folder ):
    for filename in files[2]:
        for Extent in fileExtent:
            if filename.endswith(Extent):
                inputData = files[0] + "\\" + filename
                oupPutData = files[0].replace("wangxch20170318","modif") + "\\r" + filename
                print inputData,oupPutData
                if os.path.exists(files[0].replace("wangxch20170318","modif")) == False:
                    os.mkdir(files[0].replace("wangxch20170318","modif"))
                if Extent == ".shp":
                    arcpy.Project_management(inputData, oupPutData, "PROJCS['WGS_1984_Albers',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',4000000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',96.5],PARAMETER['Standard_Parallel_1',34.0],PARAMETER['Standard_Parallel_2',37.0],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]]", "", "PROJCS['WGS_1984_Albers',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',4000000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',105.0],PARAMETER['Standard_Parallel_1',25.0],PARAMETER['Standard_Parallel_2',47.0],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]]", "NO_PRESERVE_SHAPE", "", "NO_VERTICAL")
                else:
                    arcpy.ProjectRaster_management(inputData, oupPutData, "PROJCS['WGS_1984_Albers',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',4000000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',96.5],PARAMETER['Standard_Parallel_1',34.0],PARAMETER['Standard_Parallel_2',37.0],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]]", "NEAREST", "1000 1000", "", "", "PROJCS['Albers_Conical_Equal_Area',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['false_easting',4000000.0],PARAMETER['false_northing',0.0],PARAMETER['central_meridian',105.0],PARAMETER['standard_parallel_1',25.0],PARAMETER['standard_parallel_2',47.0],PARAMETER['latitude_of_origin',0.0],UNIT['Meter',1.0]]")

在上面的代码中:
input_folder定义了输入文件的存放路径,读者可按自己实际情况进行修改。

fileExtent对input_folder文件夹中的数据进行筛选。

下面的三个for循环是从input_folder文件夹中查找指定的文件,如果读者对这段代码不熟悉,需要学习一下python基础知识。

inputData定义了从input_folder文件夹找到的文件的完整路径,oupPutData定义了做投影转换后文件的保存位置(因为我的输入文件夹中有“wangxch20170318”文件夹,所以使用python的replace方法将输入路径中的“angxch20170318”文件夹替换为“modify”文件夹,读者可按实际情况进行改写)。

print语句下面的if语句是判断输出文件夹是否存在,如果不存在则创建(这里有两点说明:1. 因为前面讲输入文件的“wangxch20170318”文件夹修改为“modify”文件夹,在python中只是对字符串进行编辑,实际上这个文件夹不存在,所以需要新建文件夹。2. 如果读者新建的目录较深,os的mkdir会报错,可以使用os的makedirs方法)。

ArcMap对矢量和栅格的投影转换方式在不同的toolbox,所以此处我们也需要对矢量和栅格使用不同的函数(具体函数的参数读者请参考ArcMap自带的help,找到对应的工具说明)。