arcpy批处理,使用栅格的范围裁剪矢量,并将裁剪后的矢量栅格化
应用场景:需要自动化地执行操作,将栅格的范围裁剪矢量,并将裁剪后的矢量栅格化。使用arcpy实现,代码如下:
import arcpy
arcpy.env.workspace = 'F:/DL_DATA/mass_buildings/test/sat'
rasters = arcpy.ListRasters("*", "tiff")
Output_Workspace = "F:/DL_DATA/mass_buildings/test/map_corr/ras/"
for ras in rasters:
img = arcpy.Raster(ras)
ext = img.extent
array = arcpy.Array()
array.add(ext.upperLeft)
array.add(ext.upperRight)
array.add(ext.lowerRight)
array.add(ext.lowerLeft)
polygon = arcpy.Polygon(array)
fname = str(img)
fname = fname.split('.')[0]
clip_in_name = 'tmp/'+fname+'_clip_in.shp'
clip_out_name = 'tmp/'+fname+'_clip_out.shp'
label_out_name = 'tmp/'+fname[:8]+'_r'
xy_tolerance = 0
arcpy.CopyFeatures_management(polygon, clip_in_name)
arcpy.Clip_analysis("F:\DL_DATA\mass_buildings\massachusetts-buildings_corrected.shp", clip_in_name, clip_out_name, xy_tolerance)
arcpy.env.extent = ext
arcpy.PolygonToRaster_conversion(clip_out_name, 'aa',label_out_name,"CELL_CENTER","NONE",1)
arcpy.RasterToOtherFormat_conversion(label_out_name, Output_Workspace, 'tiff')
print fname+'Done'
转载自:https://blog.csdn.net/lixiang_whu/article/details/79327362