How to find which points intersect with a polygon in geodants?

I am trying to use the "intersections" feature in geodata by looking at which points lie inside the polygon. However, only the first function in the frame will return as true. What am I doing wrong?

from geopandas.geoseries import *

p1 = Point(.5,.5)
p2 = Point(.5,1)
p3 = Point(1,1)

g1 = GeoSeries([p1,p2,p3])
g2 = GeoSeries([p2,p3])

g = GeoSeries([Polygon([(0,0), (0,2), (2,2), (2,0)])])

g1.intersects(g) # Flags the first point as inside, even though all are.
g2.intersects(g) # The second point gets picked up as inside (but not 3rd)
+8
source share
3 answers

According to the documentation ( http://geopandas.org/user.html#geoseries ):

Binary operations can be applied between two GeoSeries, in which case the operation is performed stepwise. Two series will be aligned by matching indices.

Your examples should not work. Therefore, if you want to check that each point is in one polygon, you will need:

poly = GeoSeries(Polygon([(0,0), (0,2), (2,2), (2,0)]))
g1.intersects(poly.ix[0]) 

Outputs:

    0    True
    1    True
    2    True
    dtype: bool

, GeoSeries:

points.intersects(poly.unary_union)

. ( ) . :

from shapely.geometry import *

p1 = Point(.5,.5)
p2 = Point(.5,1)
p3 = Point(1,1)

poly = Polygon([(0,0), (0,2), (2,2), (2,0)])

for p in [p1, p2, p3]:
    print(poly.intersects(p))

Shapely , .

+7

, -, , ( , - :

from geopandas.geoseries import *

p1 = Point(.5,.5)
p2 = Point(.5,1)
p3 = Point(1,1)

points = GeoSeries([p1,p2,p3])

poly = GeoSeries([Polygon([(0,0), (0,2), (2,2), (2,0)])])

points.intersects(poly.ix[0])

( ) :

points.intersects(poly.unary_union)
+3

, , :

import geopandas
from shapely.geometry import *

p1 = Point(.5,.5)
p2 = Point(.5,1)
p3 = Point(1,1)

g = Polygon([(0,0), (0,2), (2,2), (2,0)])

def point_inside_shape(point, shape):
    #point of type Point
    #shape of type Polygon
    pnt = geopandas.GeoDataFrame(geometry=[point], index=['A'])
    return(pnt.within(shape).iloc[0])

for p in [p1, p2, p3]:
    print(point_inside_shape(p, g))
0

Source: https://habr.com/ru/post/1589509/


All Articles