Skip to content

Closest Point of Approach #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 38 commits into from
Closed

Conversation

SiggyF
Copy link
Contributor

@SiggyF SiggyF commented Apr 13, 2025

Closest Point of Approach calculator, as discussed in #31.
Code and testcases are based on the implementation from lwgeom in postgis.

A corresponding notebook in movingpandas-examples will be also prepared as PR.

@codecov-commenter
Copy link

codecov-commenter commented Apr 13, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 99.76134% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.92%. Comparing base (3f462dc) to head (bcffa48).
Report is 36 commits behind head on main.

Files with missing lines Patch % Lines
movingpandas/cpa.py 99.36% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #457      +/-   ##
==========================================
+ Coverage   96.35%   96.92%   +0.56%     
==========================================
  Files          35       37       +2     
  Lines        4257     5131     +874     
==========================================
+ Hits         4102     4973     +871     
- Misses        155      158       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@SiggyF
Copy link
Contributor Author

SiggyF commented Apr 13, 2025

Note that the coverage report says 1 line is not covered, but it is covered in the test test_non_overlapping_time.

cpa = CPACalculator(traj_a, traj_b)
result = cpa.min()

# we should have NaT and NaN (postgis returns time of -2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's clean up the PostGIS-related comments before we finalize

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments! I'll update the PR later this week.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the leftover postgis code that I used to make sure I was in line with their tests. I kept a reference at the top of the source code, for proper attribution.

Copy link
Collaborator

@anitagraser anitagraser Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have to ask Sandro and Paul's permission. Their code is GPL and the license would require any derivative to also be GPL https://github.com/postgis/postgis/blob/1141db62ef435f7b7d2bb4cc13766daff4fe880f/liblwgeom/lwlinearreferencing.c#L1138

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that I ask them the following:

Dear Sandro, Paul,

I am working on a Closest Point of Approach calculation in the python movingpandas library, a python library for moving features / trajectory calculations, by Anita Graser (cc). I have prepared an implementation based on the lwgeom's code and tests (recoded in python). 

Is it ok with you if we release our code under the current BSD 3-Clause  license of movingpandas? 

For  reference, please see the corresponding
https://github.com/movingpandas/movingpandas/issues/31
https://github.com/SiggyF/movingpandas/tree/feature/tcpa

We attribute your work as follows.  Happy to adapt if requested. 
In the cpa module:
 # we follow the implementation of postgis
 # In partical the function segments_tcpa in
 # https://github.com/postgis/postgis/blob/master/liblwgeom/lwlinearreferencing.c

In the tests:
# Many tests here are based on the tests in lwgeom, cu_measures / test_lwgeom_tcpa:
# https://github.com/postgis/postgis/blob/master/liblwgeom/cunit/cu_measures.c

Kind regards,

Fedor Baart

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's give it a try. Please go ahead.

(What we can definitely do, is add this functionality to Trajectools, since that is already GPL.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed that we will move this code to the trajectools repository, so that it is complient with GPL license. I'll move the code there and provide the corresponding QGIS code.

I'll wrap up this PR by processing the last comments. After that I'll close it and move the code to trajectools.

A possible reuse of this branch could be from a different module as an optional dependency, like in geopandas.

@SiggyF
Copy link
Contributor Author

SiggyF commented Apr 19, 2025

Thanks for your comments. I have updated the code / documentation accordingly.

I noticed that the documentation only includes the Trajectory (hardcoded in docs / api / api.rst). I'd be happy to add a few notes on this implementation in the documentation.

@SiggyF SiggyF requested a review from anitagraser April 19, 2025 12:46
Copy link
Collaborator

@anitagraser anitagraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding a function to the Trajectory class that uses the CPACalculator? Similar to the existing distance functions, something like

Trajectory.get_cpa_distance(another_trajectory, units=None) ... returns the value of CPA(traj_a, traj_b).min()

Should also accept a units parameter for distance unit conversion.

@anitagraser
Copy link
Collaborator

anitagraser commented Apr 19, 2025

Regarding documentation: yes, please add CPACalculator to the documentation

Update: I've added the documentation.

@anitagraser
Copy link
Collaborator

And CPACalculator should be added to https://github.com/movingpandas/movingpandas/blob/main/movingpandas/__init__.py so it can be imported like the other classes

@SiggyF
Copy link
Contributor Author

SiggyF commented Apr 21, 2025

What do you think about adding a function to the Trajectory class that uses the CPACalculator? Similar to the existing distance functions, something like

Trajectory.get_cpa_distance(another_trajectory, units=None) ... returns the value of CPA(traj_a, traj_b).min()

I would not expect the somewhat domain specific function on the general trajectory, but I'm happy to add it. I would either return CPACalculator(traj_a, traj_b).min().dist (only the distance) or name it get_cpa and return CPACalculator(traj_a, traj_b).min() all information regarding closest point (time, timespan, distance, geometry).

Should also accept a units parameter for distance unit conversion.
I would need to update the implementation for non-cartesian projections. I could update the implementation to use measure_distance_euclidean vs measure_distance_geodesic. I think both would need to be updated with support for including the z coordinate. They seem to both compute 2d distances.

@SiggyF
Copy link
Contributor Author

SiggyF commented Apr 21, 2025

And CPACalculator should be added to https://github.com/movingpandas/movingpandas/blob/main/movingpandas/__init__.py so it can be imported like the other classes

I added this.

@SiggyF
Copy link
Contributor Author

SiggyF commented May 17, 2025

This PR will be moved to trajectools:

  • GPL compatibility
  • Nice extra feature for QGIS users

@SiggyF SiggyF closed this May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants