Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I´ve got a point feature class with some 7.000.000 points and I´m trying to check if any of these points is simply intersected by several polygon feature classes using 'for' loop and 'arcpy.SelectLayerByLocation'. Polygon feature classes contain just 1 polygon feature each and are saved in a folder as shapefiles.

I can create a feature layer from points and then use Select Layer by Location with "INTERSECT" parameter but my problem is that the polygon feature can be intersected by e.g. 700.000 points and the process of selection takes ages.

My question is: is there any way to break the Select Layer by Location process after it finds a first point that is intersected by the polygon? Or do I have to wait until the whole process finishes? The code is simple:

# Create a layer from feature class with 7.000.000 points 
arcpy.MakeFeatureLayer_management(pointFC, "point_lyr")

# Builds a list of polygon feature classes within a workspace
fcs = arcpy.ListFeatureClasses("", "polygon")
for polygon in fcs:
    arcpy.SelectLayerByLocation_management("point_lyr", "INTERSECT", polygon, "", "NEW_SELECTION")

    # And now instead of:

    # Count selected points after the process ends and do something if condition is met
    points_count = int(arcpy.GetCount_management("point_lyr").getOutput(0))
    if points_count != 0:
        something

    # I´d rather have

    if a point intersects the polygon for the first time:
        tell me that this happened, then break this loop and start another polygon loop

Is it possible to break a running arcpy process?

Running on Win7, ArcGIS for Desktop Basic 10.1 + Python 2.7

share|improve this question
1  
Welcome to GIS SE! You've phrased this as an ArcPy Question but really you are simply asking about the Select Layer By Location tool. How many polygons are in the feature class which you need to process? Or are you only processing one polygon against the 7 million points? What version of ArcGIS for Desktop are you using? Please use the edit button to revise/clarify it with these details. –  PolyGeo Apr 14 at 11:41
    
PolyGeo: Thank you for your response and my apologies for not being so clear. I´ve added some details about my intention, a little bit of code to clarify all this and a specification of software I use. –  jonlew Apr 14 at 12:34

1 Answer 1

up vote 4 down vote accepted

I would approach this issue in this way. One can create a feature layer consisting of just one point feature and check whether it intersects the polygon feature layer (using arcpy.da.SearchCursor). However, this would be a bit inefficient since you are dealing with so many features.

However, we could create a feature layer and check whether it intersects the polygon layer by taking into multiple points (a bulk).

First, I'd calculate a new field, say CounterField, in the point feature class (Accumulative and sequential calculations):

Expression:
autoIncrement()

Expression Type:
PYTHON_9.3

Code Block:
rec=0
def autoIncrement():
    global rec
    pStart = 1 #adjust start value, if req'd 
    pInterval = 1 #adjust interval value, if req'd
    if (rec == 0): 
        rec = pStart 
    else: 
        rec = rec + pInterval 
    return rec

Thereafter, I would run this code that would evaluate the point features in small portions and as soon some that intersecting are found, break the execution.

import arcpy
import sys
import time
arcpy.env.overwriteOutput = True #to overwrite the feature layer

def rowsinbulk():
    counter = 0
    oldbulk = 0
    bulk = 1000
    while bulk < 1000000: #number of features you have
        arcpy.MakeFeatureLayer_management(pointFC, "point_lyr","""CounterField between {0} and {1}""".format(oldbulk,bulk))
        arcpy.SelectLayerByLocation_management("point_lyr", "INTERSECT", polygonFC)
        points_count = int(arcpy.GetCount_management("point_lyr").getOutput(0))
        if points_count != 0:    
            print "run {0} times".format(counter)
            EndTime = time.clock()
            print "Finished in %s seconds" % (EndTime - StartTime)
            print "a point intersecting polygon found!"
            sys.exit()
        else:
            pass #no intersecting point found
        oldbulk = bulk
        bulk = bulk + 1000
        counter = counter + 1
        print oldbulk
        print bulk

rowsinbulk()

You would experiment a bit with the bulk size, but the code above should scale really well. I am able to process 100,000 point fc within several seconds when using 10,000 bulk incrementation size (provided that there is only one intersecting point and it has the highest CounterField value).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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