def bbox_to_square(bbox): """bbox is lat-lon, wkt-poly is lon-lat """ sw_point = bbox[0] ne_point = bbox[1] se_point = gl.from_degrees(sw_point.deg_lat, ne_point.deg_lon) nw_point = gl.from_degrees(ne_point.deg_lat, sw_point.deg_lon) return (sw_point, nw_point, ne_point, se_point, sw_point)
def get_bbox(current_location): print 'no' latitude, longitude = current_location print 'no' loc = GeoLocation.from_degrees(float(latitude), float(longitude)) print 'no' distance = 1 # 1 kilometer print 'no' SW_loc, NE_loc = loc.bounding_locations(distance) print 'no' print SW_loc print NE_loc print 'no' sw_string = SW_loc.__str__() sw_degrees = sw_string.split('=')[0] sw_degrees = sw_degrees[1:-1] south = sw_degrees[:7] west = sw_degrees[-12:-4] print 'no' ne_string = NE_loc.__str__() ne_degrees = ne_string.split('=')[0] ne_degrees = ne_degrees[1:-1] print 'no' north = ne_degrees[:7] east = ne_degrees[-12:-4] print 'no' return "{},{},{},{}".format(west, south, east, north)
def test_deg_to_rad(self): # Test degree to radian conversion loc1 = GeoLocation.from_degrees(26.062951, -80.238853) loc2 = GeoLocation.from_radians(loc1.rad_lat, loc1.rad_lon) assert (loc1.rad_lat == loc2.rad_lat and loc1.rad_lon == loc2.rad_lon and loc1.deg_lat == loc2.deg_lat and loc1.deg_lon == loc2.deg_lon)
def generate_bbox_around_point(point, distance): #distance in km point_lat = point[0] point_lon = point[1] loc = GeoLocation.from_degrees(point_lat, point_lon) SW_loc, NE_loc = loc.bounding_locations(distance) return [SW_loc.deg_lat, SW_loc.deg_lon, NE_loc.deg_lat, NE_loc.deg_lon]
def __init__(self, lat, lon, meters=1000, description=None): self.lat = lat self.lon = lon self.meters = meters self.description = description self.point = GeoLocation.from_degrees(lat, lon) self.state = None
def test_geolocation(self): loc1 = GeoLocation.from_degrees(26.062951, -80.238853) loc2 = GeoLocation.from_radians(loc1.rad_lat, loc1.rad_lon) self.assertTrue(loc1.rad_lat == loc2.rad_lat and loc1.rad_lon == loc2.rad_lon and loc1.deg_lat == loc2.deg_lat and loc1.deg_lon == loc2.deg_lon) # Test distance between two locations loc1 = GeoLocation.from_degrees(26.062951, -80.238853) loc2 = GeoLocation.from_degrees(26.060484, -80.207268) self.assertTrue(loc1.distance_to(loc2) == loc2.distance_to(loc1)) # Test bounding box loc = GeoLocation.from_degrees(26.062951, -80.238853) distance = 1 # 1 kilometer SW_loc, NE_loc = loc.bounding_locations(distance) self.assertTrue(SW_loc.deg_lon < NE_loc.deg_lon and SW_loc.deg_lat < NE_loc.deg_lat)
def set_min_radius(self, radius=None): # Radius in Km if radius is None: return bbox = GeoLocation.from_degrees( self.latitude(), self.longitude() ).bounding_locations(radius) self.south = min(self.south, bbox[0].deg_lat) self.north = max(self.north, bbox[1].deg_lat) self.west = min(self.west, bbox[0].deg_lon) self.east = max(self.east, bbox[1].deg_lon)
def circToBB(self,lon,lat,radius,unit="kilometers"): loc = GeoLocation.from_degrees(lat,lon) sw, ne = loc.bounding_locations(radius,unit) bb = { 'lon_min': sw.deg_lon, 'lon_max': ne.deg_lon, 'lat_min': sw.deg_lat, 'lat_max': ne.deg_lat } return bb
def check(self, item): lat = item['lat'] lon = item['lon'] tid = item['tid'] topic = item['topic'] try: here = GeoLocation.from_degrees(lat, lon) for wp in self.wplist: trigger = None meters = 0 try: meters = here.distance_to(wp.point) * 1000. except: pass km = "{:.2f}".format( float(meters / 1000.0) ) movement = { 'km' : km, 'tid' : tid, 'topic' : topic, 'event' : None, } very_near = meters <= wp.meters if very_near is False: if topic in self.tlist: if self.tlist[topic] == wp: trigger = TRIGGER_LEAVE self.alert(wp, trigger, item, meters, km) del self.tlist[topic] self.tlist.sync() if very_near is True: if topic not in self.tlist: trigger = TRIGGER_ENTER self.tlist[topic] = wp self.tlist.sync() self.alert(wp, trigger, item, meters, km) else: # Optional: # msg = "%s STILL AT %s (%s km)" % (topic, self.tlist[topic], km) # print msg return except: raise pass
def get_hospital_records_within_distance(zip_code, distance_in_kilometers): url = 'https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Hospitals_1/FeatureServer/0/query' lon, lat = get_user_long_lat(zip_code) loc = GeoLocation.from_degrees(lat, lon) SW_loc, NE_loc = loc.bounding_locations(distance_in_kilometers) params = { 'where': f'(LATITUDE BETWEEN {SW_loc.deg_lat} AND {NE_loc.deg_lat}) AND (LONGITUDE BETWEEN {SW_loc.deg_lon} AND {NE_loc.deg_lon})', 'f': 'json', 'outFields': '*', 'returnGeometry': 'false' } r = requests.get(url, params=params) if r: return r.json()['features'] else: raise ValueError( f"Could not complete request! Responded with code {r.status_code}")
def gen_bbox(lat, lon, distance): center = gl.from_degrees(lat, lon) SW_loc, NE_loc = center.bounding_locations(distance) return (SW_loc, NE_loc)
def find_bounds(lat, lng, d): loc = GeoLocation.from_degrees(lat, lng) SW_loc, NE_loc = loc.bounding_locations(d) return (SW_loc.deg_lat, SW_loc.deg_lon, NE_loc.deg_lat, NE_loc.deg_lon)
def test_bounding_locations_invalid_distance(self): loc = GeoLocation.from_degrees(0, 0) self.assertRaises(ValueError, loc.bounding_locations, 100, -5)
def test_bounding_locations_invalid_radius(self): loc = GeoLocation.from_degrees(0, 0) self.assertRaises(ValueError, loc.bounding_locations, -1)
def test_eq(self): loc = GeoLocation.from_degrees(26.062951, -80.238853) loc2 = GeoLocation.from_degrees(26.062951, -80.238853) assert loc == loc2
def test_distance(self): # Test distance between two locations loc1 = GeoLocation.from_degrees(26.062951, -80.238853) loc2 = GeoLocation.from_degrees(26.060484, -80.207268) assert loc1.distance_to(loc2) == loc2.distance_to(loc1)