def lookup(query, reverse=False): import geocoder place = Place() place.logger.debug(f'Looking up place (reverse:{reverse}) -- {query}') # TODO use the provider preference from the config api_key = config['mapbox']['key'] if reverse is True: loc = geocoder.mapbox(query, method='reverse', key=api_key) else: loc = geocoder.mapbox(query, key=api_key) place.logger.debug(f'> result: {loc}') place.name = loc.address place.latitude = loc.lat place.longitude = loc.lng place.city = loc.city place.state = loc.state place.country = loc.country return place
def search_location(text_to_search): global mapbox_key patterns = [ '((1?\d{0,2})(\.\d+)?)°?\s?([NSns]),?\s+((1?\d{0,2})(\.\d+)?)°?\s?([WEwe])', '(1?\d{0,2})°\s?((\d{0,2})\'\s?)?((\d{0,2}\.?\d*)\"\s?)?([NSns]),?\s+(1?\d{0,2})°\s?((\d{0,2})\'\s?)?((\d{0,2}\.?\d*)\"\s?)?([WEwe])', '(-?1?\d{0,2})°\s?((\d{0,2})\'\s?)?((\d{0,2}\.?\d*)\'\'\s?)?([NSns]),?\s+(-?1?\d{0,2})°\s?((\d{0,2})\'\s?)?((\d{0,2}\.?\d*)\'\'\s?)?([WEwe])', '((-?1?\d{1,2})(\.\d+)?),?\s+((-?1?\d{1,2})(\.\d+)?)', '([A-z]\w+),\s?([A-Z]\w+)'] match_list = [] for i, pattern_text in enumerate(patterns): pattern = re.compile(pattern_text) matches = pattern.finditer(text_to_search) for match in matches: mg = [] for j in range(len(match.groups())): try: mg.append(float(match.groups()[j])) except: if match.groups()[j]: mg.append((match.groups()[j]).lower()) else: mg.append(0) if i == 0: match_lat = mg[0]*(1 if mg[3] == "n" else -1) match_long = mg[4]*(1 if mg[7] == "e" else -1) elif i == 1: match_lat = (mg[0]+(mg[2]/60)+(mg[4]/3600))*(1 if mg[5] == "n" else -1) match_long = (mg[6]+(mg[8]/60)+(mg[10]/3600))*(1 if mg[11] == "e" else -1) elif i == 2: match_lat = (mg[0]+(mg[2]/60)+(mg[4]/3600))*(1 if mg[5] == "n" else -1) match_long = (mg[6]+(mg[8]/60)+(mg[10]/3600))*(1 if mg[11] == "e" else -1) elif i == 3: match_lat = mg[0] match_long = mg[3] else: match_lat = (geocoder.mapbox(f'{mg[0]}, {mg[1]}', key=mapbox_key).latlng)[0] match_long = (geocoder.mapbox(f'{mg[0]}, {mg[1]}', key=mapbox_key).latlng)[1] match_list.append([match_lat, match_long]) match_text = [] match_text = remove_dups([f'{match[0]}, {match[1]}' for match in match_list]) match_list = [coord.split(', ') for coord in match_text] match_list = [[float(coord[0]), float(coord[1])] for coord in match_list] match_list.append([0,0]) return match_list
def geocodes_from_addresses_mapbox(addresse_geo, API_key): """ convert addresses to latitude and longitude Mapbox Api """ geocodes = [] tomtom_geocodes = "" lat = "" long = "" g_API_key = "AIzaSyCNSyD13siR0AvcuoKMBntWw_b0xz_n_AQ" # google api for address in addresse_geo: g = geocoder.mapbox(address, key=API_key) # mapbox api to convert address to latitude and longitude r = g.json if g.ok == False: # if mapbox fail consider google api g = geocoder.google(address, key=g_API_key) # google api to convert address to latitude and longitude r = g.json if g.ok == False: # if google api fail return error message print(g.status,g.location) else: lat = r["lat"] long = r["lng"] else: lat = r["lat"] long = r["lng"] geocodes.append([lat, long]) tomtom_geocodes += "{},{}:".format(lat, long) return geocodes, tomtom_geocodes # everything is Ok return results
def async_request(location, ws, session, index, locations_list, loc_col_index, lat_col_index, long_col_index): global total, threads, mapbox_key, total_threads time.sleep((random.random() * 10)) try: arc = geocoder.mapbox(location, key=mapbox_key) l = [] # l = arc["features"]["center"] if arc.ok: # l.append(arc.json[0]) # l.append(arc.json[1]) l.append(arc.json["lat"]) l.append(arc.json["lng"]) cell = ws.cell(row=index+2, column=loc_col_index + 1) name = str(locations_list[index]) if name is not None and name != '' and name != '' and not pd.isnull(name): cell.value = capitalizeWords(str(locations_list[index])) else: cell.value = '' cell = ws.cell(row=index + 2, column=lat_col_index + 1) cell.value = l[0] cell = ws.cell(row=index + 2, column=long_col_index + 1) cell.value = l[1] else: t = Thread(target=async_request_two, args=(location, ws, session, index, locations_list, loc_col_index, lat_col_index, long_col_index)) threads.append(t) total_threads += 1 t.start() total += 1 except: t = Thread(target=async_request_two, args=(location, ws, session, index, locations_list, loc_col_index, lat_col_index, long_col_index)) threads.append(t) t.start() total += 1 total_threads += 1
def generate_text(lat, lon, config): text = ("[Google Maps](https://www.google.com/maps/search/?api=1&query=" + str(lat) + "," + str(lon) + ")") if config['geocoding']: if config['static_provider'] == "google": geocode = geocoder.google([db_poi_lat, db_poi_lon], method='reverse', key=config['static_key'], language=config['language']) elif config['static_provider'] == "osm": geocode = geocoder.mapquest([db_poi_lat, db_poi_lon], method='reverse', key=config['static_key'], language=config['language']) elif config['static_provider'] == "mapbox": geocode = geocoder.mapbox([db_poi_lat, db_poi_lon], method='reverse', key=config['static_key'], language=config['language']) else: address = "" text = (navigation + "\n" + geocode.address) return text
def geocode_get_data(address, zip_only=False): """Main function to obtain geocoding information.""" address = str(address) + ", usa" try: cached = CachedGeodata.objects.all().filter(expires__gte=time.time()).get(key=address) except ObjectDoesNotExist: if zip_only: mb_result = Mapbox(address ,types='postcode') if mb_result.latlng: result = mb_result else: result = geocoder.osm(address) else: result = geocoder.mapbox(address) cached, created = CachedGeodata.objects.get_or_create(key=address) cached.lat = result.lat cached.lon = result.lng # 1728000 is 20 days cached.expires = int(time.time()) + getattr(settings, 'DJANGO_HUD_GEODATA_EXPIRATION_INTERVAL', 1728000) cached.save() return {'zip': { 'zipcode': address, 'lat': cached.lat, 'lng': cached.lon, }}
def geocode_address(address, cached={}, mapboxtoken=None): """ Check an optional cache to see if we already have the geocoded address Otherwise, use google's API to look up the address Due to rate limiting, try a few times with an increasing wait if no address is found Args: address cached (optional) Returns: address, latitude, longitude, status """ if address in list(cached.keys()): return cached[address] if mapboxtoken: g = geocoder.mapbox(address, key=mapboxtoken) else: g = geocoder.google(address) attempts = 0 while g.address is None and attempts < 3: attempts += 1 sleep(attempts ** 2) g = geocoder.google(address) status = '' if g.status == 'OK': status = 'S' elif g.status == 'ZERO_RESULTS': status = 'F' return g.address, g.lat, g.lng, status
def get_coordinates(address): """ This function return the coordinates of an adress in France """ res = geocoder.mapbox(address + ', FRANCE', key=mpkey, maxRows=1) if len(res) > 0: return [res[0].latlng[1], res[0].latlng[0]] else: return ['na', 'na']
def geocode(self, locationType='Physical'): address_string = self.get_address_string(locationType=locationType) if len(address_string) > 1: try: results = geocoder.mapbox(address_string, key=settings.MAPBOX_TOKEN) except Exception as e: print(e) results = None if not results or len(results) < 1 or results.error: address_string = self.get_address_string( locationType=locationType, fullAddress=False) results = geocoder.mapbox(address_string, key=settings.MAPBOX_TOKEN) return results else: return []
def getStreet(latLong, key): g = geocoder.mapbox(latLong, method="reverse", key=key) g_json = g.json if g_json == None: return if 'raw' not in g_json: return if 'text' not in g_json['raw']: return # if 'raw' in g_json and 'text' in g_json['raw']: street = g_json['raw']['text'] return street
def get_state(coordinate): ''' Convert coordinate rectangles to coordinate centroids, then use geocoder to get the state. I'm using the Mapbox API to do the reverse geocoding, which involved getting an access token and setting it as an Environment Variable like so: $ export MAPBOX_ACCESS_TOKEN=<Secret Access Token> ''' lng, lat = zip(*coordinate[0]) g = geocoder.mapbox([np.mean(lat), np.mean(lng)], method='reverse') return g.state
def test_mapbox_with_bbox(): g = geocoder.mapbox(winnetka, bbox=winnetka_bbox) assert g.ok osm_count, fields_count = g.debug()[0] assert osm_count >= 2 assert fields_count >= 11 for result in g: assert (result.lng >= winnetka_bbox[0]) and (result.lng <= winnetka_bbox[2]) assert (result.lat >= winnetka_bbox[1]) and (result.lat <= winnetka_bbox[3])
def reverseLookup(coord, streets, streetsLock): g = geocoder.mapbox( coord, method='reverse', key= 'pk.eyJ1IjoiYXBwc2NsYXBwZXIiLCJhIjoiY2p2dzA4ZDZxMnEydjQ1bzF6emttaWtweiJ9.mYwgu-Jp2QpIKcy9cQY7sw' ) g_json = g.json with streetsLock: if g_json['raw']['text'] != None: streets.add(g_json['raw']['text'])
def geocoding(place): result = geocoder.mapbox(place, key=env.MAPBOX_PUB_KEY) coords = result.latlng if coords is None: return None else: if result.country == "Italy": return list(reversed(coords)) else: return None
def string_of_addrress(addr_element, API_key): """ Converts address to langitude and latitude using mapbax api """ lng_tat = '' g = geocoder.mapbox(addr_element, key=API_key) g = g.json lat = g['lat'] lng = g['lng'] lng_tat = str(lat) + ',' + str(lng) return lng_tat
def perform_create(self, serializer): address = serializer.initial_data['address'] g = geocoder.mapbox( address, key= 'pk.eyJ1IjoiZW1hZC1rYW1lbCIsImEiOiJjam40NTF6YWQybW1pM3FxdjNmZmgzNWJwIn0.BBl23kGQ_WApeC64Id07rQ' ) lat = g.latlng[0] lng = g.latlng[1] pnt = 'POINT(' + str(lng) + ' ' + str(lat) + ')' serializer.save(location=pnt)
def new_job_formaction (): if 'gcs_user' in session and session['gcs_logged_in']: date_sel = request.form.get ('date') time_sel = request.form.get ('time') datetime_sel = datetime.strptime (date_sel + ' ' + time_sel, '%Y-%m-%d %I:%M %p') drone_id = int(str (request.form.get ('drone_select'))) location_origin_lat_sel = request.form.get ('origin-lat') location_origin_lon_sel = request.form.get ('origin-long') location_dest_lat_sel = request.form.get ('dest-lat') location_dest_lon_sel = request.form.get ('dest-long') payload_id = request.form.get ('payload_select') latlng = [float (location_dest_lat_sel),float (location_dest_lon_sel)] count = int (request.form.get ('count')) g = geocoder.mapbox (latlng,method='reverse',key='pk.eyJ1IjoiY2Fub3BlZXJ1cyIsImEiOiJjandidGhuZDkwa2V2NDl0bDhvem0zcDMzIn0.g1NXF5VQiDwn66KAsr-_dw') if g.json is None: location_str_dest = "Unknown Location" else: location_str_dest = g.json['address'] latlng = [float (location_origin_lat_sel),float(location_origin_lon_sel)] g = geocoder.mapbox (latlng,method='reverse',key='pk.eyJ1IjoiY2Fub3BlZXJ1cyIsImEiOiJjandidGhuZDkwa2V2NDl0bDhvem0zcDMzIn0.g1NXF5VQiDwn66KAsr-_dw') if g.json is None: location_str_origin = "Unknown Location" else: location_str_origin = g.json['address'] job_instance = Job (datetime_sel,drone_id,location_origin_lat_sel, location_origin_lon_sel,location_dest_lat_sel,location_dest_lon_sel, location_str_dest,int (payload_id),count,location_str_origin) db.session.add (job_instance) db.session.commit () drone_sel = Drone.query.filter_by(id = drone_id).first () drone_sel.assign_job (job_instance.id) db.session.commit () return redirect ('/jobtracker',code = 302) else: return redirect ('/gcslogin',code = 302)
def _get_single_location(address: str, max_wait: int) -> t.Optional[t.Any]: result = geocoder.mapbox(address, country='us') if result.ok: return result[0] elif result.status_code == 429: # Rate limit exceeded reset_timestamp = int(result.response.headers['X-Rate-Limit-Reset']) if _wait_until_reset(reset_timestamp, max_wait): return _get_single_location(address, -1) raise Exception('Rate limit hit') else: _LOGGER.info('Failed to find {}'.format(address)) return None
def test_multi_results(): g = geocoder.mapbox(location) assert len(g) == 5 expected_results = [ 'Ottawa, Ontario, Canada', 'Ontario Court of Justice, 15 Victoria, Ottawa, Ontario K2G 3H2, Canada', 'Ontario Secondary School Teachers Federation Dis trict 25, 9 Corvus Crt, Ottawa, Ontario K2E 7Z4, Canada', 'Ontario Secondary School Teachers Federation District 25, 67 Jamie Ave, Ottawa, Ontario K2E 7Y6, Canada', 'Ontario Carlton District School Board, 60 Tiverton Dr, Ottawa, Ontario K2E 6L8, Canada', ] assert [result.address for result in g] == expected_results
def reverse_geocode(latlng): kwargs = get_reverse_kwargs() try: result = geocoder.mapbox(latlng, method='reverse', **kwargs) if result.status != 'OK': raise Exception('Over query API limit') if result and result.address: return result return None except Exception as e: logger.exception(e) return None
def geocode(unformattedAddress): address = formatAddress(unformattedAddress) mapBias = setProximity(unformattedAddress) g = geocoder.mapbox( address, proximity=mapBias, maxRows=1, key= 'pk.eyJ1IjoiYWJjb29rIiwiYSI6ImNqcnp5N3N2ZzFkeWs0NG80bnVtNmFxaDEifQ.zPkUuVuuVNGg-3DkQcBflQ' ) return g.json
def find_distance(location1, location2): location1 = str(location1) location2 = str(location2) location2_int = 0 #location1 = str(int(location1)) for digit in location2: location2_int *= 10 for d in '0123456789': location2_int += digit > d location2 = str(location2_int) #[:-2] g = open("txt.txt", "r+") g1 = geocoder.mapbox(location1, key=key) g.write(f"initialized geocoder 1 loc1-{location1} loc2-{location2} \n") bbox1 = g1.bbox g.write(f"distance func log bbox1-{bbox1} \n") lat1 = bbox1["northeast"][0] + bbox1["southwest"][0] long1 = bbox1["northeast"][1] + bbox1["southwest"][1] g2 = geocoder.mapbox(location2, key=key) bbox2 = g2.bbox g.write(f"distance func log bbox2-{bbox2} \n") lat2 = bbox2["northeast"][0] + bbox2["southwest"][0] long2 = bbox2["northeast"][1] + bbox2["southwest"][1] R = 3959.87433 # this is in miles. For Earth radius in kilometers use 6372.8 km dLat = radians(lat2 - lat1) dLon = radians(long2 - long1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) dist = R * c g.write(f"distance func log bbox1-{bbox1} bbox2-{bbox2} dist-{dist} \n") g.close() return dist
def joinEvents(): eventList = db.session.query(Event).all() firstEvent = Event.query.first() pointList = [] for event in Event.query.all(): g = geocoder.mapbox(event.area, key=ACCESS_KEY) thelng = str(g.lng) thelat = str(g.lat) theURL = 'https://www.google.com/maps?q=' + thelat + ',' + thelng + '&ll=' + thelat + ',' + thelng + '&z=13' pointList.append(theURL) print(pointList) return render_template('joinEvents.html', eventList=eventList, firstEvent=firstEvent, pointList=pointList)
def __init__(self, user, *args, **kwargs): super(AssignmentForm, self).__init__(*args, **kwargs) if user.profile.temp_address: address = user.profile.temp_address else: address = user.profile.address g = geocoder.mapbox(address, key=MB_KEY) p = Point(g.latlng[0], g.latlng[0], srid=4326) supply = Supply.objects.annotate( distance=Distance('location', p)).order_by('distance')[0:] stock = DrugStock.objects.annotate( distance=Distance('location', p)).order_by('distance')[0:] su = [(q.id, q.address) for q in supply] st = [(q.id, q.address) for q in stock] self.fields['pick_point'] = forms.ChoiceField(choices=su) self.fields['drop_point'] = forms.ChoiceField(choices=st)
def search(self, search_term, latlng, r, key, size, color, name): bbox = get_bbox(latlng, r) g = geocoder.mapbox(search_term, bbox=bbox, key=key) res = [{ "category": search_term, "category2": j.json.get("raw").get("properties").get("category"), "address": j.address, "lat": j.latlng[0], "lng": j.latlng[1], "address_short": j.address.split(",")[0], "size": size, "color": color, "legendgroup": j.address.split(",")[0], "name": j.address.split(",")[0] } for j in g] return res
def check_assignment(request): if request.method == 'POST': form = AddressForm(request.POST) if form.is_valid(): address = form.cleaned_data['address'] request.user.profile.temp_address = address request.user.profile.save() g = geocoder.mapbox(address, key=MB_KEY) p = Point(g.latlng[0],g.latlng[0], srid=4326) supply = Supply.objects.annotate(distance=Distance('location', p)).order_by('distance')[0:] stock = DrugStock.objects.annotate(distance=Distance('location', p)).order_by('distance')[0:] context = {'supply': supply, 'stock': stock} return render(request, 'service/check_assignment.html', context ) else: form = AddressForm() return render(request, 'service/find_assignment.html', {'form': form} )
def index(): eventList = db.session.query(Event).all() event_locations = create_event_markers() event_markers_image = event_markers_list pointList = [] for event in Event.query.all(): g = geocoder.mapbox(event.area, key=ACCESS_KEY) thelng = str(g.lng) thelat = str(g.lat) theURL = 'https://www.google.com/maps?q=' + thelat + ',' + thelng + '&ll=' + thelat + ',' + thelng + '&z=13' pointList.append(theURL) return render_template('map.html', eventList=eventList, event_locations=event_locations, event_markers_image=event_markers_image, pointList=pointList)
def get_search_data(self, search_term, key, initial_latlng, size, color): g = geocoder.mapbox(search_term, proximity=initial_latlng, key=key) res = [{ "category": search_term, "category2": j.json.get("raw").get("properties").get("category"), "address": j.address, "lat": j.latlng[0], "lng": j.latlng[1], "address_short": j.address.split(",")[0], "size": size, "color": color, "legendgroup": j.address.split(",")[0], "name": j.address.split(",")[0] } for j in g] latlng = [res[0]["lat"], res[0]["lng"]] return res[0:1], latlng
def get_address(location): g = None count = 0 while (not g or not g.city) and count < 10: g = geocoder.mapbox([location['lat'], location['lon']], method='reverse', key=MAPBOX_TOKEN) time.sleep(0.01) count += 1 housenumber = g.housenumber street = g.raw.get('text', '') address = "" if housenumber and street: address = "{} {}".format(housenumber, street) elif street: address = "{}".format(street) return address, g.city
def create_event_markers(): event_markers = [] for event in db.session.query(Event).all(): g = geocoder.mapbox(event.area, key=ACCESS_KEY) point = Point([g.lng, g.lat]) properties = { "description": event.desc, 'title': event.title, 'icon': 'campsite', 'marker-color': '#3bb2d0', 'event_image': event.event_image } feature = Feature(properties=properties, geometry=point) if current_user in event.subscribers.all(): event_markers.append(feature) event_markers_list.append(event.event_image) print(event.title, g.lng, g.lat) return event_markers
def run_geocode(search, country='DE', address=True): kwargs = get_kwargs() try: result = geocoder.mapbox(search, **kwargs) latlng = None address = None if result.status != 'OK': raise Exception('Over query API limit') # Here specific if address and result.raw['LocationType'] != 'address': # Only accept exact matches return if result and result.latlng: latlng = result.latlng address = result.address return latlng, address except Exception as e: logger.exception(e) return None
def test_mapbox(): g = geocoder.mapbox(location) assert g.ok osm_count, fields_count = g.debug()[0] assert osm_count >= 2 assert fields_count >= 11
json['county'] = '' city = re.search(r"\w+\s\s", line[county.end():]) if city: json['city'] = line[county.end():city.end() + county.end()].strip() address = re.search(r"\w+\s\s", line[city.end() + county.end():]) else: json['city'] = '' address = 0 if address: json['address'] = line[city.end() + county.end():city.end() + county.end() + address.end()].strip() search = json['address'] + ' ,' + json['city'] + ', Oklahoma United States' g = geocoder.mapbox(search, access_token=os.environ['MAPBOX_ACCESS_TOKEN']) try: if g.json['state'] == 'Oklahoma': json['lat'] = g.json['lat'] json['long'] = g.json['lng'] # print 'geocoded' except KeyError: json['lat'] = '' json['long'] = '' print 'exception' else: json['address'] = '' writer.writerow(json)
def test_mapbox_with_proximity(): g = geocoder.mapbox(location, proximity=ottawa) assert g.ok osm_count, fields_count = g.debug()[0] assert osm_count >= 2 assert fields_count >= 11
def test_multi_results(): g = geocoder.mapbox(location) assert len(g) == 5
def geocode4(address): g = geocoder.mapbox(address) latlng = g.latlng return latlng
def test_mapbox(): g = geocoder.mapbox(location) assert g.ok
def test_mapzen_reverse(): g = geocoder.mapbox(ottawa, method='reverse') assert g.ok