以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文
一、构建公路旅行路线列表
第一步:在公路旅行过程中,你需要决定在哪停下来。 确保先查看 Google Maps 上的所有位置,以便掌握正确的地址、城市、州等。如果用于查看位置的名称在 Google Maps 上无法起效,那么它在本节代码中也无法起效。 将所有路标添加到下面的列表中,确保它们的格式与下面的示例相同。
技术说明:由于 Google Maps API 的日常使用限制,您最多只能有70个路标,如果您想添加更多的路标,则需要向 Google 支付费用增加 API 限制值。
- # https://en.wikipedia.org/wiki/List_of_state_capitols_in_the_United_States
- all_waypoints = ['Alabama State Capitol, 600 Dexter Avenue, Montgomery, AL 36130',
- #'Alaska State Capitol, Juneau, AK',
- 'Arizona State Capitol, 1700 W Washington St, Phoenix, AZ 85007',
- 'Arkansas State Capitol, 500 Woodlane Street, Little Rock, AR 72201',
- 'L St & 10th St, Sacramento, CA 95814',
- '200 E Colfax Ave, Denver, CO 80203',
- 'Connecticut State Capitol, 210 Capitol Ave, Hartford, CT 06106',
- 'Legislative Hall: The State Capitol, Legislative Avenue, Dover, DE 19901',
- '402 S Monroe St, Tallahassee, FL 32301',
- 'Georgia State Capitol, Atlanta, GA 30334',
- #'Hawaii State Capitol, 415 S Beretania St, Honolulu, HI 96813'
- '700 W Jefferson St, Boise, ID 83720',
- 'Illinois State Capitol, Springfield, IL 62756',
- 'Indiana State Capitol, Indianapolis, IN 46204',
- 'Iowa State Capitol, 1007 E Grand Ave, Des Moines, IA 50319',
- '300 SW 10th Ave, Topeka, KS 66612',
- 'Kentucky State Capitol Building, 700 Capitol Avenue, Frankfort, KY 40601',
- 'Louisiana State Capitol, Baton Rouge, LA 70802',
- 'Maine State House, Augusta, ME 04330',
- 'Maryland State House, 100 State Cir, Annapolis, MD 21401',
- 'Massachusetts State House, Boston, MA 02108',
- 'Michigan State Capitol, Lansing, MI 48933',
- 'Minnesota State Capitol, St Paul, MN 55155',
- '400-498 N West St, Jackson, MS 39201',
- 'Missouri State Capitol, Jefferson City, MO 65101',
- 'Montana State Capitol, 1301 E 6th Ave, Helena, MT 59601',
- 'Nebraska State Capitol, 1445 K Street, Lincoln, NE 68509',
- 'Nevada State Capitol, Carson City, NV 89701',
- 'State House, 107 North Main Street, Concord, NH 03303',
- 'New Jersey State House, Trenton, NJ 08608',
- 'New Mexico State Capitol, Santa Fe, NM 87501',
- 'New York State Capitol, State St. and Washington Ave, Albany, NY 12224',
- 'North Carolina State Capitol, Raleigh, NC 27601',
- 'North Dakota State Capitol, Bismarck, ND 58501',
- 'Ohio State Capitol, 1 Capitol Square, Columbus, OH 43215',
- 'Oklahoma State Capitol, Oklahoma City, OK 73105',
- 'Oregon State Capitol, 900 Court St NE, Salem, OR 97301',
- 'Pennsylvania State Capitol Building, North 3rd Street, Harrisburg, PA 17120',
- 'Rhode Island State House, 82 Smith Street, Providence, RI 02903',
- 'South Carolina State House, 1100 Gervais Street, Columbia, SC 29201',
- '500 E Capitol Ave, Pierre, SD 57501',
- 'Tennessee State Capitol, 600 Charlotte Avenue, Nashville, TN 37243',
- 'Texas Capitol, 1100 Congress Avenue, Austin, TX 78701',
- 'Utah State Capitol, Salt Lake City, UT 84103',
- 'Vermont State House, 115 State Street, Montpelier, VT 05633',
- 'Virginia State Capitol, Richmond, VA 23219',
- 'Washington State Capitol Bldg, 416 Sid Snyder Ave SW, Olympia, WA 98504',
- 'West Virginia State Capitol, Charleston, WV 25317',
- '2 E Main St, Madison, WI 53703',
- 'Wyoming State Capitol, Cheyenne, WY 82001']
- len(all_waypoints)
接下来,必须使用 Google Maps API 注册此脚本,以便他们知道谁在用数百个 Google Maps 路由请求冲击他们的服务器。
1) 用你的 Google 账户使激活 Google Maps Distance Matrix API,Google 给出了操作说明 here
2) 复制生成的 API key 粘贴到下面的代码中
- import googlemaps
- gmaps = googlemaps.Client(key='ENTER YOUR GOOGLE MAPS KEY HERE')
接下来,我们将查询 Google Maps API,寻找所有路标之间的最短路线。
这相当于在 Google 地图网站上进行 Google 地图路线查询,只是现在我们正在使用代码自动执行数百次查询。
如果您在此部分遇到错误,那么很有可能意味着在 Google Maps 上找不到你输入的路标。另一个可能导致错误的原因是,如果在这些点之间不可能开车,例如,寻找夏威夷和佛罗里达之间的行车路线将会返回错误,除非我们发明飞行汽车。
二、收集所有路标之间最短路线
- from itertools import combinations
- waypoint_distances = {}
- waypoint_durations = {}
- for (waypoint1, waypoint2) in combinations(all_waypoints, 2):
- try:
- route = gmaps.distance_matrix(origins=[waypoint1],
- destinations=[waypoint2],
- mode='driving', # 如果是步行导航,请改为 'walking'
- # 骑行则改为'bicycling',以此类推
- language='English',
- units='metric')
- # 距离'distance' 单位是米
- distance = route['rows'][0]['elements'][0]['distance']['value']
- # 时间'duration' 单位是秒
- duration = route['rows'][0]['elements'][0]['duration']['value']
- waypoint_distances[frozenset([waypoint1, waypoint2])] = distance
- waypoint_durations[frozenset([waypoint1, waypoint2])] = duration
-
- except Exception as e:
- print('Error with finding the route between {} and {}.'.format(waypoint1, waypoint2))
现在我们有所有路标之间的路线,让我们将它们保存到一个文本文件中,这样我们也不用再麻烦 Google 了。
- with open('my-waypoints-dist-dur.tsv', 'w') as out_file:
- out_file.write('\t'.join(['waypoint1',
- 'waypoint2',
- 'distance_m',
- 'duration_s']))
-
- for (waypoint1, waypoint2) in waypoint_distances.keys():
- out_file.write('\n' +
- '\t'.join([waypoint1,
- waypoint2,
- str(waypoint_distances[frozenset([waypoint1, waypoint2])]),
- str(waypoint_durations[frozenset([waypoint1, waypoint2])])]))
以上内容转自 数析学院,如需完整内容可以直接查看原文


雷达卡




京公网安备 11010802022788号







