Using Modelbuilder for a Monte Carlo Process

05-28-2014 07:38 AM
New Contributor II

I would like to create a model or write a script that will complete the following geoprocess:

If I have a layer of polygons, I need to place two random points in each polygon, create a line from those two points, and then check if that line falls within the polygon or not.

I am using ModelBuilder, and I am able to build a model that will create two random points within each polygon and then create a line between them. I am confident that I can figure out how to do the spatial join, but I am not sure how to use that to give me an output that essentially counts if it is inside the polygon or not.

I am unsure how to correctly set up the for - function and collecting the output data that I need.


for i=1 to 10000
     Create two random points inside each polygon
     Create line between those two points
     Check if line falls completely within the polygon
     Keep a count for each polygon if the line falls within polygon or not for each round through the loop


I figured I would ask if anyone had any examples they might be able to share that accomplished something similar, or if anyone had any suggestions as to how to build this model.

Much appreciated!
0 Kudos
5 Replies
MVP Notable Contributor

You say you create 2 random points in each polygon then create line between them. Under what scenario would that line actually be outside the polygon? I could see a scenario of a line partially in a polygon (e.g. donut polygon) but both ends would be in the polygon.

Can you upload a sketch of what of a true and false scenario you are trying to test? It's not clear what you asking as your pseudo code appears to contradict your description?

0 Kudos
MVP Notable Contributor
If your scenario is something like this:


Then I would first use the Feature Vertices to points to extract one end of the line this can then be used to do a spatial join with your polygons. Having assigned the polygon ID to the point you spatial join that back to the polyline. This guarantees the correct ID for the line. You use that to quality control your output. Then its just an intersect tool with output set to LINE. If your line ID's are duplicated then you must be passing out/into the polygon.
0 Kudos
New Contributor II
Thank you for your responses so far.

This is what I mean when I talk about the line. I need to know when it falls completely within the polygon. Here is an example of a census tract I picked from Ohio:


In that image, the line drawn between two points falls completely within the polygon.

Below, they do not:

0 Kudos
Regular Contributor III
Can you do a Select By Location specifying where the line is completely within the polygon? I don't work much with ModelBuilder, but that's what I'd do manually.
0 Kudos
New Contributor II
I know how to do a select by location of that nature - but is there a way to do it so that I get an output whether or not is was successfully able to select the line in question?

Thank you very much for your input!
0 Kudos