Search in sources :

Example 1 with ModelsIOException

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

the class OmsOnlineTilesDownloader method process.

@Execute
public void process() throws Exception {
    checkNull(inPath, inServiceUrl, pEpsg, pMinzoom, pMaxzoom, pWest, pEast, pSouth, pNorth);
    CoordinateReferenceSystem boundsCrs = CrsUtilities.getCrsFromEpsg(pEpsg, null);
    CoordinateReferenceSystem mercatorCrs = CrsUtilities.getCrsFromEpsg(EPSG_MERCATOR, null);
    CoordinateReferenceSystem latLongCrs = CrsUtilities.getCrsFromEpsg(EPSG_LATLONG, null);
    ReferencedEnvelope inBounds = new ReferencedEnvelope(pWest, pEast, pSouth, pNorth, boundsCrs);
    MathTransform in2MercatorTransform = CRS.findMathTransform(boundsCrs, mercatorCrs);
    Envelope mercatorEnvelope = JTS.transform(inBounds, in2MercatorTransform);
    ReferencedEnvelope mercatorBounds = new ReferencedEnvelope(mercatorEnvelope, mercatorCrs);
    MathTransform transform = CRS.findMathTransform(boundsCrs, latLongCrs);
    Envelope latLongBounds = JTS.transform(inBounds, transform);
    Coordinate latLongCentre = latLongBounds.centre();
    File inFolder = new File(inPath);
    File baseFolder = new File(inFolder, pName);
    double w = latLongBounds.getMinX();
    double s = latLongBounds.getMinY();
    double e = latLongBounds.getMaxX();
    double n = latLongBounds.getMaxY();
    GlobalMercator mercator = new GlobalMercator();
    for (int z = pMinzoom; z <= pMaxzoom; z++) {
        // get ul and lr tile number in GOOGLE tiles
        int[] llTileXY = mercator.GoogleTile(s, w, z);
        int[] urTileXY = mercator.GoogleTile(n, e, z);
        int startXTile = Math.min(llTileXY[0], urTileXY[0]);
        int endXTile = Math.max(llTileXY[0], urTileXY[0]);
        int startYTile = Math.min(llTileXY[1], urTileXY[1]);
        int endYTile = Math.max(llTileXY[1], urTileXY[1]);
        int tileNum = 0;
        ReferencedEnvelope levelBounds = new ReferencedEnvelope();
        pm.beginTask("Generating tiles at zoom level: " + z, (endXTile - startXTile + 1));
        for (int i = startXTile; i <= endXTile; i++) {
            for (int j = startYTile; j <= endYTile; j++) {
                tileNum++;
                Envelope bounds = mercator.TileLatLonBounds(i, j, z);
                ReferencedEnvelope tmpBounds = new ReferencedEnvelope(bounds, latLongCrs);
                levelBounds.expandToInclude(tmpBounds);
                if (!doDryrun) {
                    int[] onlineTileNumbers = { i, j };
                    int[] fileNameTileNumbers = { i, j };
                    // switch( pType ) {
                    // case 1:
                    // need to convert in TMS format
                    int[] tmsNUms = mercator.TMSTileFromGoogleTile(i, j, z);
                    fileNameTileNumbers = tmsNUms;
                    // break;
                    // case 0:
                    // default:
                    // break;
                    // }
                    File imageFolder = new File(baseFolder, z + "/" + fileNameTileNumbers[0]);
                    if (!imageFolder.exists()) {
                        if (!imageFolder.mkdirs()) {
                            throw new ModelsIOException("Unable to create folder:" + imageFolder, this);
                        }
                    }
                    File imageFile = new File(imageFolder, fileNameTileNumbers[1] + ".png");
                    if (imageFile.exists()) {
                        continue;
                    }
                    String tmp = inServiceUrl.replaceFirst("ZZZ", String.valueOf(z));
                    tmp = tmp.replaceFirst("XXX", String.valueOf(onlineTileNumbers[0]));
                    tmp = tmp.replaceFirst("YYY", String.valueOf(onlineTileNumbers[1]));
                    // System.out.println(tmp);
                    URL url = new URL(tmp);
                    InputStream imgStream = null;
                    OutputStream out = null;
                    try {
                        imgStream = url.openStream();
                        out = new FileOutputStream(imageFile);
                        int read = 0;
                        byte[] bytes = new byte[1024];
                        while ((read = imgStream.read(bytes)) != -1) {
                            out.write(bytes, 0, read);
                        }
                    } catch (Exception ex) {
                        pm.errorMessage("Unable to get image: " + tmp);
                    } finally {
                        if (imgStream != null)
                            imgStream.close();
                        if (out != null) {
                            out.flush();
                            out.close();
                        }
                    }
                }
            }
            pm.worked(1);
        }
        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);
    }
    StringBuilder properties = new StringBuilder();
    properties.append("url=").append(pName).append("/ZZZ/XXX/YYY.png\n");
    properties.append("minzoom=").append(pMinzoom).append("\n");
    properties.append("maxzoom=").append(pMaxzoom).append("\n");
    properties.append("center=").append(latLongCentre.x).append(" ").append(latLongCentre.y).append("\n");
    properties.append("type=tms").append("\n");
    File propFile = new File(inFolder, pName + ".mapurl");
    FileUtilities.writeFile(properties.toString(), propFile);
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) URL(java.net.URL) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Coordinate(org.locationtech.jts.geom.Coordinate) FileOutputStream(java.io.FileOutputStream) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) File(java.io.File) Execute(oms3.annotations.Execute)

