-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add Douglas-Peucker poly simplification algorithm as PolyUtil.simplify() #201
Conversation
Whitespace changes LGTM. Rest LGTM, too. pinging @googlebot for CLA ... (I don't think this actually does anything...) @jfschmakeit @markmcd any thoughts? |
CLA checked manually. 👍 |
* @param tolerance in meters. Increasing the tolerance will result in fewer points in the | ||
* simplified line. | ||
*/ | ||
public static void simplify(List<LatLng> line, List<LatLng> simplifiedLine, double tolerance) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you using out-parameters (and void return type), instead of returning a LatLng array or List?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept the same method signature and return type from the original MyTracks implementation. However, I'd actually prefer returning a LatLng
List, so if you agree I'd be happy to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, that's preferable. Also, change the parameter name to polyline
to be consistent with the other functions in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually - does this work for closed loops (i.e. polygon simplification)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question - I think so, although I'll run some tests to see for sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it doesn't modify the lat/lons themselves, so we just need to be sure that the first and last points are always included. I presume that they are.
I can't tell just by reading the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, assertEndPoints()
test checks that the first and last points are unmodified.
8d1ab4c
to
2182d07
Compare
LGTM. Thanks for this - it's a valuable contribution. Let us know when you're done so we can merge. |
Looks like this will work for polygons too, but there are a few caveats:
I produced the yellow polygon on the right by adding a very small offset to the final "duplicate" point that closes the polygon. So, it looks like:
This seems to work without causing problems - here's the same result for an oval: So, as far as handling polygons with the
For the output of either option above, we would swap out the final point w/ the offset for the original final point before returning the list. Thoughts? I think the first option is my preference, as it keeps the library simpler - worst case scenario, a developer passes in an unclosed polygon, and they get a polygon back that's not totally simplified. This won't even be noticeable in most cases, as it's only a single extra point. |
Also, if we handle both polylines and polygons in the same |
2182d07
to
919f81e
Compare
@broady @markmcd I went ahead and implemented the Option 1 above ("Require developers to pass in a closed polygon to the So, it should be ready for your final review, as long as you're good with the above design decisions. |
* simplified shape. | ||
* @return a simplified shape produced by the Douglas-Peucker algorithm | ||
*/ | ||
public static List<LatLng> simplify(List<LatLng> shape, double tolerance) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/shape/poly/
Thank you for the diligent work on this. LGTM. Just one small nit: I think |
@broady Ok, np, I'll push a new commit shortly. |
* Douglas-Peucker line simplification implementation ported from the Google MyTracks project (https://code.google.com/p/mytracks/source/browse/MyTracks/src/com/google/android/apps/mytracks/util/LocationUtils.java#81), (c) Google 2008, licensed under Apache v2.0. * Add support for simplifying polygons to the PolyUtil.simplify() implementation * Add demo activity for PolyUtil.simplify() - shows the original line in black, as well as simplified versions of the line with multiple tolerances in different colors. Also shows origin polygons in blue, with simplified polygons in yellow * Add unit tests for PolyUtil.simplify() (covering polylines and polygons), PolyUtil.distanceToLine(), and PolyUtil.isClosedPolygon() in PolyUtilTest
919f81e
to
d724493
Compare
Ok, done. I changed a few other |
Add Douglas-Peucker line simplification algorithm as PolyUtil.simplify()
yup - I'm pretty busy with a bunch of other stuff, so I could only do a release next week. Maybe @markmcd will get to it first. We'll coordinate. |
👍 |
Just to make sure this doesn't fall through the cracks - before this is released, PR #205 should be reviewed/merged. That PR fixes an issue with polygon simplification. |
Friendly ping - @broady and @markmcd do you think you could get a new release out soon? On an unrelated note - I'm prepping a release of a major app overhaul (including port to Android Maps v2), but seeing a bizarre issue with markers jumping around on the map that's bad enough to postpone release - here's a video: So far no traction with an gmaps-api issue I filed in August. Any ideas for next steps? |
Sure thing. I was hoping to have contributed a little more myself to the next release, but I've pushed 0.4.1 anyways. |
Awesome, thanks!! |
PolyUtil.simplify()
- shows the original line in black, as well as simplified versions of the line with multiple tolerances in different colorsPolyUtil.simplify()
andPolyUtil.distanceToLine()
inPolyUtilTest
As discussed with @broady in #144 (comment).
@broady A few unrelated white space changes snuck into this commit via Android Studio - seems this happens on save, and I haven't found a way to disable it. Let me know if this is an issue - I'm using the official Android code template.