Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/analyzer/moving_pandas_trajectory_collection/analyzer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from geopandas import GeoDataFrame
from pyproj import CRS

from src.analyzer.base_analyzer import BaseAnalyzer
from src.moveapps_pickle import MoveAppsPickle
Expand Down Expand Up @@ -55,7 +56,10 @@ def __inspect(self, data: gpd.GeoDataFrame, movingpandas: mpd.TrajectoryCollecti
geom_col = movingpandas.get_geom_col()
number_positions_by_track = dict(data.dissolve(by=traj_id_col, aggfunc={traj_id_col: "count"}).drop(geom_col, axis=1).rename(columns={traj_id_col: "count"}).reset_index().values)

projection = movingpandas.get_crs()
# built from a GeoDataFrame, a collection keeps a `pyproj.CRS`, which JSON cannot encode;
# built from x/y, the string it was given. Both report as e.g. 'EPSG:4326'
crs = movingpandas.get_crs()
projection = crs.srs if isinstance(crs, CRS) else crs

#positions_bounding_box = data.total_bounds.tolist()
positions_bounding_box = data.total_bounds
Expand Down
31 changes: 31 additions & 0 deletions test/analyzer/moving_pandas_trajectory_collection/test_analyzer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import json
import os.path
import tempfile
from unittest import TestCase

import geopandas as gpd
import movingpandas as mpd
import pandas as pd
from pyproj import CRS

from src.analyzer.moving_pandas_trajectory_collection.analyzer import MovingPandasAnalyzer
from test.config.definitions import ROOT_DIR
from test.config.fixtures import gzip_copy
Expand Down Expand Up @@ -78,5 +85,29 @@ def test_it_should_analyze_a_gzip_compressed_input4_LatLon(self):
self.assertEqual(actual[10]['number_positions_by_track'], {'X742..deploy_id.56853924.': 3265, 'X746..deploy_id.20813885.': 3010, 'X749..deploy_id.20813892.': 1495})
self.assertEqual(actual[12]['n'], ['non-empty-result'])

def test_it_should_report_the_projection_of_a_collection_built_from_a_geodataframe(self):
# arrange: built from a GeoDataFrame, a collection keeps a `pyproj.CRS`, not the string it
# was given - e.g. the output of `move2_loc to MovingPandas` since link-r-python v2.2.1
collection = self.__collection_built_from_a_geodataframe()
self.assertIsInstance(collection.get_crs(), CRS)
output = os.path.join(self.tmp.name, "output_file")
pd.to_pickle(collection, output, compression='gzip')

# act: the result leaves the cargo-agent as JSON, so it has to survive the encoding
actual = json.loads(json.dumps(self.sut.analyze(path=output)))

# assert
self.assertEqual(actual[7]['projection'], 'EPSG:4326')
self.assertEqual(actual[12]['n'], ['non-empty-result'])

@staticmethod
def __collection_built_from_a_geodataframe() -> mpd.TrajectoryCollection:
points = gpd.GeoDataFrame(
{'track': ['a', 'a', 'b', 'b'], 't': pd.date_range('2026-01-01', periods=4, freq='h')},
geometry=gpd.points_from_xy([0.0, 1.0, 2.0, 3.0], [0.0, 1.0, 2.0, 3.0]),
crs='EPSG:4326',
)
return mpd.TrajectoryCollection(points, traj_id_col='track', t='t')

def __test_file(self, file_name) -> str:
return os.path.join(ROOT_DIR, 'test', 'resources', 'moving_pandas_trajectory_collection', file_name)
Loading