proffreda

CIty Data Python

Jun 29th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.21 KB | None | 0 0
  1. def parse_city(line):
  2. "Parse a line of data, as in http://www.realestate3d.com/gps/latlong.htm , and return a city code and long-lat"
  3. code, lat, long, name = line.split(None, 3)
  4. cityname, state = name.split(',')
  5. scode=code[1:-1]
  6. return scode, (float(long), float(lat))
  7.  
  8. UScityData = map(parse_city, """
  9. [TCL] 33.23 87.62 Tuscaloosa,AL
  10. [PHX] 33.43 112.02 Phoenix,AZ
  11. [PGA] 36.93 111.45 Page,AZ
  12. [TUS] 32.12 110.93 Tucson,AZ
  13. [LIT] 35.22 92.38 Little Rock,AR
  14. [SFO] 37.62 122.38 San Francisco,CA
  15. [LAX] 33.93 118.40 Los Angeles,CA
  16. [SAC] 38.52 121.50 Sacramento,CA
  17. [SAN] 32.73 117.17 San Diego,CA
  18. [SBP] 35.23 120.65 San Luis Obi,CA
  19. [EKA] 41.33 124.28 Eureka,CA
  20. [SJC] 37.37 121.92 San Jose,CA
  21. [DEN] 39.75 104.87 Denver,CO
  22. [DRO] 37.15 107.75 Durango,CO
  23. [HVN] 41.27 72.88 New Haven,CT
  24. [DOV] 39.13 75.47 Dover,DE
  25. [DCA] 38.85 77.04 Washington/Natl,DC
  26. [MIA] 25.82 80.28 Miami Intl,FL
  27. [TPA] 27.97 82.53 Tampa Intl,FL
  28. [JAX] 30.50 81.70 Jacksonville,FL
  29. [TLH] 30.38 84.37 Tallahassee,FL
  30. [ATL] 33.65 84.42 Atlanta,GA
  31. [BOI] 43.57 116.22 Boise,ID
  32. [CHI] 41.90 87.65 Chicago,IL
  33. [IND] 39.73 86.27 Indianapolis,IN
  34. [DSM] 41.53 93.65 Des Moines,IA
  35. [ICT] 37.65 97.43 Wichita,KS
  36. [LEX] 38.05 85.00 Lexington,KY
  37. [NEW] 30.03 90.03 New Orleans,LA
  38. [BOS] 42.37 71.03 Boston,MA
  39. [PWM] 43.65 70.32 Portland,ME
  40. [BGR] 44.80 68.82 Bangor,ME
  41. [DET] 42.42 83.02 Detroit,MI
  42. [STC] 45.55 94.07 St Cloud,MN
  43. [MIN] 44.98 93.26 Minneapolis,MN
  44. [DLH] 46.83 92.18 Duluth,MN
  45. [STL] 38.75 90.37 St Louis,MO
  46. [JAN] 32.32 90.08 Jackson,MS
  47. [BIL] 45.80 108.53 Billings,MT
  48. [BTM] 45.95 112.50 Butte,MT
  49. [RDU] 35.87 78.78 Raleigh-Durh,NC
  50. [INT] 36.13 80.23 Winston-Salem,NC
  51. [OMA] 41.30 95.90 Omaha/Eppley,NE
  52. [LAS] 36.08 115.17 Las Vegas,NV
  53. [EWR] 40.70 74.17 Newark Intl,NJ
  54. [ABQ] 35.05 106.60 Albuquerque,NM
  55. [SAF] 35.62 106.08 Santa Fe,NM
  56. [LRU] 32.30 106.77 Las Cruces,NM
  57. [NYC] 40.77 73.98 New York,NY
  58. [BUF] 42.93 78.73 Buffalo,NY
  59. [ALB] 42.75 73.80 Albany,NY
  60. [FAR] 46.90 96.80 Fargo,ND
  61. [BIS] 46.77 100.75 Bismarck,ND
  62. [CVG] 39.05 84.67 Cincinnati,OH
  63. [COL] 39.98 82.98 Columbus, OH
  64. [CLE] 41.42 81.87 Cleveland,OH
  65. [OKC] 35.40 97.60 Oklahoma Cty,OK
  66. [PDX] 45.60 122.60 Portland,OR
  67. [MFR] 42.37 122.87 Medford,OR
  68. [PIT] 40.35 79.93 Pittsburgh,PA
  69. [PVD] 41.73 71.43 Providence,RI
  70. [CHS] 32.90 80.03 Charleston,SC
  71. [MEM] 35.05 90.00 Memphis,TN
  72. [DAL] 32.90 97.03 Dallas,TX
  73. [LBB] 33.65 101.82 Lubbock,TX
  74. [IAH] 29.97 95.35 Houston,TX
  75. [SAT] 29.53 98.47 San Antonio,TX
  76. [ABI] 32.42 99.68 Abilene,TX
  77. [ELP] 31.79 106.42 El Paso, TX
  78. [SLC] 40.78 111.97 Salt Lake Ct,UT
  79. [MPV] 44.20 72.57 Montpelier,VT
  80. [RIC] 37.50 77.33 Richmond,VA
  81. [SEA] 47.45 122.30 Seattle,WA
  82. [ALW] 46.10 118.28 Walla Walla,WA
  83. [GRB] 44.48 88.13 Green cityBay,WI
  84. [MKE] 42.95 87.90 Milwaukee,WI
  85. [CYS] 41.15 104.82 Cheyenne,WY
  86. [SHR] 44.77 106.97 Sheridan,WY
  87. [MAD] 43.07 89.400 Madison, WI
  88. [FLG] 35.20 111.63 Flagstaff, AZ
  89. [KAN] 39.09 94.57 Kansas City, MO
  90. [LOU] 38.25 85.76 Louisville, KY
  91. [PHI] 39.95 75.16 Philadelphia, PA
  92. [NAS] 36.16 86.78 Nashville, TN
  93. [KNO] 35.97 83.94 Knoxville, TN
  94. [TAM] 27.97 82.46 Tampa, TN
  95. [PRO] 41.82 71.42 Providence, RI
  96. """.strip().splitlines())
  97.  
  98. #print UScityData
  99.  
  100. cityhash={ code : loc for (code,loc) in UScityData}
  101. #print sorted(cityhash)
  102. #print cityhash
  103. print cityhash['CVG'] # (84.67, 39.05)
  104.  
  105. import math
  106. def s_distance(loc1, loc2):
  107. (long1,lat1)= cityhash[loc1]
  108. (long2,lat2)= cityhash[loc2]
  109. degrees_to_radians = math.pi/180.0
  110. phi1 = (90.0 - lat1)*degrees_to_radians
  111. phi2 = (90.0 - lat2)*degrees_to_radians
  112. theta1 = long1*degrees_to_radians
  113. theta2 = long2*degrees_to_radians
  114. cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +
  115. math.cos(phi1)*math.cos(phi2))
  116. arc = math.acos( cos )
  117. return arc * 3960
  118.  
  119. print "Distance from CVG to LEX"
  120. print s_distance('CVG','LEX')
  121.  
  122.  
  123. def parse_roads(line):
  124. city1, city2 = line.split(',')
  125. return city1.strip(), city2.strip()
  126.  
  127. hiways = map(parse_roads, """
  128. MKE,CHI
  129. MKE, MAD
  130. MAD, CHI
  131. SEA,BTM
  132. SEA, PDX
  133. SEA, ALW
  134. PDX, SFO
  135. PDX, ALW
  136. ALW, SLC
  137. SFO, SLC
  138. SLC, LAX
  139. LAX, SAN
  140. SAN, TUS
  141. LAX, PHX
  142. TUS, PHX
  143. LAX, FLG
  144. FLG, PHX
  145. FLG, ABQ
  146. ABQ, ELP
  147. ELP, DAL
  148. ELP, SAT
  149. SAT, IAH
  150. IAH, DAL
  151. DAL, OKC
  152. ABQ, OKC
  153. ABQ, DEN
  154. SLC, DEN
  155. DEN, LAX
  156. DEN, CYS
  157. CYS, SLC
  158. CYS, BIL
  159. BIL, BTM
  160. BIL, FAR
  161. OKC, KAN
  162. DEN, KAN
  163. KAN, OMA
  164. DEN, OMA
  165. OMA, CHI
  166. OMA, FAR
  167. FAR, MIN
  168. MIN, CHI
  169. CHI, STL
  170. STL, KAN
  171. CHI, DET
  172. CHI ,IND
  173. IND, STL
  174. IND, LOU
  175. LOU, LEX
  176. IND, CVG
  177. CVG, LOU
  178. CVG, LEX
  179. STL, LOU
  180. CVG, COL
  181. COL, CLE
  182. CLE, BUF
  183. CLE, PIT
  184. CVG, PIT
  185. PIT, BUF
  186. PIT, PHI
  187. PHI, NYC
  188. PHI, DCA
  189. DCA, ATL
  190. ATL, NEW
  191. NEW, IAH
  192. NEW, MEM
  193. MEM, OKC
  194. MEM, STL
  195. MEM, NAS
  196. NAS, LOU
  197. LEX, KNO
  198. NAS, KNO
  199. KNO, ATL
  200. ATL, TAM
  201. ATL, NEW
  202. ATL, JAX
  203. JAX, MIA
  204. JAX, DCA
  205. ATL, DCA
  206. KNO, DCA
  207. DCA, PHI
  208. NYC, ALB
  209. BUF, ALB
  210. ALB, BOS
  211. NYC, BOS
  212. BOS, PRO
  213. NYC, PRO
  214. """.strip().splitlines())
  215.  
  216. #print "Distance ", hiways[0]
  217. #print s_distance(hiways[0][0],hiways[0][1])
  218.  
  219. #Test road distances
  220. #for e in hiways:
  221. # print e, s_distance(e[0],e[1])
  222.  
  223. min=1000
  224. minroad = 0
  225. for e in hiways:
  226. if s_distance(e[0],e[1]) < min :
  227. min = s_distance(e[0],e[1])
  228. minroad = e
  229. print minroad, min
  230. """
  231. from math import *
  232.  
  233. def dist(lat1,lon1,lat2,lon2):
  234. lat1=(90.0-lat1)*pi/180
  235. lat2=(90.0-lat2)*pi/180
  236. lon1=lon1*pi/180
  237. lon2=lon2*pi/180
  238. return 3963.189 * acos(sin(lat1)*cos(lon1)*sin(lat2)*cos(lon2)+
  239. sin(lat1)*sin(lon1)*sin(lat2)*sin(lon2)+cos(lat1)*cos(lat2))
  240.  
  241. def dist2(lat1,lon1,lat2,lon2):
  242. dlon = (lon2 - lon1)*pi/180
  243. dlat = (lat2 - lat1)*pi/180
  244. a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2
  245. c = 2 * asin(min(1,sqrt(a)))
  246. R=3963.189
  247. d = R * c
  248. return d
  249.  
  250. cities= [['Akron-Canton',40,55,81,26],['Ashtabula',41,51,80,48],
  251. ['Athens',39,20,82,6],
  252. ['BowlingGreen',41,23,83,38],
  253. ['Cambridge',40,4,81,35],
  254. ['Chillicothe',39,21,83,0],
  255. ['Cincinnati',39,9,84,31],
  256. ['Cleveland',41,24,81,51],
  257. ['Columbus',40,0,82,53],
  258. ['Dayton',39,54,84,13],
  259. ['Defiance',41,17,84,23],
  260. ['Findlay',41,1,83,40],
  261. ['Fremont',41,20,83,7],
  262. ['Hamilton',39,24,84,35],
  263. ['Lancaster',39,44,82,38],
  264. ['Lima',40,42,84,2],
  265. ['Mansfield',40,49,82,31],
  266. ['Marion',40,36,83,10],
  267. ['Middletown',39,31,84,25],
  268. ['Newark',40,1,82,28],
  269. ['Norwalk',41,16,82,37],
  270. ['Portsmouth',38,45,82,55],
  271. ['Sandusky',41,27,82,43],
  272. ['Springfield',39,50,83,50],
  273. ['Steubenville',40,23,80,38],
  274. ['Toledov',41,36,83,48],
  275. ['Warren',41,20,80,51],
  276. ['Wooster',40,47,81,55],
  277. ['Youngstown',41,16,80,40],
  278. ['Zanesville',39,57,81,54]]
  279.  
  280. distmat=[ \
  281. [ 0, 72, 115, 119, 59, 136, 203, 39, 99, 162, 155, 116, 92, 196, 103, 136, 57, 93, 184, 82, 66, 169, 76, 146, 55, 131, 41, 26, 46, 71 ],
  282. [ 72, 0, 186, 150, 129, 207, 270, 62, 168, 223, 189, 159, 125, 261, 175, 185, 114, 150, 249, 153, 102, 241, 102, 211, 101, 155, 35, 93, 40, 143 ],
  283. [ 115, 186, 0, 163, 57, 48, 130, 143, 62, 119, 180, 142, 148, 132, 39, 139, 104, 104, 124, 51, 136, 59, 149, 98, 106, 180, 153, 100, 153, 43 ],
  284. [ 119, 150, 163, 0, 140, 144, 161, 92, 103, 107, 9, 25, 27, 146, 125, 51, 70, 59, 135, 112, 53, 186, 47, 107, 171, 17, 144, 98, 154, 134 ],
  285. [ 59, 129, 57, 140, 0, 90, 168, 93, 69, 140, 169, 127, 118, 166, 60, 136, 71, 91, 155, 46, 99, 115, 112, 120, 54, 157, 95, 52, 95, 18 ],
  286. [ 136, 207, 48, 144, 90, 0, 82, 154, 45, 75, 152, 120, 137, 84, 32, 108, 104, 86, 76, 54, 134, 41, 146, 55, 144, 161, 177, 114, 180, 71 ],
  287. [ 203, 270, 130, 161, 168, 82, 0, 209, 105, 54, 147, 136, 168, 17, 108, 110, 156, 123, 25, 124, 177, 90, 185, 59, 223, 173, 245, 178, 250, 150 ],
  288. [ 39, 62, 143, 92, 93, 154, 209, 0, 110, 161, 131, 98, 65, 199, 122, 123, 53, 88, 187, 101, 40, 191, 45, 150, 94, 101, 52, 42, 62, 100 ],
  289. [ 99, 168, 62, 103, 69, 45, 105, 110, 0, 71, 118, 81, 93, 99, 22, 77, 59, 44, 88, 22, 88, 86, 100, 51, 121, 120, 141, 74, 145, 52 ],
  290. [ 162, 223, 119, 107, 140, 75, 54, 161, 71, 0, 96, 82, 114, 39, 84, 56, 109, 73, 28, 93, 126, 105, 132, 20, 192, 119, 202, 135, 209, 122 ],
  291. [ 155, 189, 180, 39, 169, 152, 147, 131, 118, 96, 0, 41, 65, 130, 141, 44, 102, 79, 122, 133, 91, 191, 87, 104, 205, 37, 183, 133, 193, 159 ],
  292. [ 116, 159, 142, 25, 127, 120, 136, 98, 81, 82, 41, 0, 36, 121, 104, 29, 61, 38, 111, 93, 57, 161, 57, 82, 164, 40, 148, 92, 157, 118 ],
  293. [ 92, 125, 148, 27, 118, 137, 168, 65, 93, 114, 65, 36, 0, 154, 113, 64, 47, 50, 143, 97, 26, 179, 22, 110, 145, 39, 117, 73, 127, 115 ],
  294. [ 196, 261, 132, 146, 166, 84, 17, 199, 99, 39, 130, 121, 154, 0, 106, 94, 146, 111, 12, 120, 165, 100, 172, 49, 220, 157, 237, 170, 243, 147 ],
  295. [ 103, 175, 39, 125, 60, 32, 108, 122, 22, 84, 141, 104, 113, 106, 0, 99, 75, 66, 96, 21, 106, 69, 118, 64, 115, 142, 145, 81, 148, 41 ],
  296. [ 136, 185, 139, 51, 136, 108, 110, 123, 77, 56, 44, 29, 64, 94, 99, 0, 79, 46, 84, 95, 83, 147, 86, 60, 180, 63, 171, 111, 180, 123 ],
  297. [ 57, 114, 104, 70, 71, 104, 156, 53, 59, 109, 102, 61, 47, 146, 75, 79, 0, 37, 134, 55, 31, 144, 45, 97, 103, 85, 93, 31, 101, 68 ],
  298. [ 93, 150, 104, 59, 91, 86, 123, 88, 44, 3, 79, 38, 50, 111, 66, 46, 37, 0, 99, 54, 54, 128, 63, 63, 134, 76, 131, 66, 138, 80 ],
  299. [ 184, 249, 124, 135, 155, 76, 25, 187, 88, 28, 122, 111, 143, 12, 96, 84, 134, 99, 0, 109, 153, 96, 160, 38, 209, 147, 225, 158, 231, 137 ],
  300. [ 82, 153, 51, 112, 46, 54, 124, 101, 22, 93, 133, 93, 97, 120, 21, 95, 55, 54, 109, 0, 86, 90, 100, 73, 100, 129, 124, 60, 128, 30 ],
  301. [ 66, 102, 136, 53, 99, 134, 177, 40, 88, 126, 91, 57, 26, 165, 106, 83, 31, 54, 153, 86, 0, 174, 13, 117, 120, 65, 91, 49, 101, 98 ],
  302. [ 169, 241, 59, 186, 115, 41, 90, 191, 86, 105, 191, 161, 179, 100, 69, 147, 144, 128, 96, 90, 174, 0, 187, 89, 166, 202, 209, 150, 210, 99 ],
  303. [ 76, 102, 149, 47, 112, 146, 185, 45, 100, 132, 87, 57, 22, 172, 118, 86, 45, 63, 160, 100, 13, 187, 0, 126, 131, 57, 97, 62, 107, 112 ],
  304. [ 146, 211, 98, 107, 120, 55, 59, 150, 51, 20, 104, 82, 110, 49, 64, 60, 97, 63, 38, 73, 117, 89, 126, 0, 173, 122, 187, 120, 193, 102 ],
  305. [ 55, 101, 106, 171, 54, 144, 223, 94, 121, 192, 205, 164, 145, 220, 115, 180, 103, 134, 209, 100, 120, 166, 131, 173, 0, 185, 66, 72, 61, 73 ],
  306. [ 131, 155, 180, 17, 157, 161, 173, 101, 120, 119, 37, 40, 39, 157, 142, 63, 85, 76, 147, 129, 65, 202, 57, 122, 185, 0, 154, 113, 164, 151 ],
  307. [ 41, 35, 153, 144, 95, 177, 245, 52, 141, 202, 183, 148, 117, 237, 145, 171, 93, 131, 225, 124, 91, 209, 97, 187, 66, 154, 0, 67, 10, 110 ],
  308. [ 26, 93, 100, 98, 52, 114, 178, 42, 74, 135, 133, 92, 73, 170, 81, 111, 31, 66, 158, 60, 49, 150, 62, 120, 72, 113, 67, 0, 73, 57 ],
  309. [ 46, 40, 153, 154, 95, 180, 250, 62, 145, 209, 193, 157, 127, 243, 148, 180, 101, 138, 231, 128, 101, 210, 107, 193, 61, 164, 10, 73, 0, 111 ],
  310. [ 71, 143, 43, 134, 18, 71, 150, 100, 52, 122, 159, 118, 115, 147, 41, 123, 68, 80, 137, 30, 98, 99, 112, 102, 73, 151, 110, 57, 111, 0 ] ]
  311.  
  312. """
  313. cities =[['a'],['b'],['c']]
  314. distmat = [[0,1,2],
  315. [1,0,4],
  316. [2,4,0] ]
  317. """
  318.  
  319. class city_class:
  320. def __init__(self, cityName, index, distanceList ):
  321. self.name = cityName
  322. self.index = index
  323. self._distance = distanceList
  324. self.maxRadius = 0
  325.  
  326. def pr( self ):
  327. print self.name, self.index
  328. def distance(self, city ):
  329. "QUICK LOOKUP"
  330. return self._distance[ city.index ]
  331.  
  332.  
  333. class greedy_class(city_class):
  334. def __init__(self, cityList, distanceMatrix, locList=[] ):
  335. self.cityClassList = [ city_class(cityList[x],x,distanceMatrix[x] ) \
  336. for x in range(len(cityList)) ]
  337. self.locList = locList
  338.  
  339. def pr( self ):
  340. print "CITY LIST:"
  341. for city in self.cityClassList:
  342. city.pr()
  343. print "LOCATION LIST:"
  344. for city in self.locList:
  345. city.pr()
  346.  
  347. def maxRadius(self, city):
  348. " Find the farthest radius for this city "
  349. max_radius = 0
  350. max_index = -1
  351. for city2 in self.cityClassList:
  352. thisDistance = city.distance( city2 )
  353. if thisDistance > max_radius:
  354. isMaxDistance = 1
  355. for city3 in self.locList:
  356. if city3.distance(city2) > thisDistance:
  357. isMaxDistance = 0
  358. break
  359. if isMaxDistance == 1:
  360. max_radius = thisDistance
  361. max_index = city2.index
  362. city.maxRadius = max_radius
  363. return max_radius
  364.  
  365.  
  366. def process( self, K ):
  367. """ Ireratively remove the cities with the largest radius, add to
  368. location list as most effective (distribution) points """
  369. for iteration in range(K):
  370. print "Iteration ",iteration
  371. greatest_city = self.cityClassList[0]
  372. greatest_radius = self.maxRadius( self.cityClassList[0] )
  373. for city in self.cityClassList[1:]:
  374. this_radius = self.maxRadius( city )
  375. if this_radius > greatest_radius:
  376. greatest_radius = this_radius
  377. greatest_city = city
  378. #early exit if no more good picks are to be had:
  379. if greatest_radius > 0 :
  380. self.locList.append( greatest_city )
  381. self.cityClassList.remove(greatest_city)
  382. else:
  383. return self.locList
  384. return self.locList
  385.  
  386.  
  387.  
  388.  
  389. def main():
  390. print "MAIN"
  391. cityList = [ cities[x][0] for x in range(len(cities)) ]
  392. A = greedy_class( cityList, distmat )
  393. #A.pr()
  394. locList = A.process(5)
  395. for item in locList:
  396. print item.name
  397.  
  398.  
  399. if __name__ == '__main__':
  400. main()
Advertisement
Add Comment
Please, Sign In to add comment