Exemplo n.º 1
0
    def _build(self, base_shape, meters, crs=constants.DEFAULT_CRS):

        #  translate input meters to appropriate h3 resolution
        res = self._get_appropriate_res(base_shape, meters)

        # cover the base_shape with h3 hexagonal polygons
        hexs = self._handle_polyfill(base_shape, res)

        # get actual geoms out of H3 hexagons
        # from https://geographicdata.science/book/data/h3_grid/build_sd_h3_grid.html
        polygonise = lambda hex_id: Polygon(
            h3.h3_to_geo_boundary(hex_id, geo_json=True))
        # prepare a geodf with all the H3 geoms
        all_polys = gpd.GeoDataFrame(
            {
                'geometry': list(map(polygonise, hexs)),
                'H3_INDEX': hexs
            },
            crs=constants.DEFAULT_CRS)

        # add TileID
        all_polys['TILE_ID'] = all_polys.index
        # Convert TILE_ID to have str type
        all_polys['TILE_ID'] = all_polys['TILE_ID'].astype('str')

        return all_polys
Exemplo n.º 2
0
 def from_id(cls, id: int):
     res = h3.h3_get_resolution(id)
     coord_pairs = h3.h3_to_geo_boundary(id)
     # ATTENTION: (lat, lng)! pairs
     coords = to_numpy_polygon(coord_pairs, flipped=True)
     x_coords, y_coords = coords[0], coords[1]
     surr_n_pole = lies_in_h3_cell(id, lng=0.0, lat=MAX_LAT)
     surr_s_pole = lies_in_h3_cell(id, lng=0.0, lat=-MAX_LAT)
     bounds, x_overflow = get_corrected_hex_boundaries(
         x_coords, y_coords, surr_n_pole, surr_s_pole)
     return cls(id, res, coords, bounds, x_overflow, surr_n_pole,
                surr_s_pole)
Exemplo n.º 3
0
 def _create_hexagon_polygons(self, hexagon_ids):
     # from https://geographicdata.science/book/data/h3_grid/build_sd_h3_grid.html
     return gpd.GeoDataFrame(
         {
             "geometry": [
                 Polygon(h3.h3_to_geo_boundary(hexagon_id, geo_json=True))
                 for hexagon_id in hexagon_ids
             ],
             "H3_INDEX":
             hexagon_ids,
         },
         crs=constants.DEFAULT_CRS,
     )
Exemplo n.º 4
0
    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}
Exemplo n.º 5
0
def array_to_dataframe(in_array,
                       transform,
                       h3_resolution,
                       nodata_value=None,
                       axis_order="yx",
                       compacted=True,
                       geo=False):
    """
    convert a raster/array to a pandas dataframe containing H3 indexes

    :param in_array: input 2-d array
    :param transform:  the affine transformation
    :param nodata_value: the nodata value. For these cells of the array there will be no h3 indexes generated
    :param axis_order: axis order of the 2d array. Either "xy" or "yx"
    :param h3_resolution: target h3 resolution
    :param compacted: return compacted h3 indexes (see H3 docs)
    :param geo: return a geopandas geodataframe with geometries. increases the memory usage.
    :return: pandas dataframe or geodataframe
    """
    frames = []
    for value, compacted_vec in array_to_h3(in_array,
                                            transform,
                                            h3_resolution,
                                            nodata_value=nodata_value,
                                            axis_order=axis_order).items():
        indexes = None
        if compacted:
            indexes = compacted_vec.compacted_indexes()
        else:
            indexes = compacted_vec.uncompacted_indexes_at_resolution(
                h3_resolution)
        values = np.repeat(value, len(indexes))
        frame = None
        if geo:
            frame = gp.GeoDataFrame({
                "h3index":
                indexes,
                "value":
                values,
                "geometry": [
                    Polygon(h3.h3_to_geo_boundary(h, geo_json=True))
                    for h in np.nditer(indexes)
                ],
            })
        else:
            frame = pd.DataFrame({"h3index": indexes, "value": values})
        frames.append(frame)
    return pd.concat(frames)