The GeoDataFrame
import geopandas as gpd
g1 = gpd.GeoDataFrame.from_file("poly_intersect.shp")
g1.shape
(4, 3)
1) You can use the itertools module
a) If you want to merge the intersections of the overlapping polygons
import itertools
geoms = g1['geometry'].tolist()
intersection_iter = gpd.GeoDataFrame(gpd.GeoSeries([poly[0].intersection(poly[1]) for poly in itertools.combinations(geoms, 2) if poly[0].intersects(poly[1])]), columns=['geometry'])
intersection_iter.to_file("intersection_iter.shp")
Union
union_iter = intersection_iter.unary_union
b) If you want to merge the intersected polygons change intersection
by union
(all the polygons overlap in my example)
2) You can use GeoPandas Overlay
a)
auto_inter = gpd.overlay(g1, g1, how='intersection')
auto_inter.shape
(7,4)
The resulting GeoDataframe
GeoPandas add the intersection geometries to the existing geometries, therefore
intersection = auto_inter[4:7]
intersection.to_file("intersection.shp")
Union
union = intersection.unary_union
b) use gpd.overlay(g1, g1, how='union')
The geopandas.sjoin
function only supports the 'intersects', 'within' and 'contains' predicates, and not a "nearest" one.
You can write a custom function to find the id of the nearest linestring for each polygon, and then merge on that. This could look like:
def nearest_linestring(polygon, df_lines):
idx = df_lines.geometry.distance(polygon).idxmin()
return df_lines.loc[idx, 'id']
df_polygon['id_nearest_line'] = df_polygon.geometry.apply(nearest_linestring, df_lines=df_lines)
pd.merge(df_polygon, df_lines, right_on='id_nearest_line', left_on='id',how='inner')
However, an important remark with this approach: it will only find a single nearest one, so if you had for a certain polygon multiple linestrings that are intersecting with it, it will not give them all. It should be possible to update the function for that though.
Second remark: if you have a lot of data, calculating the distance for all linestrings like the in the function above might not be very efficient. You could use spatial index to improve this, but I would only worry about that if the speed turns out to actually be a problem.
Best Answer
Intersect, sort by area and drop duplicates:
Example: