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.