noaa-ocs-modeling / OCSMesh

OCSMesh is a mesh preparation tool for coastal ocean modeling applications.
https://noaa-ocs-modeling.github.io/OCSMesh/
Creative Commons Zero v1.0 Universal
12 stars 8 forks source link

Rater's `get_multipolygon` should always return valid shape #134

Open SorooshMani-NOAA opened 8 months ago

SorooshMani-NOAA commented 8 months ago

Right now it is possible for the get_multipolygon() to return invalid polygons. This can result in further issues when msh_t() is called which results in confusing errors for the end user. Ideally OCSMesh should always return valid (multi)polygons and throw exception when not possible to return valid (multi)polygons.

SorooshMani-NOAA commented 8 months ago

Example callstack:

ValueError                                Traceback (most recent call last)
Cell In[24], line 1
----> 1 geom_msh_t = geom.msh_t()
      2 hfun = ocsmesh.Hfun(
      3     deepcopy(raster),
      4     hmin=100, #1000 #100
   (...)
      7     base_shape_crs=domain_crs
      8 )
     10 hfun.add_contour(
     11     level=0,
     12     target_size=100,
     13     expansion_rate=0.1,
     14 )

File ~/anaconda/conda_env_pchab/lib/python3.11/site-packages/ocsmesh/geom/base.py:86, in BaseGeom.msh_t(self, **kwargs)
     59 def msh_t(self, **kwargs: Any) -> jigsaw_msh_t:
     60     """Returns the `jigsawpy` representation of the geometry.
     61 
     62     This method calculates the vertex-edge representation of
   (...)
     83     passed to the mesh engine for cartesian meshing.
     84     """
---> 86     return multipolygon_to_jigsaw_msh_t(
     87         self.get_multipolygon(**kwargs),
     88         self.crs
     89     )

File ~/anaconda/conda_env_pchab/lib/python3.11/site-packages/ocsmesh/geom/base.py:146, in multipolygon_to_jigsaw_msh_t(multipolygon, crs)
    143     transformer = Transformer.from_crs(crs, utm_crs, always_xy=True)
    144     multipolygon = ops.transform(transformer.transform, multipolygon)
--> 146 msht = utils.shape_to_msh_t(multipolygon)
    147 msht.crs = crs
    148 if utm_crs is not None:

File ~/anaconda/conda_env_pchab/lib/python3.11/site-packages/ocsmesh/utils.py:2231, in shape_to_msh_t(shape)
   2228     raise ValueError(f"Invalid input shape type: {type(shape)}!")
   2230 if not shape.is_valid:
-> 2231     raise ValueError("Input contains invalid (multi)polygons!")
   2233 for polygon in shape.geoms:
   2234     if np.all(
   2235             np.asarray(
   2236                 polygon.exterior.coords).flatten() == float('inf')):

ValueError: Input contains invalid (multi)polygons!