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]]
def test5(): expected = { 617700169957507071, 617700169957769215, 617700169958031359, 617700169958293503, 617700169961177087, 617700169964847103, 617700169965109247, } out = h3.k_ring(617700169958293503, 1) assert isinstance(out, np.ndarray) assert set(out) == expected
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()]]
def neighbours(self) -> HexIdSet: return set(h3.k_ring(self.id, k=1))