首先在vscode中选择arcgis的python2.7,然后就可以顺利用arcpy进行投影转换,代码如下:
import os
import sys
import re
import arcpy
arcpy.env.workspace = r'D:\LST_8day_eartdata'
def GetRaster(file_path, out_path):
n = 0
files = os.listdir(file_path)
for file in files:
type = file.split(".")
if type[1] == "tif":
print(file)
pattern = '.tif$'
file_out = re.split(pattern, file)[0]+ "_T.tif"
raster = os.path.join(file_path, file)
out_raster = os.path.join(out_path, file_out)
dataset = r'D:\LST_8day_eartdata\test_T.tif' #参考投影文件
mask = arcpy.Describe(dataset).spatialReference
arcpy.ProjectRaster_management(raster, out_raster, mask, "NEAREST", \
"#", "", "", "#")
n = n+1
print (str(n)+" rasters are processed !!!")
if __name__ == "__main__":
data_path = r'D:\LST_8day_eartdata\Night'
out_path = r'D:\LST_8day_eartdata\tif'
GetRaster(data_path, out_path)