-
Notifications
You must be signed in to change notification settings - Fork 0
/
world_map.py
129 lines (93 loc) · 5.31 KB
/
world_map.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.models import (GMapPlot, GMapOptions, ColumnDataSource, Circle, Range1d, PanTool, WheelZoomTool, BoxSelectTool)
import numpy as np
import pandas as pd
import json
import requests
import sqlite3
def world_maps():
"""Collect sqlite data and put it into a pandas dataframe for additional cleaning and graphing"""
#use pandas to retrieve sqlite tables
conn = sqlite3.connect('meteo.db')
meteorites_geo = pd.read_sql_query('SELECT reclat, reclong FROM meteorite_data;', conn)
conn.close()
#remove NA values
meteorites_geo = meteorites_geo.dropna()
#Use pandas to convert strings into numbers(floats)
meteorites_geo['reclong'] = meteorites_geo['reclong'].astype(float)
meteorites_geo['reclat'] = meteorites_geo['reclat'].astype(float)
output_file("./graphs/world-map.html", title="Worldwide Meteorite Landings")
#This section uses Bokeh patches and json data of country boundries to map the world. This code
#is a modified version of a project worked on by Ken Alger in the treehouse.com tutorial
#'Data Visualization with Bokeh' in the video 'Plotting the World'. The map of the world
#comes from Johan Sundström who has a github at https://github.com/johan/world.geo.json
def world_map_json():
url = 'https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json'
r = requests.get(url)
json_data = r.json()
def get_coordinates(features):
depth = lambda L: isinstance(L, list) and max(map(depth, L))+1
longitudes = []
latitudes = []
for feature in json_data['features']:
coordinates = feature['geometry']['coordinates']
number_dimensions = depth(coordinates)
# one border
if number_dimensions == 3:
points = np.array(coordinates[0], 'f')
longitudes.append(points[:, 0])
latitudes.append(points[:, 1])
# several borders
else:
for shape in coordinates:
points = np.array(shape[0], 'f')
longitudes.append(points[:, 0])
latitudes.append(points[:, 1])
return longitudes, latitudes
lats, longs = get_coordinates(json_data['features'])
#took off hover for now due to performance issues
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
#make graph that is bound by Geographic Coordinates
world_map_plot = figure(plot_width=900,
plot_height=600,
title="Meteorite Landings Worldwide using JSON data for country shapes",
tools=TOOLS,
x_range=(-180, 180),
y_range=(-90, 90),
x_axis_label = "Longitude",
y_axis_label = "Latitude")
#This applies the geometic shapes to a lat,long grid
world_map_plot.patches(lats, longs, fill_color="#F1EEF6", fill_alpha=0.7, line_width=2)
#Meteorite landings data mapped over the top of the world patch in red
world_map_plot.scatter(meteorites_geo['reclong'], meteorites_geo['reclat'], fill_color="#FF0000", fill_alpha=1, line_width=.5)
#Show the data
show(world_map_plot)
def world_map_google():
"""Collect sqlite data and put it into a pandas dataframe for additional cleaning and graphing"""
#use pandas to retrieve sqlite tables
conn = sqlite3.connect('meteo.db')
meteorites_geo = pd.read_sql_query('SELECT reclat, reclong FROM meteorite_data;', conn)
conn.close()
#remove NA values
meteorites_geo = meteorites_geo.dropna()
#Use pandas to convert strings into numbers(floats)
meteorites_geo['reclong'] = meteorites_geo['reclong'].astype(float)
meteorites_geo['reclat'] = meteorites_geo['reclat'].astype(float)
output_file("./graphs/world-map.html", title="Worldwide Meteorite Landings")
#This is my current location, would like to replace this with users current GeoLocation in the future
map_options = GMapOptions(lat=38.229660599999995, lng=-85.7405836, map_type="terrain", zoom=6)
world_map_plot = GMapPlot(x_range=Range1d(), y_range=Range1d(), map_options=map_options)
world_map_plot.title.text = "Meteorite Landing Locations Worldwide"
#This is an individualized key that you must register to get
world_map_plot.api_key = "AIzaSyCTH9aO_DdSi_OxiZtuj7xie8-5kHzjjeg"
data=dict(lat=meteorites_geo['reclat'],lon=meteorites_geo['reclong'])
source = ColumnDataSource(data)
circle = Circle(x="lon", y="lat", size=5, fill_color="red", fill_alpha=0.9, line_color=None)
world_map_plot.add_glyph(source, circle)
world_map_plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool())
output_file("./graphs/google_world_map.html")
show(world_map_plot)
world_map_json()
world_map_google()