Changeset 4423 in josm


Ignore:
Timestamp:
2011-09-15T15:14:22+02:00 (13 years ago)
Author:
bastiK
Message:

applied #6834 - Imagery providers slippy map enhancements: polygon support (patch by Don-vip, modified)

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Bounds.java

    r4363 r4423  
    4343
    4444    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);
    4949    }
    5050
     
    5353        if (coords.length != 4)
    5454            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]);
    5959    }
    6060
     
    8181            throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[3]));
    8282
    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]);
    8787    }
    8888
     
    114114            throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0.0 exptected, got {1}", "lonExtent", lonExtent));
    115115
    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);
    120120    }
    121121
     
    145145    public void extend(LatLon ll) {
    146146        if (ll.lat() < minLat) {
    147             minLat = roundToOsmPrecision(ll.lat());
     147            minLat = LatLon.roundToOsmPrecision(ll.lat());
    148148        }
    149149        if (ll.lon() < minLon) {
    150             minLon = roundToOsmPrecision(ll.lon());
     150            minLon = LatLon.roundToOsmPrecision(ll.lon());
    151151        }
    152152        if (ll.lat() > maxLat) {
    153             maxLat = roundToOsmPrecision(ll.lat());
     153            maxLat = LatLon.roundToOsmPrecision(ll.lat());
    154154        }
    155155        if (ll.lon() > maxLon) {
    156             maxLon = roundToOsmPrecision(ll.lon());
     156            maxLon = LatLon.roundToOsmPrecision(ll.lon());
    157157        }
    158158    }
     
    284284        return true;
    285285    }
    286 
    287     /**
    288      * Returns the value rounded to OSM precisions, i.e. to
    289      * LatLon.MAX_SERVER_PRECISION
    290      *
    291      * @return rounded value
    292      */
    293     private double roundToOsmPrecision(double value) {
    294         return Math.round(value / LatLon.MAX_SERVER_PRECISION) * LatLon.MAX_SERVER_PRECISION;
    295     }
    296286}
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r4206 r4423  
    223223        return "LatLon[lat="+lat()+",lon="+lon()+"]";
    224224    }
    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   
    226236    /**
    227237     * Replies a clone of this lat LatLon, rounded to OSM precisions, i.e. to
     
    232242    public LatLon getRoundedToOsmPrecision() {
    233243        return new LatLon(
    234                 Math.round(lat() / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION,
    235                 Math.round(lon() / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION
     244                roundToOsmPrecision(lat()),
     245                roundToOsmPrecision(lon())
    236246        );
    237247    }
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java

    r4405 r4423  
    3939        public String getUrlString() {
    4040            return urlString;
     41        }
     42    }
     43   
     44    public static class ImageryBounds extends Bounds {
     45        public ImageryBounds(String asString, String separator) {
     46            super(asString, separator);
     47        }
     48
     49        private List<Shape> shapes = new ArrayList<Shape>();
     50       
     51        public void addShape(Shape shape) {
     52            this.shapes.add(shape);
     53        }
     54
     55        public void setShapes(List<Shape> shapes) {
     56            this.shapes = shapes;
     57        }
     58
     59        public List<Shape> getShapes() {
     60            return shapes;
    4161        }
    4262    }
     
    5272    private int defaultMaxZoom = 0;
    5373    private int defaultMinZoom = 0;
    54     private Bounds bounds = null;
     74    private ImageryBounds bounds = null;
    5575    private List<String> serverProjections;
    5676    private String attributionText;
     
    107127        res.add(attributionImage);
    108128        res.add(termsOfUseURL);
     129        // Shapes
     130        String shapesString = "";
     131        if (bounds != null) {
     132            for (Shape s : bounds.getShapes()) {
     133                if (!shapesString.isEmpty()) {
     134                    shapesString += ";";
     135                }
     136                shapesString += s.encodeAsString(",");
     137            }
     138        }
     139        res.add(shapesString.isEmpty() ? null : shapesString);
    109140        return res;
    110141    }
     
    128159        if(array.size() >= 5 && !array.get(4).isEmpty()) {
    129160            try {
    130                 bounds = new Bounds(array.get(4), ",");
     161                bounds = new ImageryBounds(array.get(4), ",");
    131162            } catch (IllegalArgumentException e) {
    132163                Main.warn(e.toString());
     
    144175        if(array.size() >= 9 && !array.get(8).isEmpty()) {
    145176            setTermsOfUseURL(array.get(8));
     177        }
     178        if(bounds != null && array.size() >= 10 && !array.get(9).isEmpty()) {
     179            try {
     180                for (String s : array.get(9).split(";")) {
     181                    bounds.addShape(new Shape(s, ","));
     182                }
     183            } catch (IllegalArgumentException e) {
     184                Main.warn(e.toString());
     185            }
    146186        }
    147187    }
     
    201241    }
    202242
    203     public void setBounds(Bounds b) {
     243    public void setBounds(ImageryBounds b) {
    204244        this.bounds = b;
    205245    }
    206246
    207     public Bounds getBounds() {
     247    public ImageryBounds getBounds() {
    208248        return bounds;
    209249    }
    210250
    211251    public void setAttributionText(String text) {
    212          attributionText = text;
     252        attributionText = text;
    213253    }
    214254
  • trunk/src/org/openstreetmap/josm/gui/preferences/ImageryPreference.java

    r4416 r4423  
    1818import java.net.MalformedURLException;
    1919import java.net.URL;
     20import java.util.ArrayList;
    2021import java.util.HashMap;
    2122import java.util.List;
     
    5152
    5253import org.openstreetmap.gui.jmapviewer.JMapViewer;
     54import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
    5355import org.openstreetmap.gui.jmapviewer.MapRectangleImpl;
     56import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon;
    5457import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle;
    5558import org.openstreetmap.josm.Main;
    56 import org.openstreetmap.josm.data.Bounds;
    5759import org.openstreetmap.josm.data.imagery.ImageryInfo;
     60import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryBounds;
    5861import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
    5962import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
    6063import org.openstreetmap.josm.data.imagery.OffsetBookmark;
     64import org.openstreetmap.josm.data.imagery.Shape;
    6165import org.openstreetmap.josm.gui.layer.ImageryLayer;
    6266import org.openstreetmap.josm.gui.layer.TMSLayer;
     
    418422            map.setZoomContolsVisible(false);
    419423            map.setPreferredSize(new Dimension(200, 200));
    420             add(map, GBC.std().insets(5, 5, 0, 0).fill(GridBagConstraints.BOTH).weight(0.333, 0.6).insets(5, 0, 0, 0));
     424            add(map, GBC.std().insets(5, 5, 0, 0).fill(GridBagConstraints.BOTH).weight(0.33, 0.6).insets(5, 0, 0, 0));
    421425
    422426            listdef.getSelectionModel().addListSelectionListener(new DefListSelectionListener());
     
    460464        // Listener of default providers list selection
    461465        private final class DefListSelectionListener implements ListSelectionListener {
    462             // The current drawn rectangles
     466            // The current drawn rectangles and polygons
    463467            private final Map<Integer, MapRectangle> mapRectangles;
     468            private final Map<Integer, List<MapPolygon>> mapPolygons;
    464469
    465470            private DefListSelectionListener() {
    466471                this.mapRectangles = new HashMap<Integer, MapRectangle>();
     472                this.mapPolygons = new HashMap<Integer, List<MapPolygon>>();
    467473            }
    468474
    469475            @Override
    470476            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
    472478                if (e.getFirstIndex() == -1) {
    473479                    map.removeAllMapRectangles();
     480                    map.removeAllMapPolygons();
    474481                    mapRectangles.clear();
     482                    mapPolygons.clear();
    475483                    // Only process complete (final) selection events
    476484                } else if (!e.getValueIsAdjusting()) {
    477485                    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                        updateBoundsAndShapes(i);
     487                    }
     488                    // If needed, adjust map to show all map rectangles and polygons
     489                    if (!mapRectangles.isEmpty() || !mapPolygons.isEmpty()) {
     490                        map.setDisplayToFitMapElements(false, true, true);
     491                        map.zoomOut();
     492                    }
     493                }
     494            }
     495           
     496            private void updateBoundsAndShapes(int i) {
     497                ImageryBounds bounds = modeldef.getRow(i).getBounds();
     498                if (bounds != null) {
     499                    List<Shape> shapes = bounds.getShapes();
     500                    if (shapes != null && !shapes.isEmpty()) {
     501                        if (listdef.getSelectionModel().isSelectedIndex(i)) {
     502                            if (!mapPolygons.containsKey(i)) {
     503                                List<MapPolygon> list = new ArrayList<MapPolygon>();
     504                                mapPolygons.put(i, list);
     505                                // Add new map polygons
     506                                for (Shape shape : shapes) {
     507                                    MapPolygon polygon = new MapPolygonImpl(shape.getPoints());
     508                                    list.add(polygon);
     509                                    map.addMapPolygon(polygon);
    486510                                }
    487                             } else if (mapRectangles.containsKey(i)) {
    488                                 // Remove previousliy drawn map rectangle
    489                                 map.removeMapRectangle(mapRectangles.get(i));
    490                                 mapRectangles.remove(i);
    491511                            }
     512                        } else if (mapPolygons.containsKey(i)) {
     513                            // Remove previously drawn map polygons
     514                            for (MapPolygon polygon : mapPolygons.get(i)) {
     515                                map.removeMapPolygon(polygon);
     516                            }
     517                            mapPolygons.remove(i);
    492518                        }
    493                     }
    494                     // If needed, adjust map to show all map rectangles
    495                     if (!mapRectangles.isEmpty()) {
    496                         map.setDisplayToFitMapRectangle();
    497                         map.zoomOut();
     519                     // Only display bounds when no polygons (shapes) are defined for this provider
     520                    } else {
     521                        if (listdef.getSelectionModel().isSelectedIndex(i)) {
     522                            if (!mapRectangles.containsKey(i)) {
     523                                // Add new map rectangle
     524                                MapRectangle rectangle = new MapRectangleImpl(bounds);
     525                                mapRectangles.put(i, rectangle);
     526                                map.addMapRectangle(rectangle);
     527                            }
     528                        } else if (mapRectangles.containsKey(i)) {
     529                            // Remove previously drawn map rectangle
     530                            map.removeMapRectangle(mapRectangles.get(i));
     531                            mapRectangles.remove(i);
     532                        }
    498533                    }
    499534                }
  • trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java

    r4419 r4423  
    1919
    2020import org.openstreetmap.josm.Main;
    21 import org.openstreetmap.josm.data.Bounds;
    2221import org.openstreetmap.josm.data.imagery.ImageryInfo;
     22import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryBounds;
    2323import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
     24import org.openstreetmap.josm.data.imagery.Shape;
    2425import org.openstreetmap.josm.io.MirroredInputStream;
    2526import org.openstreetmap.josm.io.UTFInputStreamReader;
     
    4142        SUPPORTED_PROJECTIONS,
    4243        PR,
     44        BOUNDS,
     45        SHAPE,
    4346        UNKNOWN,            // element is not recognized in the current context
    4447    }
    4548
    46     public ImageryReader(String source) throws IOException {
     49    public ImageryReader(String source) {
    4750        this.source = source;
    4851    }
     
    143146                            // 5th parameter optional for bounds
    144147                            try {
    145                                 info.setBounds(new Bounds(val[4], ","));
     148                                info.setBounds(new ImageryBounds(val[4], ","));
    146149                            } catch (IllegalArgumentException e) {
    147150                                Main.warn(e.toString());
     
    190193
    191194        ImageryInfo entry;
    192         Bounds bounds;
     195        ImageryBounds bounds;
     196        Shape shape;
    193197        List<String> supported_srs;
    194198
     
    241245                    } else if (qName.equals("bounds")) {
    242246                        try {
    243                             bounds = new Bounds(
     247                            bounds = new ImageryBounds(
    244248                                    atts.getValue("min-lat") + "," +
    245249                                    atts.getValue("min-lon") + "," +
     
    249253                            break;
    250254                        }
    251                         newState = State.ENTRY_ATTRIBUTE;
     255                        newState = State.BOUNDS;
    252256                    } else if (qName.equals("supported-projections")) {
    253257                        supported_srs = new ArrayList<String>();
    254258                        newState = State.SUPPORTED_PROJECTIONS;
     259                    }
     260                    break;
     261                case BOUNDS:
     262                    if (qName.equals("shape")) {
     263                        shape = new Shape();
     264                        newState = State.SHAPE;
     265                    }
     266                    break;
     267                case SHAPE:
     268                    if (qName.equals("point")) {
     269                        try {
     270                            shape.addPoint(atts.getValue("lat"), atts.getValue("lon"));
     271                        } catch (IllegalArgumentException e) {
     272                            break;
     273                        }
    255274                    }
    256275                    break;
     
    339358                            }
    340359                        }
    341                     } else if (qName.equals("bounds")) {
    342                         entry.setBounds(bounds);
    343                         bounds = null;
    344360                    } else if (qName.equals("attribution-text")) {
    345361                        entry.setAttributionText(accumulator.toString());
     
    359375                    }
    360376                    break;
     377                case BOUNDS:
     378                    entry.setBounds(bounds);
     379                    bounds = null;
     380                    break;
     381                case SHAPE:
     382                    bounds.addShape(shape);
     383                    shape = null;
     384                    break;
    361385                case PR:
    362386                    supported_srs.add(accumulator.toString());
Note: See TracChangeset for help on using the changeset viewer.