爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 12729|回复: 2

[求助] python中做种子蔓延分析

[复制链接]

新浪微博达人勋

发表于 2017-6-3 11:22:29 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
近期需要用到种子蔓延分析,做基于DEM的有源洪水淹没分析。简单即是下:有源淹没是指水流收到地表起伏特征的影响,某些地方即使处于低洼处,也可能由于地形阻隔而不会被淹没。也就是说不仅要考虑哪些地方地势低还要考虑这些地方是否联通。而种子蔓延算法指给定空间中一点,从它周围的四个方向或8个方向,考虑:一周围点的地势是否低于种子点;二地势低于种子点的点是否联通;
简单说这些,我找到网上已有的代码:http://cadesign.cn/bbs/thread-742-1-1.html
先在此感谢作者,但由于是草稿,未做优化,在使用中出现了问题。主要问题即是,代码运行效率低,很长时间没有运行结果。现将代码复制如下,请各位大神帮帮忙:
  1. import arcpy
  2. from arcpy import env
  3. from arcpy.sa import *
  4. import copy

  5. #original_para
  6. worksapce_path=r"D:\GISForDesign_2\data"
  7. submerge_dem=r"D:\GISForDesign_2\data\DemMerge_Clip.tif"
  8. submerge_region=r"D:\GISForDesign_2\data\submergesAreaSmall.shp"
  9. seed=r"D:\GISForDesign_2\data\seed.shp"
  10. env.overwriteOutput=True

  11. #environment
  12. env.workspace=worksapce_path
  13. env.overwriteOutput=True

  14. submergeAreaName=worksapce_path+r"\submergeArea.tif"
  15. submergeArea=arcpy.Clip_management(submerge_dem,"",submergeAreaName,submerge_region,"","ClippingGeometry")

  16. floodLevel=1200

  17. #arcpy.SetProgressor("step", "submergeCal...",0, fc_count, 1)

  18. elevationCell={}
  19. #rowCount=arcpy.GetRasterProperties_management(submergeArea,"ROWCOUNT")
  20. #columnCount=arcpy.GetRasterProperties_management(submergeArea,"COLUMNCOUNT")
  21. cellXSzie=arcpy.GetRasterProperties_management(submergeArea,"CELLSIZEX")
  22. cellYSzie=arcpy.GetRasterProperties_management(submergeArea,"CELLSIZEY")
  23. leftPosition=arcpy.GetRasterProperties_management(submergeArea,"LEFT")
  24. topPosition=arcpy.GetRasterProperties_management(submergeArea,"TOP")
  25. bottomPosition=arcpy.GetRasterProperties_management(submergeArea,"BOTTOM")

  26. rstArray=arcpy.RasterToNumPyArray(arcpy.Raster(submergeArea))
  27. rows,cols=rstArray.shape
  28. for rowNum in xrange(rows):
  29.     for colNum in xrange(cols):
  30.         elevationCell[(rowNum,colNum)]=[rstArray.item(rowNum,colNum),False]


  31. for row in arcpy.da.SearchCursor(seed,["SHAPE@XY"]):
  32.     seedXY=row[0]
  33. del row

  34. #seedValue=arcpy.GetCellValue_management(submergeArea,"%s %s" % seedXY)
  35. #originalCordi=arcpy.ExportRasterWorldFile_management (submergeArea)



  36. XNum=(seedXY[0]-float(leftPosition[0]))//float(cellXSzie[0])
  37. YNum=(seedXY[1]-float(topPosition[0]))//float(cellYSzie[0])
  38. seedCoordi=(int(abs(XNum)),int(abs(YNum)))


  39. def floodCal(floodList,floodLevel):
  40.     #if len(floodList)>0:         
  41.     x=floodList[0][0]
  42.     y=floodList[0][1]
  43.     elevationCellCopy[floodList[0]][1]=True
  44.     floodList.pop(0)
  45.     for i in range(x-1,x+2):
  46.         for j in range(y-1,y+2):
  47.             if ((i,j) in  positionKeys and elevationCellCopy[(i,j)][1]==False and elevationCellCopy[(i,j)][0]<=floodLevel):
  48.                     elevationCellCopy[(i,j)][1]=True
  49.                     floodList.append((i,j))
  50.      
  51. '''                  
  52. def recursion(floodList,floodLevel):
  53.     if len(floodList)==0:
  54.         return 1
  55.     else:
  56.         floodCal(floodList,floodLevel)
  57.         return recursion(floodList,floodLevel)
  58. '''

  59. #import sys
  60. #sys.setrecursionlimit(1000)

  61. elevationCellCopy=copy.deepcopy(elevationCell)
  62. positionKeys=elevationCellCopy.keys()
  63. floodList=[]
  64. if elevationCell[seedCoordi][0]<=floodLevel:
  65.     floodList.append(seedCoordi)  

  66. while True:
  67.     if (len(floodList)==0):break
  68.     floodCal(floodList,floodLevel)   

  69. for rowNum in xrange(rows):
  70.     for colNum in xrange(cols):
  71.         rstArray[(rowNum,colNum)]=elevationCellCopy[(rowNum,colNum)][1]



  72. resultSubmerge=arcpy.NumPyArrayToRaster(rstArray,arcpy.Point(leftPosition[0],bottomPosition[0]),float(cellXSzie[0]),float(cellYSzie[0]))



  73. #recursion(floodList,floodLevel)                  
  74.                      



  75. '''
  76. for i in range(x-1,x+2):
  77.     for j in range(y-1,y+2):
  78.         if ((i,j) in  positionKeys and elevationCellCopy[(i,j)][1]==False and elevationCellCopy[(i,j)][0]<=floodLevel):
  79.             print((i,j),elevationCellCopy[(i,j)])



  80. FalseRaster=arcpy.sa.Con(submergeArea,0,0,"Value>=0")
  81. FalseRaster=submergeArea+FalseRaster

  82. demToPointsName=worksapce_path+r"\demToPoints.shp"
  83. demToPoint=arcpy.RasterToPoint_conversion(submergeArea,demToPointsName,"Value")
  84. '''
复制代码


密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2017-6-3 15:45:20 | 显示全部楼层
不错{:eb315:}{:eb315:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2017-6-7 08:01:57 | 显示全部楼层
{:eb502:}{:eb502:}{:eb502:}{:eb502:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表