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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import arcpy
demPath="" pathList = [] elevationCell = {} cellXSzie = arcpy.GetRasterProperties_management(demPath, "CELLSIZEX") cellYSzie = arcpy.GetRasterProperties_management(demPath, "CELLSIZEY") leftPosition = arcpy.GetRasterProperties_management(demPath, "LEFT") topPosition = arcpy.GetRasterProperties_management(demPath, "TOP") bottomPosition = arcpy.GetRasterProperties_management(demPath, "BOTTOM")
rstArray = arcpy.RasterToNumPyArray(arcpy.Raster(demPath))
elevationCell = {} rows, cols = rstArray.shape for rowNum in xrange(rows): for colNum in xrange(cols): elevationCell[(rowNum, colNum)] = [rowNum, colNum, rstArray.item(rowNum, colNum), -1]
def getDirectionOfWaterFlow(rowNumber,colNumber,elevationCell): cell8list = [] for rowi in range(rowNumber - 1, rowNumber + 2): for coli in range(colNumber - 1, colNumber + 2): if (rowi < rows and coli < cols and coli >= 0 and rowi >= 0 ): if(rowi != rowNumber or coli !=colNumber): if elevationCell[rowi, coli][3] == -1: elevationcell = elevationCell[rowi, coli] cell8list.append(elevationcell) if len(cell8list)!=0: cell8list.sort(key=lambda x: (x[2]), reverse=False) nercelllist = cell8list mincell=nercelllist[0] mincell_ele = nercelllist[0][2] selfcell_ele = elevationCell[rowNum, colNum][2] if mincell_ele <= selfcell_ele: depth = 0 elevationCell[rowNum, colNumber][3] = 1 else: depth = mincell_ele - selfcell_ele elevationCell[rowNumber, colNumber][3] = 1 return mincell, depth else: return None
def getWaterFlowPath(rowNumber,colNumber,elevationCell): if getDirectionOfWaterFlow(rowNumber,colNumber,elevationCell)!=None: mincell, depth=getDirectionOfWaterFlow(rowNumber,colNumber,elevationCell) r=mincell[0] c=mincell[1] pathList.append(mincell) getWaterFlowPath(r,c,elevationCell) else: return pathList
def getWaterFlowPathRaster(pointPath,pathList,waterFlowPathRasterSaveName): for i in range(len(pathList)): r = pathList[i][0] c = pathList[i][1] elevationCell[(r, c)][3] = i for rowNum in xrange(rows): for colNum in xrange(cols): rstArray[(rowNum, colNum)] = elevationCell[(rowNum, colNum)][3] result = arcpy.NumPyArrayToRaster(rstArray, arcpy.Point(leftPosition[0], bottomPosition[0]), float(cellXSzie[0]), float(cellYSzie[0])) result.save(waterFlowPathRasterSaveName) dsc = arcpy.Describe(pointPath) coord_sys = dsc.spatialReference arcpy.DefineProjection_management(waterFlowPathRasterSaveName, coord_sys)
|