Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import arcpy, traceback, os, sys,time
  2. import itertools as itt
  3. scriptsPath=os.path.dirname(os.path.realpath(__file__))
  4. os.chdir(scriptsPath)
  5. import COMMON
  6. sys.path.append(r'C:Usersfelix_pertzigerAppDataRoamingPythonPython27site-packages')
  7. import networkx as nx
  8. RATIO = int(arcpy.GetParameterAsText(0))
  9.  
  10. try:
  11. def showPyMessage():
  12. arcpy.AddMessage(str(time.ctime()) + " - " + message)
  13. mxd = arcpy.mapping.MapDocument("CURRENT")
  14. theT=COMMON.getTable(mxd)
  15.  
  16. theNodesLayer = COMMON.getInfoFromTable(theT,1)
  17. theNodesLayer = COMMON.isLayerExist(mxd,theNodesLayer)
  18.  
  19. theLinksLayer = COMMON.getInfoFromTable(theT,9)
  20. theLinksLayer = COMMON.isLayerExist(mxd,theLinksLayer)
  21. arcpy.SelectLayerByAttribute_management(theLinksLayer, "CLEAR_SELECTION")
  22. linksFromI=COMMON.getInfoFromTable(theT,14)
  23. linksToI=COMMON.getInfoFromTable(theT,13)
  24. G=nx.Graph()
  25. arcpy.AddMessage("Adding links to graph")
  26. with arcpy.da.SearchCursor(theLinksLayer, (linksFromI,linksToI,"Times")) as cursor:
  27. for row in cursor:
  28. (f,t,c)=row
  29. G.add_edge(f,t,weight=c)
  30. del row, cursor
  31. pops=[]
  32. pops=arcpy.da.TableToNumPyArray(theNodesLayer,("P2013"))
  33. length0=nx.all_pairs_shortest_path_length(G)
  34. nNodes=len(pops)
  35. aBmNodes=[]
  36. aBig=xrange(nNodes)
  37. host=[-1]*nNodes
  38. while True:
  39. RATIO+=-1
  40. if RATIO==0:
  41. break
  42. aBig = filter(lambda x: x not in aBmNodes, aBig)
  43. p=itt.combinations(aBig, 2)
  44. pMin=1000000
  45. small=[]
  46. for a in p:
  47. S0,S1=0,0
  48. for i in aBig:
  49. p=pops[i][0]
  50. p0=length0[a[0]][i]
  51. p1=length0[a[1]][i]
  52. if p0<p1:
  53. S0+=p
  54. else:
  55. S1+=p
  56. if S0!=0 and S1!=0:
  57. sMin=min(S0,S1)
  58. sMax=max(S0,S1)
  59. df=abs(float(sMax)/sMin-RATIO)
  60. if df<pMin:
  61. pMin=df
  62. aBest=a[:]
  63. arcpy.AddMessage('%s %i %i' %(aBest,sMax,sMin))
  64. if df<0.005:
  65. break
  66. lSmall,lBig,S0,S1=[],[],0,0
  67. arcpy.AddMessage ('Ratio %i' %RATIO)
  68. for i in aBig:
  69. p0=length0[aBest[0]][i]
  70. p1=length0[aBest[1]][i]
  71. if p0<p1:
  72. lSmall.append(i)
  73. S0+=p0
  74. else:
  75. lBig.append(i)
  76. S1+=p1
  77. if S0<S1:
  78. aBmNodes=lSmall[:]
  79. for i in aBmNodes:
  80. host[i]=aBest[0]
  81. for i in lBig:
  82. host[i]=aBest[1]
  83. else:
  84. aBmNodes=lBig[:]
  85. for i in aBmNodes:
  86. host[i]=aBest[1]
  87. for i in lSmall:
  88. host[i]=aBest[0]
  89.  
  90. with arcpy.da.UpdateCursor(theNodesLayer, "rcvnode") as cursor:
  91. i=0
  92. for row in cursor:
  93. row[0]=host[i]
  94. cursor.updateRow(row)
  95. i+=1
  96.  
  97. del row, cursor
  98. except:
  99. message = "n*** PYTHON ERRORS *** "; showPyMessage()
  100. message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
  101. message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "n"; showPyMessage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement