def get_location_coordinates(self, key, address): # use mapquest address = self._escape_address(address) quoted_address = urllib.quote(address) url = self.base_address_url + '?key={0}&location={1}'.format(key, quoted_address) response = urllib2.urlopen(url) result = json.load(response) try: loc = result['results'][0]['locations'][0] return { 'lat': loc['latLng']['lat'], 'long': loc['latLng']['lng'], } except Exception: pass # if no location was found use nominatim geolocator = Nominatim( format_string="%s, Landkreis Sächsische Schweiz-Osterzgebirge, Sachsen, 01833, Deutschland") try: locations = geolocator.geocode(address, False) except GeocoderTimedOut: locations = None if locations: location = locations[0] return { 'lat': float(location.latitude), 'long': float(location.longitude), # 'address': location.address, # 'raw': location.raw, } return {}
def _serializeJsonToModel(self, object, json): try: object.setuid(json.getuid()) object.sethow(json.gethow()) COTTYPE = json.getgeoObject() if "-.-" in COTTYPE: ID = json.getattitude() COTTYPE = COTTYPE.replace('-.-', ID) else: pass object.settype(COTTYPE) point = object.point if json.getaddress(): locator = Nominatim(user_agent="myGeocoder") location = locator.geocode(json.getaddress()) point.setlon(location.longitude) point.setlat(location.latitude) else: point.setlon(json.getlongitude()) object.detail.contact.setcallsign(json.name) if json.gettimeout() != '': object.setstale(staletime=int(json.gettimeout())) else: object.setstale( staletime=RestAPIVariables.defaultGeoObjectTimeout) return object except AttributeError as e: raise Exception( 'a parameter has been passed which is not recognized with error: ' + str(e))
def get_data(self, geolocator="Nominatim"): if geolocator == "Nominatim": self.log.log("Geolocator chosen: Nominatim.") _geolocator = Nominatim(user_agent="tags-research-app") else: raise NotImplementedError("You can currently only use Nomatim as geolocator in this script.") try: geocoded_location = _geolocator.geocode(self.clean_location) sleep_val = randint(2,5) self.log.log(f"Sleeping for {sleep_val} seconds...") time.sleep(sleep_val) if geocoded_location is not None: data = { 'lat': geocoded_location.raw.get('lat', None), 'lng': geocoded_location.raw.get('lon', None), 'geolocator': geolocator, 'type': geocoded_location.raw.get('type', None), 'class': geocoded_location.raw.get('class', None), 'display_name': geocoded_location.raw.get('display_name', None), 'boundingbox': geocoded_location.raw.get('boundingbox', None) } else: self.error = True data = None except GeocoderTimedOut: if STRICT: raise RuntimeError(f"Location {self.clean_location} could not be downloaded. Perhaps you were disconnected?") else: self.error = True data = None return(data)
def form(request): state = request.GET.get('state', 'PA') address = request.GET.get('address', 'Liberty Bell') currency = request.GET.get("currency", "EUR") # if not state: state = request.POST.get('state', 'PA') #location = str(g.geocode(STATES_DICT[state])._point[:2]) ## Nominatim (from the Latin, 'by name') is a tool to search OSM data by name or address ## OSM stands for OpenStreetMap g = Nominatim() location = str(g.geocode(address)._point[:2]) params = { 'form_action': reverse_lazy('myapp:form'), 'form_method': 'get', 'form': InputForm({ 'state': state, 'address': address, 'currency': currency }), 'state': STATES_DICT[state], 'location': location } # 'currency' : CURRENCY_DICT[currency]} return render(request, 'form.html', params)
def post(self, request): form = LocationForm(request.POST) if form.is_valid(): street = form.cleaned_data['street'] city = form.cleaned_data['city'] country = form.cleaned_data['country'] locator = Nominatim(user_agent="geo_location") location = locator.geocode(f'{street}, {city}, {country}') try: latitude = location.latitude longitude = location.longitude except AttributeError: text = "Entered address is incorrect - please try again." return render(request, 'geo_location.html', { 'form': form, 'text': text }) else: url = f"https://developers.zomato.com/api/v2.1/geocode?lat={latitude}&lon={longitude}" request_json = requests.get( url, headers={'user-key': api.zomato_api}) zomato = request_json.json() return render( request, 'geo_location.html', { 'form': form, 'longitude': longitude, 'latitude': latitude, "zomato": zomato })
def firecheck(): if(request.method=='POST'): loc=request.form['location'] print(loc) try: fireloc=[] geolocator=Nominatim(timeout=15) print('Done1') location=geolocator.geocode(loc) p=location.longitude q=location.latitude print('Done2') conn=connect('firefighters.db') p1=conn.execute('Select * from adminfire where current_status="Ablaze"') print("Done3") q1=p1.fetchall() print("done4") for i in q1: s1=geodesic((p,q),(i[6],i[3])).km print(i) if(s1<5): fireloc.append(str(i[1])) print("done6") conn.close() print("done7") print(fireloc) return render_template('fireschecked.html',val1=fireloc) except: return render_template('checkforfire.html') return render_template('checkforfire.html')
def display_state(request, s='r'): ## http://127.0.0.1:8000/myapp/display_state/ default display CA ## http://127.0.0.1:8000/myapp/display_state/?state=CA state = request.GET.get('state', 'CA') statefp = STATEFP_DICT[state] state_name = STATES_DICT[state] ## Read geo (county) data from cb_2015_us_county_20m.shp filename = join(settings.STATIC_ROOT, 'myapp/cb_2015_us_county_20m.shp') counties = gpd.read_file(filename) counties["lname"] = counties["NAME"].str.lower() counties["STATEFP"] = counties["STATEFP"].astype(int) counties = counties[counties["STATEFP"] == statefp].set_index("lname") ft = "State: " + state_name + " Map" g = Nominatim() m = folium.Map(g.geocode(state_name)._point[:2], zoom_start=6) folium.GeoJson(counties).add_to(m) map_string = m._repr_html_().replace("width:90%;", "height:60%;float:top;", 1) return render(request, 'view_states.html', { "title": ft, "map_string": map_string })
def addressLongitude(request): if request.method == 'POST': getexcel = request.FILES['file'] excel_data = xlrd.open_workbook(file_contents=getexcel.read()) excel_sheet = excel_data.sheet_names() required_data = [] for sheetname in excel_sheet: sh = excel_data.sheet_by_name(sheetname) for row in range(sh.nrows): row_valaues = sh.row_values(row) required_data.append((row_valaues[0])) excel_data = xlsxwriter.Workbook('E:\\excelfolder\\address1.xlsx') worksheet = excel_data.add_worksheet() bold = excel_data.add_format({'bold': 1}) worksheet.write('A1', 'Address', bold) worksheet.write('B1', 'Latitude', bold) worksheet.write('C1', 'Longitude', bold) a = [] for address in required_data[1::]: locator = Nominatim(user_agent='myGeocoder') location = locator.geocode(address) a.append([address, location.latitude, location.longitude]) data = tuple(a) row = 1 col = 0 for addr, latitude, longitude in (data): worksheet.write_string(row, col, addr) worksheet.write_number(row, col + 1, latitude) worksheet.write_number(row, col + 2, longitude) row += 1 excel_data.close() return render(request, 'excel.html')
def geolocate_contacts(df): geolocator = Nominatim() df['lat'] = float('nan') df['lon'] = float('nan') min_wait = 1.0 cnt = 0 num_entries = len(df) for idx, it in enumerate(df['location'][:10]): sys.stdout.write("\rProcessing user # %d of %d\r" % (idx+1, num_entries)) sys.stdout.flush() if it: # avoids interrupting the loop by time-out errors try: # we also limit the rate of request to one per second # which is the maximum tolerated by nominatim t0 = time.clock() location = geolocator.geocode(df.location[idx].encode('utf-8'), exactly_one=True, timeout=5) t1 = time.clock() dt = min_wait - (t1 - t0) if dt > 0: time.sleep(dt) except: location = None if location is not None: df.lat[idx] = location.latitude df.lon[idx] = location.longitude cnt = cnt + 1
def find_distances(units, cities_list, travel_mode): distances_list = [] geolocator = Nominatim() for i in range(len(cities_list)): if i == len(cities_list)-1: return distances_list else: distance = 0 city1 = cities_list[i] city2 = cities_list[i+1] geocode1 = geolocator.geocode(city1, timeout=5) geocode2 = geolocator.geocode(city2, timeout=5) city1_lat = geocode1.latitude city1_long = geocode1.longitude city2_lat = geocode2.latitude city2_long = geocode2.longitude coord1 = (city1_lat, city1_long) coord2 = (city2_lat, city2_long) # Crow's Flying if travel_mode == 'default': distance = find_direct_distance(units, coord1, coord2) # A mode was specified, so we must call Google Maps API else: distance = find_distance_by_mode(units, travel_mode, coord1, coord2) distances_list.append(distance) return distances_list
def _serializeJsonToModel(self, object: event, json): try: point = object.point end = object.detail.getlink() if json.getaddress(): locator = Nominatim(user_agent="myGeocoder") location = locator.geocode(json.getaddress()) end.setpoint(f"{location.latitude}, {location.longitude}") # point.setlat(location.latitude) else: end.setpoint( f"{json.getlatitudeDest()}, {json.getlongitudeDest()}") end.setcallsign(json.getendName()) object.detail.setlink(end) object.detail.contact.setcallsign(json.getrouteName()) object.detail.link_attr.setmethod(json.getmethod) start = object.detail.getlink() start.setpoint(f"{json.getlatitude()}, {json.getlongitude()}") start.setcallsign(json.getstartName()) object.detail.setlink(start) if json.gettimeout() != '': object.setstale(staletime=int(json.gettimeout())) else: object.setstale( staletime=RestAPIVariables.defaultGeoObjectTimeout) return object except AttributeError as e: raise Exception( 'a parameter has been passed which is not recognized with error: ' + str(e))
def _serializeJsonToModel(self, object, json): # runs if emergency is on if isinstance(json, EmergencyPost): object.settype( RestEnumerations.emergencyTypes[json.getemergencyType()]) object.detail.contact.setcallsign(json.getname()) object.detail.emergency.settype(json.getemergencyType()) if json.getaddress(): locator = Nominatim(user_agent="myGeocoder") location = locator.geocode(json.getaddress()) object.point.setlon(location.longitude) object.point.setlat(location.latitude) else: object.point.setlon(json.getlongitude()) object.point.setlat(json.getlatitude()) DatabaseController().create_ActiveEmergency(object) return object # runs if emergency is off elif isinstance(json, EmergencyDelete): object.setuid(json.getuid()) DatabaseController().remove_ActiveEmergency( query=f'uid = "{object.uid}"') object.settype('b-a-o-can') object.detail.emergency.setcancel('true') return object
def get_weather(entities): location_name = None for item in entities: if item['name'] == 'location': location_name = item['value'] break if not location_name: return choice(ANSWERS['location_not_provided']) locator = Nominatim(user_agent='myGeocoder') location = locator.geocode(location_name) api_id = CONFIG['python_datas']['api_id'] url = f'https://api.openweathermap.org/data/2.5/weather?lat={location.latitude}&lon={location.longitude}&appid={api_id}' result = json.loads(requests.get(url).text) current_condition = result['weather'][0]['description'] response_string = choice(ANSWERS['weather_received']) response_string = response_string.replace('@city', location_name) response_string = response_string.replace('@weather', current_condition) return response_string
def do_geocode(place_name): # From place name to place object geocoder = Nominatim() try: return geocoder.geocode(place_name) except Exception: return do_geocode(place_name)
def addRoute(): # flash("Warning: this page won't submit anything to the database yet. We're working on it.") form = AddRouteForm() if form.validate_on_submit(): geolocator = Nominatim(user_agent="[PlaceHolder]", scheme='http') try: departure_location = geolocator.geocode(form.start.data) sleep(1.1) arrival_location = geolocator.geocode(form.destination.data) sleep(1.1) # sleep for 1 sec (required by Nominatim usage policy) except GeocoderTimedOut: flash(_("The geolocator is timing out! please try again")) return render_template('routes/addRoute.html', title='New Route', form=form) if departure_location is None: flash(_("The Start address is invalid")) return render_template('routes/addRoute.html', title='New Route', form=form) if arrival_location is None: flash(_("The destination address is invalid")) return render_template('routes/addRoute.html', title='New Route', form=form) if form.type.data == 'Passenger': # TODO: does this work with translation return redirect( url_for("routes_drive.overview", lat_from=departure_location.latitude, long_from=departure_location.longitude, lat_to=arrival_location.latitude, long_to=arrival_location.longitude, time=form.date.data)) if form.date.data is None or form.date.data < datetime.now(): flash(_("Date is invalid")) return render_template('routes/addRoute.html', title='New Route', form=form) if len(form.start.data) > 256: flash(_("Start data exceeds character limit")) return render_template('routes/addRoute.html', title='New Route', form=form) if len(form.destination.data) > 256: flash(_("Destination data exceeds character limit")) return render_template('routes/addRoute.html', title='New Route', form=form) if len(form.playlist.data) > 32: flash(_("Playlist data exceeds character limit")) return render_template('routes/addRoute.html', title='New Route', form=form) if not str(form.places.data).isdigit(): flash(_("Passenger places must be a number!")) return render_template('routes/addRoute.html', title='New Route', form=form) createRoute(form, departure_location, arrival_location) flash(_('New route added')) return redirect(url_for('main.index')) return render_template('routes/addRoute.html', title='New Route', form=form)
def addresTocoordinates(address): try: from geopy import Nominatim geolocator = Nominatim(user_agent="my-application",timeout=20) location = geolocator.geocode(address) return [location.latitude,location.longitude] except : return [-1,-1]
def search_latitude_longitude_geolocator(strcyte): from geopy import Nominatim geolocator = Nominatim(user_agent="sisnutri") try: result = geolocator.geocode(strcyte) return result except: return None
def calc_distance(loc1, loc2): #print('loc1: ' + loc1) #print('loc2: ' + loc2) d = distance.distance g = Nominatim(user_agent="webapp") _, loc1_geo = g.geocode(loc1) _, loc2_geo = g.geocode(loc2) return d(loc1_geo, loc2_geo).miles
def geocoding(location): """ Geocodes location to coordinates """ geocoder = Nominatim(user_agent='Programming project') time.sleep(1) loc = geocoder.geocode(location) return loc.latitude, loc.longitude
class GeoLocator: def __init__(self): self.geolocator = Nominatim(user_agent="geo-leo-test") def street_location_from_address(self, address): geocode_location = self.geolocator.geocode(address) return StreetLocation(geocode_location.latitude, geocode_location.longitude)
def addingLatLon(self): global df global lowerUpper latitude = [] longitude = [] geolocator = Nominatim() if(lowerUpper==0): for index, row in df.iterrows(): location = geolocator.geocode(row['Address']) latitude.append(location.latitude) longitude.append(location.longitude) else: for index, row in df.iterrows(): location = geolocator.geocode(row['address']) latitude.append(location.latitude) longitude.append(location.longitude) df['Latitude'] = latitude df['Longitude'] = longitude
def _geom_from_address(address): """Get a Shapely point geometry by geocoding an address with Nominatim. """ if not _has_geopy: raise ImportError('Geopy is required for address geocoding.') geoloc = Nominatim(user_agent=__name__) loc = geoloc.geocode(address) return Point(loc.longitude, loc.latitude)
def find_latitude_longitude(location): geolocator = Nominatim(user_agent="my_user_agent") location = geolocator.geocode(location) info = { 'address': location.address, 'latitude': location.latitude, 'longitude': location.longitude } return info
def getAlpha2Countries(self): geolocator = Nominatim(timeout=60) for location in self.locations: geo = geolocator.geocode(location) if geo is not None: loc = geolocator.reverse("{}, {}".format( geo.latitude, geo.longitude)) self.alpha2Countries.append( loc.raw['address']['country_code'].upper())
def get_coordinate(source): """ Takes a source as user location and return it's coordinates """ geolocator = Nominatim() try: coord = geolocator.geocode(source) return [coord.latitude, coord.longitude] except Exception as e: return e
def addressesTOcoordinates(addresses): try: geolocator = Nominatim(user_agent="bicing_bot") address1, address2 = addresses.split(',') location1 = geolocator.geocode(address1 + ', Barcelona') location2 = geolocator.geocode(address2 + ', Barcelona') return (location1.latitude, location1.longitude), (location2.latitude, location2.longitude) except: return None
def save (self, *args, **kwargs): if self.longitude is None or self.latitude is None: geolocator = Nominatim () full_add = str(self.street_number) + " " + self.street_name + " " + self.city + " " + self.country print (full_add) location = geolocator.geocode (full_add) if location is not None: self.longitude = location.longitude self.latitude = location.latitude super (sp_address, self).save (*args, **kwargs)
def add_locations_in_users_model_after_save(sender,instance,**kwargs): print("running post save") print(instance.location) _address=instance.location _id=instance.id print(instance) geolocator = Nominatim() location = geolocator.geocode(_address) print(location.latitude, location.longitude) user_to_be_updated=Users.objects.filter(id=_id).update(lat=location.latitude,lon=location.longitude)
def get_lat_and_log(self): if self.location and (self.latitude is None or self.longitude is None): geocoder = Nominatim() try: new_location = geocoder.geocode(self.location) self.location = new_location self.latitude = new_location.latitude self.longitude = new_location.longitude except GeocoderTimedOut: print("Error")
def coordinate_search(loc_names: set, gaz: pd.DataFrame, geolocator: Nominatim) -> Tuple[dict, dict]: """Search for location names using coordinates in offline gazetteer or oblineusing geolocator. Args: loc_names (set): unique location names gaz (pd.DataFrame): offline gazetteer geolocator (Nominatim): instantiated geolocator Returns: Tuple[dict, dict]: dictionary of latitude and dictionary of longitude """ lat_dict = {} long_dict = {} for loc_name in loc_names: # if location name known (dict value not empty), pass (no need to store values in dict, as they're known) # dict empty initially but updated each iteration of loop if lat_dict.get(loc_name) != None and long_dict.get(loc_name) != None: pass else: # try to get lat & long for given location from the offline gazetteer try: lat = gaz.loc[gaz['Location'] == loc_name, 'Latitude'] # find location name and return lat lat = str(lat.iloc[0]) # return first element long = gaz.loc[ gaz['Location'] == loc_name, 'Longitude'] # find location name and return long long = str(long.iloc[0]) # return first element except: pass # check values returned from gazetteer are not blank for lat and long. if not, add to respective dictionaries try: if lat != '' and long != '': lat_dict[loc_name] = lat long_dict[loc_name] = long else: location = geolocator.geocode(loc_name) except: pass # if neither values returned from geolocator are blank, populate values into respective dictionary try: if location.lat != '' and location.long != '': lat_dict[location] = location.lat long_dict[location] = location.long else: lat_dict[location] = "NA" long_dict[location] = "NA" except: pass return lat_dict, long_dict
def gps_coordinates(description): """ get the gps (latitude, longitude) from the description using the foursquare agent """ geolocator = Nominatim(user_agent='foursquare_agent') #getting the location location = geolocator.geocode(description) return location.latitude, location.longitude
def test_address(address_check=None): """The function tries to allocate geograpical information. Parameters ---------- address_check : str(optional) The 'address' is a string that contains a Swedish city name, postcode or other geographical information. Returns ------- location : geopy.location.Location(object) The 'place' is a geopy object that contains geographical informa- tion e.g. longitude and latitude etc. Raises ------ AttributeError If location is not found. GeopyError If geopy module encounters error. Exception If unexpeceted bug/error is encountered. """ # Logic for input if address_check == None: user_input = input("<<< Please enter an address: city in Sweden ") else: user_input = address_check # using geopy module to create locator object. geo_loc = Nominatim(user_agent="my_smhi_app") # Make sure location exists. try: location = geo_loc.geocode(user_input) except AttributeError as error: message = ("The location was not found: {} <--> " "None type return. \n{}.".format(location, error)) print(message) except GeopyError as errors: print("An unexpected error happenden with geopy:" " {}".format(errors)) except Exception as errors: print("Something happenden: {}".format(errors)) else: return location
def find_coordinates(place): """ str -> (number, number) Return coordinates of the place """ geolocator = Nominatim(user_agent="specify_your_app_name_here") location = geolocator.geocode(place) return location.latitude, location.longitude
def update(self, instance, validated_data): instance.name = validated_data.get('name', instance.name) instance.address = validated_data.get('address', instance.address) instance.hours = validated_data.get('hours', instance.hours) instance.phone_number = validated_data.get('phone_number', instance.phone_number) if validated_data.get('address', False): geolocator = Nominatim() location = geolocator.geocode(instance.address) instance.latitude = location.latitude instance.longitude = location.longitude instance.save() return instance
def save (self, *args, **kwargs): geolocator = Nominatim () full_add = str(self.street_number) + " " + self.street_name + " " + self.city + " " + self.country location = geolocator.geocode (full_add) if location is not None: self.longitude = location.longitude self.latitude = location.latitude else : self.longitude = 0 self.latitude = 0 print (full_add) super (sp_location, self).save (*args, **kwargs)
def get_queryset(self): """ Querysets: 1. lat and long (latitude and longitude): use to order by distance 2. search: if 5-digit *zip code, use for distance, otherwise search for park name 3. sport: only include parks with that sport 4. courts: exclude parks with no listed courts * Uses Nominatim from the geopy library to get latitude and longitude based on the zipcode. Default Ordering: by distance If no location provided, default location is Las Vegas, NV. Uses Geos Point objects to order by distance. :return: Parks ordered by distance """ qs = super().get_queryset() latitude = self.request.query_params.get('lat', None) longitude = self.request.query_params.get('long', None) search = self.request.query_params.get('search', None) sport = self.request.query_params.get('sport', None) courts = self.request.query_params.get('courts', None) pnt = None if sport: sport = sport.title() qs = qs.filter(court__sport=sport) if courts: qs = qs.annotate(count=Count('court')).exclude(count=0) if search: zip_code = re.match('^\d{5}$', search) if zip_code: geolocator = Nominatim() location = geolocator.geocode(search + ' NV') pnt = Geos('POINT(' + str(location.longitude) + ' ' + str(location.latitude) + ')', srid=4326) else: qs = qs.filter(name__icontains=search) if latitude and longitude: pnt = Geos('POINT(' + str(longitude) + ' ' + str(latitude) + ')', srid=4326) if not pnt: pnt = Geos('POINT(-115.13983 36.169941)', srid=4326) by_distance = qs.annotate(distance=Distance( 'location', pnt)).order_by('distance')[:20] return by_distance
def get_queryset(self): """ Querysets: 1. home: filter for only OPEN matches ordered by date of match upcoming 2. lat and long (latitude and longitude): use to order by distance 3. *zip code: use to order by distance 4. sport: only include parks with that sport 5. username: filter by only matches the user with that username participated in * Uses Nominatim from the geopy library to get latitude and longitude based on the zipcode. Default Ordering: by distance If no location provided, default location is Las Vegas, NV. Uses Geos Point objects to order by distance. :return: Matches ordered by distance """ qs = super().get_queryset() sport = self.request.query_params.get('sport', None) home = self.request.query_params.get('home', None) username = self.request.query_params.get('username', None) latitude = self.request.query_params.get('lat', None) longitude = self.request.query_params.get('long', None) zip_code = self.request.query_params.get('zip', None) if sport: sport = sport.title() qs = qs.filter(sport=sport).filter(is_open=True) if username: qs = qs.filter(players__username=username).order_by('-date') if home: qs = qs.filter(is_open=True).order_by('date') if latitude and longitude: pnt = Geos('POINT(' + str(longitude) + ' ' + str(latitude) + ')', srid=4326) qs = qs.filter(is_open=True) elif zip_code: geolocator = Nominatim() location = geolocator.geocode(zip_code + ' NV') pnt = Geos('POINT(' + str(location.longitude) + ' ' + str(location.latitude) + ')', srid=4326) qs = qs.filter(is_open=True) else: pnt = Geos('POINT(-115.13983 36.169941)', srid=4326) by_distance = qs.annotate(distance=Distance( 'park__location', pnt)).order_by('distance') return by_distance
def geocode(raw_address: dict) -> dict: """ Geocode an address with Nominatim (http://nominatim.openstreetmap.org). The returned osm_id is used as the primary key in the checkpoint table. So, if we can't geocode an address, it will won't make it into the database. """ g = Nominatim() json_address = {'postalcode': raw_address['postal_code'], 'street': raw_address['address'], 'city': raw_address['city'], 'country': 'Germany'} # TODO Must not default to Germany nothing = {'osm_id': None, 'lat': None, 'lon': None, 'display_name': None} if not all(json_address.values()): if DEBUG: print('Nominatim skipped {street} due to missing field(s).' .format(street=raw_address['address'])) return nothing else: try: response = g.geocode(json_address) except GeocoderTimedOut: print('Nominatim timed out for {street}.' .format(street=raw_address['address'])) geocoded = nothing else: if response is None: geocoded = nothing verb = 'failed to match' else: geocoded = response.raw verb = 'matched' if DEBUG: print('Nominatim {verb} {street}.' .format(verb=verb, street=raw_address['address'])) return geocoded
def query_location(self, query): def swap_coords(geojson): out = [] for x in geojson: if isinstance(x, list): out.append(swap_coords(x)) else: return geojson[1], geojson[0] return out nom = Nominatim() try: geo = nom.geocode(query=query, geometry='geojson', timeout=3).raw geojson = geo['geojson'] except (AttributeError, KeyError): raise FailedQuery('Query for {} did not return results.'.format(query)) self.log.info('Nominatim returned {} for {}'.format(geo['display_name'], query)) geojson['coordinates'] = swap_coords(geojson['coordinates']) self.location = shape(geojson)
def search_twitter(phone_number=None, keywords=None, radius='150'): global SEARCH_RADIUS SEARCH_RADIUS = radius if phone_number is None or not phone_number.isdigit(): return ValueError(['Invalid phone #']) if keywords is '' or keywords is None: return ValueError(["No keywords provided"]) streetAddr = query_white_pages(phone_number) geolocator = Nominatim() location = geolocator.geocode(streetAddr) if location is None: return ValueError(['Invalid phone #']) lat, lon = location.latitude, location.longitude tweets = [] tweet_coordinates = [] geocode = '%s,%s,%s%s' % (lat, lon, SEARCH_RADIUS, SEARCH_UNITS) keywords = keywords.split(',') for keyword in keywords: result = TWITTER.search(q=keyword, count=100, geocode=geocode) num_tweets = len(result['statuses']) for tweet in range(num_tweets): tweet_text = result['statuses'][tweet]['text'] name = result['statuses'][tweet]['user']['screen_name'] coordinates = result['statuses'][tweet]['coordinates'] if coordinates is not None: coordinates = coordinates['coordinates'] tweets.append([name, tweet_text, coordinates]) tweet_coordinates.append(coordinates) map_url = create_twitter_map(tweet_coordinates) return tweets
def transform_zip(next_crime_raw): geolocator = Nominatim() location = geolocator.reverse(str(str(next_crime_raw['lat']) + ", " + str(next_crime_raw['lon']))) zip = location.raw['address']['postcode'] location = geolocator.geocode(str(zip)) boundingbox = location.raw['boundingbox'] maxlat = float(boundingbox[1]) minlat = float(boundingbox[0]) maxlng = float(boundingbox[3]) minlng = float(boundingbox[2]) meanlat = (maxlat + minlat) / 2 meanlng = (maxlng + minlng) / 2 try: zip = re.search('^787d{2}$', zip).group(1) print('zip: ' + str(zip)) except Exception: pass return Zip(zip_code=zip, lat=meanlat, lng=meanlng, pop=20000, family_income=50000)
class AvgData(): """Class for calculating average data, wu_key and io_key , are for api keys for forecastio and wunderground""" def __init__(self, city_name, wu_key, io_key, offset): self.geolocator = Nominatim() self.city_name = self.geolocator.geocode(city_name) self.wu_key = wu_key self.io_key = io_key self.offset = offset def get_lat_long(self): lat = self.city_name.latitude long = self.city_name.longitude loc_data = [lat, long] return loc_data def temperature(self): lat = self.get_lat_long()[0] lng = self.get_lat_long()[1] wu_data = ScrapWu(self.get_lat_long()[0], self.get_lat_long()[ 1], self.offset, self.wu_key).get_temperature() io_data = ScrapIO(self.get_lat_long()[0], self.get_lat_long()[ 1], self.offset, self.io_key).get_avg_temperature() avg_temperature = int(wu_data + io_data) / 2 return avg_temperature def wind(self): """scraps wind from wunderground and forecast io and returns average daily in KpH""" wu_data = ScrapWu(self.get_lat_long()[0], self.get_lat_long()[ 1], self.offset, self.wu_key).get_wind() io_data = ScrapIO(self.get_lat_long()[0], self.get_lat_long()[ 1], self.offset, self.io_key).get_avg_wind() avg_wind = int(wu_data + io_data) / 2 return avg_wind def time(self): io_data = ScrapIO(self.get_lat_long()[0], self.get_lat_long()[ 1], self.offset, self.io_key).get_time() return io_data
g = glob.glob("*.md") geocoder = Nominatim() location_dict = {} location = "" permalink = "" title = "" for file in g: with open(file, 'r') as f: lines = f.read() if lines.find('location: "') > 1: loc_start = lines.find('location: "') + 11 lines_trim = lines[loc_start:] loc_end = lines_trim.find('"') location = lines_trim[:loc_end] location_dict[location] = geocoder.geocode(location) print(location, "\n", location_dict[location]) m = getorg.orgmap.create_map_obj() getorg.orgmap.output_html_cluster_map(location_dict, folder_name="../talkmap", hashed_usernames=False)
def get_attribute_location_spacy(doc) -> LocationAttribute: try: get_attribute_location_spacy.location_wrapper except AttributeError: get_attribute_location_spacy.location_wrapper = database_utils.LocationWrapper() print("attribute location logger name=" + logger.name) # try: # get_attribute_location_spacy.logger # except AttributeError: # import sys # from os.path import dirname, abspath # sys.path.insert(0, dirname(dirname(abspath(__file__)))) # import config # get_attribute_location_spacy.logger = config.get_logger() country_exceptions = [] country_candidates = [] city_exceptions = [] city_candidates = [] locations = [] # better to say "regions" -- continents and administrative geolocator = Nominatim() for ne in doc.ents: exceptions = [] candidates = [] for i in range(COUNT_ATTEMPTS): try: geocoder = geolocator.geocode(ne.orth_) break except geopy.exc.GeopyError as err: if i is COUNT_ATTEMPTS - 1: raise err if ne.label_ not in ['GPE', 'LOC', 'ORG'] or not geocoder: continue logger.debug(geocoder) logger.debug(geocoder.raw) logger.debug(geocoder.address) logger.debug(ne.label_) logger.debug(ne.lemma_) logger.debug(ne.orth_) logger.debug(ne.root.lower_) logger.debug(ne.root.head.lower_) if ne.label_ == 'LOC' or ne.label_ == 'ORG': # geocoder = geolocator.geocode(ne.orth_) gpe_list = get_attribute_location_spacy.location_wrapper.get_by_location(ne.orth_, RegionType['COUNTRY']) if ne.root.head.lower_ in NEGATIVES: # country_exceptions = ([] if country_exceptions is None else country_exceptions) country_exceptions.extend(gpe_list) else: # locations = ([] if locations is None else locations) locations.append(ne.orth_) country_candidates.extend(gpe_list) continue # otherwise # it is either a city (type='city' & label='GPE') # or a country (type='administrative' & label='GPE') type = geocoder.raw['type'] if type == 'city': exceptions = city_exceptions candidates = city_candidates elif type == 'administrative': exceptions = country_exceptions candidates = country_candidates else: logger.debug('TYPE:') logger.debug('Spacy type: ', ne.label_) logger.debug('Nominatim type: ', type) logger.debug('city') logger.debug('administrative') # although we separate the results, the processing is similar for both if ne.root.head.lower_ in NEGATIVES: exceptions.append(ne.orth_) else: candidates.append(ne.orth_) if country_candidates == [] \ and country_exceptions == [] \ and city_candidates == [] \ and city_candidates == [] \ and locations == []: return None # map(country_exceptions, lambda x: x.upper()) # map(country_candidates, lambda x: x.upper()) # map(city_exceptions, lambda x: x.upper()) # map(city_candidates, lambda x: x.upper()) # map(locations, lambda x: x.upper()) country_list = [x for x in country_candidates if x not in map(lambda x: x.upper(), country_exceptions)] city_list = [x for x in city_candidates if x not in map(lambda x: x.upper(), city_exceptions)] result = LocationAttribute(locations=locations, countries=country_list, cities=city_list) # get_attribute_location_spacy. logger.debug(result) return result
def add(): with open("extraction/other_crimes.out") as data_file: data = json.load(data_file) crime_data = iter(data['crimes']) for line in range(1): next_crime = next(crime_data) date = next_crime['date'] date = date[:date.find('T')] converted_year = convert_year(date) converted_month = convert_month(date) converted_day = convert_day(date) # # next_crime_formatted = Crime(lat=next_crime['lat'], lng=next_crime['lon'], time=datetime.date(year=converted_year, month=converted_month, day=converted_day, address=next_crime['address'], description=next_crime['link']) # next_crime_type = CrimeType(name=next_crime['type'], desc='crimes are bad') geolocator = Nominatim() location = geolocator.reverse("30.25, -97.75") zip = location.raw['address']['postcode'] location = geolocator.geocode(str(zip)) boundingbox = location.raw['boundingbox'] maxlat = float(boundingbox[1]) minlat = float(boundingbox[0]) maxlng = float(boundingbox[3]) minlng = float(boundingbox[2]) meanlat = (maxlat + minlat) / 2 meanlng = (maxlng + minlng) / 2 #next_zip = Zip(zip_code=zip, lat=meanlat, lng=meanlng) #next_week = Week( # crime_1 = Crime(lat=30.28500, lng=-97.7320000, time=datetime.date(year=2015, month=10, day=28), address="gdc", description="Graffiti of pig on building") # crime_2 = Crime(lat=30.30000, lng=-97.730000, time=datetime.date(year=2015, month=10, day=20), address="Duval Rd", description="Burglary at Quacks Bakery") # crime_3 = Crime(lat=30.27000, lng=-97.719000, time=datetime.date(year=2015, month=10, day=14), address="12th and Chicon", description="Murder on 12th and Chicon") crime_type_1 = CrimeType(name='Vandalism', desc = "Vandalism is bad") crime_type_2 = CrimeType(name='Burglary', desc = "Burglary is bad") crime_type_3 = CrimeType(name='Assault', desc = "Assault is bad") zip_1 = Zip(zip_code=78704, lat=32.123, lng=32.123, pop=20000, family_income=50000) zip_2 = Zip(zip_code=78705, lat=30.123, lng=30.123, pop=23000, family_income=30000) zip_3 = Zip(zip_code=78706, lat=35.123, lng=35.123, pop=19000, family_income=45000) week_1 = Week(start=datetime.date(year=2015, month=10, day=11), end=datetime.date(year=2015, month=10, day=17)) week_2 = Week(start=datetime.date(year=2015, month=10, day=18), end=datetime.date(year=2015, month=10, day=24)) week_3 = Week(start=datetime.date(year=2015, month=10, day=25), end=datetime.date(year=2015, month=10, day=31)) #set up all of the foreign key relationships print("Crime one week: " + str(week_1.start)) try: for crime in crimes: print('crime: ' + str(crime)) # session.add() # session.add(crime_1) # session.add(crime_2) # session.add(crime_3) session.add(crime_type_1) session.add(crime_type_2) session.add(crime_type_3) session.add(zip_1) session.add(zip_2) session.add(zip_3) session.add(week_1) session.add(week_2) session.add(week_3) print("commiting to database") session.commit() except Exception as e: print(e) session.rollback() print("Everything broke")
zip_code = '' satellite = '' for i in arg1: if (i[0] == '-z'): zip_code = i[1] else: satellite = i[1] result = get_tle(satellite) print '' print "Satellite Info: "+result result = result.split('\n') geolocator = Nominatim() location = geolocator.geocode(zip_code + ", United States") g = geocoder.elevation((location.latitude, location.longitude)) times = get_times(result, g.meters, location) #print times clear_times = [] print 'NOTE: all times are in UTC time' print '' #loop through each time found and check to see if visible for t in times: clear = is_weather_clear(str(location.latitude),str(location.longitude), t) night = is_night(str(location.latitude),str(location.longitude), t) if(clear[0] and night):
for y in ins: lat=0 lon =0 if 'longitude' in y: lat=y['latitude'] lon=y['longitude'] elif 'udo_event_location_full_street_address' in y: #continue here to run quick version loc=y['udo_event_location_full_street_address']+" Boston" loc2=None try: loc2=geolocator.geocode(loc) time.sleep(.1) except: print ("error with geocode") 'print (loc2)' if (loc2==None): continue lat = loc2.latitude lon = loc2.longitude for z in pol_stations: if (lat!=0 and lon!=0): pol=repo.tbeaudry.PS_LOC.find_one({'name':z}) y[z]=geopy.distance.vincenty((lat,lon), (pol['location']['latitude'],pol['location']['longitude'])).miles else: y[z]=999
# geoData = [row for row in geoCSV] # return (geoHeadings,geoData) #def addToMaster(masterData,accrow): # masterData.append(accrow) #def main(): # g = geocoders.Google() # acc = getFileToGeocode() # masterHeadings = "Place" ,"Lat/Long" # masterData=[] # for accrow in acc[1]: # print g.geocode(accrow[1]) # addToMaster(masterData,g.geocode(accrow[1])) # masterCSV = csv.writer(open('master.csv','wb'),dialect='excel') # masterCSV.writerow(masterHeadings) # for row in masterData: # masterCSV.writerow(row) #if __name__ == "__main__": # main() keyword=input() lokasi=geolocator.geocode(keyword) print lokasi.latitude
} if __name__ == "__main__": data_file = open("extraction/zip_data.out") data = json.load(data_file) for zip_code in zip_codes: zip_data = data[str(zip_code)] zipcode = session.query(Zip).from_statement(text('SELECT * FROM zip WHERE zip_code = :zip_code')).params(zip_code =zip_code).first() if zipcode is None: # Add a new zipcode print("zipcode not in table already: ", zip_code) geolocator = Nominatim() location = geolocator.geocode(str(zip_code)) # print(location.raw) boundingbox = location.raw['boundingbox'] maxlat = float(boundingbox[1]) minlat = float(boundingbox[0]) maxlng = float(boundingbox[3]) minlng = float(boundingbox[2]) meanlat = (maxlat + minlat) / 2 meanlng = (maxlng + minlng) / 2 new_zip = Zip(zip_code=zip_code, lat=meanlat, lng=meanlng, pop=20000, family_income=50000) try: session.add(new_zip) session.commit() except Exception as e: print(e) session.rollback()
"Nigeria", "Norway", "Pakistan", "Palestine", "Peru", "Poland", "Portugal", "Romania", "Russia", "Saint Lucia", "Serbia", "South Africa", "Spain", "Sweden", "Switzerland", "Taiwan", "Trinidad and Tobago", "Turkey", "Ukraine", "United Kingdom", "United States", "Venezuela", "Vietnam", "Yemen"] geolocator = Nominatim() for country in countries: try: location = geolocator.geocode(country) print country + "," + str(location.latitude) + "," + str(location.longitude) except: print country