From d44fbc68c78f610282d3d8e53551d2ecdd3fa8b6 Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Thu, 31 Oct 2024 10:55:19 -0400 Subject: [PATCH] Add point_to_gdf function --- leafmap/common.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/leafmap/common.py b/leafmap/common.py index 74b392d1f3..5519014b7c 100644 --- a/leafmap/common.py +++ b/leafmap/common.py @@ -14920,3 +14920,30 @@ def get_max_pixel_coords( else: return {"max_value": max_value, "coordinates": (x[0], y[0]), "crs": dst_crs} + + +def point_to_gdf(x, y, point_crs="EPSG:4326", to_crs="EPSG:4326", **kwargs): + """ + Convert a point to a GeoDataFrame. + + Args: + x (float): X coordinate of the point. + y (float): Y coordinate of the point. + point_crs (str): Coordinate Reference System of the point. + + Returns: + gpd.GeoDataFrame: GeoDataFrame containing the point. + """ + import geopandas as gpd + from shapely.geometry import Point + + # Create a Point object + point = Point(x, y) + + # Convert the Point to a GeoDataFrame + gdf = gpd.GeoDataFrame([{"geometry": point}], crs=point_crs) + + if to_crs != point_crs: + gdf = gdf.to_crs(to_crs) + + return gdf