tiledrasterproducer.java

来自「world wind java sdk 源码」· Java 代码 · 共 965 行 · 第 1/3 页

JAVA
965
字号
        Angle t0 = tile.getSector().getMinLongitude();
        Angle t2 = tile.getSector().getMaxLongitude();
        Angle t1 = Angle.midAngle(t0, t2);

        int row = tile.getRow();
        int col = tile.getColumn();

        Tile[] subTiles = new Tile[4];
        subTiles[0] = new Tile(new Sector(p0, p1, t0, t1), nextLevel, 2 * row, 2 * col);
        subTiles[1] = new Tile(new Sector(p0, p1, t1, t2), nextLevel, 2 * row, 2 * col + 1);
        subTiles[2] = new Tile(new Sector(p1, p2, t1, t2), nextLevel, 2 * row + 1, 2 * col + 1);
        subTiles[3] = new Tile(new Sector(p1, p2, t0, t1), nextLevel, 2 * row + 1, 2 * col);

        return subTiles;
    }

    //**************************************************************//
    //********************  Tile Installation  *********************//
    //**************************************************************//

    protected java.util.concurrent.ExecutorService createDefaultTileWriteService(int threadPoolSize)
    {
        // TODO: comment

        // Create a fixed thread pool, but provide a callback to release a tile write permit when a task completes.
        return new java.util.concurrent.ThreadPoolExecutor(
            // Fixed size thread pool.
            threadPoolSize, threadPoolSize,
            // This value is irrelevant, as threads only terminated when the executor is shutdown.
            0L, java.util.concurrent.TimeUnit.MILLISECONDS,
            // Provide an unbounded work queue.
            new java.util.concurrent.LinkedBlockingQueue<Runnable>())
        {
            protected void afterExecute(Runnable runnable, Throwable t)
            {
                // Invoke the superclass routine, then release a tile write permit.
                super.afterExecute(runnable, t);
                TiledRasterProducer.this.installTileRasterComplete();
            }
        };
    }

    protected void installTileRasterLater(final Tile tile, final DataRaster tileRaster, final AVList params)
    {
        // TODO: comment
        // Try to aquire a permit from the tile write semaphore.
        this.getTileWriteSemaphore().acquireUninterruptibly();
        // We've aquired the permit, now execute the installTileRaster() routine in a different thread.
        this.getTileWriteService().execute(new Runnable()
        {
            public void run()
            {
                try
                {
                    installTileRaster(tile, tileRaster, params);
                    // Dispose the data raster.
                    if (tileRaster instanceof Disposable)
                        ((Disposable) tileRaster).dispose();
                }
                catch (Throwable t)
                {
                    String message = Logging.getMessage("generic.ExceptionWhileWriting", tile);
                    Logging.logger().log(java.util.logging.Level.SEVERE, message, t);
                }
            }
        });
    }

    protected void installTileRasterComplete()
    {
        // TODO: comment
        this.getTileWriteSemaphore().release();
    }

    protected void waitForInstallTileTasks()
    {
        // TODO: comment
        try
        {
            java.util.concurrent.ExecutorService service = this.getTileWriteService();
            service.shutdown();
            // Block this thread until the executor has completed.
            while (!service.awaitTermination(1000L, java.util.concurrent.TimeUnit.MILLISECONDS))
            {}
        }
        catch (InterruptedException e)
        {
            // TODO: proper logging message
            String message = "Exception while shutting down executor";
            Logging.logger().severe(message);
        }
    }

    protected void installTileRaster(Tile tile, DataRaster tileRaster, AVList params) throws java.io.IOException
    {
        java.io.File installLocation;

        // Compute the install location of the tile.
        Object result = this.installLocationForTile(params, tile);
        if (result instanceof java.io.File)
        {
            installLocation = (java.io.File) result;
        }
        else
        {
            String message = result.toString();
            Logging.logger().severe(message);
            throw new java.io.IOException(message);
        }

        synchronized (this.fileLock)
        {
            java.io.File dir = installLocation.getParentFile();
            if (!dir.exists())
            {
                if (!dir.mkdirs())
                {
                    String message = Logging.getMessage("generic.CannotCreateFile", dir);
                    Logging.logger().warning(message);
                }
            }
        }

        // Write the tile data to the filesystem.
        String formatSuffix = params.getStringValue(AVKey.FORMAT_SUFFIX);
        DataRasterWriter[] writers = this.getDataRasterWriters();

        Object writer = this.findWriterFor(tileRaster, formatSuffix, installLocation, writers);
        if (writer instanceof DataRasterWriter)
        {
            try
            {
                ((DataRasterWriter) writer).write(tileRaster, formatSuffix, installLocation);
            }
            catch (java.io.IOException e)
            {
                String message = Logging.getMessage("generic.ExceptionWhileWriting", installLocation);
                Logging.logger().log(java.util.logging.Level.SEVERE, message, e);
            }
        }
    }

    protected Object installLocationForTile(AVList installParams, Tile tile)
    {
        String path = null;

        String s = installParams.getStringValue(AVKey.FILE_STORE_LOCATION);
        if (s != null)
            path = appendPathPart(path, s);

        s = tile.getPath();
        if (s != null)
            path = appendPathPart(path, s);

        if (path == null || path.length() < 1)
            return Logging.getMessage("DataStoreProducer.InvalidTile", tile);

        return new java.io.File(path);
    }

    protected Object findWriterFor(DataRaster raster, String formatSuffix, java.io.File destination,
        DataRasterWriter[] writers)
    {
        for (DataRasterWriter writer : writers)
        {
            if (writer.canWrite(raster, formatSuffix, destination))
                return writer;
        }

        // No writer maching this DataRaster/formatSuffix.
        return Logging.getMessage("DataRaster.CannotWrite", raster, formatSuffix, destination);
    }

    private static String appendPathPart(String firstPart, String secondPart)
    {
        if (secondPart == null || secondPart.length() == 0)
            return firstPart;
        if (firstPart == null || firstPart.length() == 0)
            return secondPart;

        firstPart = WWIO.stripTrailingSeparator(firstPart);
        secondPart = WWIO.stripLeadingSeparator(secondPart);

        return firstPart + System.getProperty("file.separator") + secondPart;
    }

    //**************************************************************//
    //********************  DataDescriptor Installation  ***********//
    //**************************************************************//

    protected void installDataDescriptor(AVList params) throws java.io.IOException
    {
        // Exit if the caller has instructed us to stop production.
        if (this.isStopped())
            return;

        DataDescriptor descriptor = new BasicDataDescriptor();
        DataDescriptorWriter writer = new BasicDataDescriptorWriter();

        Object o = params.getValue(AVKey.FILE_STORE_LOCATION);
        if (o != null)
            descriptor.setFileStoreLocation(new java.io.File(o.toString()));

        o = params.getValue(AVKey.DATA_CACHE_NAME);
        if (o != null)
            descriptor.setFileStorePath(o.toString());

        o = params.getValue(AVKey.DATASET_NAME);
        if (o != null)
            descriptor.setName(o.toString());

        o = params.getValue(AVKey.DATA_TYPE);
        if (o != null)
            descriptor.setType(o.toString());

        for (java.util.Map.Entry<String, Object> avp : params.getEntries())
        {
            String key = avp.getKey();

            // Skip key-value pairs that the DataDescriptor specially manages.
            if (key.equals(AVKey.FILE_STORE_LOCATION)
                || key.equals(AVKey.DATA_CACHE_NAME)
                || key.equals(AVKey.DATASET_NAME)
                || key.equals(AVKey.DATA_TYPE))
            {
                continue;
            }

            descriptor.setValue(key, avp.getValue());
        }

        java.io.File installLocation;
        Object result = this.installLocationForDescriptor(descriptor, writer);
        if (result instanceof java.io.File)
        {
            installLocation = (java.io.File) result;
        }
        else
        {
            String message = result.toString();
            Logging.logger().severe(message);
            throw new java.io.IOException(message);
        }

        synchronized (this.fileLock)
        {
            java.io.File dir = installLocation.getParentFile();
            if (!dir.exists())
            {
                if (!dir.mkdirs())
                {
                    String message = Logging.getMessage("generic.CannotCreateFile", dir);
                    Logging.logger().warning(message);
                }
            }
        }

        writer.setDestination(installLocation);
        writer.write(descriptor);

        this.getProductionResultsList().add(descriptor);
    }

    protected Object installLocationForDescriptor(DataDescriptor descriptor, DataDescriptorWriter writer)
    {
        String path = null;

        java.io.File file = descriptor.getFileStoreLocation();
        if (file != null)
            path = appendPathPart(path, file.getPath());

        String s = descriptor.getFileStorePath();
        if (s != null)
            path = appendPathPart(path, s);

        s = "dataDescriptor" + WWIO.makeSuffixForMimeType(writer.getMimeType());
        path = appendPathPart(path, s);

        if (path == null || path.length() < 1)
            return Logging.getMessage("DataStoreProducer.InvalidDataStoreParamters", descriptor);

        return new java.io.File(path);
    }

    //**************************************************************//
    //********************  Progress  ******************************//
    //**************************************************************//

    protected void setProgressParams(LevelSet levelSet)
    {
        Sector sector = levelSet.getSector();

        this.tileCount = 0;
        for (Level level : levelSet.getLevels())
        {
            Angle dLat = level.getTileDelta().getLatitude();
            Angle dLon = level.getTileDelta().getLongitude();
            Angle latOrigin = levelSet.getTileOrigin().getLatitude();
            Angle lonOrigin = levelSet.getTileOrigin().getLongitude();
            int firstRow = Tile.computeRow(dLat, sector.getMinLatitude(), latOrigin);
            int firstCol = Tile.computeColumn(dLon, sector.getMinLongitude(), lonOrigin);
            int lastRow  = Tile.computeRow(dLat, sector.getMaxLatitude(), latOrigin);
            int lastCol  = Tile.computeColumn(dLon, sector.getMaxLongitude(), lonOrigin);
            this.tileCount += (lastRow - firstRow + 1) * (lastCol - firstCol + 1);
        }
    }

    protected void startProgress()
    {
        this.tile = 0;
        this.firePropertyChange(AVKey.PROGRESS, null, 0d);
    }

    protected void updateProgress()
    {
        double oldProgress =   this.tile / (double) this.tileCount;
        double newProgress = ++this.tile / (double) this.tileCount;
        this.firePropertyChange(AVKey.PROGRESS, oldProgress, newProgress);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?