I have a script that I want to: run through polygons in a shapefile representing service areas > select each area > then select points from a different layer that fall within that selected polygon. (This last part isn't in my code yet.)

If I run each individual part on its own things run fine, but when I run the whole script it runs the first iteration and then I get:

Runtime Error!

Program: C:\ProgramFiles(x86)\ArcGis\Desktop10.3\bin\ArcMap.exe

R6025 - pure virtual function call"

and then it crashes.

This is my code:

import arcpy

arcpy.env.workspace = "X:\GISData\blah.gdb"

# loops through areas
with arcpy.da.SearchCursor("ServiceAreas", "Area") as cursor:
    for row in cursor:
        # select an area in the areas layer
        where_clause = "Area= '{0}'".format(row[0])
        arcpy.SelectLayerByAttribute_management("ServiceAreas","NEW_SELECTION", where_clause)

If I just run the 'with/as' and for/in part of the code, it runs fine, and prints out a list of areas as expected.

with arcpy.da.SearchCursor("ServiceAreas", "Area") as cursor:
    for row in cursor:
        # select an area in the SCADA areas layer
        where_clause = "Area= '{0}'".format(row[0])

If I just run the 'select layer by attribute' line (hard-coding my where_clause), it runs fine, selecting the appropriate polygon.

arcpy.SelectLayerByAttribute_management("ServiceAreas","NEW_SELECTION", "Area= 'LR2'")

Clearing my selection between loops doesn't help. This has me stumped. Can anyone point me in the right direction? I'm open to changing the structure of my code entirely if I'm going about this in a weird way.

  • 1
    What is the purpose of your script? It makes no sense to me. If you have a cursor, you can access the shape, which obviates the need for a SelectLayerByAttribute. Also, please specify the exact version of software in your question (if it isn't 10.3.1 will all patches, you should probably do that first) – Vince Mar 15 '18 at 22:25
  • I'm not sure if all patches are installed. I'll check when I get to work in the morning. What do you mean by access the shape with cursor? I want to select the polygon so I can use it in a later select by location step. Will that still work if I just use the cursor? – LGelb Mar 16 '18 at 0:06
  • @Vince is correct, if you specify SHAPE@ in the field list of the search cursor, you can use that geometry for the select by location operation. Otherwise, try creating "ServiceAreas" as a feature layer first and use the layer in the SelectLayerByAttribute function. – Dan Mar 16 '18 at 3:45
  • Or you could Intersect (desktop.arcgis.com/en/arcmap/10.3/tools/analysis-toolbox/…) the points against the polygons and then do your analysis on the output. This would be quicker and easier than setting up a cursor and doing multiple select by locations, depending on what you're trying achieve. – Dan Mar 16 '18 at 3:50
  • 1
    Or Spatial Join – BERA Mar 16 '18 at 6:34

The code below will do what you're looking for, however, as mentioned in the comments there may be more efficient methods to achieve what you're trying to do.

import arcpy

arcpy.env.workspace = r"E:\Temp\blah.gdb"

# loops through polygons features and selects points that fall within them
aPoints = arcpy.MakeFeatureLayer_management("POINTS_Random", "aPoints")

with arcpy.da.SearchCursor("Polygon", ["SHAPE@", "PolygonID"]) as cursor:
    for row in cursor:
        # select an area in the areas layer
        aPoly = row[0]
        aPolygonID = row[1]
        arcpy.SelectLayerByLocation_management(aPoints, "INTERSECT", aPoly, "#", "NEW_SELECTION")

        # do something to the selected points.
        arcpy.CalculateField_management(aPoints, "Name", "'" + aPolygonID + "'", "PYTHON_9.3")

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.