def downscale_h3(time_win_df,
                 agg_brothers,
                 downscale_size=2,
                 h3_index_col="h3_index"):
    # tODO: 2nd for loop should be a while with selected res at top
    # Only with this number of brother the hexagons will be scaled (max = 7)
    n_min_brothers_to_scale = 5

    time_win_h3 = time_win_df.reset_index()
    time_win_h3["h3_res"] = time_win_h3[h3_index_col].apply(
        h3.h3_get_resolution)
    downscale_resulutions = range(time_win_h3["h3_res"].min(),
                                  time_win_h3["h3_res"].min() - downscale_size,
                                  -1)

    for child_h3_res_depth, downscale_res in enumerate(downscale_resulutions):
        print("Auto downscale h3 resolution:", downscale_res)
        for idx, row in time_win_h3.iterrows():
            # Once time_win_h3 indexs get changed during the loop and the time_win_h3.iterrows()
            # is a copy of the rows, they might not exist
            if idx not in time_win_h3.index:
                continue

            h3idx = row[h3_index_col]
            cell_res = row["h3_res"]
            # If its a different res than the one we re trying to downscale, skips
            if cell_res != downscale_res:
                continue

            parent = h3.h3_to_parent(h3idx, cell_res - 1)
            # finding all the brother cells
            brother_cells = list(h3.h3_to_children(parent, cell_res))
            # dont scale if there is less than X brothers
            if time_win_h3[h3_index_col].isin(
                    brother_cells).sum() < n_min_brothers_to_scale:
                continue

            # finding all the children cells
            for childh3res in range(1, child_h3_res_depth + 1):
                brother_cells.extend(
                    list(h3.h3_to_children(parent, cell_res + childh3res)))

            brothers_df = time_win_h3[time_win_h3[h3_index_col].isin(
                brother_cells)]

            agg_result = agg_brothers(brothers_df)

            if agg_result is False:
                continue
            # set the cols to the parent values
            agg_result[h3_index_col] = parent
            agg_result["h3_res"] = cell_res - 1
            time_win_h3.loc[idx] = agg_result
            # drop the rest of the brothers
            time_win_h3.drop([i for i in brothers_df.index if i != idx],
                             inplace=True)

    return time_win_h3.set_index(h3_index_col)
Exemplo n.º 2
0
def occupied_neighbors(hex,
                       density_tgt,
                       density_max,
                       N,
                       hex_density,
                       method='siblings'):
    """

    :param hex: hex to query
    :param density_tgt: target density for hexs at this resolution
    :param density_max: maximum density at this resolution
    :param hex_density: dictionary of densities at each hex
    :param N:
    :param method: either siblings or neighbors
    :return:
    """
    # neigbhors = h3.hex_range(h, 1)
    #neigbhors = h3.h3_to_children(h3.h3_to_parent(h, resolution - 1), resolution)
    res = h3.h3_get_resolution(hex)
    if method == 'siblings':
        neighbors = h3.h3_to_children(h3.h3_to_parent(hex, res - 1), res)
    elif method == 'neighbors':
        neighbors = h3.hex_range(hex, 1)

    neighbors_above_tgt = 0
    for n in neighbors:
        if n not in hex_density:
            continue
        if hex_density[n]['clipped'] >= density_tgt:
            neighbors_above_tgt += 1
    clip = min(density_max, density_tgt * max(1,
                                              (neighbors_above_tgt - N + 1)))
    return clip
Exemplo n.º 3
0
    def get_parent_and_child_hex_dictionary(self, df, parent_resolution, output_name=None, output_json=False):
        """

        :param df: input geopandas geodataframe
        :param parent_resolution: the parent h3 resolution
        :param output_name: json dictionary name
        :param output_json: boolean, if you want to output to json file format
        :return: hex dictionary {"parent_hex_id": ["child_hex_id"]}
        """

        hex_dict = defaultdict(list)
        bbox = box(*df.total_bounds)
        parent_hexs_list = h3.polyfill(bbox.__geo_interface__, parent_resolution, geo_json_conformant=True)

        for parent in parent_hexs_list:
            child_hex = list(h3.h3_to_children(parent, parent_resolution+1))
            hex_dict[parent].append(child_hex)

        if output_json:
            output = os.path.join(self.output_folder,"json", output_name+".json")
            with open(output, 'w') as out_json:
                json.dump(hex_dict, out_json)
            print("outfile: ", output)


        return hex_dict
Exemplo n.º 4
0
def random_location(candidate_hex_addresses, resolution):
    root_hex = random.choice(candidate_hex_addresses)
    root_resolution = h3.h3_get_resolution(root_hex)

    current_resolution = root_resolution
    current_cell = root_hex

    while current_resolution < resolution:
        # Step up one resolution.
        current_resolution += 1
        # Get all the children of the current cell.
        children = h3.h3_to_children(current_cell, current_resolution)
        # Draw a random child and set it to the current cell.
        current_cell = random.choice(list(children))

    return h3.h3_to_geo(current_cell)
