AnsweredAssumed Answered

Using Python to determine majority and update field?

Question asked by ejmason_AKDOT on Nov 27, 2018
Latest reply on Jan 29, 2019 by ejmason_AKDOT

So this is my first post to the community AND i'm also new with python (i'm a Planner, not Developer), so i'm open to any helpful critiques and feedback. But here is my situation, first with the preface THEN the python bit:


I work for Transportation and we have federal reports based on few categories, but for clarity and simplicity's sake, I'll list one: Passing Zones.

Collecting Passing Zones is fairly straightforward using Collector, but the issue is that it just creates a linear collection that is not "reportable" being that it is not tied to our Route ID's (very concise explanation).

My current workaround is to take the layer from Collector and, through a mix of Vertices to Points and Locate Feature Along Route geoprocessing, attempt to allocate the Linear Collection to the proper Route ID's to make them "reportable".


However, since many of the sections obviously have intersections/ramps/parallel roadways, I end up with points that have different Route ID's in the standalone table under field "Match ID", even if I switch the parameters to a very close span. 


For illustration: (ObjectID: 1, Route MatchID: 4321024X000, Original Feature ID: 1693) 

The ObjectID indicates the Point created from vertices, MatchID indicates which route they were matched, and Feature ID is the linear feature that the points originated from.

So if I have 30 different Points (30 different ObjectID's) all with the same Original Feature ID ( i.e. 1693), majority of the MatchID's end up being the same with only a few outliers. I could change them individually if the dataset wasn't around 100,000 points for this years collection.


now Python:

In my very limited knowledge, I was using arcpy.da.SearchCursor to organize the table into a dictionary that is grouped by Original FeatureID. 

Then I was intending to use Counter from Collections to determine majority (most_common()) for the MatchID and then use UpdateCursor to update the MatchID according to majority for each Original FeatureID.


Two problems: Currently, while I have the dictionary grouped by Original FeatureID (since multiple ObjectID's have the same FeatureID), it loops through till the end and stores the last value as the Original FeatureID (terminology is probably incorrect). I need the Original FeatureID to become its own entity before it goes to the next sequential value for FeatureID.



Also, I only know how to use Counter when I add the values myself. Is there a way to be able to implement the Original FeatureID and have Counter just determine most_common() for the MatchID? or is there an alternative to my idea?


Let me know how I can explain it better! I apologize in advance for my lack of terminology and processes...

and here is my basic code layout and I attached an example spreadsheet with a small sample for reference

field_object = 'OBJECTID'
field_match = 'MatchID'
field_fid = 'ORIG_FID'
fidDict = {}

with arcpy.da.SearchCursor(ftable, fields, sql_clause=(None, 'ORDER BY ORIG_FID')) as rows:
for row in rows:
field_object = row[0]
field_match = row[1]
field_fid = row[2]
if not field_fid in fidDict:
fidDict[field_fid] = [[field_object, field_match], 1]
elif not field_object in fidDict[field_fid][0]:
fidDict[field_fid][1] += 1