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 spectral_to_gdf method #118

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion docs/examples/emit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
},
{
Expand All @@ -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": {},
Expand Down
36 changes: 36 additions & 0 deletions hypercoast/hypercoast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Loading