How can I access related records with python

2494
2
11-02-2017 02:10 PM
jonjones
New Contributor

I have a feature class of signposts.  Each signpost has one or more signs on it, which are stored in a related table (with a relationshipClass).  Each sign has a code which tells which kind of sign it is.  I want the user to be able to (in a web application) enter a specific code and find all the signs that have that code, and display them on a map.  Since the signs are in a table and don't have geometry, the only way to do this is find the signs and then their related signposts.  (as far as I know...) 

I have a client-side javascript API solution, but it is so non-robust I have to severely limit the geographic extent of the query so it doesn't just time out.  Limit it to the point that it is pretty useless.  So I want to make a geoprocessing service to handle this, but I CAN FIND NO WAY TO ACCESS RELATED RECORDS THROUGH MODELBUILDER OR PYTHON.  Despite the fact that in ArcMap the related records come back automatically and can be gotten at with the touch of a button. 

I'm at wit's end.

0 Kudos
2 Replies
MitchHolley1
MVP Regular Contributor

I'm not familiar with javascript API or how it plugs into your web service.  However, the way I usually handle related records and searching between two tables is through a dictionary. 

signs = r'path_to_signs_fc'
relTable = r'path_to_related_table'

d = {row[0]:row[1] for row in arcpy.da.SearchCursor(signs, ['code','sign'])}

with arcpy.da.SearchCursor(relTable, ['code','signs']) as cursor:
    for row in cursor:
        if row[0] in d:
            print d, row[0] #you can print, dump data into another container, etc... 
del cursor

Example of what the key, value pair dictionary looks like. 

jonjones
New Contributor

Well, it turns out that relationshipClass is not real good at handling a bunch of records at once.  I ended up just ignoring that and joining the features to the table and running select queries on that.  Works great in ArcMap, should be ok as a geoprocessing service, the only trouble now is handling the rendering of  thousands of points the user is inevitably going to try to query. 

0 Kudos