Example #1
0
 def test_split_by_date(self):
     collection = TrajectoryCollection(self.geo_df, 'id', obj_id_col='obj')
     collection = collection.split_by_date(mode='day')
     assert len(collection) == 3
Example #2
0
class TestTrajectoryCollection:
    def setup_method(self):
        df = pd.DataFrame([{
            'id': 1,
            'obj': 'A',
            'geometry': Point(0, 0),
            't': datetime(2018, 1, 1, 12, 0, 0),
            'val': 9,
            'val2': 'a'
        }, {
            'id': 1,
            'obj': 'A',
            'geometry': Point(6, 0),
            't': datetime(2018, 1, 1, 12, 6, 0),
            'val': 5,
            'val2': 'b'
        }, {
            'id': 1,
            'obj': 'A',
            'geometry': Point(6, 6),
            't': datetime(2018, 1, 1, 14, 10, 0),
            'val': 2,
            'val2': 'c'
        }, {
            'id': 1,
            'obj': 'A',
            'geometry': Point(9, 9),
            't': datetime(2018, 1, 1, 14, 15, 0),
            'val': 4,
            'val2': 'd'
        }, {
            'id': 2,
            'obj': 'A',
            'geometry': Point(10, 10),
            't': datetime(2018, 1, 1, 12, 0, 0),
            'val': 10,
            'val2': 'e'
        }, {
            'id': 2,
            'obj': 'A',
            'geometry': Point(16, 10),
            't': datetime(2018, 1, 1, 12, 6, 0),
            'val': 6,
            'val2': 'f'
        }, {
            'id': 2,
            'obj': 'A',
            'geometry': Point(16, 16),
            't': datetime(2018, 1, 2, 13, 10, 0),
            'val': 7,
            'val2': 'g'
        }, {
            'id': 2,
            'obj': 'A',
            'geometry': Point(190, 19),
            't': datetime(2018, 1, 2, 13, 15, 0),
            'val': 3,
            'val2': 'h'
        }]).set_index('t')
        self.geo_df = GeoDataFrame(df, crs=CRS_METRIC)
        self.collection = TrajectoryCollection(self.geo_df,
                                               'id',
                                               obj_id_col='obj')
        self.geo_df_latlon = GeoDataFrame(df, crs=CRS_LATLON)
        self.collection_latlon = TrajectoryCollection(self.geo_df_latlon,
                                                      'id',
                                                      obj_id_col='obj')

    def test_number_of_trajectories(self):
        assert len(self.collection) == 2

    def test_number_of_trajectories_min_length(self):
        collection = TrajectoryCollection(self.geo_df,
                                          'id',
                                          obj_id_col='obj',
                                          min_length=100)
        assert len(collection) == 1

    def test_number_of_trajectories_min_length_never_reached(self):
        collection = TrajectoryCollection(self.geo_df,
                                          'id',
                                          obj_id_col='obj',
                                          min_length=1000)
        assert len(collection) == 0

    def test_get_trajectory(self):
        assert self.collection.get_trajectory(1).id == 1
        assert self.collection.get_trajectory(1).obj_id == 'A'
        assert self.collection.get_trajectory(2).id == 2
        assert self.collection.get_trajectory(3) is None

    def test_get_start_locations(self):
        locs = self.collection.get_start_locations(columns=['val', 'val2'])
        assert len(locs) == 2
        assert locs.iloc[0].geometry in [Point(0, 0), Point(10, 10)]
        assert locs.iloc[0].traj_id in [1, 2]
        assert locs.iloc[0].obj_id == 'A'
        assert locs.iloc[0].val in [9, 10]
        assert locs.iloc[0].val2 in ['a', 'e']
        assert locs.iloc[1].geometry in [Point(0, 0), Point(10, 10)]

    def test_get_end_locations(self):
        locs = self.collection.get_end_locations(columns=['val', 'val2'])
        assert len(locs) == 2
        assert locs.iloc[0].geometry in [Point(9, 9), Point(190, 19)]
        assert locs.iloc[0].traj_id in [1, 2]
        assert locs.iloc[0].obj_id == 'A'
        assert locs.iloc[0].val in [4, 3]
        assert locs.iloc[0].val2 in ['d', 'h']
        assert locs.iloc[1].geometry in [Point(9, 9), Point(190, 19)]

    def test_generalize(self):
        collection = self.collection.generalize(
            mode='min-time-delta', tolerance=timedelta(minutes=10))
        assert len(collection) == 2
        assert collection.trajectories[0].to_linestring(
        ).wkt == 'LINESTRING (0 0, 6 6, 9 9)'
        assert collection.trajectories[1].to_linestring(
        ).wkt == 'LINESTRING (10 10, 16 16, 190 19)'

    def test_split_by_date(self):
        collection = self.collection.split_by_date(mode='day')
        assert len(collection) == 3

    def test_split_by_observation_gap(self):
        collection = self.collection.split_by_observation_gap(
            timedelta(hours=1))
        assert len(collection) == 4

    def test_get_intersecting(self):
        polygon = Polygon([(-1, -1), (-1, 1), (1, 1), (1, -1), (-1, -1)])
        collection = self.collection.get_intersecting(polygon)
        assert len(collection) == 1
        assert collection.trajectories[0] == self.collection.trajectories[0]

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

    def test_filter(self):
        assert len(self.collection.filter('obj', 'A')) == 2
        assert len(self.collection.filter('obj', 'B')) == 0

    def test_get_min_and_max(self):
        assert self.collection.get_min('val') == 2
        assert self.collection.get_max('val') == 10

    def test_plot_exists(self):
        from matplotlib.axes import Axes
        result = self.collection.plot()
        assert isinstance(result, Axes)

    def test_hvplot_exists(self):
        import holoviews
        result = self.collection_latlon.hvplot()
        assert isinstance(result, holoviews.core.overlay.Overlay)
