Search in sources :

Example 1 with TileUrlBuilder

use of gov.nasa.worldwind.util.TileUrlBuilder in project hortonmachine by TheHortonMachine.

the class MapsforgeNwwLayer method makeLevels.

private static LevelSet makeLevels(String layerName, OsmTilegenerator osmTilegenerator, Integer tileSize) throws MalformedURLException {
    AVList params = new AVListImpl();
    String cacheRelativePath = "mapsforge/" + layerName + "-tiles";
    if (tileSize == null || tileSize < 256) {
        tileSize = TILESIZE;
    }
    int finalTileSize = tileSize;
    params.setValue(AVKey.URL, cacheRelativePath);
    params.setValue(AVKey.TILE_WIDTH, finalTileSize);
    params.setValue(AVKey.TILE_HEIGHT, finalTileSize);
    params.setValue(AVKey.DATA_CACHE_NAME, cacheRelativePath);
    params.setValue(AVKey.SERVICE, "*");
    params.setValue(AVKey.DATASET_NAME, "*");
    params.setValue(AVKey.FORMAT_SUFFIX, ".png");
    params.setValue(AVKey.NUM_LEVELS, 22);
    params.setValue(AVKey.NUM_EMPTY_LEVELS, 0);
    params.setValue(AVKey.LEVEL_ZERO_TILE_DELTA, new LatLon(Angle.fromDegrees(22.5d), Angle.fromDegrees(45d)));
    params.setValue(AVKey.SECTOR, new MercatorSector(-1.0, 1.0, Angle.NEG180, Angle.POS180));
    File cacheRoot = CacheUtils.getCacheRoot();
    final File cacheFolder = new File(cacheRoot, cacheRelativePath);
    if (!cacheFolder.exists()) {
        cacheFolder.mkdirs();
    }
    params.setValue(AVKey.TILE_URL_BUILDER, new TileUrlBuilder() {

        public URL getURL(Tile tile, String altImageFormat) throws MalformedURLException {
            int zoom = tile.getLevelNumber() + 3;
            Sector sector = tile.getSector();
            double north = sector.getMaxLatitude().degrees;
            double south = sector.getMinLatitude().degrees;
            double east = sector.getMaxLongitude().degrees;
            double west = sector.getMinLongitude().degrees;
            double centerX = west + (east - west) / 2.0;
            double centerY = south + (north - south) / 2.0;
            int[] tileNumber = NwwUtilities.getTileNumber(centerY, centerX, zoom);
            int x = tileNumber[0];
            int y = tileNumber[1];
            File tileImageFolderFile = new File(cacheFolder, zoom + File.separator + x);
            if (!tileImageFolderFile.exists()) {
                tileImageFolderFile.mkdirs();
            }
            File imgFile = new File(tileImageFolderFile, y + ".png");
            try {
                if (!imgFile.exists()) {
                    BufferedImage bImg = osmTilegenerator.getImage(zoom, x, y);
                    // PrintUtilities.printRenderedImageData(bImg);
                    ImageIO.write(bImg, "png", imgFile);
                }
                return imgFile.toURI().toURL();
            } catch (IOException e) {
                return null;
            }
        }
    });
    return new LevelSet(params);
}
Also used : MalformedURLException(java.net.MalformedURLException) Sector(gov.nasa.worldwind.geom.Sector) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) Tile(gov.nasa.worldwind.util.Tile) IOException(java.io.IOException) URL(java.net.URL) BufferedImage(java.awt.image.BufferedImage) LatLon(gov.nasa.worldwind.geom.LatLon) LevelSet(gov.nasa.worldwind.util.LevelSet) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) AVList(gov.nasa.worldwind.avlist.AVList) MapFile(org.mapsforge.map.reader.MapFile) File(java.io.File) TileUrlBuilder(gov.nasa.worldwind.util.TileUrlBuilder) AVListImpl(gov.nasa.worldwind.avlist.AVListImpl)

Example 2 with TileUrlBuilder

use of gov.nasa.worldwind.util.TileUrlBuilder in project hortonmachine by TheHortonMachine.

the class RasterizedSpatialiteLasLayer method makeLevels.

private static LevelSet makeLevels(String title, Integer tileSize, boolean transparentBackground, ASpatialDb db, boolean doIntensity) throws Exception {
    String plus = doIntensity ? INTENSITY : ELEVATION;
    String cacheRelativePath = "rasterized_spatialites/" + title + "_" + plus + "-tiles";
    File cacheRoot = CacheUtils.getCacheRoot();
    final File cacheFolder = new File(cacheRoot, cacheRelativePath);
    if (!cacheFolder.exists()) {
        cacheFolder.mkdirs();
    }
    CacheUtils.clearCacheBySourceName(cacheRelativePath);
    AVList params = new AVListImpl();
    if (tileSize == null || tileSize < 256) {
        tileSize = TILESIZE;
    }
    int finalTileSize = tileSize;
    GeometryColumn spatialiteGeometryColumns = db.getGeometryColumnsForTable(LasCellsTable.TABLENAME);
    CoordinateReferenceSystem dataCrs = CRS.decode("EPSG:" + spatialiteGeometryColumns.srid);
    CoordinateReferenceSystem nwwCRS = DefaultGeographicCRS.WGS84;
    List<LasSource> lasSources = LasSourcesTable.getLasSources(db);
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    List<Polygon> polList = new ArrayList<>();
    ColorInterpolator colorInterp;
    int lasLevels = 0;
    if (!doIntensity) {
        for (LasSource lasSource : lasSources) {
            lasLevels = lasSource.levels;
            polList.add(lasSource.polygon);
            min = Math.min(min, lasSource.minElev);
            max = Math.max(max, lasSource.maxElev);
        }
        colorInterp = new ColorInterpolator(EColorTables.elev.name(), min, max, null);
    } else {
        for (LasSource lasSource : lasSources) {
            lasLevels = lasSource.levels;
            polList.add(lasSource.polygon);
            min = Math.min(min, lasSource.minIntens);
            max = Math.max(max, lasSource.maxIntens);
        }
        colorInterp = new ColorInterpolator(EColorTables.rainbow.name(), 0, 255, null);
    }
    final int _lasLevels = lasLevels;
    Geometry sourcesUnionData = CascadedPolygonUnion.union(polList);
    PreparedGeometry preparedCoverage = PreparedGeometryFactory.prepare(sourcesUnionData);
    MathTransform nww2DataTransform = CRS.findMathTransform(nwwCRS, dataCrs);
    MathTransform data2NwwTransform = CRS.findMathTransform(dataCrs, nwwCRS);
    // String urlString = folderFile.toURI().toURL().toExternalForm();
    // params.setValue(AVKey.URL, urlString);
    params.setValue(AVKey.TILE_WIDTH, finalTileSize);
    params.setValue(AVKey.TILE_HEIGHT, finalTileSize);
    params.setValue(AVKey.DATA_CACHE_NAME, cacheRelativePath);
    params.setValue(AVKey.SERVICE, "*");
    params.setValue(AVKey.DATASET_NAME, "*");
    params.setValue(AVKey.FORMAT_SUFFIX, ".png");
    params.setValue(AVKey.NUM_LEVELS, 22);
    params.setValue(AVKey.NUM_EMPTY_LEVELS, 8);
    params.setValue(AVKey.LEVEL_ZERO_TILE_DELTA, new LatLon(Angle.fromDegrees(22.5d), Angle.fromDegrees(45d)));
    params.setValue(AVKey.SECTOR, new MercatorSector(-1.0, 1.0, Angle.NEG180, Angle.POS180));
    params.setValue(AVKey.TILE_URL_BUILDER, new TileUrlBuilder() {

        public URL getURL(Tile tile, String altImageFormat) throws MalformedURLException {
            try {
                int zoom = tile.getLevelNumber() + 3;
                Sector sector = tile.getSector();
                double north = sector.getMaxLatitude().degrees;
                double south = sector.getMinLatitude().degrees;
                double east = sector.getMaxLongitude().degrees;
                double west = sector.getMinLongitude().degrees;
                double centerX = west + (east - west) / 2.0;
                double centerY = south + (north - south) / 2.0;
                int[] tileNumber = NwwUtilities.getTileNumber(centerY, centerX, zoom);
                int x = tileNumber[0];
                int y = tileNumber[1];
                Rectangle imageBounds = new Rectangle(0, 0, finalTileSize, finalTileSize);
                ReferencedEnvelope tileEnvNww = new ReferencedEnvelope(west, east, south, north, DefaultGeographicCRS.WGS84);
                AffineTransform worldToPixel = TransformationUtils.getWorldToPixel(tileEnvNww, imageBounds);
                PointTransformation pointTransformation = new PointTransformation() {

                    @Override
                    public void transform(Coordinate src, Point2D dest) {
                        worldToPixel.transform(new Point2D.Double(src.x, src.y), dest);
                    }
                };
                Polygon polygonNww = GeometryUtilities.createPolygonFromEnvelope(tileEnvNww);
                Geometry polygonData = JTS.transform(polygonNww, nww2DataTransform);
                int imgType;
                Color backgroundColor;
                boolean intersects = preparedCoverage.intersects(polygonData);
                if (transparentBackground || !intersects) {
                    imgType = BufferedImage.TYPE_INT_ARGB;
                    backgroundColor = new Color(Color.WHITE.getRed(), Color.WHITE.getGreen(), Color.WHITE.getBlue(), 0);
                } else {
                    imgType = BufferedImage.TYPE_INT_RGB;
                    backgroundColor = Color.WHITE;
                }
                BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, imgType);
                Graphics2D gr = image.createGraphics();
                gr.setPaint(backgroundColor);
                gr.fill(imageBounds);
                if (zoom < 12) {
                    Geometry sourcesUnionNww = JTS.transform(sourcesUnionData, data2NwwTransform);
                    drawSources(db, pointTransformation, sourcesUnionNww, gr, finalTileSize);
                } else if (zoom < 14) {
                    drawLevels(db, colorInterp, pointTransformation, polygonData, gr, _lasLevels, data2NwwTransform, doIntensity, finalTileSize);
                } else if (zoom < 15 && _lasLevels - 1 > 0) {
                    drawLevels(db, colorInterp, pointTransformation, polygonData, gr, _lasLevels - 1, data2NwwTransform, doIntensity, finalTileSize);
                } else if (zoom < 17 && _lasLevels - 2 > 0) {
                    drawLevels(db, colorInterp, pointTransformation, polygonData, gr, _lasLevels - 2, data2NwwTransform, doIntensity, finalTileSize);
                } else if (zoom > 18) {
                    drawPoints(db, colorInterp, pointTransformation, polygonData, gr, data2NwwTransform, doIntensity, finalTileSize);
                } else {
                    drawCells(db, colorInterp, pointTransformation, polygonData, gr, data2NwwTransform, doIntensity, finalTileSize);
                }
                File tileImageFolderFile = new File(cacheFolder, zoom + File.separator + x);
                if (!tileImageFolderFile.exists()) {
                    tileImageFolderFile.mkdirs();
                }
                File imgFile = new File(tileImageFolderFile, y + ".png");
                if (!imgFile.exists()) {
                    ImageIO.write(image, "png", imgFile);
                }
                return imgFile.toURI().toURL();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    });
    return new LevelSet(params);
}
Also used : MalformedURLException(java.net.MalformedURLException) MathTransform(org.opengis.referencing.operation.MathTransform) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) URL(java.net.URL) BufferedImage(java.awt.image.BufferedImage) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) LevelSet(gov.nasa.worldwind.util.LevelSet) Point2D(java.awt.geom.Point2D) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) GeometryColumn(org.hortonmachine.dbs.compat.GeometryColumn) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(org.locationtech.jts.geom.Polygon) TileUrlBuilder(gov.nasa.worldwind.util.TileUrlBuilder) AVListImpl(gov.nasa.worldwind.avlist.AVListImpl) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) Sector(gov.nasa.worldwind.geom.Sector) Color(java.awt.Color) PointTransformation(org.locationtech.jts.awt.PointTransformation) Tile(gov.nasa.worldwind.util.Tile) ColorInterpolator(org.hortonmachine.gears.utils.colors.ColorInterpolator) MalformedURLException(java.net.MalformedURLException) Graphics2D(java.awt.Graphics2D) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) LatLon(gov.nasa.worldwind.geom.LatLon) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Coordinate(org.locationtech.jts.geom.Coordinate) AVList(gov.nasa.worldwind.avlist.AVList) AffineTransform(java.awt.geom.AffineTransform) File(java.io.File) LasSource(org.hortonmachine.gears.io.las.databases.LasSource)

Example 3 with TileUrlBuilder

use of gov.nasa.worldwind.util.TileUrlBuilder in project hortonmachine by TheHortonMachine.

the class RasterizedFeatureCollectionLayer method makeLevels.

private static LevelSet makeLevels(String title, GTRenderer renderer, Integer tileSize, boolean transparentBackground) throws MalformedURLException {
    AVList params = new AVListImpl();
    if (tileSize == null || tileSize < 256) {
        tileSize = TILESIZE;
    }
    int finalTileSize = tileSize;
    String cacheRelativePath = "rasterized_featurecollections/" + title + "-tiles";
    // String urlString = folderFile.toURI().toURL().toExternalForm();
    // params.setValue(AVKey.URL, urlString);
    params.setValue(AVKey.TILE_WIDTH, finalTileSize);
    params.setValue(AVKey.TILE_HEIGHT, finalTileSize);
    params.setValue(AVKey.DATA_CACHE_NAME, cacheRelativePath);
    params.setValue(AVKey.SERVICE, "*");
    params.setValue(AVKey.DATASET_NAME, "*");
    params.setValue(AVKey.FORMAT_SUFFIX, ".png");
    params.setValue(AVKey.NUM_LEVELS, 22);
    params.setValue(AVKey.NUM_EMPTY_LEVELS, 0);
    params.setValue(AVKey.LEVEL_ZERO_TILE_DELTA, new LatLon(Angle.fromDegrees(22.5d), Angle.fromDegrees(45d)));
    params.setValue(AVKey.SECTOR, new MercatorSector(-1.0, 1.0, Angle.NEG180, Angle.POS180));
    File cacheRoot = CacheUtils.getCacheRoot();
    final File cacheFolder = new File(cacheRoot, cacheRelativePath);
    if (!cacheFolder.exists()) {
        cacheFolder.mkdirs();
    }
    params.setValue(AVKey.TILE_URL_BUILDER, new TileUrlBuilder() {

        public URL getURL(Tile tile, String altImageFormat) throws MalformedURLException {
            int zoom = tile.getLevelNumber() + 3;
            Sector sector = tile.getSector();
            double north = sector.getMaxLatitude().degrees;
            double south = sector.getMinLatitude().degrees;
            double east = sector.getMaxLongitude().degrees;
            double west = sector.getMinLongitude().degrees;
            double centerX = west + (east - west) / 2.0;
            double centerY = south + (north - south) / 2.0;
            int[] tileNumber = NwwUtilities.getTileNumber(centerY, centerX, zoom);
            int x = tileNumber[0];
            int y = tileNumber[1];
            Rectangle imageBounds = new Rectangle(0, 0, finalTileSize, finalTileSize);
            int imgType;
            Color backgroundColor;
            if (transparentBackground) {
                imgType = BufferedImage.TYPE_INT_ARGB;
                backgroundColor = new Color(Color.WHITE.getRed(), Color.WHITE.getGreen(), Color.WHITE.getBlue(), 0);
            } else {
                imgType = BufferedImage.TYPE_INT_RGB;
                backgroundColor = Color.WHITE;
            }
            BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, imgType);
            Graphics2D gr = image.createGraphics();
            gr.setPaint(backgroundColor);
            gr.fill(imageBounds);
            gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            try {
                synchronized (renderer) {
                    renderer.paint(gr, imageBounds, new ReferencedEnvelope(west, east, south, north, DefaultGeographicCRS.WGS84));
                    File tileImageFolderFile = new File(cacheFolder, zoom + File.separator + x);
                    if (!tileImageFolderFile.exists()) {
                        tileImageFolderFile.mkdirs();
                    }
                    File imgFile = new File(tileImageFolderFile, y + ".png");
                    if (!imgFile.exists()) {
                        ImageIO.write(image, "png", imgFile);
                    }
                    return imgFile.toURI().toURL();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    });
    return new LevelSet(params);
}
Also used : MalformedURLException(java.net.MalformedURLException) Sector(gov.nasa.worldwind.geom.Sector) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Tile(gov.nasa.worldwind.util.Tile) IOException(java.io.IOException) URL(java.net.URL) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) LatLon(gov.nasa.worldwind.geom.LatLon) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) LevelSet(gov.nasa.worldwind.util.LevelSet) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) AVList(gov.nasa.worldwind.avlist.AVList) File(java.io.File) TileUrlBuilder(gov.nasa.worldwind.util.TileUrlBuilder) AVListImpl(gov.nasa.worldwind.avlist.AVListImpl)

