Search in sources :

Example 1 with ModelsUserCancelException

use of org.hortonmachine.gears.libs.exceptions.ModelsUserCancelException in project hortonmachine by TheHortonMachine.

the class OmsTmsGenerator method process.

@Execute
public void process() throws Exception {
    try {
        checkNull(inPath, pMinzoom, pMaxzoom, pWest, pEast, pSouth, pNorth);
        if (dataCrs == null) {
            if (pEpsg != null) {
                dataCrs = CrsUtilities.getCrsFromEpsg(pEpsg, null);
            } else {
                String wkt = FileUtilities.readFile(inPrj);
                dataCrs = CRS.parseWKT(wkt);
            }
        }
        String format = null;
        if (doMbtiles) {
            mbtilesHelper = new MBTilesHelper();
            File dbFolder = new File(inPath);
            File dbFile = new File(dbFolder, pName + ".mbtiles");
            ReferencedEnvelope dataBounds = new ReferencedEnvelope(pWest, pEast, pSouth, pNorth, dataCrs);
            MathTransform data2LLTransform = CRS.findMathTransform(dataCrs, DefaultGeographicCRS.WGS84);
            Envelope llEnvelope = JTS.transform(dataBounds, data2LLTransform);
            float n = (float) llEnvelope.getMaxY();
            float s = (float) llEnvelope.getMinY();
            float w = (float) llEnvelope.getMinX();
            float e = (float) llEnvelope.getMaxX();
            format = pImagetype == 0 ? "png" : "jpg";
            mbtilesHelper.open(dbFile);
            mbtilesHelper.createTables(false);
            mbtilesHelper.fillMetadata(n, s, w, e, pName, format, pMinzoom, pMaxzoom);
        }
        int threads = getDefaultThreadsNum();
        String ext = "png";
        if (pImagetype == 1) {
            ext = "jpg";
        }
        checkCancel();
        List<String> inVectors = null;
        if (inVectorFile != null && new File(inVectorFile).exists())
            inVectors = FileUtilities.readFileToLinesList(new File(inVectorFile));
        checkCancel();
        List<String> inRasters = null;
        if (inRasterFile != null && new File(inRasterFile).exists())
            inRasters = FileUtilities.readFileToLinesList(new File(inRasterFile));
        if (inRasters == null && inVectors == null) {
            throw new ModelsIllegalargumentException("No raster and vector input maps available. check your inputs.", this, pm);
        }
        if (dataCrs == null && pEpsg == null && inPrj == null) {
            throw new ModelsIllegalargumentException("No projection info available. check your inputs.", this, pm);
        }
        final CoordinateReferenceSystem mercatorCrs = CrsUtilities.getCrsFromEpsg(EPSG_MERCATOR, null);
        ReferencedEnvelope dataBounds = new ReferencedEnvelope(pWest, pEast, pSouth, pNorth, dataCrs);
        MathTransform data2MercatorTransform = CRS.findMathTransform(dataCrs, mercatorCrs);
        Envelope mercatorEnvelope = JTS.transform(dataBounds, data2MercatorTransform);
        ReferencedEnvelope mercatorBounds = new ReferencedEnvelope(mercatorEnvelope, mercatorCrs);
        checkCancel();
        if (inZoomLimitVector != null) {
            SimpleFeatureCollection zoomLimitVector = OmsVectorReader.readVector(inZoomLimitVector);
            List<Geometry> geoms = FeatureUtilities.featureCollectionToGeometriesList(zoomLimitVector, true, null);
            MultiPolygon multiPolygon = gf.createMultiPolygon(geoms.toArray(GeometryUtilities.TYPE_POLYGON));
            // convert to mercator
            Geometry multiPolygonGeom = JTS.transform(multiPolygon, data2MercatorTransform);
            zoomLimitGeometry = PreparedGeometryFactory.prepare(multiPolygonGeom);
        }
        File inFolder = new File(inPath);
        final File baseFolder = new File(inFolder, pName);
        final ImageGenerator imgGen = new ImageGenerator(pm, mercatorCrs);
        if (inWMS != null) {
            imgGen.setWMS(inWMS);
        }
        String notLoading = "Not loading non-existing file: ";
        if (inRasters != null)
            for (String rasterPath : inRasters) {
                File file = new File(rasterPath);
                if (file.exists()) {
                    imgGen.addCoveragePath(rasterPath);
                } else {
                    pm.errorMessage(notLoading + rasterPath);
                }
            }
        if (inRasterBounds != null)
            for (GridGeometry2D rasterBounds : inRasterBounds) {
                imgGen.addCoverageRegion(rasterBounds);
            }
        if (inVectors != null)
            for (String vectorPath : inVectors) {
                File file = new File(vectorPath);
                if (file.exists()) {
                    imgGen.addFeaturePath(vectorPath, null);
                } else {
                    pm.errorMessage(notLoading + vectorPath);
                }
            }
        imgGen.setLayers();
        double w = mercatorBounds.getMinX();
        double s = mercatorBounds.getMinY();
        double e = mercatorBounds.getMaxX();
        double n = mercatorBounds.getMaxY();
        final GlobalMercator mercator = new GlobalMercator();
        for (int z = pMinzoom; z <= pMaxzoom; z++) {
            checkCancel();
            // get ul and lr tile number
            int[] llTileNumber = mercator.MetersToTile(w, s, z);
            int[] urTileNumber = mercator.MetersToTile(e, n, z);
            int startXTile = llTileNumber[0];
            int startYTile = llTileNumber[1];
            int endXTile = urTileNumber[0];
            int endYTile = urTileNumber[1];
            int tileNum = 0;
            final ReferencedEnvelope levelBounds = new ReferencedEnvelope(mercatorCrs);
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(threads);
            pm.beginTask("Generating tiles at zoom level: " + z, (endXTile - startXTile + 1) * (endYTile - startYTile + 1));
            for (int i = startXTile; i <= endXTile; i++) {
                checkCancel();
                for (int j = startYTile; j <= endYTile; j++) {
                    checkCancel();
                    double[] bounds = mercator.TileBounds(i, j, z);
                    double west = bounds[0];
                    double south = bounds[1];
                    double east = bounds[2];
                    double north = bounds[3];
                    final ReferencedEnvelope tmpBounds = new ReferencedEnvelope(west, east, south, north, mercatorCrs);
                    levelBounds.expandToInclude(tmpBounds);
                    // if there is a zoom level geometry limitation, apply it
                    if (zoomLimitGeometry != null && z > pZoomLimit) {
                        double safeExtend = tmpBounds.getWidth() > tmpBounds.getHeight() ? tmpBounds.getWidth() : tmpBounds.getHeight();
                        final ReferencedEnvelope tmp = new ReferencedEnvelope(tmpBounds);
                        tmp.expandBy(safeExtend);
                        Polygon polygon = FeatureUtilities.envelopeToPolygon(tmp);
                        if (!zoomLimitGeometry.intersects(polygon)) {
                            pm.worked(1);
                            continue;
                        }
                    }
                    if (mbtilesHelper != null) {
                        final int x = i;
                        final int y = j;
                        final int zz = z;
                        final String fformat = format;
                        tileNum++;
                        Runnable runner = new Runnable() {

                            public void run() {
                                if (!cancelModule) {
                                    try {
                                        checkCancel();
                                        BufferedImage image = imgGen.getImageWithCheck(tmpBounds, TILESIZE, TILESIZE, 0.0, pCheckcolor);
                                        if (image != null) {
                                            mbtilesHelper.addTile(x, y, zz, image, fformat);
                                        }
                                    } catch (Exception e) {
                                        pm.errorMessage(e.getMessage());
                                        cancelModule = true;
                                    }
                                }
                                pm.worked(1);
                            }
                        };
                        fixedThreadPool.execute(runner);
                    } else {
                        File imageFolder = new File(baseFolder, z + "/" + i);
                        if (!imageFolder.exists()) {
                            if (!imageFolder.mkdirs()) {
                                throw new ModelsIOException("Unable to create folder:" + imageFolder, this);
                            }
                        }
                        File ignoreMediaFile = new File(imageFolder, ".nomedia");
                        ignoreMediaFile.createNewFile();
                        final File imageFile = new File(imageFolder, j + "." + ext);
                        if (imageFile.exists()) {
                            pm.worked(1);
                            continue;
                        }
                        tileNum++;
                        final String imagePath = imageFile.getAbsolutePath();
                        final ReferencedEnvelope finalBounds = tmpBounds;
                        Runnable runner = new Runnable() {

                            public void run() {
                                if (!cancelModule) {
                                    try {
                                        if (pImagetype == 1) {
                                            imgGen.dumpJpgImage(imagePath, finalBounds, TILESIZE, TILESIZE, 0.0, pCheckcolor);
                                        } else {
                                            imgGen.dumpPngImage(imagePath, finalBounds, TILESIZE, TILESIZE, 0.0, pCheckcolor);
                                        }
                                        pm.worked(1);
                                    } catch (Exception ex) {
                                        pm.errorMessage(ex.getMessage());
                                        cancelModule = true;
                                    }
                                }
                            }
                        };
                        fixedThreadPool.execute(runner);
                    }
                }
            }
            try {
                fixedThreadPool.shutdown();
                while (!fixedThreadPool.isTerminated()) {
                    Thread.sleep(100);
                }
            } catch (InterruptedException exx) {
                exx.printStackTrace();
            }
            pm.done();
            pm.message("Zoom level: " + z + " has " + tileNum + " tiles.");
        // pm.message("Boundary covered at Zoom level: " + z + ": " + levelBounds);
        // pm.message("Total boundary wanted: " + mercatorBounds);
        }
        if (mbtilesHelper != null) {
            mbtilesHelper.createIndexes();
            mbtilesHelper.close();
        } else {
            CoordinateReferenceSystem latLongCrs = CrsUtilities.getCrsFromEpsg(EPSG_LATLONG, null);
            MathTransform transform = CRS.findMathTransform(mercatorCrs, latLongCrs);
            Envelope latLongBounds = JTS.transform(mercatorBounds, transform);
            Coordinate latLongCentre = latLongBounds.centre();
            StringBuilder properties = new StringBuilder();
            properties.append("url=").append(pName).append("/ZZZ/XXX/YYY.").append(ext).append("\n");
            properties.append("minzoom=").append(pMinzoom).append("\n");
            properties.append("maxzoom=").append(pMaxzoom).append("\n");
            properties.append("center=").append(latLongCentre.y).append(" ").append(latLongCentre.x).append("\n");
            properties.append("type=tms").append("\n");
            File propFile = new File(inFolder, pName + ".mapurl");
            FileUtilities.writeFile(properties.toString(), propFile);
        }
    } catch (ModelsUserCancelException e) {
        pm.errorMessage(ModelsUserCancelException.DEFAULTMESSAGE);
    }
}
Also used : GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) MathTransform(org.opengis.referencing.operation.MathTransform) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) BufferedImage(java.awt.image.BufferedImage) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) ModelsUserCancelException(org.hortonmachine.gears.libs.exceptions.ModelsUserCancelException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) ModelsUserCancelException(org.hortonmachine.gears.libs.exceptions.ModelsUserCancelException) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Coordinate(org.locationtech.jts.geom.Coordinate) ExecutorService(java.util.concurrent.ExecutorService) ImageGenerator(org.hortonmachine.gears.utils.images.ImageGenerator) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) File(java.io.File) Execute(oms3.annotations.Execute)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 ExecutorService (java.util.concurrent.ExecutorService)1 Execute (oms3.annotations.Execute)1 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)1 ModelsIOException (org.hortonmachine.gears.libs.exceptions.ModelsIOException)1 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)1 ModelsUserCancelException (org.hortonmachine.gears.libs.exceptions.ModelsUserCancelException)1 ImageGenerator (org.hortonmachine.gears.utils.images.ImageGenerator)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Envelope (org.locationtech.jts.geom.Envelope)1 Geometry (org.locationtech.jts.geom.Geometry)1 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)1 Polygon (org.locationtech.jts.geom.Polygon)1 PreparedGeometry (org.locationtech.jts.geom.prep.PreparedGeometry)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 MathTransform (org.opengis.referencing.operation.MathTransform)1