Advertisement
Guest User

Untitled

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