Example #1
0
    def query_radius(self,
                     location: np.ndarray,
                     r: float) -> np.ndarray:
        """
        Selects the indices of the points that lie within a given distance from
        a given location.
        :param location: Location to query in [lat, lon] format
        :param r: Radius in meters
        :return: Array of indices
        """
        lat = location[0]
        lon = location[1]
        d0 = gm.num_haversine(lat, lon, self.lat0, self.lon0)
        d1 = gm.num_haversine(lat, lon, self.lat1, self.lon1)

        i0 = np.searchsorted(self.sorted0, d0 - r)
        i1 = np.searchsorted(self.sorted0, d0 + r)
        match0 = self.idx0[i0:i1 + 1]

        i0 = np.searchsorted(self.sorted1, d1 - r)
        i1 = np.searchsorted(self.sorted1, d1 + r)
        match1 = self.idx1[i0:i1 + 1]

        intersect = np.intersect1d(match0, match1)
        dist = gm.vec_haversine(self.lats[intersect],
                                self.lons[intersect],
                                lat, lon)
        return intersect[dist <= r]
Example #2
0
    def query_knn(self, location: np.ndarray, k: int) -> (np.ndarray, np.ndarray):
        idx = h3.geo_to_h3(location[0], location[1], self.h3res)

        i = 0
        indices = np.zeros(0, dtype=np.uint64)
        ring = np.zeros(0, dtype=np.uint64)
        while indices.shape[0] < k:
            i += 2
            k_ring = h3.k_ring(idx, i)
            ring = np.setdiff1d(k_ring, ring, assume_unique=True)

            i0 = np.searchsorted(self.h3arr, ring, side='left', sorter=self.h3idx)
            i1 = np.searchsorted(self.h3arr, ring, side='right', sorter=self.h3idx)

            indices = np.hstack((indices,
                                 np.hstack([np.arange(i, j, dtype=np.uint64)
                                            for i, j in zip(i0, i1) if i != j])))

        idx = self.h3idx[indices]
        dist = gm.vec_haversine(self.locations[idx, 0],
                                self.locations[idx, 1],
                                location[0], location[1])

        dist_idx = np.argsort(dist)
        return idx[dist_idx[:k]], dist[dist_idx[:k]]
Example #3
0
 def calculate_dx(self, df: pd.DataFrame) -> pd.DataFrame:
     """
     Calculates the consecutive distance in meters.
     :param df: Input DataFrame
     :return: DataFrame with added 'dx' column.
     """
     lat0 = df[self.lat_col][:-1].to_numpy()
     lon0 = df[self.lon_col][:-1].to_numpy()
     lat1 = df[self.lat_col][1:].to_numpy()
     lon1 = df[self.lon_col][1:].to_numpy()
     dist = vec_haversine(lat0, lon0, lat1, lon1)
     df[self.dx_col] = np.insert(dist, 0, 0.0)
     return df
Example #4
0
 def query_radius(self,
                  location: np.ndarray,
                  r: float) -> (np.ndarray, np.ndarray):
     """
     Selects the indices of the points that lie within a given distance from
     a given location.
     :param location: Location to query in [lat, lon] format
     :param r: Radius in meters
     :return: Array of indices
     """
     lat = location[0, 0]
     lon = location[0, 1]
     dist = gm.vec_haversine(self.lats, self.lons, lat, lon)
     return np.argwhere(dist <= r)
Example #5
0
    def query_radius(self,
                     location: np.ndarray,
                     r: float) -> np.ndarray:
        edge_len = h3.edge_length(self.h3res, unit="m")
        idx = h3.geo_to_h3(location[0], location[1], self.h3res)

        ring = h3.k_ring(idx, 1 + int(round(r / edge_len)))

        i0 = np.searchsorted(self.h3arr, ring, side='left', sorter=self.h3idx)
        i1 = np.searchsorted(self.h3arr, ring, side='right', sorter=self.h3idx)

        indices = np.hstack([np.arange(i, j) for i, j in zip(i0, i1) if i != j])

        idx = self.h3idx[indices]
        dist = gm.vec_haversine(self.locations[idx, 0], self.locations[idx, 1],
                                location[0], location[1])
        return self.h3idx[indices[np.argwhere(dist <= r).ravel()]]
Example #6
0
    def query_knn(self, location: np.ndarray, k: int) -> (np.ndarray, np.ndarray):
        lat = location[0]
        lon = location[1]
        d0 = gm.num_haversine(lat, lon, self.lat0, self.lon0)
        d1 = gm.num_haversine(lat, lon, self.lat1, self.lon1)
        r = math.sqrt(k / self.density) * 2.0

        intersect = np.zeros(0)
        while intersect.shape[0] < k:
            s0 = np.searchsorted(self.sorted0, [d0 - r, d0 + r])
            s1 = np.searchsorted(self.sorted1, [d1 - r, d1 + r])
            intersect = np.intersect1d(self.idx0[s0[0]:s0[1] + 1],
                                       self.idx1[s1[0]:s1[1] + 1],
                                       assume_unique=True)
            r *= 4

        dist = gm.vec_haversine(self.lats[intersect],
                                self.lons[intersect],
                                lat, lon)

        idx = np.argsort(dist)
        return intersect[idx][:k], dist[idx[:k]]
Example #7
0
def calculate_sorted_distances(latitudes, longitudes, lat, lon):
    dist = gm.vec_haversine(latitudes, longitudes, lat, lon)
    idx = np.argsort(dist)
    return idx, dist[idx]
Example #8
0
 def query_knn(self, location: np.array, k: int) -> np.array:
     dist = gm.vec_haversine(self.lats, self.lons,
                             location[0], location[1])
     idx = np.argsort(dist)
     return idx[:k], dist[idx[:k]]