Skip to content

Commit

Permalink
added plot_shape()
Browse files Browse the repository at this point in the history
and fixed a minor thing in get_shape_area()
  • Loading branch information
RolfHut committed Nov 7, 2024
1 parent 2a4a467 commit 753820b
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/ewatercycle/base/forcing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
import shapely
from pyproj import Geod

import shapely.geometry
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeature

import xarray as xr
from pydantic import BaseModel
from pydantic.functional_validators import AfterValidator, model_validator
Expand Down Expand Up @@ -306,15 +311,38 @@ def variables(self) -> tuple[str, ...]:
def get_shape_area(self) -> float:
"""Return the area of the shapefile in m2.
"""
if shape == None:
if forcing.shape == None:
raise ("Shapefile not specified")
shape = fiona.open(forcing.shape)
poly = [shapely.geometry.shape(p["geometry"]) for p in shape][0]
geod = Geod(ellps="WGS84")
poly_area, _ = geod.geometry_area_perimeter(poly)
catchment_area_m2 = abs(poly_area)
return catchment_area_m2

Check warning on line 321 in src/ewatercycle/base/forcing.py

View check run for this annotation

Codecov / codecov/patch

src/ewatercycle/base/forcing.py#L315-L321

Added lines #L315 - L321 were not covered by tests



def plot_shape(self):
if forcing.shape == None:
raise ("Shapefile not specified")

Check warning on line 326 in src/ewatercycle/base/forcing.py

View check run for this annotation

Codecov / codecov/patch

src/ewatercycle/base/forcing.py#L326

Added line #L326 was not covered by tests

shape = fiona.open(forcing.shape)
poly = [shapely.geometry.shape(p["geometry"]) for p in shape][0]
w, s, e, n = poly.bounds # different order than set_extent expects

Check warning on line 330 in src/ewatercycle/base/forcing.py

View check run for this annotation

Codecov / codecov/patch

src/ewatercycle/base/forcing.py#L328-L330

Added lines #L328 - L330 were not covered by tests

# 10 % of the minimum of either the west-east extend or the north-south extend is used as
# padding around the shape.
pad = min(abs(w - e), abs(n - s)) * 0.1

Check warning on line 334 in src/ewatercycle/base/forcing.py

View check run for this annotation

Codecov / codecov/patch

src/ewatercycle/base/forcing.py#L334

Added line #L334 was not covered by tests

plt.figure(figsize=(8, 5))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_geometries(

Check warning on line 338 in src/ewatercycle/base/forcing.py

View check run for this annotation

Codecov / codecov/patch

src/ewatercycle/base/forcing.py#L336-L338

Added lines #L336 - L338 were not covered by tests
poly, crs=ccrs.PlateCarree(), facecolor='#f5b41d', edgecolor='k', alpha=0.8,
)
ax.add_feature(cfeature.COASTLINE, linewidth=1)
ax.add_feature(cfeature.RIVERS, linewidth=1)
ax.add_feature(cfeature.OCEAN, edgecolor="none", facecolor="#4287f5")

Check warning on line 343 in src/ewatercycle/base/forcing.py

View check run for this annotation

Codecov / codecov/patch

src/ewatercycle/base/forcing.py#L341-L343

Added lines #L341 - L343 were not covered by tests

ax.set_extent((w-pad, e+pad, s-pad, n+pad), crs=ccrs.PlateCarree())

Check warning on line 345 in src/ewatercycle/base/forcing.py

View check run for this annotation

Codecov / codecov/patch

src/ewatercycle/base/forcing.py#L345

Added line #L345 was not covered by tests

def __eq__(self, other):
"""Check if two Forcing objects are equal."""
Expand Down

0 comments on commit 753820b

Please sign in to comment.