Skip to content

Commit d7eefe0

Browse files
committed
Compute shape of a whole track
1 parent 5a9342a commit d7eefe0

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/py_eddy_tracker/observations/tracking.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from .. import VAR_DESCR_inv
3030
from ..generic import build_index, cumsum_by_track, distance, split_line, wrap_longitude
31-
from ..poly import create_vertice_from_2darray, polygon_overlap
31+
from ..poly import create_vertice_from_2darray, merge, polygon_overlap
3232
from .observation import EddiesObservations
3333

3434
logger = logging.getLogger("pet")
@@ -489,6 +489,43 @@ def extract_with_mask(
489489
new.track = id_translate[new.track]
490490
return new
491491

492+
def shape_polygon(self, intern=False):
493+
"""
494+
Get polygons which enclosed each track
495+
496+
:param bool intern: If True use speed contour instead of effective contour
497+
:rtype: list(array, array)
498+
"""
499+
xname, yname = self.intern(intern)
500+
return [merge(track[xname], track[yname]) for track in self.iter_track()]
501+
502+
def display_shape(self, ax, ref=None, intern=False, **kwargs):
503+
"""
504+
This function will draw shape of each track
505+
506+
:param matplotlib.axes.Axes ax: ax where drawed
507+
:param float,int ref: if defined all coordinates will be wrapped with ref like west boundary
508+
:param bool intern: If True use speed contour instead of effective contour
509+
:param dict kwargs: keyword arguments for Axes.plot
510+
:return: matplotlib mappable
511+
"""
512+
if "label" in kwargs:
513+
kwargs["label"] = self.format_label(kwargs["label"])
514+
if len(self) == 0:
515+
x, y = [], []
516+
else:
517+
polygons = self.shape_polygon(intern)
518+
x, y = list(), list()
519+
for p_ in polygons:
520+
x.append((nan,))
521+
y.append((nan,))
522+
x.append(p_[0])
523+
y.append(p_[1])
524+
x, y = concatenate(x), concatenate(y)
525+
if ref is not None:
526+
x, y = wrap_longitude(x, y, ref, cut=True)
527+
return ax.plot(x, y, **kwargs)
528+
492529
def plot(self, ax, ref=None, **kwargs):
493530
"""
494531
This function will draw path of each track

src/py_eddy_tracker/poly.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from numba import njit, prange
77
from numba import types as numba_types
8-
from numpy import array, concatenate, empty, ones, pi, where
8+
from numpy import array, concatenate, empty, nan, ones, pi, where
99
from numpy.linalg import lstsq
1010
from Polygon import Polygon
1111

@@ -379,6 +379,33 @@ def get_wrap_vertice(x0, y0, x1, y1, i):
379379
return create_vertice(x0_, y0[i]), create_vertice(x1_, y1[i])
380380

381381

382+
def merge(x, y):
383+
"""
384+
Merge all polygon of the list
385+
386+
:param array x: 2D array for a list of polygon
387+
:param array y: 2D array for a list of polygon
388+
:return: Polygons which enclosed all
389+
:rtype: array, array
390+
"""
391+
nb = x.shape[0]
392+
p = None
393+
for i in range(nb):
394+
p_ = Polygon(create_vertice(x[i], y[i]))
395+
if p is None:
396+
p = p_
397+
else:
398+
p += p_
399+
x, y = list(), list()
400+
for p_ in p:
401+
p_ = array(p_).T
402+
x.append((nan,))
403+
y.append((nan,))
404+
x.append(p_[0])
405+
y.append(p_[1])
406+
return concatenate(x), concatenate(y)
407+
408+
382409
def vertice_overlap(x0, y0, x1, y1, minimal_area=False):
383410
r"""
384411
Return percent of overlap for each item.

0 commit comments

Comments
 (0)