Grouping feature class rows based on a common attribute not working

464
3
Jump to solution
05-03-2022 08:05 AM
EricEagle
Occasional Contributor III

I've got a point feature class that is a result of BearingDistanceToLine --> GeneratePointsAlongLines.  It has generated around 150 points associated with about 400 lines.

I'm attempting to go in and add curvature offsets to each point in this feature class based on its distance from its associated origin point.  The easiest way for me to do this is to bundle the features by the ORIG_FID field and then sort by OID@ (first is the origin, last is the furthest distance).  Similar to using a track ID in trajectory data.

I'm trying to put it all in a dictionary that should look like this, and then use built in math package to do the calculations across the dictionary, and construct a new feature class (maybe faster than doing UpdateCursor).  Anyway it should look like this:

 

{1: [row, row, row, row],
 2: [row, row, row, row],
 ....
}

 

 

Here is how I'm attempting to implement, probably poorly:

 

with arcpy.da.SearchCursor("samplepoints", ["OID@", "ORIG_FID", "curvature"]) as cursor:
    uniques = sorted({row[1] for row in cursor})
    c = {i: [row for row in cursor if row[1] == i] for i in uniques}

 

This ends up creating a dictionary that does pull the unique ORIG_FID in as keys, and sets the value as a list, but adds no rows.  Even though each ORIG_FID should be common to around 150 rows.

Why is it not adding the rows even though row[1] should match i?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

Does this do it?

import arcpy
from collections import defaultdict

c = defaultdict(list)
with arcpy.da.SearchCursor("samplepoints", ["OID@", "ORIG_FID", "curvature"]) as cursor:
    for row in cursor:
        c[row[1]].append(row)

View solution in original post

3 Replies
by Anonymous User
Not applicable

I'm not fully following what you are trying to do here, but it looks like you should be using an UpdateCursor if you're trying to change values or an InsertCursor if you're trying to add new rows.

Perhaps your c value (or maybe the dictionary value?) needs to be assigned back to the data source using one of the cursors I linked.

0 Kudos
BlakeTerhune
MVP Regular Contributor

Does this do it?

import arcpy
from collections import defaultdict

c = defaultdict(list)
with arcpy.da.SearchCursor("samplepoints", ["OID@", "ORIG_FID", "curvature"]) as cursor:
    for row in cursor:
        c[row[1]].append(row)
EricEagle
Occasional Contributor III

Yes, perfect!  Thanks!

0 Kudos