diff --git a/docs/examples/emit.ipynb b/docs/examples/emit.ipynb index fdaa3ad7..3e7ee097 100644 --- a/docs/examples/emit.ipynb +++ b/docs/examples/emit.ipynb @@ -115,7 +115,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To access the selected spectral profiles from the mouse clicked location, use the `Map.spectral_to_df()` method to convert the spectral profiles to a pandas DataFrame." + "To access the selected spectral profiles from the mouse clicked location as a Pandas DataFrame, use the `Map.spectral_to_df()` method." ] }, { @@ -127,6 +127,22 @@ "m.spectral_to_df()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To access the selected spectral profiles from the mouse clicked location as a GeoPandas GeoDataFrame, use the `Map.spectral_to_gdf()` method." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m.spectral_to_gdf()" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/hypercoast/hypercoast.py b/hypercoast/hypercoast.py index 8691b219..e71bf3c1 100644 --- a/hypercoast/hypercoast.py +++ b/hypercoast/hypercoast.py @@ -772,6 +772,42 @@ def spectral_to_df(self, **kwargs): df = pd.DataFrame(self._spectral_data, **kwargs) return df + def spectral_to_gdf(self, **kwargs): + """Converts the spectral data to a GeoPandas GeoDataFrame. + + Returns: + gpd.DataFrame: The spectral data as a pandas DataFrame. + """ + import geopandas as gpd + from shapely.geometry import Point + + df = self.spectral_to_df() + + if len(df) == 0: + return None + + # Step 1: Extract the coordinates from the columns + df = df.rename(columns={"wavelength": "latlon"}) + coordinates = [col.strip("()").split() for col in df.columns[1:]] + coords = [(float(lat), float(lon)) for lat, lon in coordinates] + + # Step 2: Create Point geometries for each coordinate + points = [Point(lon, lat) for lat, lon in coords] + + # Step 3: Create a GeoDataFrame + df_transposed = df.set_index("latlon").T + + # Convert the column names to strings to ensure compatibility with GeoJSON + df_transposed.columns = df_transposed.columns.astype(str) + + # Create the GeoDataFrame + gdf = gpd.GeoDataFrame(df_transposed, geometry=points, **kwargs) + + # Set the coordinate reference system (CRS) + gdf = gdf.set_geometry("geometry").set_crs("EPSG:4326") + + return gdf + def spectral_to_csv(self, filename, index=True, **kwargs): """Saves the spectral data to a CSV file.