Changeset 9825 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2016-02-18T21:41:08+01:00 (8 years ago)
Author:
wiktorn
Message:

Improvements for native scales.

  • make ScaleList immutable object
  • reduce number of ScaleList object creation
  • use ScaleList.snapZoom for getBestZoom in WMTS

See: #12350

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

Legend:

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

    r9818 r9825  
    4747import org.openstreetmap.josm.data.projection.Projections;
    4848import org.openstreetmap.josm.gui.ExtendedDialog;
    49 import org.openstreetmap.josm.gui.layer.NativeScaleLayer.Scale;
    5049import org.openstreetmap.josm.gui.layer.NativeScaleLayer.ScaleList;
    5150import org.openstreetmap.josm.io.CachedFile;
     
    246245    private TransferMode transferMode;
    247246
     247    private ScaleList nativeScaleList;
     248
    248249    /**
    249250     * Creates a tile source based on imagery info
     
    631632
    632633        this.crsScale = getTileSize() * 0.28e-03 / proj.getMetersPerUnit();
     634
     635        Collection<Double> scales = new ArrayList<>(currentTileMatrixSet.tileMatrix.size());
     636        if (currentTileMatrixSet != null) {
     637            for (TileMatrix tileMatrix : currentTileMatrixSet.tileMatrix) {
     638                scales.add(tileMatrix.scaleDenominator * 0.28e-03);
     639            }
     640        }
     641        this.nativeScaleList = new ScaleList(scales);
    633642    }
    634643
     
    919928     */
    920929    public ScaleList getNativeScales() {
    921         ScaleList scales = new ScaleList();
    922         if (currentTileMatrixSet != null) {
    923             for (TileMatrix tileMatrix : currentTileMatrixSet.tileMatrix) {
    924                 scales.add(new Scale(tileMatrix.scaleDenominator * 0.28e-03));
    925             }
    926         }
    927         return scales;
     930        return nativeScaleList;
    928931    }
    929932
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r9818 r9825  
    156156    public NavigatableComponent() {
    157157        setLayout(null);
    158         PROP_ZOOM_RATIO.get(); // make sure it is available in preferences
    159158    }
    160159
     
    208207            }
    209208            Scale scale = scaleList.scaleZoomTimes(getScale(), PROP_ZOOM_RATIO.get(), times);
    210             return scale.scale;
     209            return scale.getScale();
    211210        } else {
    212211            return getScale() * Math.pow(PROP_ZOOM_RATIO.get(), times);
     
    245244        if (nativeScaleLayer != null) {
    246245            ScaleList scaleList = nativeScaleLayer.getNativeScales();
    247             return scaleList.getSnapScale(scale, PROP_ZOOM_RATIO.get(), floor).scale;
     246            return scaleList.getSnapScale(scale, PROP_ZOOM_RATIO.get(), floor).getScale();
    248247        } else {
    249248            return scale;
  • trunk/src/org/openstreetmap/josm/gui/layer/NativeScaleLayer.java

    r9818 r9825  
    33
    44import java.util.ArrayList;
     5import java.util.Collection;
     6import java.util.List;
    57
    68import org.openstreetmap.josm.gui.NavigatableComponent;
     
    2527         * Scale factor, same unit as in {@link NavigatableComponent}
    2628         */
    27         public double scale;
     29        private double scale;
    2830
    2931        /**
    3032         * True if this scale is native resolution for data source.
    3133         */
    32         public boolean isNative;
     34        private boolean isNative;
    3335
    3436        private int index;
     
    3739         * Constructs a new Scale with given scale, native defaults to true.
    3840         * @param scale as defined in WMTS (scaleDenominator)
    39          */
    40         public Scale(double scale) {
     41         * @param index zoom index for this scale
     42         */
     43        public Scale(double scale, int index) {
    4144            this.scale = scale;
    4245            this.isNative = true;
    43         }
    44 
    45         /**
    46          * Constructs a new Scale with given scale and native values.
    47          * @param scale as defined in WMTS (scaleDenominator)
    48          * @param isNative is this scale native to the source or not
    49          */
    50         public Scale(double scale, boolean isNative) {
    51             this.scale = scale;
    52             this.isNative = isNative;
     46            this.index = index;
    5347        }
    5448
     
    7771            return index;
    7872        }
     73
     74        public double getScale() {
     75            return scale;
     76        }
    7977    }
    8078
     
    8381     * between native resolutions
    8482     */
    85     class ScaleList extends ArrayList<Scale> {
     83    class ScaleList  {
     84        private List<Scale> scales = new ArrayList<>();
     85
     86        protected ScaleList(double[] scales) {
     87            for (int i = 0; i < scales.length; i++) {
     88                this.scales.add(new Scale(scales[i], i));
     89            }
     90        }
     91
     92        protected ScaleList() {
     93        }
     94
     95        public ScaleList(Collection<Double> scales) {
     96            int i = 0;
     97            for (Double scale: scales) {
     98                this.scales.add(new Scale(scale, i++));
     99            }
     100        }
     101
     102        protected void addScale(Scale scale) {
     103            scales.add(scale);
     104        }
    86105
    87106        /**
     
    94113            ScaleList result = new ScaleList();
    95114            Scale previous = null;
    96             for (Scale current: this) {
     115            for (Scale current: this.scales) {
    97116                if (previous != null) {
    98117                    double step = previous.scale / current.scale;
     
    102121                    for (int j = 1; j < steps; j++) {
    103122                        double intermediate = previous.scale / Math.pow(smallStep, j);
    104                         result.add(new Scale(intermediate, false));
     123                        result.addScale(new Scale(intermediate, false, current.index));
    105124                    }
    106125                }
    107                 result.add(current);
     126                result.addScale(current);
    108127                previous = current;
    109128            }
    110129            return result;
     130        }
     131
     132        /**
     133         * Get a scale from this ScaleList or a new scale if zoomed outside.
     134         * @param scale previous scale
     135         * @param floor use floor instead of round, set true when fitting view to objects
     136         * @return new {@link Scale}
     137         */
     138        public Scale getSnapScale(double scale, boolean floor) {
     139            return getSnapScale(scale, NavigatableComponent.PROP_ZOOM_RATIO.get(), floor);
    111140        }
    112141
     
    119148         */
    120149        public Scale getSnapScale(double scale, double ratio, boolean floor) {
    121             int size = size();
    122             Scale first = get(0);
    123             Scale last = get(size-1);
     150            int size = scales.size();
     151            Scale first = scales.get(0);
     152            Scale last = scales.get(size-1);
     153
    124154            if (scale > first.scale) {
    125155                double step = scale / first.scale;
     
    143173                Scale previous = null;
    144174                for (int i = 0; i < size; i++) {
    145                     Scale current = this.get(i);
     175                    Scale current = this.scales.get(i);
    146176                    if (previous != null) {
    147177                        if (scale <= previous.scale && scale >= current.scale) {
     
    207237        public String toString() {
    208238            StringBuilder stringBuilder = new StringBuilder();
    209             for (Scale s: this) {
     239            for (Scale s: this.scales) {
    210240                stringBuilder.append(s + "\n");
    211241            }
     
    215245        private Scale getNextIn(Scale scale, double ratio) {
    216246            int nextIndex = scale.getIndex() + 1;
    217             if (nextIndex <= 0 || nextIndex > size()-1) {
     247            if (nextIndex <= 0 || nextIndex > this.scales.size()-1) {
    218248                return new Scale(scale.scale / ratio, nextIndex == 0, nextIndex);
    219249            } else {
    220                 Scale nextScale = get(nextIndex);
     250                Scale nextScale = this.scales.get(nextIndex);
    221251                return new Scale(nextScale.scale, nextScale.isNative, nextIndex);
    222252            }
     
    225255        private Scale getNextOut(Scale scale, double ratio) {
    226256            int nextIndex = scale.getIndex() - 1;
    227             if (nextIndex < 0 || nextIndex >= size()-1) {
    228                 return new Scale(scale.scale * ratio, nextIndex == size()-1, nextIndex);
     257            if (nextIndex < 0 || nextIndex >= this.scales.size()-1) {
     258                return new Scale(scale.scale * ratio, nextIndex == this.scales.size()-1, nextIndex);
    229259            } else {
    230                 Scale nextScale = get(nextIndex);
     260                Scale nextScale = this.scales.get(nextIndex);
    231261                return new Scale(nextScale.scale, nextScale.isNative, nextIndex);
    232262            }
  • trunk/src/org/openstreetmap/josm/gui/layer/TMSLayer.java

    r9818 r9825  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.util.ArrayList;
     7import java.util.Collection;
    58
    69import org.apache.commons.jcs.access.CacheAccess;
     
    4548            true);
    4649
     50    private ScaleList nativeScaleList;
     51
    4752    /**
    4853     * Create a layer based on ImageryInfo
     
    5156    public TMSLayer(ImageryInfo info) {
    5257        super(info);
     58        Collection<Double> scales = new ArrayList<>(info.getMaxZoom());
     59        for (int zoom = info.getMinZoom(); zoom <= info.getMaxZoom(); zoom++) {
     60            double scale = OsmMercator.EARTH_RADIUS * Math.PI * 2 / Math.pow(2, zoom) / OsmMercator.DEFAUL_TILE_SIZE;
     61            scales.add(scale);
     62        }
     63        this.nativeScaleList = new ScaleList(scales);
    5364    }
    5465
     
    149160    @Override
    150161    public ScaleList getNativeScales() {
    151         ScaleList scales = new ScaleList();
    152         for (int zoom = info.getMinZoom(); zoom <= info.getMaxZoom(); zoom++) {
    153             double scale = OsmMercator.EARTH_RADIUS * Math.PI * 2 / Math.pow(2, zoom) / OsmMercator.DEFAUL_TILE_SIZE;
    154             scales.add(new Scale(scale));
    155         }
    156         return scales;
     162        return nativeScaleList;
    157163    }
    158 }
     164 }
  • trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java

    r9818 r9825  
    6262    protected int getBestZoom() {
    6363        if (!Main.isDisplayingMapView()) return 0;
    64         ScaleList scaleList = getNativeScales();
    65         for (int i = scaleList.size()-1; i >= 0; i--) {
    66             Scale scale = scaleList.get(i);
    67             if (scale.scale >= Main.map.mapView.getScale()) {
    68                 return i;
    69             }
    70         }
    71         return 0;
    72     }
    73 
    74     @Override
    75     protected int getMaxZoomLvl() {
    76         return getNativeScales().size()-1;
     64        return Math.max(
     65                getMinZoomLvl(),
     66                Math.min(
     67                        getNativeScales().getSnapScale(Main.map.mapView.getScale(), false).getIndex(),
     68                        getMaxZoomLvl()
     69                        )
     70                );
    7771    }
    7872
Note: See TracChangeset for help on using the changeset viewer.