I have two point shapefiles A (say, 10 points) and B (say, 30 points). I would like to create a shapefile with the lines between all points in A and all points in B (so, 300 lines). I have not found any tool to do that in geopandas. The closest I found in QGIS is the Distance to nearest hub (line to hub)
tool in QGIS. Although that creates only the lines for the closest point, I could split A into 10 files with a single point and then calculate the 300 lines. Does a similar tool exist using Geopandas?
Add a comment
|
1 Answer
You only need the Python itertools module for the Cartesian product of the geometries of the two GeoDataFrames
import itertools
import geopandas as gpd
from shapely.geometry import LineString
pt1 = gpd.read_file('points1.shp')
pt2 = gpd.read_file('points2.shp')
geom1 = pt1.geometry.values
geom2 = pt2.geometry.values
# Cartesian product
geom = []
for p1,p2 in list(itertools.product(geom1,geom2)):
geom.append(LineString([p1,p2]))
result= gpd.GeoDataFrame({'geometry':geom})