Skip to content

Commit

Permalink
Make clip check min_length, fixes #374
Browse files Browse the repository at this point in the history
  • Loading branch information
anitagraser committed Apr 10, 2024
1 parent d1ac615 commit 87b4a80
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
36 changes: 35 additions & 1 deletion movingpandas/tests/test_trajectory_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from geopandas import GeoDataFrame
from pandas import Timestamp
from pandas.testing import assert_frame_equal
from shapely.geometry import LineString, Point, Polygon
from shapely.geometry import LineString, Point, Polygon, MultiPolygon

from movingpandas.trajectory import (
TRAJ_ID_COL_NAME,
Expand Down Expand Up @@ -163,10 +163,44 @@ def test_get_intersecting(self):

def test_clip(self):
polygon = Polygon([(-1, -1), (-1, 1), (1, 1), (1, -1), (-1, -1)])
collection = self.collection.copy()
collection = self.collection.clip(polygon)
assert len(collection) == 1
assert collection.trajectories[0].to_linestring().wkt == "LINESTRING (0 0, 1 0)"

def test_clip_with_multipolygon(self):
polygon = MultiPolygon([
Polygon([(-1, -1), (-1, 1), (1, 1), (1, -1), (-1, -1)]),
Polygon([(5, 1), (7, 1), (7, 3), (5, 3), (5, 1)])
])
collection = self.collection.clip(polygon)
assert len(collection) == 2
assert collection.trajectories[0].to_linestring().wkt == "LINESTRING (0 0, 1 0)"
assert collection.trajectories[1].to_linestring().wkt == "LINESTRING (6 1, 6 3)"

""" Fails, related to https://github.com/movingpandas/movingpandas/discussions/235
def test_clip_with_multipolygon2(self):
polygon = MultiPolygon([
Polygon([(-1, -1), (-1, 1), (1, 1), (1, -1), (-1, -1)]),
Polygon([(3, -1), (3, 1), (4, 1), (4, -1), (3, -1)])
])
collection = self.collection.clip(polygon)
assert len(collection) == 2
assert collection.trajectories[0].to_linestring().wkt == "LINESTRING (0 0, 1 0)"
assert collection.trajectories[1].to_linestring().wkt == "LINESTRING (3 0, 4 0)"
"""

def test_clip_with_min_length(self):
polygon = Polygon([(-1, -1), (-1, 1), (1, 1), (1, -1), (-1, -1)])
collection = self.collection.copy()
collection.min_length = 1
collection = collection.clip(polygon)
assert len(collection) == 1
collection = self.collection.copy()
collection.min_length = 2
collection = collection.clip(polygon)
assert len(collection) == 0

def test_filter(self):
assert len(self.collection.filter("obj", "A")) == 2
assert len(self.collection.filter("obj", ["A"])) == 2
Expand Down
9 changes: 6 additions & 3 deletions movingpandas/trajectory_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ def __init__(
crs : string
CRS of the x/y coordinates
min_length : numeric
Desired minimum length of trajectories. (Shorter trajectories are
discarded.)
Desired minimum length of trajectories. Length is calculated using
CRS units, except if the CRS is geographic (e.g. EPSG:4326 WGS84)
then length is calculated in meters.
(Shorter trajectories are discarded.)
min_duration : timedelta
Desired minimum duration of trajectories. (Shorter trajectories are
discarded.)
Expand Down Expand Up @@ -438,7 +440,8 @@ def clip(self, polygon, point_based=False):
for traj in self:
try:
for intersect in traj.clip(polygon, point_based):
clipped.append(intersect)
if (intersect.get_length() >= self.min_length): # TODO also test min_duration
clipped.append(intersect)
except: # noqa E722
pass
result = copy(self)
Expand Down

0 comments on commit 87b4a80

Please sign in to comment.