def get_location(location_str): locator = Nominatim(user_agent='myGeocoder') location = locator.geocode(location_str) if not location: locator = MapQuest(MAPQUEST_API_KEY) location = locator.geocode(location_str) return location
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 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 recover_adress(df, year): locator = Nominatim(user_agent="myGeocoder") address_dict = {} add = [] for latitude, longitude in df[df["address_known"] == False][[ "latitude", "longitude" ]].itertuples(index=False): if not math.isnan(latitude): add.append((latitude, longitude)) add = set(add) with tqdm(total=len(add)) as bar: for latitude, longitude in add: bar.set_description( f"processing addresses lat={str(latitude)}, lon={longitude}") try: addresse = str(latitude) + ", " + str(longitude) address_dict[(latitude, longitude)] = locator.reverse(addresse) except Exception as e: print( "Could not find addresse for coordinates ", str(latitude), str(longitude), e, ) bar.update(1) for k, v in address_dict.items(): try: address_dict[k] = v.raw except: print("There was an issue with:", k, v) with open(f"address_{year}.pickle", "wb") as f: pickle.dump(address_dict, f) return address_dict
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 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 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 make_map(filename_without_ext, addresses, gmic_effect="fx_freaky_bw 90,20,0,0,0,0", no_display=False, prefix="LETTER"): m = StaticMap(800, 600, 80) g = gmic.Gmic("update") locator = Nominatim(user_agent="BerlinWallBreaker Agent") print("geocoding..") for address in addresses: loc = locator.geocode(address) icon_flag = IconMarker((loc.longitude, loc.latitude), './samples/icon-flag.png', 12, 32) m.add_marker(icon_flag) print(loc) image = m.render() output_filename1 = prefix + filename_without_ext + "_original.png" output_filename2 = prefix + filename_without_ext + ".png" image.save(output_filename1) print(output_filename1) if not no_display: g.run(output_filename1 + " display") if "_fx_stylize" in gmic_effect: gmic.run(output_filename1 + " " + gmic_effect + " output[2] " + output_filename2 + (" display[2]" if not no_display else "")) else: gmic.run(output_filename1 + " " + gmic_effect + " output " + output_filename2 + (" display" if not no_display else "")) print(output_filename2) return output_filename1, output_filename2
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 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 _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 createHTML(memberArray): #testPlace = memberArray[1].getPlaces() print("Creating your HTML heat map, please be patient...") lats = [] longs = [] gmap = gmplot.GoogleMapPlotter(37.616063, -76.238841, 5) # Center the Map on Northeast gmap.apikey = "API-KEY-GOES-HERE" # Hard-code my Google API code for this program for member in memberArray: # Iterate through the members passed testPlace = member.getPlaces() for placeDict in testPlace.values(): # First layer of dict is key: index | value: date/place Dict for date, place in placeDict.items(): # Second layer is key: date | value: location string if place is not None: geolocator = Nominatim(user_agent="familyTree") # Initialize geolocator location = geolocator.geocode(place, timeout=1000) # Specify timeout to avoid excessive failure if location is not None: print("Grabbing ", location, end=" ") # Let user know something is happening lat, long = location.latitude, location.longitude # Take lats and longs print(lat, long) lats.append(lat) longs.append(long) markerStr = str(member) + " Date at this location: " + str(date) + " " + place # Comment this out if you don't want markers #gmap.marker(lat, long, 'cornflowerblue', title=markerStr) gmap.heatmap(lats, longs) # Create heatmap gmap.draw("my_map.html") # Write HTML to file print("HTML document completed!")
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 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 coordi(message): if message.text == 'Get location': x = round(uniform(51.262, 56.172), 4) y = round(uniform(23.178, 32.777), 4) x, y = str(x), str(y) coord = '{},{}'.format(x, y) geolocator = Nominatim(user_agent='geobot') location = geolocator.reverse(coord) r = location.raw adr = r.get('address') cc = adr.get('country_code') loc = location.address dist = distance.distance(MINSK, coord) dist = str(dist) dist = dist.split(' ') dist = round(float(dist[0]), 2) dist = 'Distance between Minsk and this place: {} km.\nIf you want to know distance between you and this place ' \ 'send your location to bot'.format(dist) link = 'https://www.google.com/maps/place/{}'.format(coord) if cc == 'by': place.clear() place.append(coord) bot.send_message(message.chat.id, "Why don't you go to:\n{}".format(loc)) bot.send_message(message.chat.id, link) bot.send_message(message.chat.id, dist) else: print('None BY') coordi(message)
def get_location_data(lat, lon): # Nominatim takes combined lat and lon in one argument input_location = str(lat + ', ' + lon) # user_agent can be any email address geolocator = Nominatim(user_agent="*****@*****.**") # actual call to get location data location = geolocator.reverse(input_location) # the location includes a ton of data - getting the raw data to provide # more generic information address = location.raw['address'] display_location = ", ".join([ address.get('city', ''), address.get('state', ''), address.get('country', '') ]) # timezone processing requires lat and lon to be separate floats, though latitude, longitude = float(lat), float(lon) tz = pytz.timezone(TimezoneFinder().timezone_at(lng=longitude, lat=latitude)) return display_location, tz
def create(self, validated_data): print(validated_data) geolocator = Nominatim() # location_id = int(validated_data.pop('location_id', None)) lng = validated_data.pop('lng', None) lat = validated_data.pop('lat', None) street_address = validated_data.pop('street_address', None) #owner_id = int(validated_data.pop('user_id', None)) if lat > 0: location = geolocator.reverse("{}, {}".format(lng, lat)) street_address = location.address street_address = street_address.split(', ')[0] + ", " + street_address.split(', ')[1] print(street_address) else: location = geolocator.geocode(street_address) lat = location.latitude lng = location.longitude data = {'lat': lat, 'lng': lng, 'street_address': street_address} location = Location.objects.create(**data) # limitations_serializer = LimitationSerializer(data={'authority_id':10,'reason':'smth','location_id':location._get_pk_val()}, many=True) # print(limitations_serializer.initial_data) # # if limitations_serializer.is_valid(): # # print(1) # limitations = limitations_serializer.save() # print(limitations) # else: # print(limitations_serializer.errors) # #limitations_data = self.context.get('view').request.FILES # print(limitations.__dir__) return location
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()) point.setlat(json.getlatitude()) 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 getLocationsCoordinates(locations): from geopy import Nominatim from geopy.exc import GeocoderTimedOut import time geolocator = Nominatim() geoPoints = [] print("Starting converting locations to coordinates") for loc in locations: if loc in locationDict: #check if not found before if locationDict[loc] != [-999, -999]: geoPoints.append(locationDict[loc]) else: time.sleep(3) try: geoLoc = geolocator.geocode(loc) #continue if not found, but fill dictionary entry if not geoLoc: locationDict[loc] = [-999, -999] continue locationDict[loc] = [geoLoc.longitude, geoLoc.latitude] print(locationDict[loc]) geoPoints.append(locationDict[loc]) except GeocoderTimedOut as e: #limit the requests, sleep thread for a bit #in case Nominatim services denies our query print("Sleeping for 15 minutes") time.sleep(15 * 60) return geoPoints
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 tv_map(request): print(request.GET) # google_client = GoogleDriveClient() # dirs = google_client.list_dir() # print(dirs) # for dir in dirs: # images = (google_client.list_dir(dir['id'])) # if len(images) > 0: # fd = open(images[0]['name'], 'w') # google_client.download_file(images[0]['id'], fd) # local_path = request.GET.get('path') local_path = "tv/places" # insert the path to your directory image_list = [] place_list = os.listdir(STATIC_FOLDER + local_path) geolocator = Nominatim(user_agent="hengji") for place in place_list: images = os.listdir(os.path.join(STATIC_FOLDER + local_path, place)) if len(images) > 0: tmp_image = ImageHolder() tmp_image.path = local_path + '/' + place tmp_image.filename = images[0] tmp_image.city = place location = geolocator.geocode(tmp_image.city) tmp_image.latitude = location.latitude tmp_image.longitude = location.longitude image_list.append(tmp_image) print(tmp_image.filename, tmp_image.path, tmp_image.get_image_path()) return render_to_response('tv/map.html', {'images': image_list})
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 compareTeams(team, data): starters = getStarters(data[team]["players"]) hometowns = list(map(lambda x: x["hometown"], starters)) geolocator = Nominatim(user_agent="CBBScraping") school = geolocator.geocode(searchfor(team), addressdetails=True, timeout=10) if school.raw["address"]["country_code"] != "us": print(school) locs = list(map(lambda x: geolocator.geocode(x, timeout=10), hometowns)) avgLat = ( reduce( lambda x, y: x + y, list(map(lambda x: x.latitude if x != None else 39.86, locs)), ) / 5 ) avgLon = ( reduce( lambda x, y: x + y, list(map(lambda x: x.longitude if x != None else -98.6, locs)), ) / 5 ) avgLocation = (avgLat, avgLon) schoolLocation = (school.latitude, school.longitude) score = distance.distance(avgLocation, schoolLocation).miles return score
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 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 geocoding(location): """ Geocodes location to coordinates """ geocoder = Nominatim(user_agent='Programming project') time.sleep(1) loc = geocoder.geocode(location) return loc.latitude, loc.longitude
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 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 _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 get_city_on_coord(lat, lon): geolocator = Nominatim(user_agent='test/1') location = geolocator.reverse(f'{lat},{lon}', language='en') loc_dict = location.raw if 'city' in loc_dict['address'].keys(): return loc_dict['address']['city'] else: return loc_dict['address']['municipality']
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 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 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 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 doAction(self,r,sr,words,sensorList): os.system("espeak -s 120 'searching'") lastFix = sensorList['location'].getLastData() if lastFix["lastFix"] == None: return "I don't know where you are" if("how high up" in words or "altitude" in words): return "you are " + str(int(float(lastFix["lastFix"]["alt"]))) + " meters up." geoloc = Nominatim() coor = str(lastFix["lastFix"]['lat'])+", "+str(lastFix["lastFix"]['lon']) loc = geoloc.reverse(coor,timeout=10) loc1 = loc.address.split(", ") del loc1[-4:] return "you are at " + str(loc1)
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. 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 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 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 doAction(self,r,sr,words,sensorList): if "how's the weather in" in words: location = words.split("how's the weather in")[-1].strip(" ") else: os.system("espeak -s 120 'searching'") lastFix = sensorList['location'].getLastData() if lastFix["lastFix"] == None: return "I don't know where you are" geoloc = Nominatim() coor = str(lastFix["lastFix"]['lat'])+", "+str(lastFix["lastFix"]['lon']) loc = geoloc.reverse(coor,timeout=10) loc1 = loc.address location = loc1.split(", ")[-2] location = pywapi.get_location_ids(location) if len(location) == 0: return "location not found" locationId = next (iter (location.keys())) w = pywapi.get_weather_from_weather_com(locationId, units="imperial") return "It is " + w["current_conditions"]["temperature"] + "degrees and " + w["current_conditions"]["text"]
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 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 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
'017E': 200000, } 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)
# # Run this from the _talks/ directory, which contains .md files of all your talks. # This scrapes the location YAML field from each .md file, geolocates it with # geopy/Nominatim, and uses the getorg library to output data, HTML, # and Javascript for a standalone cluster map. # # Requires: glob, getorg, geopy import glob import getorg from geopy import Nominatim 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]
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")
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_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
sys.exit(2) 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)
import csv from geopy import Nominatim geolocator=Nominatim() ##corpus_path='/home/ahmad/Documents/kerja2 (data)/tes.csv' #def getFileToGeocode(): # geoCSV = csv.reader(open(corpus_path,'r')) # geoHeadings = geoCSV.next() # 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__":
zip_dict[z.replace(" ", "")] = city with open('data/crashdata/chicago_crashes.json') as file: data = json.load(file) with open('data/routes.kml') as k_file: kml_str = k_file.read() k = kml.KML() k.from_string(kml_str) print(len(k._features)) geolocator = Nominatim() accidents = data['accidents'] # hood_info will store whatever we want to know about a given neighborhood # ex: ' # {hood: [[incidents], total_incedents, severe, ...] # we can continue adding info to the dict hood_info = defaultdict(list) i = 0 for v in accidents: # if i == 50: # break try: zippy = \ geolocator.reverse(str(v['lat']) + ', ' + str(v['lng'])).raw['display_name'].split('Illinois')[1].split( ',')[1].replace(" ", "") except Exception:
def get_geo_location(lat, long): geo = Nominatim() string = "{0} , {1}".format(lat, long) geo_location = geo.reverse(string, timeout=5) return geo_location.address
"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
def get_zip(next_crime_raw): geolocator = Nominatim() location = geolocator.reverse(str(str(next_crime_raw['lat']) + ", " + str(next_crime_raw['lon']))) return location.raw['address']['postcode']
med_events= ['Request EMS and BPD Response', 'UNABLE TO DETERMINE IF CONS/MOVING (E) (F) (P)', 'UNCONSCIOUS PERSON (E) (F) (P)', 'CARDIAC EVENT','ASSIST EMS OFFICIALS ONSCENE (E) (P)','BURNS','PERSON STABBED (P) (E)','MOTOR VEHICLE ACCIDENT','INJURY'] pol_stations=['District A-1 Police Station', 'District D-4 Police', 'District E-13 Police Station', 'District B-3 Police Station', 'District E-18 Police Station', 'District D-14 Police Station', 'Boston Police Headquarters', 'District A-7 Police Station', 'District C-6 Police Station', 'District B-2 Police Station', 'District E-5 Police Station', 'District C-11 Police Station'] hos_stations=['Beth Israel Deaconess Medical Center East Cam', 'Boston City Hospital', 'Boston Specialty & Rehabilitation Hospital', 'Boston Medical Center', "Brigham And Women's Hospital", 'Carney Hospital', "Children's Hospital", 'Dana-farber Cancer Institute', 'Faulkner Hospital', "Franciscan Children's Hospital", 'Kindred Hospital', 'Jewish Memorial Hospital', 'Lemuel Shattuck Hospital', 'Massachusetts Eye & Ear Infirmary', 'Massachusetts General Hospital', 'New England Baptist Hospital', 'Beth Israel Deaconess Medical Center West Cam', 'New England Medical Center', "St. Elizabeth's Hospital", "St. Margaret's Hospital For Women", 'Shriners Burns Institute', 'Spaulding Rehabilitation Hospital', 'Arbour Hospital', 'Va Hospital', 'VA Hospital', 'Hebrew Rehabilitation Center'] repo.dropPermanent("PS_EVENTS") repo.dropPermanent("HS_EVENTS") repo.createPermanent("PS_EVENTS") repo.createPermanent("HS_EVENTS") print('CREATING PS_EVENTS') geolocator = geopy.geocoders.GoogleV3() geolocator = Nominatim() for x in pol_events: ins=repo.tbeaudry.P_911.find({'cad_event_type':x}) 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