Example 2 with ModelsIOException

use of org.hortonmachine.gears.libs.exceptions.ModelsIOException 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)

Example 3 with ModelsIOException

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

the class OmsVectorWriter method process.

// PARAM NAMES STOP
@Execute
public void process() throws Exception {
    checkNull(file);
    File vectorFile = new File(file);
    if (inVector.size() == 0) {
        pm.message("Warning, not writing an empty vector to file: " + vectorFile.getName());
        return;
    }
    String name = vectorFile.getName();
    if (name.toLowerCase().endsWith(HMConstants.SHP) || (pType != null && pType.equals(HMConstants.SHP))) {
        if (vectorFile.exists() && !doOverwrite) {
            throw new ModelsIOException("Overwriting is disabled. First delete the data.", this);
        }
        OmsShapefileFeatureWriter.writeShapefile(vectorFile.getAbsolutePath(), inVector, pm);
    } else if (name.toLowerCase().contains("." + HMConstants.GPKG)) {
        if (!name.contains(HMConstants.DB_TABLE_PATH_SEPARATOR)) {
            throw new ModelsIllegalargumentException("The table name needs to be specified in the geopackage path after the #.", this);
        }
        String[] split = file.split(HMConstants.DB_TABLE_PATH_SEPARATOR);
        if (split.length == 1 || split[1].trim().length() == 0) {
            throw new ModelsIllegalargumentException("The geopackage contains several tables, the table neame needs to be specified in the path after the #.", this);
        }
        SqlName table = SqlName.m(split[1]);
        String dbPath = split[0];
        try (GeopackageCommonDb db = (GeopackageCommonDb) EDb.GEOPACKAGE.getSpatialDb()) {
            boolean existed = db.open(dbPath);
            db.initSpatialMetadata(null);
            CoordinateReferenceSystem crs = inVector.getBounds().getCoordinateReferenceSystem();
            int srid = CrsUtilities.getSrid(crs);
            db.addCRS("EPSG", srid, crs.toWKT());
            if (db.hasTable(table) && !doOverwrite) {
                throw new ModelsIOException("Overwriting is disabled. First delete the data.", this);
            }
            if (!db.hasTable(table) || !existed) {
                SpatialDbsImportUtils.createTableFromSchema(db, inVector.getSchema(), table, null, false);
            }
            SpatialDbsImportUtils.importFeatureCollection(db, inVector, table, -1, false, pm);
        }
    } else {
        throw new IOException("Format is currently not supported for file: " + name);
    }
}
Also used : ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) SqlName(org.hortonmachine.dbs.utils.SqlName) GeopackageCommonDb(org.hortonmachine.dbs.geopackage.GeopackageCommonDb) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) IOException(java.io.IOException) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) File(java.io.File) Execute(oms3.annotations.Execute)

Example 4 with ModelsIOException

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

the class PipeCombo method getJunction.

