Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add points_to_line function #874

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14236,3 +14236,64 @@ def convert_geometry(x):
gdf.set_crs(crs, inplace=True)

return gdf


def points_to_line(
data: Union[str, pd.DataFrame],
src_lat: str,
src_lon: str,
dst_lat: str,
dst_lon: str,
crs: str = "EPSG:4326",
**kwargs: Any,
) -> "gpd.GeoDataFrame":
"""
Converts source and destination coordinates into a GeoDataFrame with LineString geometries.

Args:
data (Union[str, pd.DataFrame, gpd.GeoDataFrame]): Input data which can be a file path or a DataFrame.
src_lat (str): Column name for source latitude.
src_lon (str): Column name for source longitude.
dst_lat (str): Column name for destination latitude.
dst_lon (str): Column name for destination longitude.
crs (str, optional): Coordinate reference system. Defaults to "EPSG:4326".
**kwargs (Any): Additional arguments passed to the file reading functions.

Returns:
gpd.GeoDataFrame: A GeoDataFrame with LineString geometries.
"""
import geopandas as gpd
from shapely.geometry import LineString

if isinstance(data, str):
if data.endswith(".parquet"):
gdf = pd.read_parquet(data, **kwargs)
elif data.endswith(".csv"):
gdf = pd.read_csv(data, **kwargs)
elif data.endswith(".json"):
gdf = pd.read_json(data, **kwargs)
elif data.endswith(".xlsx"):
gdf = pd.read_excel(data, **kwargs)
else:
gdf = gpd.read_file(data, **kwargs)

elif isinstance(data, pd.DataFrame) or isinstance(data, gpd.GeoDataFrame):
gdf = data.copy()
else:
raise ValueError(
"Unsupported data type. Please provide a file path or a DataFrame."
)

# Assuming you have a GeoDataFrame 'gdf' with the source and destination coordinates
def create_polyline(row):
source_point = (row[src_lon], row[src_lat])
dst_point = (row[dst_lon], row[dst_lat])
return LineString([source_point, dst_point])

# Apply the function to create the polyline geometry
gdf["geometry"] = gdf.apply(create_polyline, axis=1)

# Set the GeoDataFrame's geometry column to the newly created geometry column
gdf = gdf.set_geometry("geometry")
gdf.crs = crs
return gdf
Loading