-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLocationsToMap.py
More file actions
63 lines (52 loc) · 2.15 KB
/
LocationsToMap.py
File metadata and controls
63 lines (52 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Sarah Barden
'''Takes an ISBN code and returns a map highlighting the locations of
the Author, Plot and Publisher.'''
import folium # library for data visualization on maps
from opencage.geocoder import OpenCageGeocode # geocoding service
from key import geo_key
from time import sleep
def geocode(place):
"""
Uses the OpenCage geocoder to get latitude and longitude of a place.
INPUT: a place name as an attribute of a book object
OUTPUT: a list containing the latitude and longitude for that place
"""
geocoder = OpenCageGeocode(geo_key)
location = geocoder.geocode(place, key=geo_key)
# We search wikipedia for 3 locations, but don't always get 3.
# If a location isn't found, it's an empty list, so we do not plot it.
if location == []:
pass
else:
longitude = location[0]['geometry']['lng'] # lat/long are within a nested
latitude = location[0]["geometry"]["lat"] # results dictionary under geometry
# print(latitude, longitude)
return [latitude, longitude]
def plotGraph(book):
"""
Takes a book object and uses geocode() to find locations of three attributes:
author location, publisher location, and plot location.
INPUT: a book object
OUTPUT: a map with pins on locations
"""
# Creates the map
map1 = folium.Map(zoom_start=2, tiles='Mapbox bright')
# Adds marker for the each location
locations = [book.authorLoc, book.publisherLoc, book.plotLoc]
colors = ['red', 'green', 'blue']
popups = ['{} is from {}'.format(book.author, book.authorLoc),
'This edition was published in {}'.format(book.publisherLoc),
'{} takes place in {}'.format(book.title, book.plotLoc)]
# Loops through lists above to create markers
for i in range(0, 3):
if locations[i] == []:
pass
else:
folium.Marker(geocode(locations[i]),
icon=folium.Icon(color=colors[i]),
popup=popups[i]
).add_to(map1)
sleep(1) # Delays calls to open cage geocoder
map1.save('map.html')
if __name__ == '__main__':
pass