# -*- coding: utf-8 -*- 批量实现shp转raster 2021/12/24 importarcpyimportosimportre#检查Arcgis的权限arcpy.CheckOutExtension(spatial)arcpy.gp.overwriteOutput1#设置了输出范围及输出坐标系这三行也可注释掉arcpy.env.overwriteOutputTruearcpy.env.extentarcpy.Extent(-6098370.77434,-5465665.01316,7540629.22566,4821334.98684)arcpy.env.outputCoordinateSystemarcpy.SpatialReference(C:\\Users\\DELL\\Desktop\\test\\proj.prj)#shp文件所在文件夹input_shp_pathrC:\\Users\\DELL\\Desktop\\test\\#输出raster文件的文件夹output_raster_pathrC:\\Users\\DELL\\Desktop\\select_catchment_raster\\#读取input_shp_path中的shp文件arcpy.env.workspaceinput_shp_path shpsarcpy.ListFiles(*.shp)#遍历shp文件forshpinshps:#原shp文件名格式形如 4116_Canada_03NF001_Boundary_Line.shp#提取文件名删除编号后缀作为输出文件的文件名printshp all_index[substr.start()forsubstrinre.finditer(_,shp)]catchment_nameshp[int(all_index[0]1):int(all_index[3])]print(catchment_name)raster_out_nameoutput_raster_pathcatchment_name.tif#设置了栅格大小cellsize为1公里arcpy.PolygonToRaster_conversion(shp,Area,raster_out_name,CeLL_CENTER,Area,1000)