Skip to content

Improve PolyUtil.isLocationOnPath to also tell where the location is on the polyline#361

Merged
stephenmcd merged 8 commits into
googlemaps:masterfrom
noberasco:master
Feb 23, 2017
Merged

Improve PolyUtil.isLocationOnPath to also tell where the location is on the polyline#361
stephenmcd merged 8 commits into
googlemaps:masterfrom
noberasco:master

Conversation

@noberasco
Copy link
Copy Markdown
Contributor

The methods provided to know whether a given location lies on the path of a polyline are good, but they could be even better if they also told where on the polyline the location is.

This in turn allows, for instance, to tap on any point on a polyline and get statistical information about the tapped point:
screenshot_20170222-000613-640

@noberasco
Copy link
Copy Markdown
Contributor Author

It seems travis fails because the emulator doesn't respond in time...

@barbeau
Copy link
Copy Markdown
Collaborator

barbeau commented Feb 22, 2017

@noberasco thanks! Don't worry about Travis, the emulator images are messed up so it currently always fails. Could you please add a unit test for the methods that take the index as a parameter?

@barbeau
Copy link
Copy Markdown
Collaborator

barbeau commented Feb 22, 2017

Sorry, I meant methods that return index.

double lng2 = toRadians(point2.longitude);
if (isOnSegmentGC(lat1, lng1, lat2, lng2, lat3, lng3, havTolerance)) {
return true;
return idx;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It may be useful to clearly state the meaning of a return of 0 (idx==0), notably for closed==true. It may be more intuitive to map the "closing" segment on idx==n-1 instead of idx==0 as is now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See my comment above: tap on the closing segment ==> return N

*
* with a default tolerance of 0.1 meters.
*/
public static int locationIndexOnPath(LatLng point, List<LatLng> polyline,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please document the returned "int"; mention the range and in which situations negative values are returned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, will do.

* is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing
* segment between the first point and the last point is not included.
*/
public static int locationIndexOnPath(LatLng point, List<LatLng> polyline,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please document the returned int.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, will do.

return locationIndexOnPath(point, polyline, geodesic, DEFAULT_TOLERANCE);
}

private static int locationIndexOnEdgeOrPath(LatLng point, List<LatLng> poly, boolean closed,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You may want to document here the interaction of "closed" with the returned value.

I think a useful return could be:
returns 0 if "point" is between poly[0] and poly[1], 1 if between poly[1] and poly[2], etc. If closed==true, returns N-1 if point is between poly[n-1] and poly[0]. -- but the current implementation does not achieve this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In my opinion, it would make more sense to return N if the point is between poly[n-1] and poly[0]. This would allow to distinguish between:

  • point is between poly[n-2] and poly[n-1]
  • point is between poly[n-1] and poly[0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes; but if size(polyline)==N, and you number the segments starting at 0, then the last segment (between poly[n-2] and poly[n-1]) is N-2; thus the segment "after last" is N-1. (i.e., I was taking N to be the number of points of the polyline, not the number of segments). Otherwise I think we agree.

@noberasco
Copy link
Copy Markdown
Contributor Author

As requested I:

  • added documentation on the returned int values
  • added unit tests for the new methods

Additionally, I:

  • fixed a compilation issue on KmlPolygonTest.java (possibly my java compiler is more up to date than yours?)

For the time being, I did not change the logic for the corner case 'closed==true and point is on the return segment'. I'm waiting for your feedback on my own counter-proposal. However, it has to be said that the new public methods locationIndexOnPath always pass 'false' to closed, so the whole point is kind of moot.


// Two points.
locationIndexCase(makeList(1, 2, 3, 5), new LatLng(1, 2), 0);
locationIndexCase(makeList(1, 2, 3, 5), new LatLng(3, 5), 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Probably here we should have here a return of 0, because (3,5) is considered part of the first segment [(1,2), (3,5)]. The 1 you get is a side-effect of the current implem, which considers [(1,2), (1,2)] as segment0 and [(1,2), (3,5)] as segment1.

@preda
Copy link
Copy Markdown

preda commented Feb 22, 2017 via email

@noberasco
Copy link
Copy Markdown
Contributor Author

Done!

Copy link
Copy Markdown

@preda preda left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks!
(feel free to address the little test observation as you see fit).

import java.util.ArrayList;
import java.util.List;

public class KmlPolygonTest extends TestCase {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I believe these changes to KmlPolygonTest are unrelated to adding the ability to return the index for the closest point on the line. If so, can you please remove these changes from this changeset?

Copy link
Copy Markdown
Contributor Author

@noberasco noberasco Feb 22, 2017

Choose a reason for hiding this comment

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

They fix a compilation issue I have on my side (possibly my java compiler is more up to date than yours?). They are pretty harmless, but if you prefer I'll file them as a separate pull request.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Try rebasing this on master branch - there were some changes to the KML package name and method return types in the last release. That should fix it.

return (idx >= 0);
}

/**
Copy link
Copy Markdown
Collaborator

@barbeau barbeau Feb 22, 2017

Choose a reason for hiding this comment

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

Could you please change the comment format for all methods to include the @param and @return fields in the comments so the Javadocs generate correctly? See simplify() for an example. On a related note, the poly name in the comments doesn't match the name of any method parameters (I assume this should be polyline).

@noberasco
Copy link
Copy Markdown
Contributor Author

OK, this should address the latest changes requested:

@preda
Copy link
Copy Markdown

preda commented Feb 23, 2017 via email

@barbeau
Copy link
Copy Markdown
Collaborator

barbeau commented Feb 23, 2017

@noberasco Thanks, this looks good to me!

@stephenmcd Do you want to be the one to squash and merge?

@noberasco BTW, you're right on the KmlPolygonTest test compilation issue - looks like this must have snuck in at the end of #351 just prior to merge. Could you please open a separate PR for that?

@stephenmcd
Copy link
Copy Markdown
Contributor

Thanks everyone!

@stephenmcd stephenmcd merged commit f7320a8 into googlemaps:master Feb 23, 2017
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