Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geopandas point in polygon

I have a GeoDataFrame of polygons (~30) and a GeoDataFrame of Points (~10k)

I'm looking to create 30 new columns (with appropriate polygon names) in my GeoDataFrame of Points with a simple boolean True/False if the point is present in the polygon.

As an example, the GeoDataFrame of Polygons is this:

id  geometry
foo POLYGON ((-0.18353,51.51022, -0.18421,51.50767, -0.18253,51.50744, -0.1794,51.50914))
bar POLYGON ((-0.17003,51.50739, -0.16904,51.50604, -0.16488,51.50615, -0.1613,51.5091))

The GeoDataFrame of Points is like this:

counter     points
   1     ((-0.17987,51.50974))
   2     ((-0.16507,51.50925))

Expected output:

counter          points        foo    bar
   1    ((-0.17987,51.50974))  False  False
   1    ((-0.16507,51.50925))  False  False

I can do this manually by:

foo = df_poly.loc[df_poly.id=='foo']
df_points['foo'] = df_points['points'].map(lambda x: True if foo.contains(x).any()==True else False

But given that I have 30 polygons, I was wondering if there is a better way. Appreciate any help!

like image 228
Kvothe Avatar asked Jan 04 '18 14:01

Kvothe


People also ask

How do you find a point inside a polygon?

One simple way of finding whether the point is inside or outside a simple polygon is to test how many times a ray, starting from the point and going in any fixed direction, intersects the edges of the polygon. If the point is on the outside of the polygon the ray will intersect its edge an even number of times.

How do you find if a point is in a polygon Python?

How to check if a point is inside a polygon in Python. To perform a Point in Polygon (PIP) query in Python, we can resort to the Shapely library's functions . within(), to check if a point is within a polygon, or . contains(), to check if a polygon contains a point.

Can GeoPandas read shapefile?

You can also read in shapefiles as GeoDataFrames. The code below reads the ecoregions shapefile into a GeoDataFrame after first importing GeoPandas. If the shapefile is stored locally you can pass the relative or absolute path in place of the URL, as you did for reading it into a Spark DataFrame.

What is geopandas?

GeoPandas! GeoPandas is an open source project to make working with geospatial data in Python easier. GeoPandas extends the datatypes used by pandas to allow spatial operations on geometric types. The idea is to get our data into a GeoDataFrame which is basically a DataFrame with a geometry type (vector data) Series ( GeoSeries ).

How to convert list of coordinates to shapely polygon in geopandas?

Since GeoPandas uses shapely library for constructing geometries, we convert the list of coordinates to a shapely Polygon object. Note the optional second argument to the h3_to_geo_boundary function which we have set to True which returns the coordinates in the (x,y) order compared to default (lat,lon)

How to check if a polygon is within a given polygon?

you need to iterate over the points and check one at a time if it is within () the polygon specified if you have many polygons and just one point and you want to find out which polygon contains the point Another typical geospatial operation is to see if a geometry intersect or touches another one.

What is a point-in-polygon (PIP)?

That is called a Point-in-Polygon (PIP). point-in-polygon ( PIP) : A spatial operation in which points from one feature dataset are overlaid on the polygons of another to determine which points are contained within the polygons. First let’s get the earthquake data into a GeoDataFrame.


Video Answer


1 Answers

Not really clear what kind of data structures you actually have. Also, all your expected results are False, so that's kind of hard to check. Assuming GeoSeries and GeoDataFrames, I would do this:

from shapely.geometry import Point, Polygon
import geopandas

polys = geopandas.GeoSeries({
    'foo': Polygon([(5, 5), (5, 13), (13, 13), (13, 5)]),
    'bar': Polygon([(10, 10), (10, 15), (15, 15), (15, 10)]),
})

_pnts = [Point(3, 3), Point(8, 8), Point(11, 11)]
pnts = geopandas.GeoDataFrame(geometry=_pnts, index=['A', 'B', 'C'])
pnts = pnts.assign(**{key: pnts.within(geom) for key, geom in polys.items()})

print(pnts)

And that gives me:

        geometry    bar    foo
A    POINT (3 3)  False  False
B    POINT (8 8)  False   True
C  POINT (11 11)   True   True
like image 79
Paul H Avatar answered Oct 17 '22 04:10

Paul H