class TestTrajectoryCollection:

    def setup_method(self):
        df = pd.DataFrame([
            {'id': 1, 'obj': 'A', 'geometry': Point(0, 0), 't': datetime(2018,1,1,12,0,0), 'val': 9},
            {'id': 1, 'obj': 'A', 'geometry': Point(6, 0), 't': datetime(2018,1,1,12,6,0), 'val': 5},
            {'id': 1, 'obj': 'A', 'geometry': Point(6, 6), 't': datetime(2018,1,1,12,10,0), 'val': 2},
            {'id': 1, 'obj': 'A', 'geometry': Point(9, 9), 't': datetime(2018,1,1,12,15,0), 'val': 4},
            {'id': 2, 'obj': 'A', 'geometry': Point(10, 10), 't': datetime(2018,1,1,12,0,0), 'val': 10},
            {'id': 2, 'obj': 'A', 'geometry': Point(16, 10), 't': datetime(2018,1,1,12,6,0), 'val': 6},
            {'id': 2, 'obj': 'A', 'geometry': Point(16, 16), 't': datetime(2018,1,2,12,10,0), 'val': 7},
            {'id': 2, 'obj': 'A', 'geometry': Point(190, 19), 't': datetime(2018,1,2,12,15,0), 'val': 3}
        ]).set_index('t')
        self.geo_df = GeoDataFrame(df, crs=CRS_METRIC)
        self.collection = TrajectoryCollection(self.geo_df, 'id', obj_id_col='obj')
        self.geo_df_latlon = GeoDataFrame(df, crs=CRS_LATLON)
        self.collection_latlon = TrajectoryCollection(self.geo_df_latlon, 'id', obj_id_col='obj')

    def test_number_of_trajectories(self):
        assert len(self.collection) == 2

    def test_number_of_trajectories_min_length(self):
        collection = TrajectoryCollection(self.geo_df, 'id', obj_id_col='obj', min_length=100)
        assert len(collection) == 1

    def test_number_of_trajectories_min_length_never_reached(self):
        collection = TrajectoryCollection(self.geo_df, 'id', obj_id_col='obj', min_length=1000)
        assert len(collection) == 0

    def test_split_by_date(self):
        collection = self.collection.split_by_date(mode='day')
        assert len(collection) == 3

    def test_get_trajectory(self):
        assert self.collection.get_trajectory(1).id == 1
        assert self.collection.get_trajectory(1).obj_id == 'A'
        assert self.collection.get_trajectory(2).id == 2
        assert self.collection.get_trajectory(3) is None

    def test_filter(self):
        assert len(self.collection.filter('obj', 'A')) == 2
        assert len(self.collection.filter('obj', 'B')) == 0

    def test_get_min_and_max(self):
        assert self.collection.get_min('val') == 2
        assert self.collection.get_max('val') == 10

    def test_get_start_locations(self):
        locs = self.collection.get_start_locations(columns=['val'])
        assert len(locs) == 2
        assert locs.iloc[0].geometry in [Point(0, 0), Point(10, 10)]
        assert locs.iloc[0].traj_id in [1, 2]
        assert locs.iloc[0].obj_id == 'A'
        assert locs.iloc[0].val in [9, 10]
        assert locs.iloc[1].geometry in [Point(0, 0), Point(10, 10)]

    def test_plot_exists(self):
        from matplotlib.axes import Axes
        result = self.collection.plot()
        assert isinstance(result, Axes)

    def test_hvplot_exists(self):
        import holoviews
        result = self.collection_latlon.hvplot()
        assert isinstance(result, holoviews.core.overlay.Overlay)