-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmali_timehistory.py
81 lines (67 loc) · 2.9 KB
/
mali_timehistory.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
import numpy as np
import pandas as pd
import make_geojson as mgj
def save_map(df):
"""Not currently used - this was an experiment in a making the map a
different way, using the 'folium' Python library (which wraps leafletjs)
"""
import folium
minlong = df['lognitude'].min()
maxlong = df['lognitude'].max()
minlat = df['latitude'].min()
maxlat = df['latitude'].max()
m = folium.Map([minlat, minlong], zoom_start=6)
for i in range(df.shape[0]):
folium.CircleMarker(location=[df['latitude'][i],
df['lognitude'][i]],
radius=df['population'][i],
popup=df['name'][i], color='r',
fill_color='r').add_to(m)
m.save("out.html")
def get_loctype(location, date_index):
"""Returns a pandas Series of the location type for each day.
Locations with a changetime have type *town* before that day, and *conflict*
after it.
"""
n_days = len(date_index)
changetime = location.time
if pd.isnull(changetime):
loctype = location.location_type
else:
#0:changetime, loctype = "town"
loctype = ['town'] * int(changetime)
#changetime:-1, loctype = "conflict"
loctype +=['conflict'] * int(n_days - changetime)
return pd.Series(loctype, index=date_index)
def get_population(timeseries_data, name, defaultpop=100):
"""Get the population time series for the named location.
If the name is not in the data provided, construct a time series with a
constant default population, so that the marker shows up on the map.
"""
try:
return timeseries_data[name]
except KeyError:
print("Warning:", name, "missing from time series data")
return pd.Series(defaultpop, index=timeseries_data.index)
def make_features(locations_file='mlocations.csv',
timeseries_file='malioutput.csv',
startdate='2012-02-29'):
locations = pd.read_csv(locations_file)
timeseries = pd.read_csv(timeseries_file)
n_days = timeseries.shape[0]
# Construct an index with real dates rather than day numbers
timeseries.index = pd.date_range(startdate, periods=n_days)
features = []
for location in locations.itertuples(name='Location'):
latlon = (location.latitude, location.longitude)
loctype_by_day = get_loctype(location, timeseries.index)
population_by_day = get_population(timeseries, location.name)
data_for_location = pd.DataFrame({'loctype': loctype_by_day,
'population': population_by_day})
feature = mgj.make_gj_points(latlon, location.name, data_for_location)
features.extend(feature)
return features
if __name__ == '__main__':
features = make_features()
# Write to file
mgj.write_geojson_from_features('mali.jsonp', features)