Skip to content

Commit

Permalink
Cleanup and remove shapely2 func
Browse files Browse the repository at this point in the history
  • Loading branch information
SorooshMani-NOAA committed Sep 14, 2023
1 parent 91fb3c9 commit 138f6c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
25 changes: 8 additions & 17 deletions ocsmesh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from scipy.interpolate import ( # type: ignore[import]
RectBivariateSpline, griddata)
from scipy import sparse, constants
from shapely import force_2d
from shapely.geometry import ( # type: ignore[import]
Polygon, MultiPolygon,
box, GeometryCollection, Point, MultiPoint,
Expand Down Expand Up @@ -2259,38 +2258,30 @@ def shape_to_msh_t_2(shape: Union[Polygon, MultiPolygon]) -> jigsaw_msh_t:
if not isinstance(shape, gpd.GeoDataFrame):
gdf_shape = gpd.GeoDataFrame(geometry=[shape])

df_pts = (
df_lonlat = (
gdf_shape
.geometry
.transform(force_2d)
.boundary
.explode(ignore_index=True)
.map(lambda i: i.coords)
.explode()
.apply(lambda s: pd.Series({'lon': s[0], 'lat': s[1]}))
.reset_index() # put polygon index in the dataframe
.drop_duplicates() # drop duplicates within polygons
)


df_pts_2 = (
pd.DataFrame(
df_pts.tolist(),
index=df_pts.index,
columns=['lon', 'lat']
)
.reset_index()
.drop_duplicates() # Polygon index is used for duplicate removal
)
df_seg = (
df_pts_2.join(
df_pts_2.groupby('index').transform(np.roll, 1, axis=0),
df_lonlat.join(
df_lonlat.groupby('index').transform(np.roll, 1, axis=0),
lsuffix='_1',
rsuffix='_2'
).dropna()
.set_index('index')
)

df_nodes = (
df_pts_2.drop(columns='index')
.drop_duplicates()
df_lonlat.drop(columns='index') # drop to allow cross poly dupl
.drop_duplicates() # drop duplicates across multiple polygons
.reset_index(drop=True) # renumber nodes
.reset_index() # add node idx as df data column
.set_index(['lon','lat'])
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies = [
"jigsawpy", "matplotlib", "netCDF4", "numba",
"numpy>=1.21", # introduce npt.NDArray
"pyarrow", "rtree", "pyproj>=3.0", "rasterio", "scipy",
"shapely>=2", "triangle", "typing_extensions", "utm",
"shapely", "triangle", "typing_extensions", "utm",
]
dynamic = ["version"]

Expand Down

0 comments on commit 138f6c1

Please sign in to comment.