Exemplo n.º 5
0
    def to_children(self) -> List[Tile]:
        """Maps current tile to children Tile objects

        Returns
        -------
        List[Tile]
            List of children Tile objects
        """

        if self.grid_type == "s2":

            children_ids = s2.s2_to_children(self.tile_id)

        elif self.grid_type == "h3":

            children_ids = h3.h3_to_children(self.tile_id)

        elif self.grid_type in ("bing", "quadtree"):

            children_ids = quadtree.tile_to_children(self.tile_id)

        return [self.id_to_tile(cid) for cid in children_ids]
Exemplo n.º 6
0
def get_h3_cells(res, extent=None):
    
    """Get h3 cells for given resolution

    Parameters:
    res (int): h3 resolution 
    extent (list): Extent as array of 2 lon lat pairs to get raster values for
    Returns:
    Pandas dataframe
   """
    if extent:
        set_hex = list(h3.polyfill_geojson(extent, res=res))
    else:    
        set_hex_0 = list(h3.get_res0_indexes())
        set_hex = []
        if res == 0:
            set_hex = set_hex_0
        else:
            for i in set_hex_0:
                set_hex.extend(list(h3.h3_to_children(i, res)))
    df = pd.DataFrame({"cell_id": set_hex})
    
    return df
Exemplo n.º 7
0
def create_h3_geom_cells_global(resolutions, table, export_type, db_engine=''):
    """Create geometry for h3 cells globally for given resolutions

        Parameters:
        db_engine (sqlalchemy.engine): sqlalchemy database engine
        resolutions(array): array of integer h3 resolution levels
        table(string): table name for postgres database
        export_type(string): where to export 'geojson' or 'postgres'

        Returns:
        none
    """
    for res in resolutions:
        set_hex_0 = list(h3.get_res0_indexes())
        set_hex = []
        if res == 0:
            set_hex = set_hex_0
        else:
            for i in set_hex_0:
                set_hex.extend(list(h3.h3_to_children(i, res)))
        if export_type == 'postgres':
            gdf = pd.GeoDataFrame({"cell_id": set_hex})
            gdf['geometry'] = gdf["cell_id"].apply(lambda x:(Polygon(h3.h3_to_geo_boundary(x, geo_json=True)).wkb))

            print('finish caclulating geometry {} {}'.format(res, time.asctime(time.localtime(time.time()))))

            gdf.to_postgis(table + str(res), db_engine, if_exists='replace')
            print('finish import to db {} {}'.format(res, time.asctime(time.localtime(time.time()))))

        elif export_type == 'geojson':
            transformer = Transformer.from_crs("epsg:4326", 'proj=isea')
            gdf = gpd.GeoDataFrame({"cell_id": set_hex})
            gdf['geometry'] = gdf.cell_id.apply(lambda x: Polygon(h3.h3_to_geo_boundary(x, geo_json=True)))
            print('finish caclulating geometry {} {}'.format(res, time.asctime(time.localtime(time.time()))))
            gdf['area'] = gdf.geometry.apply(lambda x: transform(transformer.transform, x).area)
            gdf.to_file("{}{}.geojson".format(table, res), driver='GeoJSON')
            print('finish import to geojson {} {}'.format(res, time.asctime(time.localtime(time.time()))))
Exemplo n.º 8
0
 def test_h3_to_children(self):
     test_hexagon = '8828308281fffff'
     children = h3.h3_to_children(test_hexagon, 9)
     self.assertEqual(len(children), 7, 'got all 7 children back')
