Here's my starting point, I have the following two point datasets
data1 = {'x': [580992.9581, 580992.9111, 580993.3539, 580993.4957, 580991.9780],
'y': [4275268.7194, 4275267.6678, 4275267.2215, 4275268.1749, 4275267.6297]}
df1 = pd.DataFrame(data1)
data2 = {'x': [580992.7155, 580993.4258, 580992.7421, 580991.7034, 580992.7554, 580993.5837, 580993.0002, 580993.8348, 580991.2176, 580992.0536],
'y': [4275267.2733, 4275267.7455, 4275266.7449, 4275268.8644, 4275266.4493, 4275267.5785, 4275268.5525, 4275268.9687, 4275267.6972, 4275267.7937]}
df2 = pd.DataFrame(data2)
I'm wanting to, in the most efficient way, calculate the Convex Hull of each of the dataframes, then determine which points are in the convex hull of the other dataframe.
So create the hull_of_df1
and the hull_of_df2
.
To do this either Scipy's `ConvexHull(df1)' or in Shapely, let's use scipy.
from scipy.spatial import ConvexHull
hull_of_df1 = ConvexHull(df1)
hull_of_df2 = ConvexHull(df2)
Now, I need to assign True
to any points from df1
that are in hull_of_df2
.
The long way to do this would be something like:
for point in df1:
if point.within(hull_of_df2):
df1['in_hull'] = True
else:
df1['in_hull'] = False
And repeat for the other dataframe and other convex hull.
Ideally I just want to generate a new column and append it to the end of the df1
dataframe. So for those indexes which are within df1
assign in_hull == True
. I do not want to delete any points in the dataframes, just record which points are and arn't in the opposite convex hull.