Changeset 14427 in josm for trunk/src/org


Ignore:
Timestamp:
2018-11-16T21:51:56+01:00 (6 years ago)
Author:
wiktorn
Message:

Fix canceling precaching imageries along GPX track

  • when GPX is big it was possibile to cancel task before precacheTask was set in

PrecacheWmsTask.realRun(). This caused NPE and prevented cleaning download
queue

  • PrecacheTask.tileLoadingFinished updated progress monitor even if task was

already cancelled and this led to invalid state exception in ProgressMonitor,
as there are many download tasks run in parallel and we do not cancel them if
they already started. Now, after download is cancelled, no updates are issued

Closes: #16966

Location:
trunk/src/org/openstreetmap/josm/gui/layer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java

    r14360 r14427  
    17701770    public class PrecacheTask implements TileLoaderListener {
    17711771        private final ProgressMonitor progressMonitor;
    1772         private int totalCount;
     1772        private final int totalCount;
    17731773        private final AtomicInteger processedCount = new AtomicInteger(0);
    17741774        private final TileLoader tileLoader;
     1775        private final Set<Tile> requestedTiles;
    17751776
    17761777        /**
    17771778         * @param progressMonitor that will be notified about progess of the task
     1779         * @param bufferY
     1780         * @param bufferX
     1781         * @param points
    17781782         */
    1779         public PrecacheTask(ProgressMonitor progressMonitor) {
     1783        public PrecacheTask(ProgressMonitor progressMonitor, List<LatLon> points, double bufferX, double bufferY) {
    17801784            this.progressMonitor = progressMonitor;
    17811785            this.tileLoader = getTileLoaderFactory().makeTileLoader(this, getHeaders(tileSource), minimumTileExpire);
     
    17841788                        TMSCachedTileLoader.getNewThreadPoolExecutor("Precache downloader"));
    17851789            }
     1790            requestedTiles = new ConcurrentSkipListSet<>(
     1791                    (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getKey(), o2.getKey()));
     1792            for (LatLon point: points) {
     1793                TileXY minTile = tileSource.latLonToTileXY(point.lat() - bufferY, point.lon() - bufferX, currentZoomLevel);
     1794                TileXY curTile = tileSource.latLonToTileXY(CoordinateConversion.llToCoor(point), currentZoomLevel);
     1795                TileXY maxTile = tileSource.latLonToTileXY(point.lat() + bufferY, point.lon() + bufferX, currentZoomLevel);
     1796
     1797                // take at least one tile of buffer
     1798                int minY = Math.min(curTile.getYIndex() - 1, minTile.getYIndex());
     1799                int maxY = Math.max(curTile.getYIndex() + 1, maxTile.getYIndex());
     1800                int minX = Math.min(curTile.getXIndex() - 1, minTile.getXIndex());
     1801                int maxX = Math.max(curTile.getXIndex() + 1, maxTile.getXIndex());
     1802
     1803                for (int x = minX; x <= maxX; x++) {
     1804                    for (int y = minY; y <= maxY; y++) {
     1805                        requestedTiles.add(new Tile(tileSource, x, y, currentZoomLevel));
     1806                    }
     1807                }
     1808            }
     1809
     1810            this.totalCount = requestedTiles.size();
     1811            this.progressMonitor.setTicksCount(requestedTiles.size());
     1812
    17861813        }
    17871814
     
    18131840            int processed = this.processedCount.incrementAndGet();
    18141841            if (success) {
    1815                 this.progressMonitor.worked(1);
    1816                 this.progressMonitor.setCustomText(tr("Downloaded {0}/{1} tiles", processed, totalCount));
     1842                synchronized (progressMonitor) {
     1843                    if (!this.progressMonitor.isCanceled()) {
     1844                        this.progressMonitor.worked(1);
     1845                        this.progressMonitor.setCustomText(tr("Downloaded {0}/{1} tiles", processed, totalCount));
     1846                    }
     1847                }
    18171848            } else {
    18181849                Logging.warn("Tile loading failure: " + tile + " - " + tile.getErrorMessage());
     
    18251856        public TileLoader getTileLoader() {
    18261857            return tileLoader;
     1858        }
     1859
     1860        /**
     1861         * Execute the download
     1862         */
     1863        public void run() {
     1864            TileLoader loader = getTileLoader();
     1865            for (Tile t: requestedTiles) {
     1866                if (!progressMonitor.isCanceled()) {
     1867                    loader.createTileLoaderJob(t).submit();
     1868                }
     1869            }
     1870
    18271871        }
    18281872    }
     
    18401884     * @return precache task representing download task
    18411885     */
    1842     public AbstractTileSourceLayer<T>.PrecacheTask downloadAreaToCache(final ProgressMonitor progressMonitor, List<LatLon> points,
     1886    public AbstractTileSourceLayer<T>.PrecacheTask getDownloadAreaToCacheTask(final ProgressMonitor progressMonitor, List<LatLon> points,
    18431887            double bufferX, double bufferY) {
    1844         PrecacheTask precacheTask = new PrecacheTask(progressMonitor);
    1845         final Set<Tile> requestedTiles = new ConcurrentSkipListSet<>(
    1846                 (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getKey(), o2.getKey()));
    1847         for (LatLon point: points) {
    1848             TileXY minTile = tileSource.latLonToTileXY(point.lat() - bufferY, point.lon() - bufferX, currentZoomLevel);
    1849             TileXY curTile = tileSource.latLonToTileXY(CoordinateConversion.llToCoor(point), currentZoomLevel);
    1850             TileXY maxTile = tileSource.latLonToTileXY(point.lat() + bufferY, point.lon() + bufferX, currentZoomLevel);
    1851 
    1852             // take at least one tile of buffer
    1853             int minY = Math.min(curTile.getYIndex() - 1, minTile.getYIndex());
    1854             int maxY = Math.max(curTile.getYIndex() + 1, maxTile.getYIndex());
    1855             int minX = Math.min(curTile.getXIndex() - 1, minTile.getXIndex());
    1856             int maxX = Math.max(curTile.getXIndex() + 1, maxTile.getXIndex());
    1857 
    1858             for (int x = minX; x <= maxX; x++) {
    1859                 for (int y = minY; y <= maxY; y++) {
    1860                     requestedTiles.add(new Tile(tileSource, x, y, currentZoomLevel));
    1861                 }
    1862             }
    1863         }
    1864 
    1865         precacheTask.totalCount = requestedTiles.size();
    1866         precacheTask.progressMonitor.setTicksCount(requestedTiles.size());
    1867 
    1868         TileLoader loader = precacheTask.getTileLoader();
    1869         for (Tile t: requestedTiles) {
    1870             loader.createTileLoaderJob(t).submit();
    1871         }
     1888        PrecacheTask precacheTask = new PrecacheTask(progressMonitor, points, bufferX, bufferY);
     1889
    18721890        return precacheTask;
    18731891    }
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackAction.java

    r14336 r14427  
    6060        @Override
    6161        protected void realRun() throws SAXException, IOException, OsmTransferException {
    62             precacheTask = layer.downloadAreaToCache(progressMonitor, points, 0, 0);
     62            precacheTask = layer.getDownloadAreaToCacheTask(progressMonitor, points, 0, 0);
     63            precacheTask.run();
    6364            synchronized (this) {
    6465                try {
Note: See TracChangeset for help on using the changeset viewer.