Here's my use case--it is related to small neighborhood classification, but it could be anything where, during a loop, the ground conditions change (the state / grouping of individual areas change), and further iterations must relate to the new state of the system.
The problem is at this point more conceptual than code, so bear with me for no reproducible example.
I am looping over a list of small areas. I run spatial queries in postgis
to get a list of all neighboring areas.
Each area can be thought of as a geographic "seed" for a cluster: If each seed's neighbors meet a certain size criteria, they are added to the list (the cluster "grows") and assigned a cluster ID.
Edit: Added detail
My starting point is a census tract layer in postgis, which I query for adjacent tracts. Say, tract 1 has as its neighbors tracts 2, 3, 5, and 6. Those tracts have a bunch of attributes, such as employment and population. So the idea is to treat, here, tract 1 as a potential geographic cluster seed, and, for each of the neighbors, add them if they meet certain data conditions, could be size of the population. In the example, let's say we add tracts 2 and 3, but not 5 and 6 (their population may be too small).
Seed 1 has now grown, with two additional tracts. I will then union those, have a larger area and query the original tract layer for neighbors of the now larger seed. Repeat until no neigbors fit the criteria, then move to the next tract in the original list.
Here's where I am having trouble with the concept:
- I run through a seed area and enumerate all its neighbors, and at the end of each iteration, I want to reflect that there is now potentially a bigger seed, and the seed will have a new potential list of neighbors it can "absorb".
So in a sense, I want to run a loop on the original list, but it will be modified and re-queried every time I exhaust the list of neighbors for seed X. The next iteration, then will face a new state of the geographic system.
I could do this in a linear fashion, repeating code blocks, but that is hardly ideal. Or maybe iterations are a bad idea--"walk" through a list and then do
So to boil it into one question, how can I set up an iteration where the state changes, which is different from typical list comprehensions of the sort where options are fully enumerated from the start based on some initial state criteria, and have subsequent tests reflect the new state of the (geographic) system?