Performing selections
The previous subsetting example is one way to select data. There are many other ways to subset data for further analysis. In this section, we'll examine some of them.
Point in polygon formula
We briefly discussed the point in polygon formula in Chapter 1, Learning Geospatial Analysis with Python, as a common type of geospatial operation. You'll find that it is one of the most useful formulas out there. The formula is relatively straightforward. The following function performs this check using the Ray Casting method. This method draws a line from the test point all the way through the polygon and counts the number of times it crosses the polygon boundary. If the count is even, the point is outside the polygon. If it is odd, then it's inside. This particular implementation also checks to see if the point is on the edge of the polygon, as shown here:
def point_in_poly(x,y,poly): # check if point is a vertex if (x,y) in poly: return True # check if point is on...