I am new to Python/ArcPy.

Currently I am trying to iterate through a point feature class and apply the tool "arcpy.AggregatePoints_cartography" to the group of point features for every new time step and as well if the ID (field "ID" not "OBJECTID" changes. The attribute table looks like this:

Attribute table

The field "Monat" includes values up to 12. And the ID-field varies between the values 1 and 2.

I tried to use a search cursor in combination with a counter "LaufID", but the Output Feature class only contains two polygons, but there should be 24 polygons. Please help?

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:/Users/.../Geodatabase.mdb"
laufID = -1

with arcpy.da.SearchCursor("Schatten", ["ID","Tag","Monat"]) as cursorP:
    for pnt in cursorP:
        ID = pnt[0]
        day = pnt[1]
        mon = pnt[2]

        if laufID == -1:
            laufID = ID

        if laufID != ID:
            arcpy.AggregatePoints_cartography("Schatten", "Schatten_Aggr", "100 meters")
  • Is it ok with unique combinations of ID, Tag, Monat or do you want as you say whenever any of them changes? – BERA Mar 20 '17 at 9:11
  • Unique combinations are ok. – map93 Mar 20 '17 at 9:23
  • No it's ArcMap 10.4. – map93 Mar 20 '17 at 9:53

So if you want the aggregated polygons in separate feature classes, this code, will make it. See the last line to optionally merge all polygons in one feature class:

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:/Users/.../Geodatabase.mdb"

arcpy.Statistics_analysis("Schatten", "Combi", "OBJECTID COUNT", "ID; Tag; Monat")

with arcpy.da.SearchCursor("Combi", ["ID, Tag, Monat"]) as cur:
    for row in cur:
        arcpy.MakeFeatureLayer_management("Schatten", "lyr", """ID = {} AND Tag = {} AND Monat = {}""".format(row[0], row[1], row[2]))
        arcpy.AggregatePoints_cartography("lyr", """Aggr_{}_{}_{}""".format(row[0], row[1], row[2]), "100 meters")

# add this line if you want to merge all polygons in one single feature class:
arcpy.Merge_management(arcpy.ListFeatureClasses("Aggr_*"), "All_polygons")
  • The x[0], x[1]... in lines 11/12 are labled with warnings so that a syntax error (invalid syntax) occurs – map93 Mar 20 '17 at 10:38
  • The first part of the Code is working somehow (I think there's a bracket missing in line 11 after x[2],) , but the Loop seems not to work correct, because in the geodatabase only one result Feature class is found. – map93 Mar 20 '17 at 11:00
  • I can't figure it out, but the loop still somehow creates only one polygon. – map93 Mar 20 '17 at 11:16
  • This is the ouput for the print statement: 1 1 1 – map93 Mar 20 '17 at 11:29
  • The zip is not creating all possible combinations of ID, Tag and Monat? – BERA Mar 20 '17 at 11:40

Whilst @map93 was seeking a python solution, this can be done very simply in model builder, the model is nothing more than below:


Note I add the 3 fields to the Group By parameter of the iterator and this gives the unique combinations. If you want to merge them into a single dataset feed the output of Aggregate points into a collects tool and expose that from a sub-model to a master model with a single merge tool.

As iterators do not export to python, you could create this model and call it from your script by importing the toolbox and calling it.

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.