0

I'm working with H3 map cells to build a list of param dicts. Each h3_cell value will have children, and those children will have children, etc. I want to append to a list when each cell's pre-formatted params passed to requests URL returns a JSON blob with field total <= 1000.

If the origin cell returns <= 1000, then there should be no further processing. If the origin cell returns > 1000, then I want to find the children cells and do the same processing until all subsequent children return <= 1000.

I think I'm part of the way there, but wondering if I missing something to prevent infinite recursion? Thanks for your help!

def build_params(h3_cell, term):
    cell_coords = h3.h3_to_geo(h3_cell)
    param_list = []
    
    prep_params = {
        'latitude': cell_coords[0],
        'longitude': cell_coords[1],
        'categories': term,
    }
    
    r = requests.get(
        url = url,
        headers = headers,
        params = prep_params
    )
    sleep(1)
    
    if r.json().get('total',0) <= 1000:
        param_list.append(prep_params)
        return param_list
    else:
        children = h3.h3_to_children(h3_cell)
        for child in children:
            build_params(child, term)
        return param_list
parent (returns JSON total value of 2000 - this is not the number of children)
    child1 (800)
    child2 (1100)
        child2child1 (500)
        child2child2 (600)
    child3 (100)

In the example above, I would want child1, child3, child2child1, and child2child2 to compromise the returned params. Any node/cell that returns a total value > 1000 would then check its subsequent children until reaching every child < 1000.

alpacafondue
  • 353
  • 3
  • 16

1 Answers1

0

Not sure about being an infinite loop or not, but you might get a maximum recursion depth exception if the number of children returning > 1000 is big

Check this question on how to find the maximum recursion depth and modify it if needed.

  • I would not expect in the example I'm using that recursion would get too deep. For an example:`parent (2000) >> child1 (500) / child2 (400) / child3 (1100) >> child3child1 (500) child3child2 (600)` is probably as deep as I might expect. I would want the final list to contain `child1, child2, child3child1, child3child2`.\ – alpacafondue Feb 02 '23 at 02:14