Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import urllib.parse
  2. from openpyxl import load_workbook, Workbook
  3. import requests
  4. from math import radians, cos, sin, asin, sqrt
  5. import xlwt
  6.  
  7. """load datafile"""
  8. workbook = load_workbook('CompaniesF.xlsm')
  9. Companysheet = workbook.get_sheet_by_name("Companysheet")
  10. Networksheet = workbook.get_sheet_by_name("Networksheet")
  11. networkco = []
  12. compco = []
  13.  
  14.  
  15. """search for column with latitude/longitude - customers"""
  16. numberlatC = -1
  17. i = 0
  18. for col in Companysheet.iter_cols():
  19. if col[0].value == "LAT" :
  20. numberlatC = i
  21. i+=1
  22.  
  23. numberlongC = -1
  24. j = 0
  25. for col in Companysheet.iter_cols():
  26. if col[0].value == "LNG" :
  27. numberlongC = j
  28. j+=1
  29.  
  30. numberlatN = -1
  31. k = 0
  32. for col in Networksheet.iter_cols():
  33. if col[0].value == "LAT" :
  34. numberlatN = k
  35. k+=1
  36.  
  37. numberlongN = -1
  38. l = 0
  39. for col in Networksheet.iter_cols():
  40. if col[0].value == "LNG" :
  41. numberlongN = l
  42. l+=1
  43.  
  44. latC = [row[numberlatC].value for row in Companysheet.iter_rows(min_row=3, max_row=73000) ]
  45. #print(latC)
  46. latCFloat = [float(i) for i in latC]
  47.  
  48. longC = [row[numberlongC].value for row in Companysheet.iter_rows(min_row=3, max_row=73000)]
  49. longCFloat = [float(i) for i in longC]
  50. compco = [list(x) for x in zip(latCFloat, longCFloat)]
  51.  
  52. #networkco = [[latN, lotN]]
  53. latN = [row[numberlatN].value for row in Networksheet.iter_rows(min_row=3, max_row=103)]
  54. latNFloat = [float(i) for i in latN]
  55.  
  56. longN = [row[numberlongN].value for row in Networksheet.iter_rows(min_row=3, max_row=103)]
  57. longNFloat = [float(i) for i in longN]
  58. networkco = [list(x) for x in zip(latNFloat, longNFloat)]
  59.  
  60.  
  61. def CoordinatesDistance(A, B):
  62. # haversine formula
  63. x1,y1 = A
  64. x2,y2 = B
  65. dlon = y2 - y1
  66. dlat = x2 - x1
  67. a = sin(dlat/2)**2 + cos(x1) * cos(x2) * sin(dlon/2)**2
  68. c = 2 * asin(sqrt(a))
  69. r = 6371 # Radius of earth in kilometers. Use 3956 for miles
  70. distance = c*r
  71. distances = []
  72. distances.append([distance])
  73. return distance
  74.  
  75.  
  76. def shortest_distance_pair(Comp, Net):
  77. pairs = []
  78. shortestfinal = []
  79. for P in Comp:
  80. shortest_distance = 999999
  81. for Y in Net:
  82. distance = CoordinatesDistance(P,Y)
  83. #customer_distance.append(distance)
  84. if distance < shortest_distance:
  85. shortest_distance = distance
  86. sdp = [(P,Y)]
  87. pairs.append(sdp)
  88. shortestfinal.append(shortest_distance)
  89. return shortestfinal
  90.  
  91. output = shortest_distance_pair(compco, networkco)
  92.  
  93.  
  94. wb = Workbook(write_only = True)
  95. ws = wb.create_sheet()
  96.  
  97. ws.append(output)
  98. wb.save("output.xlsx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement