/
evis.py
72 lines (63 loc) · 2.21 KB
/
evis.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
# query for data
'''
SELECT date, lat, lon, cnt from (
SELECT date, lat, lon, COUNT(*) as cnt
FROM (
SELECT REGEXP_REPLACE(STRING(date), '(....)(..)(..)(..)(..)(..)', r'\1-\2-\3 \4:\5:\6') date,
REGEXP_REPLACE(
REGEXP_EXTRACT(
SPLIT(V2Locations, ';'),
r'^[2-5]#.*?#.*?#.*?#.*?#(.*?#.*?)#'),
'^(.*?)#(.*?)$', '\\1') AS lat,
REGEXP_REPLACE(
REGEXP_EXTRACT(
SPLIT(V2Locations, ';'),
r'^[2-5]#.*?#.*?#.*?#.*?#(.*?#.*?)#'),
'^(.*?)#(.*?)$', '\\2') AS lon,
FROM [gdelt-bq:gdeltv2.gkg@-86400000-]
)
WHERE lat is not null
GROUP BY date, lat, lon
ORDER BY date DESC
)
WHERE cnt >= 3
limit 10000;
'''
import sys
import numpy
import datetime as dt
from collections import defaultdict
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
def main():
data = numpy.genfromtxt("data.csv", dtype=None, delimiter=',', names=True)
#lat - data[x][1]
#lon - data[x][2]
print(data)
# Note that we're drawing on a regular matplotlib figure, so we set the
# figure size just like we would any other.
plt.figure(figsize=(28,18))
# Create the Basemap
event_map = Basemap(projection='merc',
resolution='l', area_thresh=1000.0, # Low resolution
lat_0 = 55.0, lon_0=60.0, # Map center
llcrnrlon=-179, llcrnrlat=-72, # Lower left corner
urcrnrlon=179, urcrnrlat=78) # Upper right corner
# Draw important features
event_map.drawcoastlines()
event_map.drawcountries()
event_map.fillcontinents(color='0.8') # Light gray
event_map.drawmapboundary()
a = [f for f in data if int(f[2])<500]
b = [f for f in data if int(f[2])<500]
# Draw the points on the map:
for i in range(len(data)-8000):
x, y = event_map(data[i][2], data[i][1]) # Convert lat, long to y,x
marker_size = data[i][3]/4
event_map.plot(x,y, 'ro', markersize=marker_size, alpha=0.3)
plt.savefig('map.png', bbox_inches='tight')
if __name__ == '__main__':
main()