Attached patch makes the code of the generate-sea feature a bit more readable. Results should not be changed otherwise this is a bug.

I want to reuse some parts of this code for mulitpolygon tile bounds handling. So this is a preliminary work to extract some parts of the generate-sea code from the Osm5XmlHandler.

WanMil
Index: src/uk/me/parabola/mkgmap/reader/osm/xml/Osm5XmlHandler.java
===================================================================
--- src/uk/me/parabola/mkgmap/reader/osm/xml/Osm5XmlHandler.java        
(revision 1563)
+++ src/uk/me/parabola/mkgmap/reader/osm/xml/Osm5XmlHandler.java        
(working copy)
@@ -1309,29 +1309,12 @@
                                                hNext = hFirst;
 
                                        Coord p;
-                                       if (hit.compareTo(hNext) < 0) {
-                                               log.info("joining: ", hit, 
hNext);
-                                               for (int i=hit.edge; 
i<hNext.edge; i++) {
-                                                       EdgeHit corner = new 
EdgeHit(i, 1.0);
-                                                       p = 
corner.getPoint(seaBounds);
-                                                       log.debug("way: ", 
corner, p);
-                                                       
w.addPointIfNotEqualToLastPoint(p);
-                                               }
-                                       }
-                                       else if (hit.compareTo(hNext) > 0) {
-                                               log.info("joining: ", hit, 
hNext);
-                                               for (int i=hit.edge; i<4; i++) {
-                                                       EdgeHit corner = new 
EdgeHit(i, 1.0);
-                                                       p = 
corner.getPoint(seaBounds);
-                                                       log.debug("way: ", 
corner, p);
-                                                       
w.addPointIfNotEqualToLastPoint(p);
-                                               }
-                                               for (int i=0; i<hNext.edge; 
i++) {
-                                                       EdgeHit corner = new 
EdgeHit(i, 1.0);
-                                                       p = 
corner.getPoint(seaBounds);
-                                                       log.debug("way: ", 
corner, p);
-                                                       
w.addPointIfNotEqualToLastPoint(p);
-                                               }
+                                       log.info("joining: ", hit, hNext);
+                                       for (Edge ed = hit.edge; ed != 
hNext.edge; ed = ed.next()) {
+                                               EdgeHit corner = new 
EdgeHit(ed, 1.0);
+                                               p = corner.getPoint(seaBounds);
+                                               log.debug("way: ", corner, p);
+                                               
w.addPointIfNotEqualToLastPoint(p);
                                        }
                                        
w.addPointIfNotEqualToLastPoint(hNext.getPoint(seaBounds));
                                }
@@ -1460,27 +1443,57 @@
                }
        }
 
+       
+       private static enum Edge {
+               SOUTHERN, EASTERN, NORTHERN, WESTERN;
+
+               public Edge next() {
+                       return values() [(this.ordinal()+1) % values().length];
+               }
+
+               
+               public Coord getPoint(Area a, double hitFraction) {
+                       switch (this) {
+                       case SOUTHERN:
+                               return new Coord(a.getMinLat(), (int) 
(a.getMinLong() + hitFraction
+                                               * (a.getMaxLong() - 
a.getMinLong())));
+
+                       case EASTERN:
+                               return new Coord((int) (a.getMinLat() + 
hitFraction
+                                               * (a.getMaxLat() - 
a.getMinLat())), a.getMaxLong());
+
+                       case NORTHERN:
+                               return new Coord(a.getMaxLat(), (int) 
(a.getMaxLong() - hitFraction
+                                               * (a.getMaxLong() - 
a.getMinLong())));
+
+                       case WESTERN:
+                               return new Coord((int) (a.getMaxLat() - 
hitFraction
+                                               * (a.getMaxLat() - 
a.getMinLat())), a.getMinLong());
+                       default:
+                               throw new ExitException("illegal state");
+                       }
+               }
+       }
+       
        /**
         * Specifies where an edge of the bounding box is hit.
         */
