def make_grid(zone_unit_km, min_long, max_long, min_lat, max_lat, consider_visualization=False): W_end_gps, E_end_gps = (min_long, (min_lat + max_lat) / float(2)), (max_long, (min_lat + max_lat) / float(2)) S_end_gps, N_end_gps = ((min_lat + max_lat) / float(2), min_lat), ((min_lat + max_lat) / float(2), max_lat) width_km = (vincenty(W_end_gps, E_end_gps).meters) / float(METER1000) height_km = (vincenty(S_end_gps, N_end_gps).meters) / float(METER1000) num_cols, num_rows = map(int, map(ceil, [v / float(zone_unit_km) for v in [height_km, width_km]])) # hl_length_gps, vl_length_gps = max_long - min_long, max_lat - min_lat xaxis_unit, yaxis_unit = hl_length_gps / float(num_cols), vl_length_gps / float(num_rows) x_points = [min_long + i * xaxis_unit for i in xrange(num_cols)] y_points = [min_lat + j * yaxis_unit for j in xrange(num_rows)] p0, p1 = (y_points[0], x_points[0]), (y_points[0], x_points[1]) x_dist = (vincenty(p0, p1).meters) / float(METER1000) p0, p1 = (y_points[0], x_points[0]), (y_points[1], x_points[0]) y_dist = (vincenty(p0, p1).meters) / float(METER1000) d = VincentyDistance(kilometers=zone_unit_km) print p0, d.destination(point=p0, bearing=0).latitude, d.destination(point=p0, bearing=0).longitude # print x_dist, y_dist, len(x_points), len(y_points) assert False return xaxis_unit, yaxis_unit, x_points, y_points
def get_circle_centers(b1, b2, radius): """ the function covers the area within the bounds with circles this is done by calculating the lat/lng distances and the number of circles needed to fill the area as these circles only intersect at one point, an additional grid with a (+radius,+radius) offset is used to cover the empty spaces :param b1: bounds :param b2: bounds :param radius: specified radius, adapt for high density areas :return: list of circle centers that cover the area between lower/upper """ sw = Point(b1) ne = Point(b2) # north/east distances dist_lat = int(vincenty(Point(sw[0], sw[1]), Point(ne[0], sw[1])).meters) dist_lng = int(vincenty(Point(sw[0], sw[1]), Point(sw[0], ne[1])).meters) def cover(p_start, n_lat, n_lng, r): _coords = [] for i in range(n_lat): for j in range(n_lng): v_north = VincentyDistance(meters=i * r * 2) v_east = VincentyDistance(meters=j * r * 2) _coords.append( v_north.destination(v_east.destination(point=p_start, bearing=90), bearing=0)) return _coords def _calc_base(dist): """ Calculation for base cover """ return math.ceil((dist - radius) / (2 * radius)) + 1 def _calc_offset(dist): """ Calculation for offset cover """ return math.ceil((dist - 2 * radius) / (2 * radius)) + 1 coords = [] # get circles for base cover coords += cover(sw, _calc_base(dist_lat), _calc_base(dist_lng), radius) # update south-west for second cover vc_radius = VincentyDistance(meters=radius) sw = vc_radius.destination(vc_radius.destination(point=sw, bearing=0), bearing=90) # get circles for offset cover coords += cover(sw, _calc_offset(dist_lat), _calc_offset(dist_lng), radius) # only return the coordinates return [c[:2] for c in coords]
def cover(p_start, n_lat, n_lng, r): _coords = [] for i in range(n_lat): for j in range(n_lng): v_north = VincentyDistance(meters=i * r * 2) v_east = VincentyDistance(meters=j * r * 2) _coords.append(v_north.destination(v_east.destination(point=p_start, bearing=90), bearing=0)) return _coords
def getNeighbors(loc, level=15, spread=700): distance = VincentyDistance(meters=spread) center = (loc.latitude, loc.longitude, 0) p1 = distance.destination(point=center, bearing=45) p2 = distance.destination(point=center, bearing=225) p1 = s2sphere.LatLng.from_degrees(p1[0], p1[1]) p2 = s2sphere.LatLng.from_degrees(p2[0], p2[1]) rect = s2sphere.LatLngRect.from_point_pair(p1, p2) region = s2sphere.RegionCoverer() region.min_level = level region.max_level = level cells = region.get_covering(rect) return sorted([c.id() for c in cells])
def getNeighbors(loc, level=15, spread=700): distance = VincentyDistance(meters=spread) center = (loc[0], loc[1], 0) p1 = distance.destination(point=center, bearing=45) p2 = distance.destination(point=center, bearing=225) p1 = s2sphere.LatLng.from_degrees(p1[0], p1[1]) p2 = s2sphere.LatLng.from_degrees(p2[0], p2[1]) rect = s2sphere.LatLngRect.from_point_pair(p1, p2) region = s2sphere.RegionCoverer() region.min_level = level region.max_level = level cells = region.get_covering(rect) return sorted([c.id() for c in cells])
def get_malls(): ifpath = opath.join(dpath['geo'], 'mall-data.csv') ofpath = opath.join(dpath['home'], 'mallsInfo(%.2f).pkl' % ZONE_UNIT_KM) if opath.exists(ofpath): with open(ofpath, 'rb') as fp: malls = pickle.load(fp) return malls malls = {} mover = VincentyDistance(kilometers=ZONE_UNIT_KM) with open(ifpath) as r_csvfile: reader = csv.DictReader(r_csvfile) for row in reader: name = row['name'] lat, lon = map(float, [row[cn] for cn in ['latitude', 'longitude']]) p0 = [lat, lon] # moved_points = [] for d1, d2 in [(WEST, NORTH), (EAST, NORTH), (EAST, SOUTH), (WEST, SOUTH)]: mp = mover.destination(point=p0, bearing=d1) mp = mover.destination(point=mp, bearing=d2) moved_points.append(mp) # polygon_LatLon = [(mp.latitude, mp.longitude) for mp in moved_points] polygon_LatLon.append( (moved_points[0].latitude, moved_points[0].longitude)) malls[name] = (lat, lon, polygon_LatLon) with open(ofpath, 'wb') as fp: pickle.dump(malls, fp) return malls
def next(self, field): from geopy.distance import VincentyDistance longitude = random.randint(-179, 179) if self.longitude is None else self.longitude latitude = random.randint(-179, 179) if self.latitude is None else self.latitude radius = random.randint(100, 6000) if self.radius is None else self.radius d = VincentyDistance(kilometers=radius) bearing = random.randint(0, 359) p = d.destination((latitude, longitude), bearing) return Point(x=p.longitude, y=p.latitude)
def Circle_Geolocations(df): lat = df['Lat'] long = df['Long'] degree = df['Degree'] center = geopy.Point(lat, long) mile_distance = df['Max Travel'] d = Vincenty(miles=mile_distance) destination = d.destination(point=center, bearing=degree) return destination.latitude, destination.longitude
def make_grid(zone_unit_km, min_long, max_long, min_lat, max_lat): mover = VincentyDistance(kilometers=zone_unit_km) x = min_long x_points = [] while x < max_long: x_points.append(x) # p0 = [min_lat, x] moved_point = mover.destination(point=p0, bearing=EAST) x = moved_point.longitude y = min_lat y_points = [] while y < max_lat: y_points.append(y) # p0 = [y, min_long] moved_point = mover.destination(point=p0, bearing=NORTH) y = moved_point.latitude return x_points, y_points
def get_sgGrid(): sgGrid_fpath = opath.join(pf_dpath, 'sgGrid(%.1fkm).pkl'% ZONE_UNIT_KM) if opath.exists(sgGrid_fpath): with open(sgGrid_fpath, 'rb') as fp: lats, lngs = pickle.load(fp) return lats, lngs # sgBorder = get_sgBorder() min_lng, max_lng = 1e400, -1e400 min_lat, max_lat = 1e400, -1e400 for poly in sgBorder: for lat, lng in poly: if lng < min_lng: min_lng = lng if lng > max_lng: max_lng = lng if lat < min_lat: min_lat = lat if lat > max_lat: max_lat = lat # mover = VincentyDistance(kilometers=ZONE_UNIT_KM) # lats, lngs = [], [] lat = min_lat while lat < max_lat: lats += [lat] p0 = [lat, min_lng] moved_point = mover.destination(point=p0, bearing=NORTH) lat = moved_point.latitude lats += [lat] lon = min_lng while lon < max_lng: lngs += [lon] p0 = [min_lat, lon] moved_point = mover.destination(point=p0, bearing=EAST) lon = moved_point.longitude lngs += [lon] # with open(sgGrid_fpath, 'wb') as fp: pickle.dump([lats, lngs], fp) return lats, lngs
def make_grid(zone_unit_km, min_long, max_long, min_lat, max_lat, consider_visualization=False): W_end_gps, E_end_gps = (min_long, (min_lat + max_lat) / float(2)), (max_long, (min_lat + max_lat) / float(2)) S_end_gps, N_end_gps = ((min_lat + max_lat) / float(2), min_lat), ((min_lat + max_lat) / float(2), max_lat) width_km = (vincenty(W_end_gps, E_end_gps).meters) / float(METER1000) height_km = (vincenty(S_end_gps, N_end_gps).meters) / float(METER1000) num_cols, num_rows = map( int, map(ceil, [v / float(zone_unit_km) for v in [height_km, width_km]])) # hl_length_gps, vl_length_gps = max_long - min_long, max_lat - min_lat xaxis_unit, yaxis_unit = hl_length_gps / float( num_cols), vl_length_gps / float(num_rows) x_points = [min_long + i * xaxis_unit for i in xrange(num_cols)] y_points = [min_lat + j * yaxis_unit for j in xrange(num_rows)] p0, p1 = (y_points[0], x_points[0]), (y_points[0], x_points[1]) x_dist = (vincenty(p0, p1).meters) / float(METER1000) p0, p1 = (y_points[0], x_points[0]), (y_points[1], x_points[0]) y_dist = (vincenty(p0, p1).meters) / float(METER1000) d = VincentyDistance(kilometers=zone_unit_km) print p0, d.destination(point=p0, bearing=0).latitude, d.destination( point=p0, bearing=0).longitude # print x_dist, y_dist, len(x_points), len(y_points) assert False return xaxis_unit, yaxis_unit, x_points, y_points
def get_sgGrid(): lons_ofpath = opath.join(dpath['geo'], 'sgLons(%.1fkm)' % ZONE_UNIT_KM) lats_ofpath = opath.join(dpath['geo'], 'sgLats(%.1fkm)' % ZONE_UNIT_KM) if opath.exists(lons_ofpath + '.npy'): sgLons = np.load(lons_ofpath + '.npy', 'r+') sgLats = np.load(lats_ofpath + '.npy', 'r+') return sgLons, sgLats # min_lon, max_lon = 1e400, -1e400, min_lat, max_lat = 1e400, -1e400 sgMainBorder = get_sgMainBorder() for lon, lat in sgMainBorder: min_lon, max_lon = min(min_lon, lon), max(max_lon, lon) min_lat, max_lat = min(min_lat, lat), max(max_lat, lat) # mover = VincentyDistance(kilometers=ZONE_UNIT_KM) # lons = [] lon = min_lon while lon < max_lon: lons += [lon] p0 = [min_lat, lon] moved_point = mover.destination(point=p0, bearing=EAST) lon = moved_point.longitude lons.sort() np.save(lons_ofpath, np.array(lons)) # lats = [] lat = min_lat while lat < max_lat: lats += [lat] p0 = [lat, min_lon] moved_point = mover.destination(point=p0, bearing=NORTH) lat = moved_point.latitude lats.sort() np.save(lats_ofpath, np.array(lats)) return lons, lats
def points_between(start, end, numpoints): distance = VincentyDistance() distances = [] latitudes = [] longitudes = [] lat0 = start.latitude lon0 = start.longitude lat1 = end.latitude lon1 = end.longitude if np.isclose(lat0, lat1): # Constant latitude latitudes = np.ones(numpoints) * lat0 longitudes = np.linspace(lon0, lon1, num=numpoints) for lon in longitudes: distances.append(distance.measure(start, geopy.Point(lat0, lon))) if lon1 > lon0: b = 90 else: b = -90 elif np.isclose(lon0, lon1): # Constant longitude latitudes = np.linspace(lat0, lat1, num=numpoints) longitudes = np.ones(numpoints) * lon0 for lat in latitudes: distances.append(distance.measure(start, geopy.Point(lat, lon0))) if lat1 > lat0: b = 0 else: b = 180 else: # Great Circle total_distance = distance.measure(start, end) distances = np.linspace(0, total_distance, num=numpoints) b = bearing(lat0, lon0, lat1, lon1) for d in distances: p = distance.destination(start, b, d) latitudes.append(p.latitude) longitudes.append(p.longitude) return distances, latitudes, longitudes, b
def interpolate_gps_positions(start_point, end_point, interpolate_ratio): """ .. codeauthor:: Nihal Mohan <*****@*****.**> Function to interpolate between two GPS Coordinates by a ratio in 2D Example Input: :: {'Lat': 16.1235371256305, 'Lng': 75.27075953678545, 'Alt': 875.142}, {'Lat': 16.2334347125635, 'Lng': 75.22123563678545, 'Alt': 893.146}, 0.75 :param dict start_point: Start point coordinates in decimal degrees :param dict end_point: End point coordinates in decimal degrees :param float interpolate_ratio: Ratio at which the interpolation should happen from the start :return: Latitude and longitude of the interpolated GPS points :rtype: Tuple(float) """ if any(not isinstance(point, dict) for point in [start_point, end_point]): raise TypeError( "start_point and end_point inputs should be dictionaries." " Refer to documentation!" ) try: interpolate_ratio = float(interpolate_ratio) except ValueError: raise TypeError( 'Interpolate ratio is required to be a floating value.' ' Conversion to float failed!' ) else: if interpolate_ratio > 1: raise ValueError( 'Interpolate ratio should be Less than 1. ' 'This is Interpolation. Not Extrapolation!' ) distance_travelled = distance( (start_point['Lat'], start_point['Lng']), (end_point['Lat'], end_point['Lng']), ).meters dist_required = distance_travelled * interpolate_ratio start = geopy.point.Point(start_point['Lat'], start_point['Lng']) d = VincentyDistance(meters=dist_required) bearing = calculate_initial_compass_bearing( (start_point['Lat'], start_point['Lng']), (end_point['Lat'], end_point['Lng']), ) interpolated_point = d.destination(point=start, bearing=bearing) # Altitude interpolation if the altitudes were present in the argument start_alt = start_point.get('Alt', None) end_alt = end_point.get('Alt', None) if start_alt is not None and end_alt is not None: interpolated_alt = start_alt + (end_alt - start_alt) * interpolate_ratio return ( interpolated_point.latitude, interpolated_point.longitude, interpolated_alt, ) else: return interpolated_point.latitude, interpolated_point.longitude
from geopy.distance import vincenty, VincentyDistance p1 = (1.276772,103.612184) p2 = (1.383266,103.965440) NORTH, EAST, SHOUTH, WEST = 0, 90, 180, 270 mover = VincentyDistance(kilometers=1) p3 = mover.destination(point=p1, bearing=EAST) p4 = mover.destination(point=p1, bearing=SHOUTH) import folium cp = ((p1[0] + p2[0]) / float(2), (p1[1] + p2[1]) / float(2)) map_osm = folium.Map(location=[cp[0], cp[1]], zoom_start=11) map_osm.add_children(folium.PolyLine(locations=[(p1[0], p1[1]), (p2[0], p2[1])], weight=0.5)) for p, label in [(p1, 'p1'), (p2, 'p2'), (cp, 'Distance (p1, p2): %f m' % vincenty(p1, p2).meters), (p3,'p3 (1km East from p1)'), (p4,'p4 (1km South from p1)')]: folium.Marker((p[0], p[1]), popup=label).add_to(map_osm) map_osm.save('geopy_example.html')