转载画长空_yin的文章
os.sep
为了不同系统下例如Windows or Lunix 写路径时候的代码在不同的平台上都能运行,os.sep 根据你所处的平台,自动地采用相应的分割符号。
批量定义金字塔
import arcpy
arcpy.env.workspace = "E:/MODIS/温度" #设置工作空间
tifflist = arcpy.ListRasters("","tif") #将该目录下的tif格式的影像全部创建金字塔,tif可换成其他格式
for tiff in tifflist:
arcpy.BuildPyramids_management(tiff)
# **基于python的grd格式批量转为tif格式**
grd格式是传统的arcinfo格式,需要将grd转换为tif格式,本文采用arcgis自带的python批量转换。
import arcpy
arcpy.env.workspace="D:\\chazhi\\" #存放数据的文件夹
a=arcpy.ListRasters("*","grd") #得到文件夹下所有的grd名称
for i in a:
arcpy.RasterToOtherFormat_conversion(i,"D:\\chazhi\\tif\\","TIFF")
基于Python的批量定义投影
应用场景:MODIS NDVI/EVI数据需要转换为跟自己数据一致的投影
定义坐标系信息
可以先定义一个Shpfile的投影信息,然后把它的.prj文件用记事本打开,记得修改里面的符号“为'。
#栅格图像的投影定义
import arcpy
# 设置工作空间
arcpy.env.workspace="E:\\GPP\\test2"
# 得到所有tif格式影像
rasterlist = arcpy.ListRasters("*", "tif")
# 定义坐标系统
Coordinate_System = "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',0.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]]"
# 循环
for raster in rasterlist:
print str(raster)
arcpy.DefineProjection_management(raster, Coordinate_System)
print("OK!") # 运行完毕,显示OK。
# 要素类的投影定义
import arcpy
##### 设置工作空间
arcpy.env.workspace="E:\\GPP\\test2"
# 得到所有要素
featurelist = arcpy.ListFeatureClasses()
# define coordinate system
Coordinate_System = "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',0.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]]"
#循环
for ft in featurelist:
print str(ft)
arcpy.DefineProjection_management(ft, Coordinate_System)
print("OK!")
# os 文件路径及对字符串操作
import sys, os
import arcpy
dir_file = r"C:\Users\Administrator\Desktop\ANUSPLIN\grd"
files = os.listdir(dir_file)
for f in files:
if os.path.splitext(f)[1] == '.grd':
Input_raster_file = dir_file + os.sep + f
print Input_raster_file
Raster_Format = "TIFF"
Output_Workspace = r"C:\Users\Administrator\Desktop\ANUSPLIN\grd"
basename = os.path.splitext(f)[0]
Output_raster = Output_Workspace + os.sep + basename + ".tif"
arcpy.RasterToOtherFormat_conversion(Input_raster_file, Output_Workspace, Raster_Format)
print Out_raster