How to iterate through a dataset until a subset of a certain size is created?

945
5
Jump to solution
09-25-2023 07:30 PM
Alyssa_68
New Contributor

I'm trying to do something fairly simple (I think) in modelbuilder but I can't wrap my head around how the iterators and logic operators work. Basically, I have a dataset with thousands of points and I want to randomly select one point, then select all the points within a certain distance and store them in a new feature class. The model I've built (picture attached) accomplishes this task up until this point. However, I would like this process to continue repeating itself until the new feature class has a certain number of rows. I was thinking of using the "If Row Count Is" operator, but I don't know where exactly to add it or how to point it to the steps I want repeated. Any help would be appreciated!

0 Kudos
1 Solution

Accepted Solutions
SiqiLi
by Esri Contributor
Esri Contributor

The selection part can go in between While and Append tool.

In the image below, 

  • The If Row Count Is tool checks whether the "Target" dataset has over 200 rows.
  • If row count is greater than 200, the model stops running.
  • If row count is less than or equal to 200, 
    Frist, select one row from "points" dataset which has the smallest RandID.
    Use this point as Selecting Features in the Select Layer By Location tool to select "points" within 100km.
    Selected points from the Select Layer By Location tool are appended to the "Target" dataset and removed from "points" dataset.
  • Then the model checks whether the "Target" dataset has over 200 rows. If not, repeat the above process.


    SiqiLi_0-1695823359278.png

 

View solution in original post

5 Replies
DuncanHornby
MVP Notable Contributor

To loop within models you need to be using an iterator. Explore help file and look at the examples.

0 Kudos
curtvprice
MVP Esteemed Contributor

I think you are almost there, just use the Stop tool (in the utility dropdown in the ribbon) with the variable labeled True at right as the precondition.

0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor

Hi @Alyssa_68 , hope the following workflow could help.

In the model below, I am using If Row Count Is tool to check if the input dataset "points" has over than 50 rows. The While tool output is connected to Append tool as Precondition link. This model will keep running and stop when the "points" dataset reaches over 50 rows.

  • When the "points" dataset has less than or equal to 50 rows, the If Row Count Is tool "false" output variable returns value "true". This matches with the While tool parameter setting. And it will continue running the Append tool.
  • If it has over 50 rows, the "False" variable returns value false. The Append tool will not run.
    SiqiLi_0-1695757066270.png

 

0 Kudos
Alyssa_68
New Contributor

That answer is almost perfect! The only issue is I want to select a random point and then also have any points within a buffered radius also be selected and appended to the dataset. Then, ideally, deleted from the original point dataset so as the model continues iterating, it will select a new random point without any repeats. My initial dataset has a column of random numbers called RandID.

 

Thanks so much for your help!

0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor

The selection part can go in between While and Append tool.

In the image below, 

  • The If Row Count Is tool checks whether the "Target" dataset has over 200 rows.
  • If row count is greater than 200, the model stops running.
  • If row count is less than or equal to 200, 
    Frist, select one row from "points" dataset which has the smallest RandID.
    Use this point as Selecting Features in the Select Layer By Location tool to select "points" within 100km.
    Selected points from the Select Layer By Location tool are appended to the "Target" dataset and removed from "points" dataset.
  • Then the model checks whether the "Target" dataset has over 200 rows. If not, repeat the above process.


    SiqiLi_0-1695823359278.png