Example 4 with TileUrlBuilder

use of gov.nasa.worldwind.util.TileUrlBuilder in project hortonmachine by TheHortonMachine.

the class RasterizedShapefilesFolderNwwLayer method makeLevels.

private static LevelSet makeLevels(String title, File folderFile, GTRenderer renderer, Integer tileSize, boolean transparentBackground) throws MalformedURLException {
    AVList params = new AVListImpl();
    if (tileSize == null || tileSize < 256) {
        tileSize = TILESIZE;
    }
    int finalTileSize = tileSize;
    String tileFolderName = title == null ? folderFile.getName() : title;
    String cacheRelativePath = "shapefilefolders/" + tileFolderName + "-tiles";
    // String urlString = folderFile.toURI().toURL().toExternalForm();
    // params.setValue(AVKey.URL, urlString);
    params.setValue(AVKey.TILE_WIDTH, finalTileSize);
    params.setValue(AVKey.TILE_HEIGHT, finalTileSize);
    params.setValue(AVKey.DATA_CACHE_NAME, cacheRelativePath);
    params.setValue(AVKey.SERVICE, "*");
    params.setValue(AVKey.DATASET_NAME, "*");
    params.setValue(AVKey.FORMAT_SUFFIX, ".png");
    params.setValue(AVKey.NUM_LEVELS, 22);
    params.setValue(AVKey.NUM_EMPTY_LEVELS, 0);
    params.setValue(AVKey.LEVEL_ZERO_TILE_DELTA, new LatLon(Angle.fromDegrees(22.5d), Angle.fromDegrees(45d)));
    params.setValue(AVKey.SECTOR, new MercatorSector(-1.0, 1.0, Angle.NEG180, Angle.POS180));
    File cacheRoot = CacheUtils.getCacheRoot();
    final File cacheFolder = new File(cacheRoot, cacheRelativePath);
    if (!cacheFolder.exists()) {
        cacheFolder.mkdirs();
    }
    params.setValue(AVKey.TILE_URL_BUILDER, new TileUrlBuilder() {

        public URL getURL(Tile tile, String altImageFormat) throws MalformedURLException {
            int zoom = tile.getLevelNumber() + 3;
            Sector sector = tile.getSector();
            double north = sector.getMaxLatitude().degrees;
            double south = sector.getMinLatitude().degrees;
            double east = sector.getMaxLongitude().degrees;
            double west = sector.getMinLongitude().degrees;
            double centerX = west + (east - west) / 2.0;
            double centerY = south + (north - south) / 2.0;
            int[] tileNumber = NwwUtilities.getTileNumber(centerY, centerX, zoom);
            int x = tileNumber[0];
            int y = tileNumber[1];
            Rectangle imageBounds = new Rectangle(0, 0, finalTileSize, finalTileSize);
            int imgType;
            Color backgroundColor;
            if (transparentBackground) {
                imgType = BufferedImage.TYPE_INT_ARGB;
                backgroundColor = new Color(Color.WHITE.getRed(), Color.WHITE.getGreen(), Color.WHITE.getBlue(), 0);
            } else {
                imgType = BufferedImage.TYPE_INT_RGB;
                backgroundColor = Color.WHITE;
            }
            BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, imgType);
            Graphics2D gr = image.createGraphics();
            gr.setPaint(backgroundColor);
            gr.fill(imageBounds);
            gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            try {
                synchronized (renderer) {
                    renderer.paint(gr, imageBounds, new ReferencedEnvelope(west, east, south, north, DefaultGeographicCRS.WGS84));
                    File tileImageFolderFile = new File(cacheFolder, zoom + File.separator + x);
                    if (!tileImageFolderFile.exists()) {
                        tileImageFolderFile.mkdirs();
                    }
                    File imgFile = new File(tileImageFolderFile, y + ".png");
                    if (!imgFile.exists()) {
                        ImageIO.write(image, "png", imgFile);
                    }
                    return imgFile.toURI().toURL();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    });
    return new LevelSet(params);
}
Also used : MalformedURLException(java.net.MalformedURLException) Sector(gov.nasa.worldwind.geom.Sector) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Tile(gov.nasa.worldwind.util.Tile) IOException(java.io.IOException) URL(java.net.URL) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) LatLon(gov.nasa.worldwind.geom.LatLon) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) LevelSet(gov.nasa.worldwind.util.LevelSet) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) AVList(gov.nasa.worldwind.avlist.AVList) File(java.io.File) TileUrlBuilder(gov.nasa.worldwind.util.TileUrlBuilder) AVListImpl(gov.nasa.worldwind.avlist.AVListImpl)