-       private static class EdgeHit implements Comparable<EdgeHit>
-       {
-               private final int edge;
-               private final double t;
+       private static class EdgeHit implements Comparable<EdgeHit> {
+               private final Edge edge;
+               private final double hitFraction;
 
-               EdgeHit(int edge, double t) {
+               EdgeHit(Edge edge, double hitFraction) {
                        this.edge = edge;
-                       this.t = t;
+                       this.hitFraction = hitFraction;
                }
 
                public int compareTo(EdgeHit o) {
-                       if (edge < o.edge)
-                               return -1;
-                       else if (edge > o.edge)
+                       int edgeCmp = edge.compareTo(o.edge);
+                       if (edgeCmp != 0)
+                               return edgeCmp;
+                       else if (hitFraction > o.hitFraction)
                                return +1;
-                       else if (t > o.t)
-                               return +1;
-                       else if (t < o.t)
+                       else if (hitFraction < o.hitFraction)
                                return -1;
                        else
                                return 0;
@@ -1489,34 +1502,18 @@
                public boolean equals(Object o) {
                        if (o instanceof EdgeHit) {
                                EdgeHit h = (EdgeHit) o;
-                               return (h.edge == edge && Double.compare(h.t, 
t) == 0);
-                       }
-                       else
+                               return (h.edge == edge && 
Double.compare(h.hitFraction, hitFraction) == 0);
+                       } else
                                return false;
                }
 
                Coord getPoint(Area a) {
                        log.info("getPoint: ", this, a);
-                       switch (edge) {
-                       case 0:
-                               return new Coord(a.getMinLat(), (int) 
(a.getMinLong() + t * (a.getMaxLong()-a.getMinLong())));
-
-                       case 1:
-                               return new Coord((int)(a.getMinLat() + t * 
(a.getMaxLat()-a.getMinLat())), a.getMaxLong());
-
-                       case 2:
-                               return new Coord(a.getMaxLat(), 
(int)(a.getMaxLong() - t * (a.getMaxLong()-a.getMinLong())));
-
-                       case 3:
-                               return new Coord((int)(a.getMaxLat() - t * 
(a.getMaxLat()-a.getMinLat())), a.getMinLong());
-
-                       default:
-                               throw new ExitException("illegal state");
-                       }
+                       return edge.getPoint(a, hitFraction);
                }
 
                public String toString() {
-                       return "EdgeHit " + edge + "@" + t;
+                       return "EdgeHit " + edge + "@" + hitFraction;
                }
        }
 
@@ -1535,19 +1532,15 @@
                int maxLong = a.getMaxLong();
 
                log.info(String.format("getEdgeHit: (%d %d) (%d %d %d %d)", 
lat, lon, minLat, minLong, maxLat, maxLong));
-               if (lat <= minLat+tolerance) {
-                       return new EdgeHit(0, ((double)(lon - 
minLong))/(maxLong-minLong));
-               }
-               else if (lon >= maxLong-tolerance) {
-                       return new EdgeHit(1, ((double)(lat - 
minLat))/(maxLat-minLat));
-               }
-               else if (lat >= maxLat-tolerance) {
-                       return new EdgeHit(2, ((double)(maxLong - 
lon))/(maxLong-minLong));
-               }
-               else if (lon <= minLong+tolerance) {
-                       return new EdgeHit(3, ((double)(maxLat - 
lat))/(maxLat-minLat));
-               }
-               else
+               if (lat <= minLat + tolerance) {
+                       return new EdgeHit(Edge.SOUTHERN, ((double) (lon - 
minLong)) / (maxLong - minLong));
+               } else if (lon >= maxLong - tolerance) {
+                       return new EdgeHit(Edge.EASTERN, ((double) (lat - 
minLat)) / (maxLat - minLat));
+               } else if (lat >= maxLat - tolerance) {
+                       return new EdgeHit(Edge.NORTHERN, ((double) (maxLong - 
lon)) / (maxLong - minLong));
+               } else if (lon <= minLong + tolerance) {
+                       return new EdgeHit(Edge.WESTERN, ((double) (maxLat - 
lat)) / (maxLat - minLat));
+               } else
                        return null;
        }
 
@@ -1566,34 +1559,29 @@
                log.info(String.format("getNextEdgeHit: (%d %d) (%d %d %d %d)", 
lat, lon, minLat, minLong, maxLat, maxLong));
                // shortest distance to border (init with distance to southern 
border) 
                int min = lat - minLat;
-               // number of edge as used in getEdgeHit. 
-               // 0 = southern
-               // 1 = eastern
-               // 2 = northern
-               // 3 = western edge of Area a
-               int i = 0;
+               Edge edge = Edge.SOUTHERN;
                // normalized position at border (0..1)
-               double l = ((double)(lon - minLong))/(maxLong-minLong);
+               double l = ((double) (lon - minLong)) / (maxLong - minLong);
                // now compare distance to eastern border with already known 
distance
                if (maxLong - lon < min) {
                        // update data if distance is shorter
                        min = maxLong - lon;
-                       i = 1;
-                       l = ((double)(lat - minLat))/(maxLat-minLat);
+                       edge = Edge.EASTERN;
+                       l = ((double) (lat - minLat)) / (maxLat - minLat);
                }
                // same for northern border
                if (maxLat - lat < min) {
                        min = maxLat - lat;
-                       i = 2;
-                       l = ((double)(maxLong - lon))/(maxLong-minLong);
+                       edge = Edge.NORTHERN;
+                       l = ((double) (maxLong - lon)) / (maxLong - minLong);
                }
                // same for western border
                if (lon - minLong < min) {
-                       i = 3;
-                       l = ((double)(maxLat - lat))/(maxLat-minLat);
+                       edge = Edge.WESTERN;
+                       l = ((double) (maxLat - lat)) / (maxLat - minLat);
                }
                // now created the EdgeHit for found values
-               return new EdgeHit(i, l);
+               return new EdgeHit(edge, l);
        } 
        
        private void concatenateWays(List<Way> ways, Area bounds) {
_______________________________________________
mkgmap-dev mailing list
[email protected]
http://www.mkgmap.org.uk/mailman/listinfo/mkgmap-dev

Reply via email to