Commit 9191e16a authored by Мария Григорьева's avatar Мария Григорьева
Browse files

Replace affiliation_geolocation.py

parent 930b5a24
......@@ -2,9 +2,10 @@ import logging
import pandas as pd
from elasticsearch import Elasticsearch
from geopy.exc import GeocoderTimedOut
from geopy.geocoders import Nominatim
from geopy.geocoders import Nominatim, Yandex, GeoNames
geolocator = Nominatim(user_agent="specify_your_app_name_here")
# geolocator = Yandex(lang='en_US')
LOG_FILENAME = 'affiliations.log'
......@@ -41,28 +42,17 @@ def scroller(es, index, request, pagesize=250, scroll_timeout="2m", **kwargs):
yield item
def get_location(address, city, code):
def get_location(city, code):
try:
location = geolocator.geocode(address,
addressdetails=True,
exactly_one=True,
country_codes=[code],
timeout=30)
if location is not None:
logger.info((location.latitude, location.longitude))
logger.info(city)
logger.info(code)
return location.latitude, location.longitude
else:
try:
location = geolocator.geocode(city,
exactly_one=True,
country_codes=[code],
timeout=30)
if location is not None:
logger.info((location.latitude, location.longitude))
......@@ -71,6 +61,7 @@ def get_location(address, city, code):
else:
logger.info('Not found')
return None, None
except GeocoderTimedOut as e:
......@@ -100,13 +91,11 @@ def main():
longitude = address['longitude']
if latitude is None and longitude is None:
logger.info(f'ID={id}')
addr = entry['_source']['metadata']['addresses'][0]
code = addr['country_code']
address = addr['postal_address'][-3:]
city = addr['cities'][0] if 'cities' in addr else code
latitude, longitude = get_location(address, city, code)
latitude, longitude = get_location(city, code)
affiliations_list.append({'id': id,
'lat': latitude,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment