def test_warning_with_no_api_key(self): """ Yandex warns if no API key is present """ with warnings.catch_warnings(record=True) as w: Yandex() self.assertEqual(len(w), 1)
def tracks_info(self, request, pk): result = [] started_at = request.query_params.get('started_at') finished_at = request.query_params.get('finished_at') tracks = Track.objects.filter(vehicle=pk, started_at__gt=started_at, finished_at__lt=finished_at) geolocator = Yandex(api_key='3a9de515-8d7f-49d0-a0eb-24f7d5d13533') for track in tracks: start_place_coords = GEOSGeometry(track.route[0]).coords[::-1] finished_place_coords = GEOSGeometry(track.route[-1]).coords[::-1] start_place = geolocator.reverse(start_place_coords, kind='locality') finished_place = geolocator.reverse(finished_place_coords, kind='locality') result.append({ 'started_at': track.started_at, 'finished_at': track.finished_at, 'started_place': start_place.address if start_place is not None else 'Неизвестное местоположение', 'finished_place': finished_place.address if finished_place is not None else 'Неизвестное местоположение' }) return Response(result)
def __init__(self): self.url = "http://www.planecrashinfo.com" self.subdomain = "/database.htm" self.data = [] self.geolocator = Yandex() self.reason_classifier = ( ReasonClassifier("../train/summary_train_set.txt"))
async def test_failure_with_invalid_api_key(self): async with self.inject_geocoder(Yandex(api_key='bad key')): with pytest.raises(GeocoderInsufficientPrivileges): await self.geocode_run( {"query": "площадь Ленина Донецк"}, {} )
def write_to_db_one(data_list,start_number): counter = start_number for item in data_list: if counter <= (start_number - 1 + 25000): try: db_session.query(Flat.href).filter(Flat.href==item["href"]).first()[0] except TypeError: item_type = item["type"] date = item["date"] metro_station = item["metro_station"] object_address = item["obj_address"] rooms = item["rooms"] area = item["area"] floor = item["floor"] href = item["href"] source = item["source"] name = item["name"] try: geolocator = Yandex() location = geolocator.geocode(object_address) object_latitude = location.latitude object_longitude = location.longitude except (GeocoderTimedOut): geolocator = GoogleV3() location = geolocator.geocode(object_address) object_latitude = location.latitude object_longitude = location.longitude db_item = Flat(item_type, date, metro_station, object_address, rooms, area, floor, href, source, name, object_latitude, object_longitude) db_session.add(db_item) db_session.commit() counter += 1 else: break
def test_failure_with_invalid_api_key(self): """ Yandex.geocode with incorrect api_key """ self.geocoder = Yandex(api_key='bad key') with self.assertRaises(GeocoderInsufficientPrivileges): self.geocode_run({"query": "площадь Ленина Донецк"}, {})
def get_03_uhouse(city=None, street=None, house=None, geocoder=None): pref_addr = ['РФ'] #, 'Нижний Новгород'] if city: pref_addr.append(city) else: pref_addr.append('Нижний Новгород') if street: pref_addr.append(street) if house: pref_addr.append(str(house)) # print ', '.join(pref_addr) if len(pref_addr) < 2: return 10 if geocoder == 'Yandex': from geopy.geocoders import Yandex geolocator = Yandex() else: from geopy.geocoders import Nominatim geolocator = Nominatim() try: location = geolocator.geocode( ', '.join(pref_addr)) #, exactly_one = False) if location == None: return None address = location.address pos = (location.latitude, location.longitude) if location.raw: return pos, address, location.raw['name'] else: return pos, address except geopy.exc.GeocoderServiceError: return 11
def get_location(city=None, street=None, house=None, geocoder=None): pref_addr = ['РФ'] #, 'Нижний Новгород'] if city: pref_addr.append(city) else: pref_addr.append('Нижний Новгород') if street: pref_addr.append(street) if house: pref_addr.append(str(house)) print ', '.join(pref_addr) if len(pref_addr) < 2: return 10 if geocoder == 'Yandex': from geopy.geocoders import Yandex geolocator = Yandex() else: from geopy.geocoders import Nominatim geolocator = Nominatim() try: location = geolocator.geocode( ', '.join(pref_addr)) #, exactly_one = False) if location == None: return None if type(location) == list: print 'len location list:', len(location) for l in location: print '\t', l.address print '\t', (l.latitude, l.longitude) else: # print 'type(location)', type(location) print '\t', location.address print '\t', (location.latitude, location.longitude) if location.raw: # out_dict (location.raw, 'location.raw') print '\tname', location.raw['name'] except geopy.exc.GeocoderServiceError: return 11
def test_reverse_kind_param(self): self.geocoder = Yandex() self.reverse_run( { "query": (55.743659, 37.408055), "kind": "locality", "exactly_one": True }, {"address": "Москва, Россия"})
def get_location(country): try: from geopy.geocoders import Yandex geo_locator = Yandex(lang='en_US') location = geo_locator.geocode(country, timeout=10) return location except ImportError: raise ImportError('Cannot import geocoder.')
def parseAndSaveAdresses(object): geolocator = Yandex() for subObject in object: location = geolocator.geocode(subObject.addr) subObject.lat = location.latitude subObject.lon = location.longitude subObject.save() print(subObject.lat, subObject.lon) time.sleep(3)
def reverse_geolocate(lat, lon, yandex=False, try_all=True): data = {} if yandex: geolocator = Yandex(lang='en_US') location = geolocator.reverse(str(lat) + ", " + str(lon), timeout=10) if location is None or not len(location): if try_all: return reverse_geolocate(lat, lon, False, False) return data location = location[0] data["country"] = location.address.split(",")[-1].strip() try: data["city"] = location.address.split(",")[-3].strip() except: data["city"] = location.address.split(",")[0].strip() try: data["street"] = " ".join(location.address.split(",")[:-3]).strip() except: data["street"] = location.address else: geolocator = Nominatim() location = geolocator.reverse(str(lat) + ", " + str(lon), timeout=10) if location is None and try_all: return reverse_geolocate(lat, lon, True, False) data["country"] = location.address.split(",")[-1].strip() data["zip"] = None # check for zip code if location.address.split(",")[-2].strip().replace("-", "").replace( "_", "").replace(" ", "").isdigit(): data["zip"] = location.address.split(",")[-2].strip() data["state"] = location.address.split(",")[-3].strip() try: data["region"] = location.address.split(",")[-4].strip() data["city"] = location.address.split(",")[-5].strip() data["street"] = " ".join( location.address.strip().split(",")[:-5]) except: data["city"] = location.address.split(",")[-0].strip() data["region"] = data["state"] data["street"] = data["city"] else: data["state"] = location.address.split(",")[-2].strip() try: data["region"] = location.address.split(",")[-3].strip() data["city"] = location.address.split(",")[-4].strip() data["street"] = " ".join( location.address.strip().split(",")[:-4]) except: data["city"] = location.address.split(",")[-0].strip() data["street"] = location.address data["latitude"] = location.latitude data["longitude"] = location.longitude data["country"] = location.address.split(",")[-1].strip() data["address"] = location.address return data
def test_params(self): """ Yandex.geocode with api_key and lang set """ self.geocoder = Yandex( api_key= 'AGg6X1QBAAAAk0ZiFwIAUfmxqDgGv6n7bhzuCl5D4MC31ZoAAAAAAAAAAADSboKTjoZyt88aQGXUGHUdJ3JHqQ==', lang='uk_UA') self.geocode_run( {"query": "площа Леніна Донецьк"}, { 'raw': { "metaDataProperty": { "GeocoderMetaData": { "kind": "street", "text": "Україна, Донецьк, площа Леніна", "precision": "street", "AddressDetails": { "Country": { "AddressLine": "Донецьк, площа Леніна", "CountryNameCode": "UA", "CountryName": "Україна", "AdministrativeArea": { "AdministrativeAreaName": "Донецька область", "SubAdministrativeArea": { "SubAdministrativeAreaName": "Донецька міська рада", "Locality": { "LocalityName": "Донецьк", "Thoroughfare": { "ThoroughfareName": "площа Леніна" } } } } } } } }, "description": "Донецьк, Україна", "name": "площа Леніна", "boundedBy": { "Envelope": { "lowerCorner": "37.804575 48.001669", "upperCorner": "37.805805 48.002538" } }, "Point": { "pos": "37.805186 48.002104" } } }, )
def reverse_geolocate(lat, lon): geolocator = Nominatim() location = geolocator.reverse(str(lat) + ", " + str(lon), timeout=10) if location is None: geolocator = Yandex(lang='en_US') location = geolocator.reverse(str(lat) + ", " + str(lon), timeout=10) if location is None or not len(location): return {} location = location[0] return location.__dict__
def test_reverse(self): """ Yandex.reverse """ self.geocoder = Yandex() self.reverse_run( {"query": "40.75376406311989, -73.98489005863667"}, { "latitude": 40.75376406311989, "longitude": -73.98489005863667 }, )
def __init__(self): self.geocoders = [ TimedGeocoder(Nominatim()), TimedGeocoder(GoogleV3()), TimedGeocoder(Yandex()), TimedGeocoder(ArcGIS()), TimedGeocoder(GeocodeFarm()), TimedGeocoder(Photon()) ] self.next = 0 self.size = len(self.geocoders)
def getLocation(self,city, geolocator = Yandex()): try: # geolocator = Yandex(); gcode = geolocator.geocode(city) latitude = gcode.latitude longitude = gcode.longitude except: latitude = float(0) longitude = float(0) return latitude, longitude
def test_with_api_key(self): """ Yandex.geocode with api_key """ self.geocoder = Yandex(api_key=env['YANDEX_KEY']) self.geocode_run( {"query": "площадь Ленина Донецк"}, { "latitude": 48.002104, "longitude": 37.805186 }, )
def test_unicode_name(self): """ Yandex.geocode unicode """ self.geocoder = Yandex() self.geocode_run( {"query": "площадь Ленина Донецк"}, { "latitude": 48.002104, "longitude": 37.805186 }, )
def getLocation(city): try: geolocator = Yandex() # geolocator = Nominatim(); gcode = geolocator.geocode(city) latitude = gcode.latitude longitude = gcode.longitude return latitude, longitude except: return float(0), float(0)
def test_params(self): """ Yandex.geocode with lang """ self.geocoder = Yandex(lang='uk_UA') self.geocode_run( {"query": "площа Леніна Донецьк"}, { "address": "площа Леніна, Донецьк, Україна", "latitude": 48.002104, "longitude": 37.805186 }, )
def get_location(uni): a = "University of" b = "University" geolocator = Yandex() location = geolocator.geocode(uni) location2 = geolocator.geocode(a + uni) location3 = geolocator.geocode(uni + b) if location is not None: return (location) elif location2 is not None: return (location2) else: return (location3)
def geocode_address(addr, service='ArcGIS'): if service == 'Open': geolocator = Nominatim(user_agent="specify_your_app_name_here") elif service == 'Google': geolocator = GoogleV3(user_agent="specify_your_app_name_here") elif service == 'Yandex': geolocator = Yandex(user_agent="specify_your_app_name_here") elif service == 'ArcGIS': geolocator = ArcGIS(user_agent="specify_your_app_name_here") else: return False g2 = geolocator.geocode(addr) return g2.raw
def reverse_geocode(coords, service='ArcGIS'): if service == 'Open': geolocator = Nominatim(user_agent="specify_your_app_name_here") elif service == 'Google': geolocator = GoogleV3(user_agent="specify_your_app_name_here") elif service == 'Yandex': geolocator = Yandex(user_agent="specify_your_app_name_here") elif service == 'ArcGIS': geolocator = ArcGIS(user_agent="specify_your_app_name_here") else: return False g2 = geolocator.reverse(f'{float(coords[0])}, {float(coords[1])}') return g2
def geolocate(address, yandex=False, try_all=True): data = {} if yandex: geolocator = Yandex(lang='en_US') location = geolocator.geocode(address, timeout=10) if location is None and try_all: return geolocate(address, False, False) elif location is None: return {"address": address} data["country"] = location.address.split(",")[-1].strip() try: data["city"] = location.address.split(",")[-3].strip() except: data["city"] = location.address.split(",")[0].strip() try: data["street"] = " ".join(location.address.split(",")[:-3]).strip() except: data["street"] = location.address else: geolocator = Nominatim() location = geolocator.geocode(address, timeout=10) if location is None and try_all: return geolocate(address, True, False) data["country"] = location.address.split(",")[-1].strip() # check for zip code if location.address.split(",")[-2].strip().replace("-", "").replace( "_", "").replace(" ", "").isdigit(): data["zip"] = location.address.split(",")[-2].strip() data["state"] = location.address.split(",")[-3].strip() try: data["region"] = location.address.split(",")[-4].strip() data["city"] = location.address.split(",")[-5].strip() except: data["city"] = location.address.split(",")[-0].strip() else: data["state"] = location.address.split(",")[-2].strip() try: data["region"] = location.address.split(",")[-3].strip() data["city"] = location.address.split(",")[-4].strip() except: data["city"] = location.address.split(",")[-0].strip() data["street"] = location.address data["address"] = location.address data["latitude"] = location.latitude data["longitude"] = location.longitude return data
def price(request): if request.method == 'POST': form = PriceForm(request.POST) if form.is_valid(): url_price = 'https://groozgo.ru/api/order/calc_price' url_drive = 'https://groozgo.ru/api/order/find_drivers' headers = {'Content-Type': 'application/json; charset=utf-8', 'X-Requested-With': 'XMLHttpRequest'} cityfrom = form.cleaned_data.get('cityfrom', None) cityto = form.cleaned_data.get('cityto', None) weight = form.cleaned_data.get('weight', None) nds = form.cleaned_data.get('nds', None) nal = form.cleaned_data.get('nal', None) from_value = "Россия, " + str(cityfrom) to_value = "Россия, " + str(cityto) geolocator = Yandex() loc_from = geolocator.geocode(str(cityfrom)) loc_to = geolocator.geocode(str(cityto)) loc_from_coord = (loc_from.latitude, loc_from.longitude) loc_to_coord = (loc_to.latitude, loc_to.longitude) distance = vincenty(loc_from_coord, loc_to_coord).meters data_price = {"distance": distance, "weight": str(weight), "from_administrative_area": cityfrom, "to_administrative_area": cityto, "mkad_distance": 23813.79, "is_refrigerator": False, "is_isotherm": False, "is_insurance": False, "insurance_sum": 0, "porters_count": "0", "destination_points": {}, "destination_points_amount": 0, "is_documents_back": False, "is_online_tracking": False, "back_doc_address": "", "from_value": from_value, "to_value": to_value} price_temp = requests.post(url_price, json=data_price, headers=headers) price = round(price_temp.json()['totalPrice']) data_drive = {"payment_info": {"with_nds": nds, "without_nds": False, "cash": nal, "cashless": True}, "services": {"isotherm": False, "refrigerator": False}, "administrative_area_from": cityfrom, "administrative_area_to": cityto, "cargo_weight": str(weight)} drive = requests.post(url_drive, json=data_drive, headers=headers) return render(request, 'ok.html', {'price': price, 'drive': drive.text}) else: form = PriceForm() return render(request, 'main.html', {'form': form})
def Index(request): # # # from django.contrib.sites.shortcuts import get_current_site # current_site = get_current_site(request) # for city in City.objects.all(): # with open('links.txt', 'a', encoding='utf-8') as file: # file.write('https://'+current_site.domain+city.get_absolute_url()+'\n') # for city in City.objects.all(): # for spec in Specialization.objects.all(): # with open('links.txt', 'a', encoding='utf-8') as file: # file.write('https://'+current_site.domain+reverse('mainapp:LawyersBySpecialization', args=[city.slug, spec.slug])+'\n') # for lawyer in Lawyer.objects.all().order_by('id'): # with open('links.txt', 'a', encoding='utf-8') as file: # file.write('https://'+current_site.domain+lawyer.get_absolute_url()+'\n') # # if request.GET.get('lat'): from django.conf import settings from geopy.geocoders import Yandex from django.http import HttpResponse, JsonResponse import json geo = Yandex(api_key=settings.YANDEX_MAP_TOKEN) lat = request.GET.__getitem__('lat') lng = request.GET.__getitem__('lng') city_name = geo.reverse((lat, lng), exactly_one=True, timeout=5, kind="locality").address.split(', ')[0] try: city_slug = City.objects.get(name=city_name).slug request.session['city'] = city_slug except: pass return HttpResponse(json.dumps( { 'city_slug': city_slug, 'city_name': city_name }, ensure_ascii=False), content_type='application/json') else: # specializations = Specialization.objects.annotate(num_subspecs = Count('related')).filter(num_subspecs__gt=0).order_by('num_subspecs').reverse() return render(request, 'index.html')
def write_to_db_one(data_list): geolocator = Yandex() for item in data_list: try: db_session.query(Flat.href).filter(Flat.href==item["href"]).first()[0] except TypeError: item_type = item["type"] date = item["date"] metro_station = item["metro_station"] object_address = item["obj_address"] rooms = item["rooms"] area = item["area"] floor = item["floor"] href = item["href"] source = item["source"] name = item["name"] location = geolocator.geocode(object_address) object_latitude = location.latitude object_longitude = location.longitude db_item = Flat(item_type, date, metro_station, object_address, rooms, area, floor, href, source, name, object_latitude, object_longitude) db_session.add(db_item) db_session.commit()
def geoloc(city, adress, hnum): geolocator = Yandex() w = str(city), str(adress), str(hnum) adress = ' '.join(w) location = geolocator.geocode(adress) return list([location.latitude, location.longitude])
# -*- coding:utf8 -*- from geopy.geocoders import Nominatim, GoogleV3, Yandex import sys import codecs import json import re import csv from sys import argv if __name__ == "__main__": # sys.stdin = codecs.getreader('utf8')(sys.stdin) sys.stdout = codecs.getwriter('utf8')(sys.stdout) geolocator = Yandex() #geolocator = GoogleV3(api_key="AIzaSyDEjxgSQ6KDanVIRs2_S2BUI_PwEvIklII") #, secret_key="YVUA6X1jWYVoyS44e7y9ic9u") #geolocator = Nominatim() pairreader = csv.reader(sys.stdin, delimiter=',', quotechar='"') for row in pairreader: id = row[0].decode('utf8') if int(id) % 12 != int(argv[1]): continue _address = row[1].decode('utf8') address = re.sub(r'^"|"$', "", _address) address = re.sub(r'^\d+\,?\s*', '', address) location = geolocator.geocode(address) #"175 5th Avenue NYC") #print location print "%s,%f,%f,\"%s\",%s" % (