I would like to be able to run a whole load of aggregate polygons with a variety of aggregation distances, using Arcpy. I have successfully done it using model builder, but specifically want to do it using ArcPy as I am comparing the environments for research as a part of degree. The code I have below is a start, but it errors out and was hoping someone would be able to help!

import arcpy
from arcpy import env

env.workspace = "E:/temp/Data.gdb"

in_point_features = "E:/points.shp"
out_feature_class = "E:/points"+aggregation_distance ".shp"
aggregation_distance = ["100 meters", "200 Meters"]

arcpy.AggregatePoints_cartography(in_point_features, out_feature_class, aggregation_distance)

1 Answer 1


Welcome to GSE. I guess you're getting an error due to your out_feature_class as it's defined incorrectly. Also, if you want to run this process for multiple aggregation distances, you need to create a loop.

Try this:

import arcpy
from arcpy import env

env.workspace = r"E:\temp\Data.gdb"

in_point_features = r"E:\points.shp"
aggregation_distance = ["100 meters", "200 Meters"]

for distance in aggregation_distance:
    out_feature_class = """E:\points_{0}_meters.shp""".format(distance[0:2])
    arcpy.AggregatePoints_cartography(in_point_features, out_feature_class, distance)

Note the three following things:

  • The outfile is created within the loop, so it is set differently for each loop. Note that spaces in file names are problematic. Therefore I used the [0:2] expression and added the _meters separately.
  • Try to use r"C:\Pathname" instead of "C:\Pathname". This helps to prevent errors with paths. Also, \ usually works better than / in paths.
  • By using the built-in function .format(), putting together strings becomes much easier. Simply insert variables in {} and refer to them in the format function.

In case this wasn't what you're after, please update your question with some extra information.

Update 4th of March '16:

I was thinking about the variable for the output shape name again. The method in my code isn't the prettiest of options as you'd have problems with distances that only have 2 digits or more than 3. The easiest option would be to create another list with the plain numbers of your aggregation_distance parameter. Or you could use a .find() approach and locate the space in the aggregation_distance strings and isolate everything to the left of the space.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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