Ticket #6834: patch.diff
File patch.diff, 39.5 KB (added by , 14 years ago) |
---|
-
src/org/openstreetmap/gui/jmapviewer/JMapViewer.java
27 27 import javax.swing.event.ChangeListener; 28 28 29 29 import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker; 30 import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon; 30 31 import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle; 31 32 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache; 32 33 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; … … 56 57 57 58 protected List<MapMarker> mapMarkerList; 58 59 protected List<MapRectangle> mapRectangleList; 60 protected List<MapPolygon> mapPolygonList; 59 61 60 62 protected boolean mapMarkersVisible; 61 63 protected boolean mapRectanglesVisible; 64 protected boolean mapPolygonsVisible; 62 65 63 66 protected boolean tileGridVisible; 64 67 … … 115 118 tileController = new TileController(tileSource, tileCache, this); 116 119 mapMarkerList = new LinkedList<MapMarker>(); 117 120 mapRectangleList = new LinkedList<MapRectangle>(); 121 mapPolygonList = new LinkedList<MapPolygon>(); 118 122 mapMarkersVisible = true; 119 123 mapRectanglesVisible = true; 124 mapPolygonsVisible = true; 120 125 tileGridVisible = false; 121 126 setLayout(null); 122 127 initializeZoomSlider(); … … 246 251 } 247 252 248 253 /** 249 * Sets the displayed map pane and zoom level so that all map markers are254 * Sets the displayed map pane and zoom level so that all chosen map elements are 250 255 * visible. 251 256 */ 252 public void setDisplayToFitMapMarkers() { 253 if (mapMarkerList == null || mapMarkerList.size() == 0) 257 public void setDisplayToFitMapElements(boolean markers, boolean rectangles, boolean polygons) { 258 int nbElemToCheck = 0; 259 if (markers && mapMarkerList != null) 260 nbElemToCheck += mapMarkerList.size(); 261 if (rectangles && mapRectangleList != null) 262 nbElemToCheck += mapRectangleList.size(); 263 if (polygons && mapPolygonList != null) 264 nbElemToCheck += mapPolygonList.size(); 265 if (nbElemToCheck == 0) 254 266 return; 267 255 268 int x_min = Integer.MAX_VALUE; 256 269 int y_min = Integer.MAX_VALUE; 257 270 int x_max = Integer.MIN_VALUE; 258 271 int y_max = Integer.MIN_VALUE; 259 272 int mapZoomMax = tileController.getTileSource().getMaxZoom(); 260 for (MapMarker marker : mapMarkerList) { 261 int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax); 262 int y = OsmMercator.LatToY(marker.getLat(), mapZoomMax); 263 x_max = Math.max(x_max, x); 264 y_max = Math.max(y_max, y); 265 x_min = Math.min(x_min, x); 266 y_min = Math.min(y_min, y); 273 274 if (markers) { 275 for (MapMarker marker : mapMarkerList) { 276 int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax); 277 int y = OsmMercator.LatToY(marker.getLat(), mapZoomMax); 278 x_max = Math.max(x_max, x); 279 y_max = Math.max(y_max, y); 280 x_min = Math.min(x_min, x); 281 y_min = Math.min(y_min, y); 282 } 267 283 } 284 285 if (rectangles) { 286 for (MapRectangle rectangle : mapRectangleList) { 287 x_max = Math.max(x_max, OsmMercator.LonToX(rectangle.getBottomRight().getLon(), mapZoomMax)); 288 y_max = Math.max(y_max, OsmMercator.LatToY(rectangle.getTopLeft().getLat(), mapZoomMax)); 289 x_min = Math.min(x_min, OsmMercator.LonToX(rectangle.getTopLeft().getLon(), mapZoomMax)); 290 y_min = Math.min(y_min, OsmMercator.LatToY(rectangle.getBottomRight().getLat(), mapZoomMax)); 291 } 292 } 293 294 if (polygons) { 295 for (MapPolygon polygon : mapPolygonList) { 296 for (Coordinate c : polygon.getPoints()) { 297 int x = OsmMercator.LonToX(c.getLon(), mapZoomMax); 298 int y = OsmMercator.LatToY(c.getLat(), mapZoomMax); 299 x_max = Math.max(x_max, x); 300 y_max = Math.max(y_max, y); 301 x_min = Math.min(x_min, x); 302 y_min = Math.min(y_min, y); 303 } 304 } 305 } 306 268 307 int height = Math.max(0, getHeight()); 269 308 int width = Math.max(0, getWidth()); 270 // System.out.println(x_min + " < x < " + x_max);271 // System.out.println(y_min + " < y < " + y_max);272 // System.out.println("tiles: " + width + " " + height);273 309 int newZoom = mapZoomMax; 274 310 int x = x_max - x_min; 275 311 int y = y_max - y_min; 276 312 while (x > width || y > height) { 277 // System.out.println("zoom: " + zoom + " -> " + x + " " + y);278 313 newZoom--; 279 314 x >>= 1; 280 315 y >>= 1; … … 286 321 y /= z; 287 322 setDisplayPosition(x, y, newZoom); 288 323 } 289 324 290 325 /** 291 326 * Sets the displayed map pane and zoom level so that all map markers are 292 327 * visible. 293 328 */ 294 public void setDisplayToFitMapRectangle() { 295 if (mapRectangleList == null || mapRectangleList.size() == 0) 296 return; 297 int x_min = Integer.MAX_VALUE; 298 int y_min = Integer.MAX_VALUE; 299 int x_max = Integer.MIN_VALUE; 300 int y_max = Integer.MIN_VALUE; 301 int mapZoomMax = tileController.getTileSource().getMaxZoom(); 302 for (MapRectangle rectangle : mapRectangleList) { 303 x_max = Math.max(x_max, OsmMercator.LonToX(rectangle.getBottomRight().getLon(), mapZoomMax)); 304 y_max = Math.max(y_max, OsmMercator.LatToY(rectangle.getTopLeft().getLat(), mapZoomMax)); 305 x_min = Math.min(x_min, OsmMercator.LonToX(rectangle.getTopLeft().getLon(), mapZoomMax)); 306 y_min = Math.min(y_min, OsmMercator.LatToY(rectangle.getBottomRight().getLat(), mapZoomMax)); 307 } 308 int height = Math.max(0, getHeight()); 309 int width = Math.max(0, getWidth()); 310 // System.out.println(x_min + " < x < " + x_max); 311 // System.out.println(y_min + " < y < " + y_max); 312 // System.out.println("tiles: " + width + " " + height); 313 int newZoom = mapZoomMax; 314 int x = x_max - x_min; 315 int y = y_max - y_min; 316 while (x > width || y > height) { 317 // System.out.println("zoom: " + zoom + " -> " + x + " " + y); 318 newZoom--; 319 x >>= 1; 320 y >>= 1; 321 } 322 x = x_min + (x_max - x_min) / 2; 323 y = y_min + (y_max - y_min) / 2; 324 int z = 1 << (mapZoomMax - newZoom); 325 x /= z; 326 y /= z; 327 setDisplayPosition(x, y, newZoom); 329 public void setDisplayToFitMapMarkers() { 330 setDisplayToFitMapElements(true, false, false); 328 331 } 329 332 330 333 /** 334 * Sets the displayed map pane and zoom level so that all map rectangles are 335 * visible. 336 */ 337 public void setDisplayToFitMapRectangles() { 338 setDisplayToFitMapElements(false, true, false); 339 } 340 341 /** 342 * Sets the displayed map pane and zoom level so that all map polygons are 343 * visible. 344 */ 345 public void setDisplayToFitMapPolygons() { 346 setDisplayToFitMapElements(false, false, true); 347 } 348 349 /** 331 350 * Calculates the latitude/longitude coordinate of the center of the 332 351 * currently displayed map area. 333 352 * … … 506 525 507 526 // g.drawString("Tiles in cache: " + tileCache.getTileCount(), 50, 20); 508 527 528 if (mapPolygonsVisible && mapPolygonList != null) { 529 for (MapPolygon polygon : mapPolygonList) { 530 paintPolygon(g, polygon); 531 } 532 } 533 509 534 if (mapRectanglesVisible && mapRectangleList != null) { 510 535 for (MapRectangle rectangle : mapRectangleList) { 511 Coordinate topLeft = rectangle.getTopLeft(); 512 Coordinate bottomRight = rectangle.getBottomRight(); 513 if (topLeft != null && bottomRight != null) { 514 Point pTopLeft = getMapPosition(topLeft.getLat(), topLeft.getLon(), false); 515 Point pBottomRight = getMapPosition(bottomRight.getLat(), bottomRight.getLon(), false); 516 if (pTopLeft != null && pBottomRight != null) { 517 rectangle.paint(g, pTopLeft, pBottomRight); 518 } 519 } 536 paintRectangle(g, rectangle); 520 537 } 521 538 } 522 539 … … 525 542 paintMarker(g, marker); 526 543 } 527 544 } 545 528 546 paintAttribution(g); 529 547 } 530 548 … … 539 557 } 540 558 541 559 /** 560 * Paint a single rectangle. 561 */ 562 protected void paintRectangle(Graphics g, MapRectangle rectangle) { 563 Coordinate topLeft = rectangle.getTopLeft(); 564 Coordinate bottomRight = rectangle.getBottomRight(); 565 if (topLeft != null && bottomRight != null) { 566 Point pTopLeft = getMapPosition(topLeft, false); 567 Point pBottomRight = getMapPosition(bottomRight, false); 568 if (pTopLeft != null && pBottomRight != null) { 569 rectangle.paint(g, pTopLeft, pBottomRight); 570 } 571 } 572 } 573 574 /** 575 * Paint a single polygon. 576 */ 577 protected void paintPolygon(Graphics g, MapPolygon polygon) { 578 List<Coordinate> coords = polygon.getPoints(); 579 if (coords != null && coords.size() >= 3) { 580 List<Point> points = new LinkedList<Point>(); 581 for (Coordinate c : coords) { 582 Point p = getMapPosition(c, false); 583 if (p == null) { 584 return; 585 } 586 points.add(p); 587 } 588 polygon.paint(g, points); 589 } 590 } 591 592 /** 542 593 * Moves the visible map pane. 543 594 * 544 595 * @param x … … 660 711 return mapRectangleList; 661 712 } 662 713 714 public void setMapPolygonList(List<MapPolygon> mapPolygonList) { 715 this.mapPolygonList = mapPolygonList; 716 repaint(); 717 } 718 719 public List<MapPolygon> getMapPolygonList() { 720 return mapPolygonList; 721 } 722 663 723 public void addMapMarker(MapMarker marker) { 664 724 mapMarkerList.add(marker); 665 725 repaint(); … … 690 750 repaint(); 691 751 } 692 752 753 public void addMapPolygon(MapPolygon polygon) { 754 mapPolygonList.add(polygon); 755 repaint(); 756 } 757 758 public void removeMapPolygon(MapPolygon polygon) { 759 mapPolygonList.remove(polygon); 760 repaint(); 761 } 762 763 public void removeAllMapPolygons() { 764 mapPolygonList.clear(); 765 repaint(); 766 } 767 693 768 public void setZoomContolsVisible(boolean visible) { 694 769 zoomSlider.setVisible(visible); 695 770 zoomInButton.setVisible(visible); … … 735 810 /** 736 811 * Enables or disables painting of the {@link MapRectangle} 737 812 * 738 * @param map MarkersVisible813 * @param mapRectanglesVisible 739 814 * @see #addMapRectangle(MapRectangle) 740 815 * @see #getMapRectangleList() 741 816 */ … … 744 819 repaint(); 745 820 } 746 821 822 public boolean isMapPolygonsVisible() { 823 return mapPolygonsVisible; 824 } 825 826 /** 827 * Enables or disables painting of the {@link MapPolygon} 828 * 829 * @param mapPolygonsVisible 830 * @see #addMapPolygon(MapPolygon) 831 * @see #getMapPolygonList() 832 */ 833 public void setMapPolygonsVisible(boolean mapPolygonsVisible) { 834 this.mapPolygonsVisible = mapPolygonsVisible; 835 repaint(); 836 } 837 747 838 /* 748 839 * (non-Javadoc) 749 840 * -
src/org/openstreetmap/gui/jmapviewer/MapPolygonImpl.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.gui.jmapviewer; 3 4 import java.awt.BasicStroke; 5 import java.awt.Color; 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 8 import java.awt.Point; 9 import java.awt.Polygon; 10 import java.awt.Stroke; 11 import java.util.List; 12 13 import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon; 14 15 /** 16 * @author Vincent 17 * 18 */ 19 public class MapPolygonImpl implements MapPolygon { 20 21 private List<Coordinate> points; 22 private Color color; 23 private Stroke stroke; 24 25 public MapPolygonImpl(List<Coordinate> points) { 26 this(points, Color.BLUE, new BasicStroke(2)); 27 } 28 29 public MapPolygonImpl(List<Coordinate> points, Color color, Stroke stroke) { 30 this.points = points; 31 this.color = color; 32 this.stroke = stroke; 33 } 34 35 /* (non-Javadoc) 36 * @see org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon#getPoints() 37 */ 38 @Override 39 public List<Coordinate> getPoints() { 40 return this.points; 41 } 42 43 /* (non-Javadoc) 44 * @see org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon#paint(java.awt.Graphics, java.util.List) 45 */ 46 @Override 47 public void paint(Graphics g, List<Point> points) { 48 Polygon polygon = new Polygon(); 49 for (Point p : points) { 50 polygon.addPoint(p.x, p.y); 51 } 52 paint(g, polygon); 53 } 54 55 /* (non-Javadoc) 56 * @see org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon#paint(java.awt.Graphics, java.awt.Polygon) 57 */ 58 @Override 59 public void paint(Graphics g, Polygon polygon) { 60 // Prepare graphics 61 Color oldColor = g.getColor(); 62 g.setColor(color); 63 Stroke oldStroke = null; 64 if (g instanceof Graphics2D) { 65 Graphics2D g2 = (Graphics2D) g; 66 oldStroke = g2.getStroke(); 67 g2.setStroke(stroke); 68 } 69 // Draw 70 g.drawPolygon(polygon); 71 // Restore graphics 72 g.setColor(oldColor); 73 if (g instanceof Graphics2D) { 74 ((Graphics2D) g).setStroke(oldStroke); 75 } 76 } 77 78 /* (non-Javadoc) 79 * @see java.lang.Object#toString() 80 */ 81 @Override 82 public String toString() { 83 return "MapPolygon [points=" + points + "]"; 84 } 85 } -
src/org/openstreetmap/gui/jmapviewer/MapRectangleImpl.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.gui.jmapviewer; 3 3 4 import java.awt.BasicStroke; 4 5 import java.awt.Color; 5 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 6 8 import java.awt.Point; 9 import java.awt.Stroke; 7 10 8 11 import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle; 9 12 import org.openstreetmap.josm.data.Bounds; … … 16 19 17 20 private Coordinate topLeft; 18 21 private Coordinate bottomRight; 19 Color color; 22 private Color color; 23 private Stroke stroke; 20 24 21 25 public MapRectangleImpl(Bounds bounds) { 22 this(bounds, Color.BLUE );26 this(bounds, Color.BLUE, new BasicStroke(2)); 23 27 } 24 28 25 public MapRectangleImpl(Bounds bounds, Color color ) {29 public MapRectangleImpl(Bounds bounds, Color color, Stroke stroke) { 26 30 this.topLeft = new Coordinate(bounds.getMax().lat(), bounds.getMin().lon()); 27 31 this.bottomRight = new Coordinate(bounds.getMin().lat(), bounds.getMax().lon()); 28 32 this.color = color; 33 this.stroke = stroke; 29 34 } 30 35 31 36 /* (non-Javadoc) … … 49 54 */ 50 55 @Override 51 56 public void paint(Graphics g, Point topLeft, Point bottomRight) { 57 // Prepare graphics 58 Color oldColor = g.getColor(); 52 59 g.setColor(color); 60 Stroke oldStroke = null; 61 if (g instanceof Graphics2D) { 62 Graphics2D g2 = (Graphics2D) g; 63 oldStroke = g2.getStroke(); 64 g2.setStroke(stroke); 65 } 66 // Draw 53 67 g.drawRect(topLeft.x, topLeft.y, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y); 68 // Restore graphics 69 g.setColor(oldColor); 70 if (g instanceof Graphics2D) { 71 ((Graphics2D) g).setStroke(oldStroke); 72 } 54 73 } 55 74 56 75 @Override -
src/org/openstreetmap/gui/jmapviewer/interfaces/MapPolygon.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.gui.jmapviewer.interfaces; 3 4 import java.awt.Graphics; 5 import java.awt.Point; 6 import java.awt.Polygon; 7 import java.util.List; 8 9 import org.openstreetmap.gui.jmapviewer.Coordinate; 10 11 /** 12 * Interface to be implemented by polygons that can be displayed on the map. 13 * 14 * @author Vincent 15 */ 16 public interface MapPolygon { 17 18 /** 19 * @return Latitude/Longitude of each point of polygon 20 */ 21 public List<Coordinate> getPoints(); 22 23 /** 24 * Paints the map rectangle on the map. The <code>points</code> 25 * are specifying the coordinates within <code>g</code> 26 * 27 * @param g 28 * @param points 29 */ 30 public void paint(Graphics g, List<Point> points); 31 32 /** 33 * Paints the map rectangle on the map. The <code>polygon</code> 34 * is specifying the coordinates within <code>g</code> 35 * 36 * @param g 37 * @param polygon 38 */ 39 public void paint(Graphics g, Polygon polygon); 40 } -
src/org/openstreetmap/josm/data/Bounds.java
42 42 } 43 43 44 44 public Bounds(double minlat, double minlon, double maxlat, double maxlon) { 45 this.minLat = roundToOsmPrecision(minlat);46 this.minLon = roundToOsmPrecision(minlon);47 this.maxLat = roundToOsmPrecision(maxlat);48 this.maxLon = roundToOsmPrecision(maxlon);45 this.minLat = LatLon.roundToOsmPrecision(minlat); 46 this.minLon = LatLon.roundToOsmPrecision(minlon); 47 this.maxLat = LatLon.roundToOsmPrecision(maxlat); 48 this.maxLon = LatLon.roundToOsmPrecision(maxlon); 49 49 } 50 50 51 51 public Bounds(double [] coords) { 52 52 CheckParameterUtil.ensureParameterNotNull(coords, "coords"); 53 53 if (coords.length != 4) 54 54 throw new IllegalArgumentException(MessageFormat.format("Expected array of length 4, got {0}", coords.length)); 55 this.minLat = roundToOsmPrecision(coords[0]);56 this.minLon = roundToOsmPrecision(coords[1]);57 this.maxLat = roundToOsmPrecision(coords[2]);58 this.maxLon = roundToOsmPrecision(coords[3]);55 this.minLat = LatLon.roundToOsmPrecision(coords[0]); 56 this.minLon = LatLon.roundToOsmPrecision(coords[1]); 57 this.maxLat = LatLon.roundToOsmPrecision(coords[2]); 58 this.maxLon = LatLon.roundToOsmPrecision(coords[3]); 59 59 } 60 60 61 61 public Bounds(String asString, String separator) throws IllegalArgumentException { … … 80 80 if (!LatLon.isValidLon(values[3])) 81 81 throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[3])); 82 82 83 this.minLat = roundToOsmPrecision(values[0]);84 this.minLon = roundToOsmPrecision(values[1]);85 this.maxLat = roundToOsmPrecision(values[2]);86 this.maxLon = roundToOsmPrecision(values[3]);83 this.minLat = LatLon.roundToOsmPrecision(values[0]); 84 this.minLon = LatLon.roundToOsmPrecision(values[1]); 85 this.maxLat = LatLon.roundToOsmPrecision(values[2]); 86 this.maxLon = LatLon.roundToOsmPrecision(values[3]); 87 87 } 88 88 89 89 public Bounds(Bounds other) { … … 113 113 if (lonExtent <= 0.0) 114 114 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0.0 exptected, got {1}", "lonExtent", lonExtent)); 115 115 116 this.minLat = roundToOsmPrecision(center.lat() - latExtent / 2);117 this.minLon = roundToOsmPrecision(center.lon() - lonExtent / 2);118 this.maxLat = roundToOsmPrecision(center.lat() + latExtent / 2);119 this.maxLon = roundToOsmPrecision(center.lon() + lonExtent / 2);116 this.minLat = LatLon.roundToOsmPrecision(center.lat() - latExtent / 2); 117 this.minLon = LatLon.roundToOsmPrecision(center.lon() - lonExtent / 2); 118 this.maxLat = LatLon.roundToOsmPrecision(center.lat() + latExtent / 2); 119 this.maxLon = LatLon.roundToOsmPrecision(center.lon() + lonExtent / 2); 120 120 } 121 121 122 122 @Override public String toString() { … … 144 144 */ 145 145 public void extend(LatLon ll) { 146 146 if (ll.lat() < minLat) { 147 minLat = roundToOsmPrecision(ll.lat());147 minLat = LatLon.roundToOsmPrecision(ll.lat()); 148 148 } 149 149 if (ll.lon() < minLon) { 150 minLon = roundToOsmPrecision(ll.lon());150 minLon = LatLon.roundToOsmPrecision(ll.lon()); 151 151 } 152 152 if (ll.lat() > maxLat) { 153 maxLat = roundToOsmPrecision(ll.lat());153 maxLat = LatLon.roundToOsmPrecision(ll.lat()); 154 154 } 155 155 if (ll.lon() > maxLon) { 156 maxLon = roundToOsmPrecision(ll.lon());156 maxLon = LatLon.roundToOsmPrecision(ll.lon()); 157 157 } 158 158 } 159 159 … … 283 283 return false; 284 284 return true; 285 285 } 286 287 /**288 * Returns the value rounded to OSM precisions, i.e. to289 * LatLon.MAX_SERVER_PRECISION290 *291 * @return rounded value292 */293 private double roundToOsmPrecision(double value) {294 return Math.round(value / LatLon.MAX_SERVER_PRECISION) * LatLon.MAX_SERVER_PRECISION;295 }296 286 } -
src/org/openstreetmap/josm/data/coor/LatLon.java
222 222 @Override public String toString() { 223 223 return "LatLon[lat="+lat()+",lon="+lon()+"]"; 224 224 } 225 225 226 /** 227 * Returns the value rounded to OSM precisions, i.e. to 228 * LatLon.MAX_SERVER_PRECISION 229 * 230 * @return rounded value 231 */ 232 public static double roundToOsmPrecision(double value) { 233 return Math.round(value / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION; 234 } 235 226 236 /** 227 237 * Replies a clone of this lat LatLon, rounded to OSM precisions, i.e. to 228 238 * MAX_SERVER_PRECISION … … 231 241 */ 232 242 public LatLon getRoundedToOsmPrecision() { 233 243 return new LatLon( 234 Math.round(lat() / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION,235 Math.round(lon() / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION244 roundToOsmPrecision(lat()), 245 roundToOsmPrecision(lon()) 236 246 ); 237 247 } 238 248 -
src/org/openstreetmap/josm/data/imagery/ImageryInfo.java
51 51 private int maxZoom = 0; 52 52 private int defaultMaxZoom = 0; 53 53 private int defaultMinZoom = 0; 54 private Bounds bounds = null; 54 private List<Bounds> bounds = new ArrayList<Bounds>(); 55 private List<Shape> shapes = new ArrayList<Shape>(); 55 56 private List<String> serverProjections; 56 57 private String attributionText; 57 58 private String attributionImage; … … 101 102 } else { 102 103 res.add(maxZoom != 0 ? String.valueOf(maxZoom) : null); 103 104 } 104 res.add(bounds != null ? bounds.encodeAsString(",") : null); 105 // Bounds 106 String boundsString = ""; 107 for (Bounds b : bounds) { 108 if (!boundsString.isEmpty()) { 109 boundsString += ";"; 110 } 111 boundsString += b.encodeAsString(","); 112 } 113 res.add(boundsString.isEmpty() ? null : boundsString); 114 // Attribution, terms of use 105 115 res.add(attributionText); 106 116 res.add(attributionLinkURL); 107 117 res.add(attributionImage); 108 118 res.add(termsOfUseURL); 119 // Shapes 120 String shapesString = ""; 121 for (Shape s : shapes) { 122 if (!shapesString.isEmpty()) { 123 shapesString += ";"; 124 } 125 shapesString += s.encodeAsString(","); 126 } 127 res.add(shapesString.isEmpty() ? null : shapesString); 109 128 return res; 110 129 } 111 130 … … 127 146 } 128 147 if(array.size() >= 5 && !array.get(4).isEmpty()) { 129 148 try { 130 bounds = new Bounds(array.get(4), ","); 149 for (String s : array.get(4).split(";")) { 150 addBounds(new Bounds(s, ",")); 151 } 131 152 } catch (IllegalArgumentException e) { 132 153 Main.warn(e.toString()); 133 154 } … … 144 165 if(array.size() >= 9 && !array.get(8).isEmpty()) { 145 166 setTermsOfUseURL(array.get(8)); 146 167 } 168 if(array.size() >= 10 && !array.get(9).isEmpty()) { 169 try { 170 for (String s : array.get(9).split(";")) { 171 addShape(new Shape(s, ",")); 172 } 173 } catch (IllegalArgumentException e) { 174 Main.warn(e.toString()); 175 } 176 } 147 177 } 148 178 149 179 public ImageryInfo(ImageryInfo i) { … … 157 187 this.pixelPerDegree=i.pixelPerDegree; 158 188 this.eulaAcceptanceRequired = null; 159 189 this.bounds = i.bounds; 190 this.shapes = i.shapes; 160 191 this.attributionImage = i.attributionImage; 161 192 this.attributionLinkURL = i.attributionLinkURL; 162 193 this.attributionText = i.attributionText; … … 200 231 this.maxZoom = maxZoom; 201 232 } 202 233 203 public void setBounds(Bounds b) {204 this.bounds = b;234 public void addBounds(Bounds b) { 235 this.bounds.add(b); 205 236 } 206 237 207 public Bounds getBounds() { 238 public void setBounds(List<Bounds> list) { 239 this.bounds = list; 240 } 241 242 public List<Bounds> getBounds() { 208 243 return bounds; 209 244 } 210 245 246 public void addShape(Shape s) { 247 this.shapes.add(s); 248 } 249 250 public void setShapes(List<Shape> list) { 251 this.shapes = list; 252 } 253 254 public List<Shape> getShapes() { 255 return shapes; 256 } 257 211 258 public void setAttributionText(String text) { 212 259 attributionText = text; 213 260 } 214 261 215 262 public void setAttributionImage(String text) { -
src/org/openstreetmap/josm/data/imagery/Shape.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.data.imagery; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import java.text.MessageFormat; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.openstreetmap.gui.jmapviewer.Coordinate; 11 import org.openstreetmap.josm.data.coor.LatLon; 12 import org.openstreetmap.josm.tools.CheckParameterUtil; 13 14 /** 15 * @author Vincent 16 * 17 */ 18 public class Shape { 19 20 private List<Coordinate> coords = new ArrayList<Coordinate>(); 21 22 public Shape(String asString, String separator) throws IllegalArgumentException { 23 CheckParameterUtil.ensureParameterNotNull(asString, "asString"); 24 String[] components = asString.split(separator); 25 if (components.length % 2 != 0) 26 throw new IllegalArgumentException(MessageFormat.format("Even number of doubles excpected in string, got {0}: {1}", components.length, asString)); 27 for (int i=0; i<components.length; i+=2) { 28 addPoint(components[i], components[i+1]); 29 } 30 } 31 32 public Shape() { 33 } 34 35 public String encodeAsString(String separator) { 36 StringBuffer sb = new StringBuffer(); 37 for (Coordinate c : coords) { 38 if (sb.length() != 0) { 39 sb.append(separator); 40 } 41 sb.append(c.getLat()).append(separator).append(c.getLon()); 42 } 43 return sb.toString(); 44 } 45 46 public List<Coordinate> getPoints() { 47 return coords; 48 } 49 50 public void addPoint(String sLat, String sLon) throws IllegalArgumentException { 51 CheckParameterUtil.ensureParameterNotNull(sLat, "sLat"); 52 CheckParameterUtil.ensureParameterNotNull(sLon, "sLon"); 53 54 double lat, lon; 55 56 try { 57 lat = Double.parseDouble(sLat); 58 if (!LatLon.isValidLat(lat)) 59 throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", lat)); 60 } catch (NumberFormatException e) { 61 throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLat)); 62 } 63 64 try { 65 lon = Double.parseDouble(sLon); 66 if (!LatLon.isValidLon(lon)) 67 throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", lon)); 68 } catch (NumberFormatException e) { 69 throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLon)); 70 } 71 72 coords.add(new Coordinate(LatLon.roundToOsmPrecision(lat), LatLon.roundToOsmPrecision(lon))); 73 } 74 } -
src/org/openstreetmap/josm/gui/preferences/ImageryPreference.java
17 17 import java.io.IOException; 18 18 import java.net.MalformedURLException; 19 19 import java.net.URL; 20 import java.util.ArrayList; 20 21 import java.util.HashMap; 21 22 import java.util.List; 22 23 import java.util.Locale; … … 50 51 import javax.swing.table.TableColumnModel; 51 52 52 53 import org.openstreetmap.gui.jmapviewer.JMapViewer; 54 import org.openstreetmap.gui.jmapviewer.MapPolygonImpl; 53 55 import org.openstreetmap.gui.jmapviewer.MapRectangleImpl; 56 import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon; 54 57 import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle; 55 58 import org.openstreetmap.josm.Main; 56 59 import org.openstreetmap.josm.data.Bounds; … … 58 61 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 59 62 import org.openstreetmap.josm.data.imagery.ImageryLayerInfo; 60 63 import org.openstreetmap.josm.data.imagery.OffsetBookmark; 64 import org.openstreetmap.josm.data.imagery.Shape; 61 65 import org.openstreetmap.josm.gui.layer.ImageryLayer; 62 66 import org.openstreetmap.josm.gui.layer.TMSLayer; 63 67 import org.openstreetmap.josm.gui.layer.WMSLayer; … … 459 463 460 464 // Listener of default providers list selection 461 465 private final class DefListSelectionListener implements ListSelectionListener { 462 // The current drawn rectangles 463 private final Map<Integer, MapRectangle> mapRectangles; 466 // The current drawn rectangles and polygons 467 private final Map<Integer, List<MapRectangle>> mapRectangles; 468 private final Map<Integer, List<MapPolygon>> mapPolygons; 464 469 465 470 private DefListSelectionListener() { 466 this.mapRectangles = new HashMap<Integer, MapRectangle>(); 471 this.mapRectangles = new HashMap<Integer, List<MapRectangle>>(); 472 this.mapPolygons = new HashMap<Integer, List<MapPolygon>>(); 467 473 } 468 474 469 475 @Override 470 476 public void valueChanged(ListSelectionEvent e) { 471 // First index is set to -1 when the list is refreshed, so discard all map rectangles 477 // First index is set to -1 when the list is refreshed, so discard all map rectangles and polygons 472 478 if (e.getFirstIndex() == -1) { 473 479 map.removeAllMapRectangles(); 480 map.removeAllMapPolygons(); 474 481 mapRectangles.clear(); 482 mapPolygons.clear(); 475 483 // Only process complete (final) selection events 476 484 } else if (!e.getValueIsAdjusting()) { 477 485 for (int i = e.getFirstIndex(); i<=e.getLastIndex(); i++) { 478 Bounds bounds = modeldef.getRow(i).getBounds(); 479 if (bounds != null) { 480 if (listdef.getSelectionModel().isSelectedIndex(i)) { 481 if (!mapRectangles.containsKey(i)) { 482 // Add new map rectangle 483 MapRectangle rectangle = new MapRectangleImpl(bounds); 484 mapRectangles.put(i, rectangle); 485 map.addMapRectangle(rectangle); 486 } 487 } else if (mapRectangles.containsKey(i)) { 488 // Remove previousliy drawn map rectangle 489 map.removeMapRectangle(mapRectangles.get(i)); 490 mapRectangles.remove(i); 486 updateBounds(i); 487 updateShapes(i); 488 } 489 // If needed, adjust map to show all map rectangles and polygons 490 if (!mapRectangles.isEmpty() || !mapPolygons.isEmpty()) { 491 map.setDisplayToFitMapElements(false, true, true); 492 map.zoomOut(); 493 } 494 } 495 } 496 497 private void updateBounds(int i) { 498 List<Bounds> bounds = modeldef.getRow(i).getBounds(); 499 if (bounds != null && !bounds.isEmpty()) { 500 if (listdef.getSelectionModel().isSelectedIndex(i)) { 501 if (!mapRectangles.containsKey(i)) { 502 List<MapRectangle> list = new ArrayList<MapRectangle>(); 503 mapRectangles.put(i, list); 504 // Add new map rectangles 505 for (Bounds b : bounds) { 506 MapRectangle rectangle = new MapRectangleImpl(b); 507 list.add(rectangle); 508 map.addMapRectangle(rectangle); 491 509 } 492 510 } 511 } else if (mapRectangles.containsKey(i)) { 512 // Remove previously drawn map rectangles 513 for (MapRectangle rectangle : mapRectangles.get(i)) { 514 map.removeMapRectangle(rectangle); 515 } 516 mapRectangles.remove(i); 493 517 } 494 // If needed, adjust map to show all map rectangles 495 if (!mapRectangles.isEmpty()) { 496 map.setDisplayToFitMapRectangle(); 497 map.zoomOut(); 518 } 519 } 520 521 private void updateShapes(int i) { 522 List<Shape> shapes = modeldef.getRow(i).getShapes(); 523 if (shapes != null && !shapes.isEmpty()) { 524 if (listdef.getSelectionModel().isSelectedIndex(i)) { 525 if (!mapPolygons.containsKey(i)) { 526 List<MapPolygon> list = new ArrayList<MapPolygon>(); 527 mapPolygons.put(i, list); 528 // Add new map polygons 529 for (Shape shape : shapes) { 530 MapPolygon polygon = new MapPolygonImpl(shape.getPoints()); 531 list.add(polygon); 532 map.addMapPolygon(polygon); 533 } 534 } 535 } else if (mapPolygons.containsKey(i)) { 536 // Remove previously drawn map polygons 537 for (MapPolygon polygon : mapPolygons.get(i)) { 538 map.removeMapPolygon(polygon); 539 } 540 mapPolygons.remove(i); 498 541 } 499 542 } 500 543 } -
src/org/openstreetmap/josm/io/imagery/ImageryReader.java
21 21 import org.openstreetmap.josm.data.Bounds; 22 22 import org.openstreetmap.josm.data.imagery.ImageryInfo; 23 23 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 24 import org.openstreetmap.josm.data.imagery.Shape; 24 25 import org.openstreetmap.josm.io.MirroredInputStream; 25 26 import org.openstreetmap.josm.io.UTFInputStreamReader; 26 27 import org.openstreetmap.josm.tools.Utils; … … 142 143 if (val.length >= 5 && !val[4].isEmpty()) { 143 144 // 5th parameter optional for bounds 144 145 try { 145 info. setBounds(new Bounds(val[4], ","));146 info.addBounds(new Bounds(val[4], ",")); 146 147 } catch (IllegalArgumentException e) { 147 148 Main.warn(e.toString()); 148 149 } … … 190 191 191 192 ImageryInfo entry; 192 193 Bounds bounds; 194 Shape shape; 193 195 List<String> supported_srs; 194 196 195 197 @Override public void startDocument() { … … 249 251 break; 250 252 } 251 253 newState = State.ENTRY_ATTRIBUTE; 254 } else if (qName.equals("shape")) { 255 shape = new Shape(); 256 newState = State.ENTRY_ATTRIBUTE; 252 257 } else if (qName.equals("supported-projections")) { 253 258 supported_srs = new ArrayList<String>(); 254 259 newState = State.SUPPORTED_PROJECTIONS; 255 260 } 256 261 break; 262 case ENTRY_ATTRIBUTE: 263 if (qName.equals("point")) { 264 shape.addPoint(atts.getValue("lat"), atts.getValue("lon")); 265 } 266 break; 257 267 case SUPPORTED_PROJECTIONS: 258 268 if (qName.equals("pr")) { 259 269 newState = State.PR; … … 339 349 } 340 350 } 341 351 } else if (qName.equals("bounds")) { 342 entry. setBounds(bounds);352 entry.addBounds(bounds); 343 353 bounds = null; 354 } else if (qName.equals("shape")) { 355 entry.addShape(shape); 356 shape = null; 344 357 } else if (qName.equals("attribution-text")) { 345 358 entry.setAttributionText(accumulator.toString()); 346 359 } else if (qName.equals("attribution-url")) {