Good morning everyone;
I am trying to extract (x,y) values from points that are inside a polygon.
DF2 =
Coord_X Coord_Y
773686.694500 9963113.176875
773695.107375 9963167.598125
773707.252750 9963146.229750
773694.324000 9963092.721625
773686.197375 9963082.305875
773687.040875 9963167.699125
773727.531625 9963101.132000
773659.458625 9963152.513625
773696.472625 9963155.930750
773639.421625 9963091.894250
773639.421625 9963080.668875
773746.696125 9963121.726250
773667.951000 9963060.118625
773688.386375 9963140.699875
773694.572625 9963133.687500
773695.140250 9963177.481500
773694.225000 9963067.096375
773686.595000 9963102.952500
773639.401375 9963111.634000
773648.363250 9963113.248875
773640.049375 9963131.062375
773640.697750 9963141.208375
773649.267000 9963122.730250
773694.508250 9963103.219125
773662.812625 9963144.331125
773640.265375 9963123.938500
773653.061250 9963168.799625
773740.705500 9963102.361000
773639.853875 9963103.983125
773663.669000 9963141.736250
773656.719750 9963105.392750
773721.230250 9963148.101000
773705.000000 9963176.000000
773754.354000 9963120.056625
773699.880250 9963085.736625
773686.694500 9963113.176875
773695.107375 9963167.598125
773727.531625 9963101.132000
773696.472625 9963155.930750
773688.386375 9963140.699875
773694.572625 9963133.687500
773694.225000 9963067.096375
773639.401375 9963111.634000
773640.697750 9963141.208375
773640.265375 9963123.938500
773740.705500 9963102.361000
773705.000000 9963176.000000
773727.531625 9963101.132000
773688.386375 9963140.699875
773694.572625 9963133.687500
773694.225000 9963067.096375
773639.401375 9963111.634000
773640.697750 9963141.208375
773640.265375 9963123.938500
773727.531625 9963101.132000
The code is:
from shapely.geometry import Polygon
poly = Polygon([[773642.25342880527, 9963094.3392374925],
[773662.7163213623, 9963189.5526211839],
[773773.26644345513, 9963191.592321692],
[773771.91631169291, 9963098.0998664293],
[773667.59566028137, 9963054.4342967868]])
x,y = poly.exterior.xy
plt.scatter(DF2["Coord_X"], DF2["Coord_Y"], marker='+',linewidth=1,c="r", s=90)
plt.plot(x, y, color='y', alpha=2, linewidth=2, solid_capstyle='round', zorder=2)
plt.show()
If we draw the polygon and the points, they are shown below.
Since I could extract only points that are inside the polygon, maybe someone has some idea, thanks a lot for your help.
You are using
shapely
and that library already gives you the necessary tools to check whether or not a given point is inside a given polygon:You just have to couple this with pandas to filter all those rows in which that condition is true. In your case, I see that you have all the data in a dataframe called
DF2
, so you could do:Basically,
DF2.apply(lambda, axis=1)
it applies the lambda function in question to each row of the dataframe, and returns a Series with the result of each execution. What we do is call that lambdapoly.contains()
, creating aPoint
with the coordinates pulled from that row. The result of each call to lalambda
will beTrue
orFalse
, so in the end elDF2.apply()
returns a series (column) ofTrue
s andFalse
s. We apply that as an index ofDF2[]
to select only those rows in which it has leftTrue
.The result is now only the rows that correspond to points within the polygon.
To demonstrate it, I will paint with a blue X all the points of
DF2
and with a red point all the points of the resulting dataframedentro
(in addition to the polygon in yellow):And this is the result: