I am trying to speed up some current code that I have for looping through lines, getting their segments and ultimately the vertex points in Python.

Can anyone recommend a faster method than what I currently have (perhaps using a map function or dictionary)?

My code is very slow, especially when dealing with large numbers of features.

    sup_list = [row for row in arcpy.da.SearchCursor(sup_lyr, fields)]

    # with arcpy.da.SearchCursor(sup_lyr, fields) as sup_cursor:
    for sup_row in pbar(sup_list, total=result, ascii=True):
        exp = str(tracking_field) + " = {0}".format(sup_row[3])
        sup_sel = arcpy.SelectLayerByAttribute_management(sup_lyr, "NEW_SELECTION", exp)
        inf_sel = arcpy.SelectLayerByLocation_management(inf_lyr, "WITHIN_A_DISTANCE_GEODESIC", sup_sel, "50 Meters"
                                                     , "NEW_SELECTION", "NOT_INVERT")
        sup_vertexList = []
        sup_proj_geom = sup_row[1].projectAs(calc_utm(sup_row[1].firstPoint))
        for part in sup_proj_geom:
            for pnt in part:
                sup_vertexList.append([pnt.Y, pnt.X])
        with arcpy.da.SearchCursor(inf_sel, fields) as inf_cursor:
            for inf_row in inf_cursor:
                inf_vertexList = []
                inf_proj_geom = inf_row[1].projectAs(calc_utm(inf_row[1].firstPoint))
                for part in inf_proj_geom:
                    for pnt in part:
                        inf_vertexList.append([pnt.Y, pnt.X])
New contributor
AttilaTheHun is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Did you try the method explained here? gis.stackexchange.com/questions/26369/… – Richard Morgan Jun 12 at 17:19
  • 1
    Show the lines prior to the code you added, are you using a cursor inside a cursor? What does the function calc_utm do? – BERA Jun 12 at 17:19
  • I have updated the code in my sample to add more at the beginning, if that is helpful. – AttilaTheHun Jun 12 at 17:35
  • 1
    A Spatial Join using the Many To One option would probably make the whole thing faster with the layer of the inner loop being the target layer and the layer with the attribute selection criteria being the join layer. Then there would be a common attribute selection on the Spatial Join features that would match your outer loop layer and you could use a dictionary to match them. The spatial join and attribute dictionary matching would run hundreds of times faster than large sets of spatial selections, and it becomes increasingly efficient as you add more features. – Richard Fairhurst Jun 12 at 17:49
  • 2
    Using Desktop, the easiest way to get a code snippet for a tool is to open the tool like you normally would (Analysis Tools.tbx->Overlay-Spatial Join), set up the parameters the way you want (Target layer, Join Layer, Output Name, JOIN_MANY_TO_ONE, optionally keep only matched features for performance, adjust field list to just include fields you need for performance, WITHIN_A_DISTANCE_GEODESIC, 50 Meters) and run it to test. Then you can open the Results tab (Geoprocessing menu->Results), right click the tool and choose Copy As Python Snippet. Paste into Python and customize as needed. – Richard Fairhurst Jun 12 at 18:34

Your Answer

AttilaTheHun is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.