From 5d4ab7362037265c7b8040d8b1760ecd7ec236d5 Mon Sep 17 00:00:00 2001 From: Andrew Davie Date: Wed, 30 Mar 2016 15:14:24 +1100 Subject: [PATCH] Edge cases handled gracefully for low point-count arrays Sitations with low point-counts may have been problematic. Now, 0, 1 and 2 point arrays are explicitly handled. Added a copy constructor to WptPt to allow copies to be made, rather than returning original array. Cases where no data is returned always returns a zero-sized array rather than a null array. --- OsmAnd/src/net/osmand/plus/GPXUtilities.java | 21 ++++++++++++++++ .../plus/views/AsynchronousResampler.java | 25 +++++++++++++------ .../src/net/osmand/plus/views/Renderable.java | 1 + 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GPXUtilities.java b/OsmAnd/src/net/osmand/plus/GPXUtilities.java index 68760bd836..8099f433d4 100644 --- a/OsmAnd/src/net/osmand/plus/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/plus/GPXUtilities.java @@ -109,6 +109,27 @@ public class GPXUtilities { public WptPt() { } + public WptPt(WptPt toCopy) { + this.lat = toCopy.lat; + this.lon = toCopy.lon; + if (toCopy.name != null) { + this.name = new String(toCopy.name); + } + if (toCopy.link != null) { + this.link = new String(toCopy.link); + } + if (toCopy.category != null) { + this.category = new String(toCopy.category); + } + this.time = toCopy.time; + this.ele = toCopy.ele; + this.speed = toCopy.speed; + this.hdop = toCopy.hdop; + this.deleted = toCopy.deleted; + this.colourARGB = toCopy.colourARGB; + this.distance = toCopy.distance; + } + public void setDistance(double dist) { distance = dist; } diff --git a/OsmAnd/src/net/osmand/plus/views/AsynchronousResampler.java b/OsmAnd/src/net/osmand/plus/views/AsynchronousResampler.java index fbe46572d4..a7fbd8dcfc 100644 --- a/OsmAnd/src/net/osmand/plus/views/AsynchronousResampler.java +++ b/OsmAnd/src/net/osmand/plus/views/AsynchronousResampler.java @@ -16,7 +16,7 @@ public abstract class AsynchronousResampler extends AsyncTask(); // so we NEVER return a null list } @Override protected void onPostExecute(String result) { @@ -35,10 +35,10 @@ public abstract class AsynchronousResampler extends AsyncTask resampleTrack(List pts, double dist) { - ArrayList newPts = new ArrayList<>(); + List newPts = new ArrayList<>(); int ptCt = pts.size(); - if (ptCt > 0) { + if (ptCt > 1) { GPXUtilities.WptPt lastPt = pts.get(0); double segSub = 0; @@ -76,6 +76,11 @@ public abstract class AsynchronousResampler extends AsyncTask points = rs.getPoints(); culled = resampleTrack(points, segmentSize); - if (!isCancelled()) { + if (!isCancelled() && !culled.isEmpty()) { GPXUtilities.WptPt lastPt = points.get(0); lastPt.speed = 0; @@ -211,10 +216,9 @@ public abstract class AsynchronousResampler extends AsyncTask(); - int nsize = points.size(); - if (nsize > 0) { + if (nsize > 2) { + culled = new ArrayList<>(); survivor = new boolean[nsize]; cullRamerDouglasPeucer(0, nsize - 1); if (!isCancelled()) { @@ -224,6 +228,11 @@ public abstract class AsynchronousResampler extends AsyncTask(); + for (GPXUtilities.WptPt pt : points) { + culled.add(new GPXUtilities.WptPt(pt)); + } } return isCancelled() ? "" : "OK"; } diff --git a/OsmAnd/src/net/osmand/plus/views/Renderable.java b/OsmAnd/src/net/osmand/plus/views/Renderable.java index 684bc7e5e7..64b734fb4a 100644 --- a/OsmAnd/src/net/osmand/plus/views/Renderable.java +++ b/OsmAnd/src/net/osmand/plus/views/Renderable.java @@ -78,6 +78,7 @@ public class Renderable { // When the asynchronous task has finished, it calls this function to set the 'culled' list public void setRDP(List cull) { + assert cull!=null; culled = cull; // Find the segment's bounding box, to allow quick draw rejection later