Example 5 with TileUrlBuilder

use of gov.nasa.worldwind.util.TileUrlBuilder in project hortonmachine by TheHortonMachine.

the class GeopackageTilesNwwLayer method makeLevels.

private static LevelSet makeLevels(File gpkgFile, GeopackageCommonDb gpkgDb, String tableName) throws Exception {
    AVList params = new AVListImpl();
    SqlName tName = SqlName.m(tableName);
    String cacheRelativePath = "gpkg/" + gpkgFile.getName() + "-" + tName.name + "-tiles";
    TileEntry tile = gpkgDb.tile(tName);
    int tileSize = tile.getTileMatricies().get(0).getTileWidth();
    String urlString = gpkgFile.toURI().toURL().toExternalForm();
    params.setValue(AVKey.URL, urlString);
    params.setValue(AVKey.TILE_WIDTH, tileSize);
    params.setValue(AVKey.TILE_HEIGHT, tileSize);
    params.setValue(AVKey.DATA_CACHE_NAME, cacheRelativePath);
    params.setValue(AVKey.SERVICE, "*");
    params.setValue(AVKey.DATASET_NAME, "*");
    final String _imageFormat = "png";
    params.setValue(AVKey.FORMAT_SUFFIX, "." + _imageFormat);
    params.setValue(AVKey.NUM_LEVELS, 22);
    params.setValue(AVKey.NUM_EMPTY_LEVELS, 0);
    params.setValue(AVKey.LEVEL_ZERO_TILE_DELTA, new LatLon(Angle.fromDegrees(22.5d), Angle.fromDegrees(45d)));
    params.setValue(AVKey.SECTOR, new MercatorSector(-1.0, 1.0, Angle.NEG180, Angle.POS180));
    File cacheRoot = CacheUtils.getCacheRoot();
    final File cacheFolder = new File(cacheRoot, cacheRelativePath);
    if (!cacheFolder.exists()) {
        cacheFolder.mkdirs();
    }
    List<Integer> tileZoomLevelsWithData = gpkgDb.getTileZoomLevelsWithData(tName);
    params.setValue(AVKey.TILE_URL_BUILDER, new TileUrlBuilder() {

        public URL getURL(Tile tile, String altImageFormat) throws MalformedURLException {
            int zoom = tile.getLevelNumber() + 3;
            // int x = tile.getColumn();
            // int y = tile.getRow();
            Sector sector = tile.getSector();
            double north = sector.getMaxLatitude().degrees;
            double south = sector.getMinLatitude().degrees;
            double east = sector.getMaxLongitude().degrees;
            double west = sector.getMinLongitude().degrees;
            double centerX = west + (east - west) / 2.0;
            double centerY = south + (north - south) / 2.0;
            int[] zxy = MercatorUtils.getTileNumber(centerY, centerX, zoom);
            int x = zxy[1];
            int y = zxy[2];
            try {
                StringBuilder sb = new StringBuilder();
                sb.append(zoom);
                sb.append(File.separator);
                sb.append(x);
                File tileImageFolderFile = new File(cacheFolder, sb.toString());
                if (!tileImageFolderFile.exists()) {
                    tileImageFolderFile.mkdirs();
                }
                sb = new StringBuilder();
                sb.append(y);
                sb.append(".");
                sb.append(_imageFormat);
                File imgFile = new File(tileImageFolderFile, sb.toString());
                if (!imgFile.exists()) {
                    BufferedImage bImg = null;
                    byte[] bytes = gpkgDb.getTile(tName, x, y, zoom);
                    if (bytes != null) {
                        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                        bImg = ImageIO.read(bais);
                    } else {
                        // try downscaling from higher resolutions
                        // look in the other levels
                        int from = zoom + 1;
                        int to = Math.min(zoom + 5, 19);
                        for (int higherZoom = from; higherZoom < to; higherZoom++) {
                            if (!tileZoomLevelsWithData.contains(higherZoom)) {
                                continue;
                            }
                            BufferedImage img = TileUtilities.getTileFromDifferentZoomlevel(gpkgDb, tName.name, x, y, zoom, tileSize, higherZoom);
                            if (img != null) {
                                bImg = img;
                                break;
                            }
                        }
                        if (bImg == null) {
                            // then try upscaling from lower resolution
                            from = zoom - 1;
                            int zoomLevelTotry = 5;
                            to = Math.max(zoom - zoomLevelTotry, 1);
                            for (int lowerZoom = from; lowerZoom > to; lowerZoom--) {
                                if (!tileZoomLevelsWithData.contains(lowerZoom)) {
                                    continue;
                                }
                                BufferedImage img = TileUtilities.getTileFromDifferentZoomlevel(gpkgDb, tName.name, x, y, zoom, tileSize, lowerZoom);
                                if (img != null) {
                                    bImg = img;
                                    break;
                                }
                            }
                        }
                    }
                    if (DEBUG) {
                        if (bImg == null && DEBUG_ALSO_WITHOUT_IMAGE) {
                            bImg = new BufferedImage(tileSize, tileSize, BufferedImage.TYPE_INT_ARGB);
                        }
                        if (bImg != null) {
                            Graphics2D g2d = (Graphics2D) bImg.getGraphics();
                            g2d.setColor(Color.black);
                            g2d.drawRect(0, 0, TILESIZE, TILESIZE);
                            g2d.drawString(zoom + "/" + x + "/" + y, 3, TILESIZE / 2);
                            g2d.dispose();
                        }
                    }
                    if (bImg != null) {
                        ImageIO.write(bImg, _imageFormat, imgFile);
                    } else {
                        return null;
                    }
                }
                return imgFile.toURI().toURL();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    });
    return new LevelSet(params);
}
Also used : MalformedURLException(java.net.MalformedURLException) Sector(gov.nasa.worldwind.geom.Sector) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) Tile(gov.nasa.worldwind.util.Tile) TileEntry(org.hortonmachine.dbs.geopackage.TileEntry) URL(java.net.URL) BufferedImage(java.awt.image.BufferedImage) MalformedURLException(java.net.MalformedURLException) Graphics2D(java.awt.Graphics2D) LatLon(gov.nasa.worldwind.geom.LatLon) LevelSet(gov.nasa.worldwind.util.LevelSet) ByteArrayInputStream(java.io.ByteArrayInputStream) SqlName(org.hortonmachine.dbs.utils.SqlName) MercatorSector(gov.nasa.worldwind.layers.mercator.MercatorSector) AVList(gov.nasa.worldwind.avlist.AVList) File(java.io.File) TileUrlBuilder(gov.nasa.worldwind.util.TileUrlBuilder) AVListImpl(gov.nasa.worldwind.avlist.AVListImpl)

Aggregations

AVList (gov.nasa.worldwind.avlist.AVList)11 AVListImpl (gov.nasa.worldwind.avlist.AVListImpl)11 LatLon (gov.nasa.worldwind.geom.LatLon)11 MercatorSector (gov.nasa.worldwind.layers.mercator.MercatorSector)11 LevelSet (gov.nasa.worldwind.util.LevelSet)11 Tile (gov.nasa.worldwind.util.Tile)11 TileUrlBuilder (gov.nasa.worldwind.util.TileUrlBuilder)11 BufferedImage (java.awt.image.BufferedImage)11 File (java.io.File)11 MalformedURLException (java.net.MalformedURLException)11 URL (java.net.URL)11 Sector (gov.nasa.worldwind.geom.Sector)9 Graphics2D (java.awt.Graphics2D)9 Rectangle (java.awt.Rectangle)6 IOException (java.io.IOException)6 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)6 Color (java.awt.Color)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 Polygon (org.locationtech.jts.geom.Polygon)2