Search in sources :

Example 1 with LasSource

use of org.hortonmachine.gears.io.las.databases.LasSource 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 2 with LasSource

use of org.hortonmachine.gears.io.las.databases.LasSource in project hortonmachine by TheHortonMachine.

the class DatabaseLasDataManager method getOverallEnvelope.

@Override
public synchronized ReferencedEnvelope getOverallEnvelope() throws Exception {
    if (referencedEnvelope2D == null) {
        checkOpen();
        List<LasSource> lasSources = LasSourcesTable.getLasSources(spatialDb);
        ReferencedEnvelope total = new ReferencedEnvelope(crs);
        for (LasSource lasSource : lasSources) {
            Envelope envelopeInternal = lasSource.polygon.getEnvelopeInternal();
            total.expandToInclude(envelopeInternal);
        }
        referencedEnvelope2D = total;
        referencedEnvelope2DList.add(referencedEnvelope2D);
    }
    return referencedEnvelope2D;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) LasSource(org.hortonmachine.gears.io.las.databases.LasSource)

Example 3 with LasSource

use of org.hortonmachine.gears.io.las.databases.LasSource in project hortonmachine by TheHortonMachine.

the class DatabaseLasDataManager method getEnvelope3D.

@Override
public synchronized ReferencedEnvelope3D getEnvelope3D() throws Exception {
    if (referencedEnvelope3D == null) {
        checkOpen();
        List<LasSource> lasSources = LasSourcesTable.getLasSources(spatialDb);
        ReferencedEnvelope total = new ReferencedEnvelope(crs);
        double minZ = Double.POSITIVE_INFINITY;
        double maxZ = Double.NEGATIVE_INFINITY;
        for (LasSource lasSource : lasSources) {
            Envelope envelopeInternal = lasSource.polygon.getEnvelopeInternal();
            total.expandToInclude(envelopeInternal);
            minZ = Math.min(minZ, lasSource.minElev);
            maxZ = Math.max(maxZ, lasSource.maxElev);
        }
        referencedEnvelope3D = new ReferencedEnvelope3D(total.getMinX(), total.getMaxX(), total.getMinY(), total.getMaxY(), minZ, maxZ, crs);
    }
    return referencedEnvelope3D;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) ReferencedEnvelope3D(org.geotools.geometry.jts.ReferencedEnvelope3D) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) LasSource(org.hortonmachine.gears.io.las.databases.LasSource)

Example 4 with LasSource

use of org.hortonmachine.gears.io.las.databases.LasSource in project hortonmachine by TheHortonMachine.

the class DatabaseLasDataManager method getOverviewFeatures.

@Override
public synchronized SimpleFeatureCollection getOverviewFeatures() throws Exception {
    if (overviewFeatures == null) {
        SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
        b.setName("overview");
        b.setCRS(crs);
        b.add("the_geom", Polygon.class);
        b.add("name", String.class);
        SimpleFeatureType type = b.buildFeatureType();
        SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
        overviewFeatures = new DefaultFeatureCollection();
        List<LasSource> lasSources = LasSourcesTable.getLasSources(spatialDb);
        for (int i = 0; i < lasSources.size(); i++) {
            LasSource lasSource = lasSources.get(i);
            String name = lasSource.name;
            Object[] objs = new Object[] { lasSource.polygon, name };
            builder.addAll(objs);
            SimpleFeature feature = builder.buildFeature(null);
            ((DefaultFeatureCollection) overviewFeatures).add(feature);
        }
    }
    return overviewFeatures;
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) LasSource(org.hortonmachine.gears.io.las.databases.LasSource) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

LasSource (org.hortonmachine.gears.io.las.databases.LasSource)4 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)3 Envelope (org.locationtech.jts.geom.Envelope)2 AVList (gov.nasa.worldwind.avlist.AVList)1 AVListImpl (gov.nasa.worldwind.avlist.AVListImpl)1 LatLon (gov.nasa.worldwind.geom.LatLon)1 Sector (gov.nasa.worldwind.geom.Sector)1 MercatorSector (gov.nasa.worldwind.layers.mercator.MercatorSector)1 LevelSet (gov.nasa.worldwind.util.LevelSet)1 Tile (gov.nasa.worldwind.util.Tile)1 TileUrlBuilder (gov.nasa.worldwind.util.TileUrlBuilder)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 Point2D (java.awt.geom.Point2D)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1