def geocode_address(address, provider): ''' Выполняет геокодирование адреса ''' logger.info(f"Geocoding address '{address}'...") if provider == "yandex": geo = geocoder.yandex(location=address, lang="ru-RU", kind="house") elif provider == "osm": url = os.getenv("NOMINATIM_URL") address_norm = normalize_address(address) address_parts = [ address_norm["city"]["value"], "{} {}".format(address_norm["street"]["type"], address_norm["street"]["value"]), address_norm["house"]["value"], ] if "building" in address_norm: address_parts.append(address_norm["building"]["value"]) address = ", ".join(address_parts) geo = geocoder.osm(location=address, url=url) else: logger.warning(f"Unknown geocoder provider '{provider}'") return logger.debug(geo) data = geo.json assert isinstance(data, dict) data.update(provider=provider) return data
def geolocate(address, try_all=True): try: # see https://astral.readthedocs.io/en/latest/#cities a = lookup(address, database()) return a.latitude, a.longitude except: pass # use online geocoder location_data = geocoder.geonames(address, method='details', key='jarbas') if not location_data.ok: location_data = geocoder.geocodefarm(address) if not location_data.ok: location_data = geocoder.osm(address) if try_all: # more are just making it slow if not location_data.ok: location_data = geocoder.google(address) if not location_data.ok: location_data = geocoder.arcgis(address) if not location_data.ok: location_data = geocoder.bing(address) if not location_data.ok: location_data = geocoder.canadapost(address) if not location_data.ok: location_data = geocoder.yandex(address) if not location_data.ok: location_data = geocoder.tgos(address) if location_data.ok: location_data = location_data.json lat = location_data.get("lat") lon = location_data.get("lng") return lat, lon raise ValueError
def create_issue(): request_json = request.get_json() if len(request_json) < 4: return error('Not enough data for issue creation') name = request_json['name'] description = request_json['description'] image = request_json['image'] coordinates = request_json['coordinates'] lat = coordinates.get('lat') lng = coordinates.get('lng') if not (lat and lng): return error('Coordinates are not valid') if type(coordinates) != dict: return error('Coordinates must be in json') try: geo_response = geocoder.yandex([lat, lng], method='reverse') except ValueError as err: return error(str(err)) coordinates = { 'lat': geo_response.lat, 'lng': geo_response.lng, 'address': geo_response.address } postgres.create_issue(str(uuid4()), name, description, image, coordinates) return jsonify('Issue successfully created')
def get_address_from_coordinates_spb(crd): kinds = ['house', 'district', 'locality'] result = {} for kind in kinds: geocode_yandex = geocoder.yandex( crd, method='reverse', kind=kind, lang='RU', key='7bb41e4d-5a27-4b8c-a856-ee56d3284019') yandex_address_array = geocode_yandex.address.split(',') print(yandex_address_array) if len(yandex_address_array) == 4: result = { 'city': yandex_address_array[1].strip(), 'street': yandex_address_array[2].strip(), 'house': yandex_address_array[3].strip(), } geocode_mapquest = geocoder.mapquest(crd, method='reverse', key=settings.MAPQUEST_MAP_KEY) result.update({'postal': geocode_mapquest.postal}) else: result = { 'city': yandex_address_array[1].strip(), } return result
def show_weather(self): try: city = self.setcity.text() country = self.setcountry.text() location = geocoder.yandex(city + ' ' + country) data = forecast(KEY, location.latlng[0], f'{location.latlng[1]}?lang=ru&units=si') except Exception: answ = QMessageBox.question( self, 'Ошибка', 'Город/страна не найден(а) Попробуете еще раз?') if answ == QMessageBox.No: self.close() else: cc = '' while ', ' not in cc: cc, btn = QInputDialog.getText(self, 'Погода для народа', 'Введите город, страну:') if not btn: self.close() break if btn: cc = cc.split(', ') self.setcity.setText(cc[0]) self.setcountry.setText(cc[1]) self.show_weather() else: options = self.set_options(self.prm) timeper = self.set_timeper(self.tp) weather = Weather(data, options, timeper, self.view) weather.show_widget()
def perform_create(self, serializer): address = serializer.initial_data['address'] g = geocoder.yandex(address) latitude = g.latlng[0] longitude = g.latlng[1] pnt = 'POINT({} {})'.format(str(longitude), str(latitude)) serializer.save(location=pnt)
def geocode_address(input_address): """ Geocode an address to (lat, lon) """ try: latlng = geocoder.arcgis(input_address).latlng return latlng except: pass try: latlng = geocoder.osm(input_address).latlng return latlng except: pass try: latlng = geocoder.ottawa(input_address).latlng return latlng except: pass try: latlng = [float(s) for s in geocoder.yandex(input_address).latlng] return latlng except: pass
def get_users_by_geo(self, location, api): #получаю координаты места g = geocoder.yandex(location) #получаю список мест, связанных с нужным мне местом data = api.location_search(g.json['lat'], g.json['lng'], query=None) acc_by_geo = [] #идем в цикле по каждому месту for mesto in data['venues']: #идентификатор места id_point = mesto['external_id'] #новостная лента fl = api.feed_location(id_point) #если в массиве данных по месту есть посты то идем их собирать try: acc_by_geo.extend(self.convert(fl['ranked_items'], 'following')) #посты выдаются массивами по N штук #потому мы берем next_max_id чтобы получить следующий набор #так продолжаем в цикле пока не соберем все посты в этом месте mid = fl['next_max_id'] while mid: try: mfl = api.feed_location(id_point, max_id = mid) mid = mfl['next_max_id'] acc_by_geo.extend(self.convert(mfl['items'], 'following')) tt = randint(6, 13) time.sleep(tt) except KeyError as e: # return 'Error in %s' % (e) break except KeyError as e: continue # tl = randint(3, 6) # time.sleep(tl) return acc_by_geo
def geocoding_from_address(sender, instance, **kwargs): if instance.address: g = geocoder.yandex(instance.address) latitude = g.latlng[0] longitude = g.latlng[1] pnt = 'POINT({} {})'.format(str(longitude), str(latitude)) # instance.location = Point(str(longitude), str(latitude), srid=4326) instance.location = GEOSGeometry(pnt, srid=4326)
def geocode_address(address): ''' Выполняет геокодирование адреса ''' geo = geocoder.yandex(location=address, lang="ru-RU", kind="house") data = geo.json assert isinstance(data, dict) data.update(provider="yandex") return data
def get_address_by_string(string): try: loc = geocoder.yandex(string).json reviewer_town, reviewer_country = loc.get("city", ""), loc.get("country", "") except: reviewer_town, reviewer_country = string, string return reviewer_town, reviewer_country
def set_location_from_tg(self, location: telegram.Location): self.latitude = location.latitude self.longitude = location.longitude geo = geocoder.yandex([self.latitude, self.longitude], method='reverse') self.place = geo.description or geo.address self.save() logger.info(f'Added location {self.place}')
def reverseGeocoder(point): try: g = geocoder.yandex(point, method='reverse') if (g.state == None): g = geocoder.google(point, method='reverse') print(g.state) return g.state except: return None
def address(self): g = geocoder.yandex([ str(self.geoposition).split(',')[0], str(self.geoposition).split(',')[1] ], method='reverse', lang='ru-RU') address = g.address return address
def geolocate(address, service="geocodefarm"): data = {} if service == "geocodefarm": location_data = geocoder.geocodefarm(address) elif service == "osm": location_data = geocoder.osm(address) elif service == "google": location_data = geocoder.google(address) elif service == "arcis": location_data = geocoder.arcgis(address) elif service == "bing": location_data = geocoder.bing(address) elif service == "canadapost": location_data = geocoder.canadapost(address) elif service == "yandex": location_data = geocoder.yandex(address) elif service == "tgos": location_data = geocoder.tgos(address) else: raise ValueError("Unknown geocoder service") if location_data.ok: location_data = location_data.json data["country"] = location_data.get("country") data["country_code"] = location_data.get("country_code") data["region"] = location_data.get("region") data["address"] = location_data.get("address") data["state"] = location_data.get("state") data["confidence"] = location_data.get("confidence") data["lat"] = location_data.get("lat") data["lon"] = location_data.get("lng") data["city"] = location_data.get("city") data["postal"] = location_data.get("postal") data["timezone"] = location_data.get("timezone_short") else: return None location = { "city": { "name": data["city"], "state": { "name": data["state"], "country": { "code": data["country_code"], "name": data["country"] } } }, "coordinate": { "latitude": data["lat"], "longitude": data["lon"] }, "timezone": { "name": data["timezone"] } } return location
def save(self, *args, **kwargs): # http://geocoder.readthedocs.io g = geocoder.yandex(u'{} {}'.format(self.city, self.address), lang='ru_RU') geo_json = g.geojson self.geo_json = geo_json self.address = geo_json['features'][0]['properties']['address'] self.longitude = geo_json['features'][0]['properties']['lng'] self.latitude = geo_json['features'][0]['properties']['lat'] super().save(*args, **kwargs)
def getUserAddress(user): #print (user) def parse_url(tweet_user): url = URL_INIT + tweet_user.strip('@') return url try: url = parse_url(user) response = urlopen(url) except: return None html = response.read() soup = BeautifulSoup(html) location = soup.find('span', 'ProfileHeaderCard-locationText') if location: location = location.text.strip('\n').strip() if location: if ',' in location: splitted_location = location.split(',') else: splitted_location = re.split('|;|-|/|°|#', location) #print (splitted_location) try: if splitted_location: located_location = geocoder.yandex(splitted_location[0]) else: located_location = geocoder.yandex(location) if located_location: geocode = str(located_location.latlng[0]) + "," + str( located_location.latlng[1]) #print (geocode) return geocode else: #return location #print "---No location--" return None except GeocoderTimedOut as e: print("Error: geocode failed on input %s with message %s" % (location, e))
def main(): data = [] with open('ttt.csv') as csvfile: ttt = csv.DictReader(csvfile) for row in ttt: data.append(row) print(len(data)) for item in tqdm(data): if not item['address']: data.remove(item) print(len(data)) for item in tqdm(data): geo = geocoder.yandex(item['address']) if geo.ok: print(geo.lat) item['lat'] = geo.lat item['lng'] = geo.lng else: print("addr %s not ok" % str(item['address'])) data.remove(item) for item in tqdm(data): if "lat" not in item.keys(): geo = geocoder.yandex(item['address']) if geo.ok: print(geo.lat) item['lat'] = geo.lat item['lng'] = geo.lng else: print("addr %s not ok" % str(item['address'])) data.remove(item) with open('ttt2.json', 'w') as jsonfile: json.dump(data, jsonfile)
def createMap(year): mapMov = folium.Map(tiles="Mapbox Control Room") input_file = open("locations.list", "rb") fg_USA = folium.FeatureGroup(name="USA") fg_UA = folium.FeatureGroup(name="UA") fg_world = folium.FeatureGroup(name="Whole world") count = 0 while True: try: line = input_file.readline() if year in str(line): tmp = line.strip().split(b"\t") tmp = [elem.decode("UTF-8") for elem in tmp] if tmp[-1].endswith(")"): loc = geocoder.yandex(tmp[-2]) assert loc is not None find(tmp[-2], tmp[0], loc.latlng, fg_USA, fg_UA, fg_world) count += 1 else: loc = geocoder.yandex(tmp[-1]) assert loc is not None find(tmp[-1], tmp[0], loc.latlng, fg_USA, fg_UA, fg_world) count += 1 if count >= 1000000: break except EOFError: break except UnicodeDecodeError: pass except AttributeError: pass except AssertionError: pass input_file.close() mapMov.add_child(fg_USA) mapMov.add_child(fg_UA) mapMov.add_child(fg_world) mapMov.save('Map_movies2.html')
def get_address_by_coordinates(coordinates: tuple) -> Optional[AnyStr]: """ Return address string value by coordinates :param coordinates: Coordinates (latitude, longitude) :return: string value """ latitude = coordinates[0] longitude = coordinates[1] location = geocoder.yandex([latitude, longitude], method='reverse', lang='ru-RU') if not location.json: return None return location.json.get('address')
def get_coordinates(address, prefix): ''' Запрашивает координаты дома по его адресу ''' try: if prefix not in address: address = prefix + ", " + address print(f"[get_coordinates] Getting coordinates for '{address}'", flush=True) data = geocoder.yandex(location=address, lang="ru-RU", kind="house") geo = data.json assert isinstance(geo, dict) return dict(longitude=float(geo["lng"]), latitude=float(geo["lat"])) except Exception as err: print(f"[get_coordinates] Error: {err}", flush=True)
def get_coordinates_from_address_gtn(address): kinds = ['house', 'locality'] for kind in kinds: geocode_yandex = geocoder.yandex( address, method='reverse', kind=kind, lang='RU', key='7bb41e4d-5a27-4b8c-a856-ee56d3284019') try: lat = float(geocode_yandex.lat) lng = float(geocode_yandex.lng) except: continue return [lat, lng]
def get_gcs(list_names): gcs = [] for name in list_names: g = geocoder.yandex(name) if g.status == 'OK': gc = g.latlng gcs.append(gc) gcs = list(map(str, gcs)) gcs = [gc.replace('[', '') for gc in gcs] gcs = [gc.replace(']', '') for gc in gcs] gcs = [gc.replace(', ', ',') for gc in gcs] gcs = [gc.replace("'", "") for gc in gcs] gcs = [i for i in gcs if i != 'None'] else: print('status: {}'.format(g.status)) return gcs
def get_moscow_district_from_address(sender, instance, **kwargs): if instance.address: g = geocoder.yandex(instance.address) latitude = str(g.latlng[0]) longitude = str(g.latlng[1]) district_full_name = get_moscow_district(longitude, latitude) print(district_full_name) instance.district = get_district_short_name(district_full_name) print(instance.district) # def perform_create(self, serializer): # address = serializer.initial_data['address'] # g = geocoder.yandex(address) # latitude = g.latlng[0] # longitude = g.latlng[1] # pnt = 'POINT({} {})'.format(str(longitude), str(latitude)) # serializer.save(location=pnt)
def get_city_name_en(city_name): """ Get city name (Eng) by city name(Ru) :param city_name: city name(Ru) :return: Dict of city name(Eng), location(latitude), location(longitude) """ geo_data = {} try: g = geocoder.yandex(city_name) geo_data['city'] = str(g.json['city']).encode('utf-8') geo_data['longitude_min'] = g.json['bbox']['southwest'][1] geo_data['longitude_max'] = g.json['bbox']['northeast'][1] geo_data['latitude_min'] = g.json['bbox']['southwest'][0] geo_data['latitude_max'] = g.json['bbox']['northeast'][0] return geo_data except Exception, e: logger.error( str(e) + '-->City name: {0} and Geo data: {1}'.format(city_name, geo_data)) return geo_data
def get_address_from_coordinates_gtn(crd): kinds = ['house', 'locality'] for kind in kinds: geocode_yandex = geocoder.yandex( crd, method='reverse', kind=kind, lang='RU', key='7bb41e4d-5a27-4b8c-a856-ee56d3284019') yandex_address_array = geocode_yandex.address.split(',') if len(yandex_address_array) > 3: result = { 'city': yandex_address_array[2].strip(), 'street': yandex_address_array[3].strip(), 'house': yandex_address_array[4].strip(), } else: result = { 'city': yandex_address_array[2].strip(), } return result
def geocode(self, words): result = None search = None for index in range(len(words)): search = ' '.join(words[:index+1]) g = geocoder.yandex(search, lang='ru-RU') if g: found_place = g.json['address'].split(',')[-1] # если результат совпадает на 67% с поиском if compare(found_place, search) >= 67: result = g break elif index+1 >= len(found_place.split(' ')): break else: break if result and result.json['quality'] in ['country', 'province', 'locality', 'area']: return {'text': search, 'type': 'place', 'address': result.json['address']} return None
def geolocate(self, address, try_all=True): if address in self.geocache: return self.geocache[address] try: # should be installed from default skills from astral.geocoder import database, lookup # see https://astral.readthedocs.io/en/latest/#cities a = lookup(address, database()) self.geocache[address] = (a.latitude, a.longitude) return a.latitude, a.longitude except: pass # use online geocoder location_data = geocoder.osm(address) if not location_data.ok: location_data = geocoder.geocodefarm(address) if try_all: # more are just making it slow if not location_data.ok: location_data = geocoder.google(address) if not location_data.ok: location_data = geocoder.arcgis(address) if not location_data.ok: location_data = geocoder.bing(address) if not location_data.ok: location_data = geocoder.canadapost(address) if not location_data.ok: location_data = geocoder.yandex(address) if not location_data.ok: location_data = geocoder.tgos(address) if location_data.ok: location_data = location_data.json lat = location_data.get("lat") lon = location_data.get("lng") self.geocache[address] = (lat, lon) return lat, lon raise ValueError
def try_geocode(stops): geo_stops = [] for stop in tqdm(stops, desc="Геокодинг остановок"): try: resp = gc.yandex('Москва, остановка ' + stop) if resp.ok: geo_stops.append({ "stop_name": stop, 'lat': resp.lat, 'lng': resp.lng }) stops.remove(stop) except KeyboardInterrupt: with open( os.path.join( 'data', datetime.now().strftime("%Y_%m_&d_") + 'stops_geocoded.json'), "w") as geofile: json.dump(geo_stops, geofile, ensure_ascii=False) raise except: logging.error('Could not geocode %s' % stop) return stops, geo_stops
for row in reader: full_address = " ".join([filter_text(row[int(address)]) for address in addresses]) if full_address.strip(): if heading: full_heading = filter_text(row[int(heading)]) print(full_heading) full_name = " ".join([filter_text(row[int(name)]) for name in names]) print(full_name) print(full_address) if link: full_link = "<a target=\"_blank\" href=\"{0}\">{0}</a>".format(filter_text(row[int(link)])) print(full_link) g = geocoder.google(full_address) if not g.latlng: g = geocoder.yandex(full_address) # print(g.latlng) if g.latlng: latitude, longitude = g.latlng # print(latitude, longitude) popup = "" if heading: popup += "<h3>" + full_heading + "</h3>" popup += full_name + "<br />" + full_address if link: popup += "<br />" + full_link markers.append(javascript.substitute(lat=latitude, lng=longitude, popup=popup )) outstring = html.substitute(markers="\n".join(markers)) with open(args.outfile, "w") as output: output.write(outstring)
from os import path from ruamel import yaml import geocoder data_folder = path.join(path.dirname(__file__), "../data") data_file = path.join(data_folder, "universities.yml") with open(data_file, encoding="utf-8") as points_file: universities = yaml.load(points_file, yaml.RoundTripLoader) for n, university in enumerate(universities): location = university.get("location") + ", " + university.get("title") coordinates = geocoder.yandex(location, kind=None).json if coordinates is None or len(coordinates) == 0: text = "Location not found: " + location if university.get("geo") is not None: print(text + " (saved previous)") continue else: raise Exception(text) if coordinates["ok"] is not True: raise Exception("Location not resolved: ", location) new_geo = {"lat": coordinates.get("lat"), "lng": coordinates.get("lng")} if university.get("geo") is None: university['geo'] = new_geo if university['geo'] != new_geo:
def test_yandex(): g = geocoder.yandex(location) assert g.ok
def test_multi_results(): g = geocoder.yandex(location, maxRows=3) assert len(g) == 3
def test_yandex_reverse(): g = geocoder.yandex(coordinates, method='reverse') assert g.ok
with open(path.join(data_folder, "cities.yml")) as cities_file: cities_array = ruamel.yaml.load(cities_file, ruamel.yaml.RoundTripLoader) cities = {} for city in cities_array: cities[city['name']] = city['geo'] for event in events: if 'location' not in event: print("Location of '" + event['title'] + "' is not specified") continue location = event['location'] if location in cities: continue geocoded_coordinates = geocoder.yandex(location).json if len(geocoded_coordinates) == 0: print("Location not found: ", location) exit(-1) for city in cities_array: coordinates = city['geo'] if abs(coordinates['lat'] - float(geocoded_coordinates['lat'])) < 0.1 and abs(coordinates['lng'] - float(geocoded_coordinates['lng'])) < 0.1: is_same = None while is_same is None: user_input = input("Are " + location + " and " + city['name'] + " the same place? (y/n)") if user_input == "y" or user_input == "yes": is_same = True elif user_input == "n" or user_input == "no": is_same = False else: print("Please, enter 'y' or 'n'")
def get_location_yandex(address): # location = yandex.geocode(address) location = geocoder.yandex(address) if location.status != 'OK': raise Exception('Status code is not OK', location.status, address) return location
def test_yandex_reverse(): g = geocoder.yandex(istanbul, method='reverse') assert g.ok