Exemplo n.º 9
0
def sample_neighbor(hotspots, density_tgt, density_max, R, N):

    # ==============================================================
    # Part 1, find hexs and density of hexs containing interactive
    #         hotspots at target resolution
    # ==============================================================

    # determine density of occupied "tgt_resolution" hexs.  This sets our initial conditions.  I also track "actual" vs
    # clipped density to find discrepancies
    #hex_density will be keys of hexs (all resolutions) with a value of dict(clipped=0, actual=0)
    hex_density = dict()
    interactive = 0
    for h in hotspots:
        if is_interactive(h):
            hex = h3.h3_to_parent(h['location'], R)
            interactive += 1
            # initialize the hex if not in dictionary
            if hex not in hex_density:
                hex_density[hex] = dict(clipped=0, actual=0, unclipped=0)
            hex_density[hex]['clipped'] += 1
            hex_density[hex]['actual'] += 1
            hex_density[hex]['unclipped'] += 1

    for h in hex_density.keys():
        clip = occupied_neighbors(h,
                                  density_tgt,
                                  density_max,
                                  N,
                                  hex_density,
                                  method='neighbors')

        hex_density[h]['clipped'] = min(hex_density[h]['clipped'], clip)
        hex_density[h]['limit'] = clip

    print(f"{len(hotspots)} hotspots")
    print(f"{len(hex_density)} unique res {R} hexs")
    print(f"{lone_wolfs} lone wolfs")
    print(f"{interactive} interactive hotspots")
    #build a set of R resolution hexs, occupied child hexs are how we build occupied hexs for parent levels
    occupied_higher_res = set(hex_density.keys())

    # ==============================================================
    # Part 2, go from high to low res, clipping density and determining
    #         densities of parent hexs
    # ==============================================================

    # iterate through resultion from just above target to 1 clipping child densities and calculating appropriate hex
    # densities at "resolution"
    for resolution in range(R - 1, 0, -1):
        # hold set of hex's to evaluate
        occupied_hexs = set(
            [])  # key = parent hex, values = list of child hexs
        # density target and limit at  child's resolution.  This is simply scaled up by increased area
        density_res_tgt = density_tgt * 7**(R - resolution)
        density_res_max = density_max * 7**(R - resolution)

        # 1. find all occupied hexs at this resolution based on child hexs
        for h in occupied_higher_res:
            occupied_hexs.add(h3.h3_to_parent(h, resolution))

        for h in occupied_hexs:
            children = h3.h3_to_children(h, resolution + 1)

            # calculate density of this hex by summing the clipped density of its children
            hex_raw_density = 0
            hex_unclipped_density = 0
            for c in children:
                if c in hex_density:

                    hex_raw_density += hex_density[c]['clipped']
                    hex_unclipped_density += hex_density[c]['actual']
            hex_density[h] = dict(clipped=hex_raw_density,
                                  actual=hex_unclipped_density,
                                  unclipped=hex_raw_density)

        # now that we have unclipped densities of each occupied hex at this resolution, iterate through all occupied
        # hexs again and apply clipping by looking at neighbors:

        for h in occupied_hexs:
            #neigbhors = h3.hex_range(h, 1)
            #neigbhors = h3.h3_to_children(h3.h3_to_parent(h, resolution - 1), resolution)
            clip = occupied_neighbors(h,
                                      density_res_tgt,
                                      density_res_max,
                                      N,
                                      hex_density,
                                      method='neighbors')

            hex_density[h]['clipped'] = min(hex_density[h]['clipped'], clip)
            hex_density[h]['limit'] = clip
        occupied_higher_res = list(occupied_hexs)

        print(
            f"total of {len(occupied_hexs)} occupied hexes at resolution {resolution}"
        )
        # occupied hex's at this resolution are child hexs in next resolution
        child_hexs = occupied_hexs

    # ==============================================================
    # Part 3, print / store analysis
    # ==============================================================

    # occupied_hex's is now the top level hex evaluated.  Start here for descending to target a hotspot
    top_count = 0
    for h in occupied_hexs:
        #print(f"hex {h} has density {hex_density[h]}")
        top_count += hex_density[h]['clipped']

    print(f"total density of all top level hexs = {top_count}")
    # for k in hex_density.keys():
    #     hex_density[k]['border'] = h3.h3_to_geo_boundary(k, False)

    interactive_hspots = 0

    with open(f'hex_occupancy_R{R}_N{N}_tgt{density_tgt}_max{density_max}.csv',
              'w',
              newline='') as csvfile:
        hex_writer = csv.writer(csvfile,
                                delimiter=',',
                                quotechar='"',
                                quoting=csv.QUOTE_MINIMAL)
        hex_writer.writerow([
            'hex', 'resolution', 'density_clipped', 'density_actual',
            'density_limit'
        ])
        for k in hex_density:
            hex_writer.writerow([
                k,
                h3.h3_get_resolution(k), hex_density[k]['clipped'],
                hex_density[k]['actual'], hex_density[k]['limit']
            ])

    with open(
            f'hotspot_RewardScale_R{R}_N{N}_tgt{density_tgt}_max{density_max}.csv',
            'w',
            newline='') as csvfile:
        hspot_writer = csv.writer(csvfile,
                                  delimiter=',',
                                  quotechar='"',
                                  quoting=csv.QUOTE_MINIMAL)
        hspot_writer.writerow(
            ['address', 'name', 'city', 'state', 'reward_scale'])
        # iterate through all interactive hotspots and evaluate probability of targeting. this will be outputted to CSV
        for hspot in hotspots:
            # start at top level and iterate through determining odds of selection
            if not is_interactive(hspot):
                continue
            interactive_hspots += 1
            scale = 1
            probability = 1
            #for res in range(1, R+1):
            for res in range(R, 0, -1):
                hex = h3.h3_to_parent(hspot['location'], res)
                scale_orig = scale
                scale *= hex_density[hex]['clipped'] / hex_density[hex][
                    'unclipped']
                if hspot['name'] == 'daring-carmine-penguin':
                    print(
                        f"{hex} h3res:{res} has density clipped/unclipped of {hex_density[hex]['clipped']:3d}/{hex_density[hex]['unclipped']:3d}, scale reduced: {scale_orig:.3f} to {scale:.3f}"
                    )
                sibling_total = hex_density[hex]['clipped']
                sibling_unclipped = hex_density[hex]['actual']

            hspot_writer.writerow([
                hspot['address'], hspot['name'],
                hspot['geocode']['short_city'],
                hspot['geocode']['short_state'], f"{scale:.5f}"
            ])
            # print(f"hotspot {hspot['name']:30} has {sibling_unclipped} hotspots in res8 cell, probability {probability*100:.8f}%")

        print(f"total of {interactive_hspots} interactive hotspots")