private static SimpleFeature getJunction(STRtree junctionsTree, int pipeId, Envelope env) throws ModelsIOException {
    List resList = junctionsTree.query(env);
    if (resList.size() != 1) {
        throw new ModelsIOException("Could not identify 1 junction for pipe " + pipeId + ". Check your data.", "PipeCombo");
    }
    SimpleFeature junctionF = (SimpleFeature) resList.get(0);
    return junctionF;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 5 with ModelsIOException

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

the class GeopaparazziSpatialiteCreator method process.

// VARS DOCS END
@Execute
public void process() throws Exception {
    checkNull(inGeopaparazzi, inShapefilesFolder);
    if (pEncoding == null || pEncoding.trim().length() == 0) {
        pEncoding = "UTF-8";
    }
    if (pSizeFactor < 1) {
        pSizeFactor = 3;
    }
    if (pLinesWidthFactor < 1) {
        pLinesWidthFactor = 6;
    }
    File shpFolder = new File(inShapefilesFolder);
    File[] shpfiles = shpFolder.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".shp");
        }
    });
    if (shpfiles.length == 0) {
        throw new ModelsIOException("The supplied folder doesn't contain any shapefile.", this);
    }
    try (ASpatialDb db = new GTSpatialiteThreadsafeDb()) {
        if (!db.open(inGeopaparazzi)) {
            db.initSpatialMetadata(null);
        }
        if (!db.hasTable(SqlName.m(GeopaparazziDatabaseProperties.PROPERTIESTABLE))) {
            GeopaparazziDatabaseProperties.createPropertiesTable(db);
        } else {
            QueryResult qres1 = db.getTableRecordsMapFromRawSql("select * from dataproperties", 10);
            pm.message("Dataproperties already existing: ");
            for (Object[] objs : qres1.data) {
                pm.message(Arrays.toString(objs));
            }
            pm.message("----------------------------------");
        }
        pm.beginTask("Importing shapefiles...", shpfiles.length);
        for (File shpFile : shpfiles) {
            SqlName name = SqlName.m(FileUtilities.getNameWithoutExtention(shpFile));
            if (db.hasTable(name)) {
                pm.errorMessage("Table already existing: " + name);
                continue;
            }
            SimpleFeatureCollection fc = OmsVectorReader.readVector(shpFile.getAbsolutePath());
            SimpleFeatureType schema = fc.getSchema();
            CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
            String epsgStr = CrsUtilities.getCodeFromCrs(crs);
            String sirdStr = epsgStr.substring(5);
            int srid = Integer.parseInt(sirdStr);
            EGeometryType geomType = EGeometryType.forGeometryDescriptor(schema.getGeometryDescriptor());
            ESpatialiteGeometryType spatialiteGeometryType = geomType.toSpatialiteGeometryType();
            HMImportExportUtils.importShapefileThroughVirtualTable(db, name, shpFile.getAbsolutePath(), pEncoding, srid, spatialiteGeometryType);
            Style style = SldUtilities.getStyleFromFile(shpFile);
            if (style != null) {
                String uniqueName = "/#" + name + "#geometry";
                StyleWrapper styleWrapper = new StyleWrapper(style);
                List<FeatureTypeStyleWrapper> featureTypeStylesWrapperList = styleWrapper.getFeatureTypeStylesWrapperList();
                if (featureTypeStylesWrapperList.size() > 0) {
                    List<RuleWrapper> rulesWrapperList = new ArrayList<>();
                    for (FeatureTypeStyleWrapper ftsWrapper : featureTypeStylesWrapperList) {
                        List<RuleWrapper> rulesWrappers = ftsWrapper.getRulesWrapperList();
                        rulesWrapperList.addAll(rulesWrappers);
                    }
                    if (rulesWrapperList.size() == 1) {
                        RuleWrapper ruleWrapper = rulesWrapperList.get(0);
                        SymbolizerWrapper geometrySymbolizersWrapper = ruleWrapper.getGeometrySymbolizersWrapper();
                        if (geometrySymbolizersWrapper != null) {
                            org.hortonmachine.dbs.utils.BasicStyle gpStyle = createBaseStyle(db, uniqueName, rulesWrapperList);
                            populateStyleObject(gpStyle, geometrySymbolizersWrapper);
                            GeopaparazziDatabaseProperties.updateStyle(db, gpStyle);
                        }
                    } else if (rulesWrapperList.size() > 1) {
                        org.hortonmachine.dbs.utils.BasicStyle gpStyle = createBaseStyle(db, uniqueName, rulesWrapperList);
                        gpStyle.themeMap = new HashMap<>();
                        for (RuleWrapper ruleWrapper : rulesWrapperList) {
                            SymbolizerWrapper geometrySymbolizersWrapper = ruleWrapper.getGeometrySymbolizersWrapper();
                            org.hortonmachine.dbs.utils.BasicStyle themeStyle = createBaseStyle(null, uniqueName, rulesWrapperList);
                            populateStyleObject(themeStyle, geometrySymbolizersWrapper);
                            Filter filter = ruleWrapper.getRule().getFilter();
                            if (filter instanceof IsEqualsToImpl) {
                                IsEqualsToImpl equalsFilter = (IsEqualsToImpl) filter;
                                Expression expression1 = equalsFilter.getExpression1();
                                Expression expression2 = equalsFilter.getExpression2();
                                setFilter(gpStyle, themeStyle, expression1);
                                setFilter(gpStyle, themeStyle, expression2);
                            }
                        }
                        GeopaparazziDatabaseProperties.updateStyle(db, gpStyle);
                    } else {
                        pm.errorMessage("Unable to export SLD for: " + shpFile);
                        continue;
                    }
                }
            }
            pm.worked(1);
        }
        pm.done();
        QueryResult qres = db.getTableRecordsMapFromRawSql("select * from dataproperties", 100);
        pm.message("Dataproperties inserted: ");
        int theme = qres.names.indexOf(org.hortonmachine.dbs.utils.BasicStyle.THEME);
        for (Object[] objs : qres.data) {
            String themeString = objs[theme].toString().replaceAll("\\s+", " ");
            if (themeString.length() > 20) {
                objs[theme] = themeString.substring(0, 15) + "...";
            }
            pm.message(Arrays.toString(objs));
        }
    }
}
Also used : HashMap(java.util.HashMap) FeatureTypeStyleWrapper(org.hortonmachine.gears.utils.style.FeatureTypeStyleWrapper) ArrayList(java.util.ArrayList) RuleWrapper(org.hortonmachine.gears.utils.style.RuleWrapper) FilenameFilter(java.io.FilenameFilter) QueryResult(org.hortonmachine.dbs.compat.objects.QueryResult) SqlName(org.hortonmachine.dbs.utils.SqlName) Style(org.geotools.styling.Style) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ASpatialDb(org.hortonmachine.dbs.compat.ASpatialDb) GTSpatialiteThreadsafeDb(org.hortonmachine.gears.spatialite.GTSpatialiteThreadsafeDb) LineSymbolizerWrapper(org.hortonmachine.gears.utils.style.LineSymbolizerWrapper) TextSymbolizerWrapper(org.hortonmachine.gears.utils.style.TextSymbolizerWrapper) PolygonSymbolizerWrapper(org.hortonmachine.gears.utils.style.PolygonSymbolizerWrapper) PointSymbolizerWrapper(org.hortonmachine.gears.utils.style.PointSymbolizerWrapper) SymbolizerWrapper(org.hortonmachine.gears.utils.style.SymbolizerWrapper) IsEqualsToImpl(org.geotools.filter.IsEqualsToImpl) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) ESpatialiteGeometryType(org.hortonmachine.dbs.datatypes.ESpatialiteGeometryType) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) StyleWrapper(org.hortonmachine.gears.utils.style.StyleWrapper) FeatureTypeStyleWrapper(org.hortonmachine.gears.utils.style.FeatureTypeStyleWrapper) FilenameFilter(java.io.FilenameFilter) Filter(org.opengis.filter.Filter) Expression(org.opengis.filter.expression.Expression) ModelsIOException(org.hortonmachine.gears.libs.exceptions.ModelsIOException) File(java.io.File) EGeometryType(org.hortonmachine.gears.utils.geometry.EGeometryType) Execute(oms3.annotations.Execute)

Aggregations

ModelsIOException (org.hortonmachine.gears.libs.exceptions.ModelsIOException)14 File (java.io.File)9 Execute (oms3.annotations.Execute)8 Coordinate (org.locationtech.jts.geom.Coordinate)7 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)6 ArrayList (java.util.ArrayList)5 Envelope (org.locationtech.jts.geom.Envelope)5 List (java.util.List)4 Geometry (org.locationtech.jts.geom.Geometry)4 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)3 DirectPosition2D (org.geotools.geometry.DirectPosition2D)3 LineString (org.locationtech.jts.geom.LineString)3 Point (java.awt.Point)2 BufferedImage (java.awt.image.BufferedImage)2 FilenameFilter (java.io.FilenameFilter)2 ExecutorService (java.util.concurrent.ExecutorService)2 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)2 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)2 SqlName (org.hortonmachine.dbs.utils.SqlName)2 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)2