From 8277d39a31599b9447df47602f09710d36b10416 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 14 Sep 2012 23:01:58 +0200 Subject: [PATCH] Slightly change multipolygons impl --- .../src/net/osmand/data/Multipolygon.java | 59 +++++---------- .../src/net/osmand/data/Ring.java | 72 +++++-------------- 2 files changed, 35 insertions(+), 96 deletions(-) diff --git a/DataExtractionOSM/src/net/osmand/data/Multipolygon.java b/DataExtractionOSM/src/net/osmand/data/Multipolygon.java index 84ed4dd612..d1416eadb2 100644 --- a/DataExtractionOSM/src/net/osmand/data/Multipolygon.java +++ b/DataExtractionOSM/src/net/osmand/data/Multipolygon.java @@ -104,34 +104,24 @@ public class Multipolygon { * @param longitude lon to check * @return true if this multipolygon is correct and contains the point */ - public boolean containsPoint(double latitude, double longitude){ - - - TreeSet outers = new TreeSet(); - TreeSet inners = new TreeSet(); - - for (Ring outer : getOuterRings()) { - if (outer.containsPoint(latitude, longitude)) { - outers.add(outer); - } + public boolean containsPoint(double latitude, double longitude) { + boolean outerContain = false; + for (Ring outer : getOuterRings()) { + if (outer.containsPoint(latitude, longitude)) { + outerContain = true; + break; } - - for(Ring inner : getInnerRings()) { - if (inner.containsPoint(latitude, longitude)) { - inners.add(inner); - } + } + if (!outerContain) { + return false; + } + for (Ring inner : getInnerRings()) { + if (inner.containsPoint(latitude, longitude)) { + return false; } - - if(outers.size() == 0) return false; - if(inners.size() == 0) return true; - - Ring smallestOuter = outers.first(); - Ring smallestInner = inners.first(); - - // if the smallest outer is in the smallest inner, the multiPolygon contains the point - - return smallestOuter.isIn(smallestInner); - + } + + return true; } /** @@ -179,11 +169,8 @@ public class Multipolygon { * @return */ public int countOuterPolygons() { - groupInRings(); return zeroSizeIfNull(getOuterRings()); - - } /** @@ -285,14 +272,6 @@ public class Multipolygon { return MapUtils.getWeightCenterForNodes(points); } - /** - * check if a cache has been created - * @return true if the cache exists - */ - public boolean hasCache() { - return outerRings != null && innerRings != null; - } - /** * Create the cache
* The cache has to be null before it will be created @@ -315,10 +294,10 @@ public class Multipolygon { //make a clone of the inners set // this set will be changed through execution of the method - SortedSet inners = new TreeSet(getInnerRings()); + ArrayList inners = new ArrayList(getInnerRings()); // get the set of outer rings in a variable. This set will not be changed - SortedSet outers = new TreeSet(getOuterRings()); + ArrayList outers = new ArrayList(getOuterRings()); ArrayList multipolygons = new ArrayList(); // loop; start with the smallest outer ring @@ -329,7 +308,7 @@ public class Multipolygon { m.addOuterWays(outer.getWays()); // Search the inners inside this outer ring - SortedSet innersInsideOuter = new TreeSet(); + ArrayList innersInsideOuter = new ArrayList(); for (Ring inner : inners) { if (inner.isIn(outer)) { innersInsideOuter.add(inner); diff --git a/DataExtractionOSM/src/net/osmand/data/Ring.java b/DataExtractionOSM/src/net/osmand/data/Ring.java index 3c392cf969..6eec88c883 100644 --- a/DataExtractionOSM/src/net/osmand/data/Ring.java +++ b/DataExtractionOSM/src/net/osmand/data/Ring.java @@ -17,12 +17,12 @@ import net.osmand.osm.Way; * @author sander * */ -public class Ring implements Comparable{ +public class Ring { /** * This is a list of the ways added by the user * The order can be changed with methods from this class */ - private ArrayList ways; + private final ArrayList ways; /** * This is the closure of the ways added by the user * So simple two-node ways are added to close the ring @@ -40,17 +40,10 @@ public class Ring implements Comparable{ * Construct a Ring with a list of ways * @param ways the ways that make up the Ring */ - public Ring(List ways) { - this.ways = new ArrayList(); - this.ways.addAll(ways); + private Ring(List ways) { + this.ways = new ArrayList(ways); } - /** - * Construct an empty Ring - */ - public Ring() { - this.ways = new ArrayList(); - } /** * Get the ways added to the Ring. @@ -61,19 +54,6 @@ public class Ring implements Comparable{ public List getWays() { return ways; } - - /** - * Add a way to the Ring - * @param w the way to add - */ - public void addWay(Way w) { - // Reset the cache - closedWays = null; - closedBorder = null; - // Add the way - ways.add(w); - } - /** * Get the closed ways that make up the Ring * This method will sort the ways, so it is CPU intensive @@ -178,38 +158,30 @@ public class Ring implements Comparable{ closedWays = new ArrayList(); return; } - ArrayList> multiLines = createMultiLines(ways); + closedWays = new ArrayList(ways); - // TODO try to close rings which consist out of multiple segments. - // This is a data fault, but it could be solved a bit by OsmAnd - if (multiLines.size() != 1) return; - - ArrayList multiLine = multiLines.get(0); - - closedWays = multiLine; - - long[] endNodes = getMultiLineEndNodes(multiLine); + long[] endNodes = getMultiLineEndNodes(ways); if (endNodes[0] != endNodes[1]) { - if(multiLine.get(0).getNodes() == null) { + if(ways.get(0).getNodes() == null) { Way w = new Way(0L); w.addNode(endNodes[0]); w.addNode(endNodes[1]); closedWays.add(w); } else { Node n1 = null, n2 = null; - if (multiLine.get(0).getFirstNodeId() == endNodes[0]) { - n1 = multiLine.get(0).getNodes().get(0); + if (ways.get(0).getFirstNodeId() == endNodes[0]) { + n1 = ways.get(0).getNodes().get(0); } else { - int index = multiLine.get(0).getNodes().size() - 1; - n1 = multiLine.get(0).getNodes().get(index); + int index = ways.get(0).getNodes().size() - 1; + n1 = ways.get(0).getNodes().get(index); } - int lastML = multiLine.size() - 1; - if (multiLine.get(lastML).getFirstNodeId() == endNodes[0]) { - n2 = multiLine.get(lastML).getNodes().get(0); + int lastML = ways.size() - 1; + if (ways.get(lastML).getFirstNodeId() == endNodes[0]) { + n2 = ways.get(lastML).getNodes().get(0); } else { - int index = multiLine.get(lastML).getNodes().size() - 1; - n2 = multiLine.get(lastML).getNodes().get(index); + int index = ways.get(lastML).getNodes().size() - 1; + n2 = ways.get(lastML).getNodes().get(index); } Way w = new Way(0L); @@ -524,18 +496,6 @@ public class Ring implements Comparable{ } - @Override - /** - * @return -1 if this Ring is inside r
- * 1 if r is inside this Ring
- * 0 otherwise (Rings are next to each other, Rings intersect or Rings are malformed) - */ - public int compareTo(Ring r) { - if (this.isIn(r)) return -1; - if (r.isIn(this)) return 1; - return 0; - } - /** * If this Ring is not complete * (some ways are not initialized