-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathgeolocation.py
357 lines (335 loc) · 15.6 KB
/
geolocation.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from __future__ import unicode_literals
from __future__ import print_function
import logging
import requests
import utilities
from bson.objectid import ObjectId
import json
def query_cliff(sentence, host, port):
"""
Takes a sentence from a news article, passes it to the CLIFF geolocation
service, and extracts the relevant data that CLIFF returns.
Parameters
----------
sentence: String.
Text from which an event was coded.
Returns
-------
lat: String.
Latitude of a location.
lon: String.
Longitude of a location.
placeName: String.
The name of the most precise location extracted from the sentence.
stateName: String.
The name of the state/region/province extracted from the sentence.
countryCode: String.
The ISO 3 character country code of the country extracted from the sentence.
"""
logger = logging.getLogger('pipeline_log')
payload = {"q": sentence}
place_info = {'lat': '', 'lon': '', 'placeName': '', 'countryCode': '',
'stateName': '', 'restype' : ''}
cliff_address = "{}:{}/CLIFF-2.0.0/parse/text".format(host, port)
try:
located = requests.get(cliff_address, params=payload).json()
except Exception as e:
logger.warning('There was an error requesting geolocation. {}'.format(e))
return place_info
try:
focus = located['results']['places']['focus']
except:
return place_info
# logger.warning(focus)
if not focus:
return place_info
# If there's a city, we want that.
if focus['cities']:
# If there's more than one city, we just want the first.
# (That's questionable, but eh).
if len(focus['cities']) > 1:
try:
lat = focus['cities'][0]['lat']
lon = focus['cities'][0]['lon']
placeName = focus['cities'][0]['name']
countryCode = focus['cities'][0]['countryCode']
#countryDetails = focus['countries']
#for deet in countryDetails:
# if deet['countryCode'] == countryCode:
# countryName = deet['name']
# else:
# countryName = '' # shouldn't need these...
stateCode = focus['cities'][0]['countryCode']
stateDetails = focus['states']
for deet in stateDetails:
if deet['stateCode'] == stateCode:
stateName = deet['name']
else:
stateName = ''
place_info = {'lat': lat, 'lon': lon, 'placeName': placeName,
'restype': 'city', 'countryCode': countryCode,
'stateName': stateName}
return place_info
except:
logger.warning("Error on story. (multiple cities)")
logger.info(sentence)
return place_info
# If there's only one city, we're good to go.
elif len(focus['cities']) == 1:
try:
lat = focus['cities'][0]['lat']
lon = focus['cities'][0]['lon']
placeName = focus['cities'][0]['name']
countryCode = focus['cities'][0]['countryCode']
#countryDetails = focus['countries']
#for deet in countryDetails:
# if deet['countryCode'] == countryCode:
# countryName = deet['name']
# else:
# countryName = ''
stateCode = focus['cities'][0]['stateCode']
stateDetails = focus['states']
for deet in stateDetails:
if deet['stateCode'] == stateCode:
stateName = deet['name']
else:
stateName = ''
place_info = {'lat': lat, 'lon': lon, 'placeName': placeName,
'restype': 'city', 'countryCode': countryCode,
'stateName': stateName}
return place_info
except:
logger.warning("Error on story. (city == 1).")
logger.info(sentence)
return place_info
# If there's no city, we'll take a state.
elif (len(focus['states']) > 0) & (len(focus['cities']) == 0):
#if len(focus['states']) > 1:
# stateslist = focus['states'][0]
# lat = stateslist['states']['lat']
# lon = stateslist['states']['lon']
# placename = statelist['states']['name']
if len(focus['states']) == 1:
try:
lat = focus['states'][0]['lat']
lon = focus['states'][0]['lon']
placeName = focus['states'][0]['name']
countryCode = focus['states'][0]['countryCode']
#countryDetails = focus['countries']
#for deet in countryDetails:
# if deet['countryCode'] == countryCode:
# countryName = deet['name']
# else:
# countryName = ''
place_info = {'lat': lat, 'lon': lon, 'placeName': placeName,
'restype': 'state', 'countryCode': countryCode,
'stateName': stateName}
return place_info
except:
logger.warning("""Error on story. (cities == 0, states !=
0)""")
logger.info(sentence)
return place_info
#if ((focus['cities'] == []) & len(focus['states']) > 0):
# lat = focus['cities']['lat']
# lon = focus['cities']['lon']
# placename = focus['cities']['name']
elif (len(focus['cities']) == 0) & (len(focus['states']) == 0):
try:
lat = focus['countries'][0]['lat']
lon = focus['countries'][0]['lon']
countryCode = focus['countries'][0]['countryCode']
placeName = focus['countries'][0]['name']
place_info = {'lat': lat, 'lon': lon, 'placeName': placeName,
'restype': 'country', 'countryCode': countryCode,
'stateName': ''}
return place_info
except:
logger.warning("""Error on story. (cities == 0, states == 0)""")
logger.info(sentence)
return place_info
def iso_convert(iso2c):
"""
Takes a two character ISO country code and returns the corresponding 3
character ISO country code.
Parameters
----------
iso2c: A two character ISO country code.
Returns
-------
iso3c: A three character ISO country code.
"""
iso_dict = {"AD":"AND", "AE":"ARE", "AF":"AFG", "AG":"ATG", "AI":"AIA",
"AL":"ALB", "AM":"ARM", "AO":"AGO", "AQ":"ATA", "AR":"ARG",
"AS":"ASM", "AT":"AUT", "AU":"AUS", "AW":"ABW", "AX":"ALA",
"AZ":"AZE", "BA":"BIH", "BB":"BRB", "BD":"BGD", "BE":"BEL",
"BF":"BFA", "BG":"BGR", "BH":"BHR", "BI":"BDI", "BJ":"BEN",
"BL":"BLM", "BM":"BMU", "BN":"BRN", "BO":"BOL", "BQ":"BES",
"BR":"BRA", "BS":"BHS", "BT":"BTN", "BV":"BVT", "BW":"BWA",
"BY":"BLR", "BZ":"BLZ", "CA":"CAN", "CC":"CCK", "CD":"COD",
"CF":"CAF", "CG":"COG", "CH":"CHE", "CI":"CIV", "CK":"COK",
"CL":"CHL", "CM":"CMR", "CN":"CHN", "CO":"COL", "CR":"CRI",
"CU":"CUB", "CV":"CPV", "CW":"CUW", "CX":"CXR", "CY":"CYP",
"CZ":"CZE", "DE":"DEU", "DJ":"DJI", "DK":"DNK", "DM":"DMA",
"DO":"DOM", "DZ":"DZA", "EC":"ECU", "EE":"EST", "EG":"EGY",
"EH":"ESH", "ER":"ERI", "ES":"ESP", "ET":"ETH", "FI":"FIN",
"FJ":"FJI", "FK":"FLK", "FM":"FSM", "FO":"FRO", "FR":"FRA",
"GA":"GAB", "GB":"GBR", "GD":"GRD", "GE":"GEO", "GF":"GUF",
"GG":"GGY", "GH":"GHA", "GI":"GIB", "GL":"GRL", "GM":"GMB",
"GN":"GIN", "GP":"GLP", "GQ":"GNQ", "GR":"GRC", "GS":"SGS",
"GT":"GTM", "GU":"GUM", "GW":"GNB", "GY":"GUY", "HK":"HKG",
"HM":"HMD", "HN":"HND", "HR":"HRV", "HT":"HTI", "HU":"HUN",
"ID":"IDN", "IE":"IRL", "IL":"ISR", "IM":"IMN", "IN":"IND",
"IO":"IOT", "IQ":"IRQ", "IR":"IRN", "IS":"ISL", "IT":"ITA",
"JE":"JEY", "JM":"JAM", "JO":"JOR", "JP":"JPN", "KE":"KEN",
"KG":"KGZ", "KH":"KHM", "KI":"KIR", "KM":"COM", "KN":"KNA",
"KP":"PRK", "KR":"KOR", "XK":"XKX", "KW":"KWT", "KY":"CYM",
"KZ":"KAZ", "LA":"LAO", "LB":"LBN", "LC":"LCA", "LI":"LIE",
"LK":"LKA", "LR":"LBR", "LS":"LSO", "LT":"LTU", "LU":"LUX",
"LV":"LVA", "LY":"LBY", "MA":"MAR", "MC":"MCO", "MD":"MDA",
"ME":"MNE", "MF":"MAF", "MG":"MDG", "MH":"MHL", "MK":"MKD",
"ML":"MLI", "MM":"MMR", "MN":"MNG", "MO":"MAC", "MP":"MNP",
"MQ":"MTQ", "MR":"MRT", "MS":"MSR", "MT":"MLT", "MU":"MUS",
"MV":"MDV", "MW":"MWI", "MX":"MEX", "MY":"MYS", "MZ":"MOZ",
"NA":"NAM", "NC":"NCL", "NE":"NER", "NF":"NFK", "NG":"NGA",
"NI":"NIC", "NL":"NLD", "NO":"NOR", "NP":"NPL", "NR":"NRU",
"NU":"NIU", "NZ":"NZL", "OM":"OMN", "PA":"PAN", "PE":"PER",
"PF":"PYF", "PG":"PNG", "PH":"PHL", "PK":"PAK", "PL":"POL",
"PM":"SPM", "PN":"PCN", "PR":"PRI", "PS":"PSE", "PT":"PRT",
"PW":"PLW", "PY":"PRY", "QA":"QAT", "RE":"REU", "RO":"ROU",
"RS":"SRB", "RU":"RUS", "RW":"RWA", "SA":"SAU", "SB":"SLB",
"SC":"SYC", "SD":"SDN", "SS":"SSD", "SE":"SWE", "SG":"SGP",
"SH":"SHN", "SI":"SVN", "SJ":"SJM", "SK":"SVK", "SL":"SLE",
"SM":"SMR", "SN":"SEN", "SO":"SOM", "SR":"SUR", "ST":"STP",
"SV":"SLV", "SX":"SXM", "SY":"SYR", "SZ":"SWZ", "TC":"TCA",
"TD":"TCD", "TF":"ATF", "TG":"TGO", "TH":"THA", "TJ":"TJK",
"TK":"TKL", "TL":"TLS", "TM":"TKM", "TN":"TUN", "TO":"TON",
"TR":"TUR", "TT":"TTO", "TV":"TUV", "TW":"TWN", "TZ":"TZA",
"UA":"UKR", "UG":"UGA", "UM":"UMI", "US":"USA", "UY":"URY",
"UZ":"UZB", "VA":"VAT", "VC":"VCT", "VE":"VEN", "VG":"VGB",
"VI":"VIR", "VN":"VNM", "VU":"VUT", "WF":"WLF", "WS":"WSM",
"YE":"YEM", "YT":"MYT", "ZA":"ZAF", "ZM":"ZMB", "ZW":"ZWE",
"CS":"SCG", "AN":"ANT"}
try:
iso3c = iso_dict[iso2c]
return iso3c
except KeyError:
print('Bad code: ' + iso2c)
iso3c = "NA"
return iso3c
def query_mordecai(sentence, host, port):
"""
Takes a sentence from a news article, passes it to the Mordecai geolocation
service, and extracts the relevant data that Mordecai returns.
Parameters
----------
sentence: String.
Text from which an event was coded.
host: String
Host where Mordecai is running (taken from config)
port: String
Port that Mordecai service is listening on
Returns
-------
lat: String.
Latitude of a location.
lon: String.
Longitude of a location.
placeName: String.
The name of the most precise location extracted from the sentence.
stateName: String.
The name of the state/region/province extracted from the sentence.
countryCode: String.
The ISO 3 character country code of the country extracted from the sentence.
"""
headers = {'Content-Type': 'application/json'}
data = {'text': sentence}
data = json.dumps(data)
dest = "{0}:{1}/places".format(host, port)
out = requests.post(dest, data=data, headers=headers)
return json.loads(out.text)
def test_mordecai(sentence, host, port):
"""
Check if Mordecai service is up and responding on given host and port.
Parameters
----------
sentence: String.
Text from which an event was coded.
"""
def mordecai(events, file_details, server_details, geo_details):
"""
Pulls out a database ID and queries the Mordecai geolocation system
running locally and find location information within the sentence.
Parameters
----------
events: Dictionary.
Contains filtered events from the one-a-day filter. Keys are
(DATE, SOURCE, TARGET, EVENT) tuples, values are lists of
IDs, sources, and issues.
Returns
-------
events: Dictionary.
Same as in the parameter but with the addition of a value that is
a list of lon, lat, placeName, stateName, countryCode.
"""
coll = utilities.make_conn(file_details.db_db, file_details.db_collection,
file_details.auth_db, file_details.auth_user,
file_details.auth_pass)
for event in events:
event_id, sentence_id = events[event]['ids'][0].split('_')
# print(event_id)
result = coll.find_one({'_id': ObjectId(event_id.split('_')[0])})
sents = utilities.sentence_segmenter(result['content'])
query_text = sents[int(sentence_id)]
geo_info = query_mordecai(query_text, geo_details.mordecai_host,
geo_details.mordecai_port)
try:
# temporary hack: take the first location:
geo_info = geo_info[0]
# NA is for ADM1, which mord doesn't return. See issue #2
events[event]['geo'] = (geo_info['lon'], geo_info['lat'],
geo_info['placename'], "NA", geo_info['countrycode'])
except Exception as e:
events[event]['geo'] = ("NA", "NA", "NA", "NA", "NA")
return events
def cliff(events, file_details, server_details, geo_details):
"""
Pulls out a database ID and runs the ``query_cliff`` function to hit MIT's
CLIFF/CLAVIN geolocation system running locally and find location
information within the sentence. Note, this function calls back to the database
where stories are stored.
Parameters
----------
events: Dictionary.
Contains filtered events from the one-a-day filter. Keys are
(DATE, SOURCE, TARGET, EVENT) tuples, values are lists of
IDs, sources, and issues.
Returns
-------
events: Dictionary.
Same as in the parameter but with the addition of a value that is
a list of lon, lat, placeName, stateName, countryCode.
"""
coll = utilities.make_conn(file_details.db_db, file_details.db_collection,
file_details.auth_db, file_details.auth_user,
file_details.auth_pass)
for event in events:
event_id, sentence_id = events[event]['ids'][0].split('_')
# print(event_id)
result = coll.find_one({'_id': ObjectId(event_id.split('_')[0])})
sents = utilities.sentence_segmenter(result['content'])
query_text = sents[int(sentence_id)]
geo_info = query_cliff(query_text, geo_details.cliff_host,
geo_details.cliff_port)
if geo_info:
try:
if geo_info['countryCode'] != "":
geo_info['countryCode'] = iso_convert(geo_info['countryCode'])
except:
logger.warning("""Error converting country codes.""")
events[event]['geo'] = (geo_info['lon'], geo_info['lat'],
geo_info['placeName'],
geo_info['stateName'],
geo_info['countryCode'])
# Add in country and restype here
return events