Pic of ModelI would like to take the intersection of two areas (one randomly generated), and do this n times. I can do it and get one result, but I can't save all the intersection areas to a file at the end.

The problem is that when I run my model more than once it says that my buffer output file already exists, which I can imagine would happen again for the intersection. What I'd like to happen is to run my model where I produce new random points everytime, then buffer them and intersect the dissolved buffer with another area and then get out the area. Then I want to do this several times.

If this isn't worded very well then just let me know and I can explain what I mean in another way. Thanks a lot

  • What ArcGIS for Desktop version are you using? Would you be able to edit your question to include that detail and a diagram showing what you want to happen in the first few iterations. I'll +1 your question in case you need more reputation to make including pictures easy. I assume that you have seen ModelBuilder's Iterators. – PolyGeo Aug 23 '13 at 8:53
  • I'm using ArcMap 10.1, it says I need 10 rep points to post an image, but I can alter the question to have a few more details. – John_Smith Aug 23 '13 at 9:46
  • If you'd like to use the model builder option -> export to Python script, I'd be happy to help you customize the output. – Roy Aug 23 '13 at 12:39
  • pastebin.com/5j3qY1Fe Here it is as a Python script, that's for offering to help @Roy. I have written it now so that the buffer and iteration change as I run a for loop, the problem no is that I cannot seem to store my intersection areas in a file. – John_Smith Sep 2 '13 at 9:39

Two additions should make this work assuming I understand what you're looking to do here.

Setting the overwrite environment variable to true will skip errors where the dataset already exists. Since you only need random points within the loop we can get rid of them after each iteration.

Second, if you set up a loop, you can append your counter to the filename when you run your intersect. This will save all of your files rather than overwriting them. If you'd rather have more descriptive names you could create a python list (example below), and append an item from that list to the filename on each iteration.

# example of a python list
list = ["filename1", "filename2", "filename3"]

To access the item in a list, use the counter in the loop.

# While the counter is less than the length of the list (we start counting from 0 instead of 1) print the list item
i = 0
while i < len(list):
    print list[i]
    i += 1

Example code:

# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True

# Local variables:
Python_Intersect_prac = "D:\\Python_Intersect_prac"
Plume_dissolve1 = "Plume_dissolve1"
Random_Hs_shp = "D:\\Python_Intersect_prac\\Random_Hs.shp"
Random_Hs_b_shp = "C:\\Users\\c1022376\\My Documents\\Random_Hs_b.shp"
Random_Hs_b_Intersect = "C:\\Users\\c1022376\\My Documents\\ArcGIS\\Default1.gdb\\Random_Hs_b_Intersect"

# Initialize counter
i = 0

# How many times would you like to loop? (integer)
j = 5 # this will run 5 times

# the following code will iterate j times
while i < j:
    # Process: Create Random Points
    arcpy.CreateRandomPoints_management(Python_Intersect_prac, "Random_Hs", "", "-180 -90 180 90", "35", "0 Unknown", "POINT", "0")

    # Process: Buffer
    arcpy.Buffer_analysis(Random_Hs_shp, Random_Hs_b_shp, "4 DecimalDegrees", "FULL", "ROUND", "ALL", "")

    # Process: Intersect
    arcpy.Intersect_analysis("'C:\\Users\\c1022376\\My Documents\\Random_Hs_b" + i + ".shp' #;Plume_dissolve1 #", Random_Hs_b_Intersect, "ALL", "", "INPUT")

    # Iterate the counter
    i += 1

In model builder, to avoid overwrites in iterators, you have to use inline variables. See ESRI help:


For your particular use case, example 2 explain what you have to do:

When the iterator Iterate Feature Classes runs, it creates an output variable for both the path and name of the feature class. The value in the Name variable can be used to construct the path to the projected feature class. When the tool executes, %Name% is replaced with the name of the feature class.

Iterator output as inline variable

  • +1 Right on. This answer would be especially good if it were stand alone--perhaps copy and paste Example 2 into your answer or provide a short example. – Aaron Aug 29 '13 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.