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?
Solved! Go to Solution.
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)
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.
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)
Yes, perfect! Thanks!