-1

I have a shapefile of all the counties that make up my state. Using the shapefile (which contains geometric for the district polygons) I was able to use geopandas to plot the shapes in a figure. I have some addresses that I have geocoded into latitude and longitude coordinates and I'd like to be able to determine which county (or polygon) the coordinates are within. I see that geopandas has a within function, but I don't quite understand how to use it.

The end goal will be for a user to input and address and the program returns the county name. There are only a few dozen counties so I was thinking of using a for loop to iterate through the rows and check each polygon to see if the provided coordinate lies within.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 23 '22 at 01:51

1 Answers1

-1

I believe shapely's documentations will be helpful to understand how within function works. But, i order to give you more insight, try code below:

from shapely.geometry import Point, Polygon
pol = Polygon([[0,0], [0,2], [2,2], [2,0]])
pnt = Point(1,1)
pnt.within(pol)

result:

True

Explanation

pol is a rectangle(square) with 2 units on each side. We used created a new point with coordinates of (1,1) which locates exactly in the center of the square. Using within function on the Point object and giving the Polygon as an argument to this function you can check if the point is in the area or not.

TheFaultInOurStars
  • 3,464
  • 1
  • 8
  • 29