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 true_parents(self) -> HexIdSet: """ hexagons do not cleanly subdivide into seven finer hexagons. the child hexagon cells protrude from the parent (cf. https://h3geo.org/docs/highlights/indexing) and hence a cell does not have a single, but actually up to 2 "true" parents returns: the hex ids of all parent cells which any of the cell points belong """ if self.res == 0: raise ValueError("not defined for resolution 0") lower_res = self.res - 1 # NOTE: (lat,lng) pairs! coord_pairs = h3.h3_to_geo_boundary(self.id) return {h3.geo_to_h3(pt[0], pt[1], lower_res) for pt in coord_pairs}
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 geo_to_h3_array(locations, resolution: int = 12): hexes = [h3.geo_to_h3(locations[i, 0], locations[i, 1], resolution) for i in range(locations.shape[0])] return hexes
def test1(): assert h3.geo_to_h3(37.7752702151959, -122.418307270836, 9) == 617700169958293503
def lies_in_h3_cell(h: int, lng: float, lat: float) -> bool: res = h3.h3_get_resolution(h) return h3.geo_to_h3(lat, lng, res) == h
def geo_to_h3(lng: float, lat: float) -> int: return h3.geo_to_h3(lat, lng, SHORTCUT_H3_RES)