1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import re import arcpy import os import re from arcpy import env
arcpy.CheckOutExtension("3D") env.workspace = os.getenv("TEMP")
zones = arcpy.GetParameterAsText(0)
dem = arcpy.GetParameterAsText(1)
output_dir=arcpy.GetParameterAsText(2)
desc = arcpy.Describe(dem) extent = desc.Extent cell_size = desc.meanCellWidth
flooded_mask = arcpy.sa.CreateConstantRaster(0, "FLOAT", cell_size, extent)
with open(output_dir+'wh.txt', encoding='utf-8') as f: text = f.read()
pattern = r'分区(\d+)水位:(\d+\.?\d*)'
partitions = {} for match in re.finditer(pattern, text): if "无法" in match.group(0): continue partition = int(match.group(1)) level = float(match.group(2)) partitions[partition] = level count = 7 with arcpy.da.SearchCursor(zones, ["FID", "水量","SHAPE@"]) as cursor: for row in cursor: oid = row[0] shape=row[2] h=partitions[oid] clipped_dem = arcpy.sa.ExtractByMask(dem, shape,analysis_extent=extent)
mask=arcpy.sa.Con(arcpy.sa.Raster(clipped_dem) < h, h ,0) mask = arcpy.sa.Con(arcpy.sa.IsNull(mask), 0,mask) flooded_mask += mask if oid%100==0: count+=1 mask_dem =arcpy.sa.Con(arcpy.sa.Raster(flooded_mask) > 0, flooded_mask-dem, 0) arcpy.CopyRaster_management(mask_dem, output_dir+str(count)+'.tif') flooded_mask = arcpy.sa.CreateConstantRaster(0, "FLOAT", cell_size, extent)
|