Search in sources :

Example 1 with Name

use of oms3.annotations.Name in project hortonmachine by TheHortonMachine.

the class Mapurl2MbtilesConverter method process.

@Execute
public void process() throws Exception {
    checkNull(inFile);
    File mapurlFile = new File(inFile);
    HashMap<String, String> metadataMap = FileUtilities.readFileToHashMap(inFile, "=", false);
    String url = metadataMap.get("url");
    if (url == null) {
        throw new ModelsIllegalargumentException("The supplied file doesn't seem to be a valid HortonMachine mapurl file.", this, pm);
    }
    if (url.endsWith("jpg")) {
        format = "jpg";
    } else {
        format = "png";
    }
    File dbFile = FileUtilities.substituteExtention(mapurlFile, "mbtiles");
    String tilesetName = FileUtilities.getNameWithoutExtention(mapurlFile);
    File folderFile = new File(mapurlFile.getParentFile(), tilesetName);
    mbtilesHelper = new MBTilesHelper();
    mbtilesHelper.open(dbFile);
    mbtilesHelper.createTables(false);
    File[] zFolders = folderFile.listFiles();
    List<File> xFolder = new ArrayList<File>();
    for (File zFolder : zFolders) {
        File[] xFiles = zFolder.listFiles();
        for (File xFile : xFiles) {
            if (xFile.isDirectory()) {
                xFolder.add(xFile);
            }
        }
    }
    final GlobalMercator mercator = new GlobalMercator();
    int minZ = 1000;
    int maxZ = -1000;
    EggClock clock = new EggClock("Time check: ", " sec");
    clock.startAndPrint(System.out);
    for (File xFile : xFolder) {
        String zStr = xFile.getParentFile().getName();
        final int z = Integer.parseInt(zStr);
        minZ = min(minZ, z);
        maxZ = max(maxZ, z);
        String xStr = xFile.getName();
        final int x = Integer.parseInt(xStr);
        final File[] yFiles = xFile.listFiles(new FilenameFilter() {

            public boolean accept(File arg0, String name) {
                boolean endsWithPng = name.endsWith("png");
                boolean endsWithJpg = name.endsWith("jpg");
                if (endsWithPng || endsWithJpg) {
                    return true;
                }
                return false;
            }
        });
        for (File yFile : yFiles) {
            String yStr = FileUtilities.getNameWithoutExtention(yFile);
            int y = Integer.parseInt(yStr);
            Envelope wsen = mercator.TileLatLonBounds(x, y, z);
            n = max(n, wsen.getMaxY());
            e = max(e, wsen.getMaxX());
            s = max(s, wsen.getMinY());
            w = max(w, wsen.getMinX());
            try {
                BufferedImage image = ImageIO.read(yFile);
                mbtilesHelper.addTile(x, y, z, image, format);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            if (imageIndex % 1000 == 0) {
                pm.message("Images inserted in db: " + imageIndex);
                clock.printTimePassedInSeconds(System.out);
            }
            imageIndex++;
        }
    }
    mbtilesHelper.fillMetadata((float) n, (float) s, (float) w, (float) e, "tilesetName", format, minZ, maxZ);
    mbtilesHelper.createIndexes();
    mbtilesHelper.close();
}
Also used : ArrayList(java.util.ArrayList) Envelope(org.locationtech.jts.geom.Envelope) BufferedImage(java.awt.image.BufferedImage) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) EggClock(org.hortonmachine.gears.utils.time.EggClock) FilenameFilter(java.io.FilenameFilter) File(java.io.File) Execute(oms3.annotations.Execute)

Example 2 with Name

use of oms3.annotations.Name in project hortonmachine by TheHortonMachine.

the class OmsMapcalc method process.

@SuppressWarnings("nls")
@Execute
public void process() throws Exception {
    if (!concatOr(outRaster == null, doReset)) {
        return;
    }
    String script = pFunction;
    script = script.trim();
    Jiffle jiffle = new Jiffle();
    jiffle.setScript(script);
    jiffle.compile();
    JiffleDirectRuntime jiffleRuntime = jiffle.getRuntimeInstance();
    CoordinateTransform jiffleCRS = null;
    // gather maps
    for (GridCoverage2D mapGC : inRasters) {
        if (regionParameters == null) {
            regionParameters = CoverageUtilities.getRegionParamsFromGridCoverage(mapGC);
            crs = mapGC.getCoordinateReferenceSystem();
            worldBounds = mapGC.getEnvelope2D().getBounds2D();
            Rectangle gridBounds = mapGC.getGridGeometry().getGridRange2D().getBounds();
            jiffleCRS = getTransform(worldBounds, gridBounds);
            double xRes = regionParameters.get(CoverageUtilities.XRES).doubleValue();
            double yRes = regionParameters.get(CoverageUtilities.YRES).doubleValue();
            jiffleRuntime.setWorldByResolution(worldBounds, xRes, yRes);
        }
        RenderedImage renderedImage = mapGC.getRenderedImage();
        // add map
        String name = mapGC.getName().toString();
        jiffleRuntime.setSourceImage(name, renderedImage, jiffleCRS);
    }
    if (regionParameters == null) {
        throw new ModelsIllegalargumentException("No map has been supplied.", this.getClass().getSimpleName(), pm);
    }
    int nCols = regionParameters.get(CoverageUtilities.COLS).intValue();
    int nRows = regionParameters.get(CoverageUtilities.ROWS).intValue();
    long pixelsNum = (long) nCols * nRows;
    if (pixelsNum < totalCount) {
        totalCount = pixelsNum;
    }
    updateInterval = pixelsNum / totalCount;
    String destName = jiffleRuntime.getDestinationVarNames()[0];
    WritableRenderedImage destImg = ImageUtils.createConstantImage(nCols, nRows, 0d);
    jiffleRuntime.setDestinationImage(destName, destImg, jiffleCRS);
    jiffleRuntime.evaluateAll(new NullProgressListener());
    outRaster = CoverageUtilities.buildCoverage(destName, destImg, regionParameters, crs);
// // create the executor
// JiffleExecutor executor = new JiffleExecutor();
// JiffleEventListener listener = new JiffleEventListener(){
// 
// @Override
// public void onFailureEvent( JiffleEvent event ) {
// 
// }
// 
// @Override
// public void onCompletionEvent( JiffleEvent event ) {
// JiffleExecutorResult result = event.getResult();
// Map<String, RenderedImage> imgMap = result.getImages();
// RenderedImage destImage = imgMap.get(destName);
// outRaster = CoverageUtilities.buildCoverage(destName, destImage, regionParameters, crs);
// executor.shutdown();
// }
// };
// executor.addEventListener(listener);
// 
// executor.submit(jiffleRuntime, new JiffleProgressListener(){
// private long count = 0;
// public void update( long done ) {
// if (count == done) {
// pm.worked(1);
// count = count + updateInterval;
// }
// }
// 
// public void start() {
// pm.beginTask("Processing maps...", (int) totalCount);
// }
// 
// public void setUpdateInterval( double propPixels ) {
// }
// 
// public void setUpdateInterval( long numPixels ) {
// }
// 
// public void setTaskSize( long numPixels ) {
// count = updateInterval;
// }
// 
// public long getUpdateInterval() {
// if (updateInterval == 0) {
// return 1;
// }
// return updateInterval;
// }
// 
// public void finish() {
// pm.done();
// }
// });
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) Jiffle(it.geosolutions.jaiext.jiffle.Jiffle) Rectangle(java.awt.Rectangle) JiffleDirectRuntime(it.geosolutions.jaiext.jiffle.runtime.JiffleDirectRuntime) WritableRenderedImage(java.awt.image.WritableRenderedImage) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) NullProgressListener(it.geosolutions.jaiext.jiffle.runtime.NullProgressListener) WritableRenderedImage(java.awt.image.WritableRenderedImage) RenderedImage(java.awt.image.RenderedImage) CoordinateTransform(it.geosolutions.jaiext.jiffle.runtime.CoordinateTransform) AffineCoordinateTransform(it.geosolutions.jaiext.jiffle.runtime.AffineCoordinateTransform) Execute(oms3.annotations.Execute)

Example 3 with Name

use of oms3.annotations.Name in project hortonmachine by TheHortonMachine.

the class ModelsSupporter method generateTemplate.

public static String generateTemplate(Object parent) {
    Class<?> class1 = parent.getClass();
    String name = class1.getSimpleName();
    StringBuilder sb = new StringBuilder();
    String newName = name;
    String varName = name.toLowerCase();
    sb.append("def ").append(varName).append(" = new ");
    sb.append(newName).append("()\n");
    java.lang.reflect.Field[] fields = class1.getFields();
    for (java.lang.reflect.Field field : fields) {
        String fname = field.getName();
        if (fname.equals("pm") || fname.equals("gf") || fname.equals("doProcess") || fname.equals("doReset")) {
            continue;
        }
        Class<?> cl = field.getType();
        Out out = field.getAnnotation(Out.class);
        if (out != null) {
            continue;
        }
        if (cl.isAssignableFrom(String.class)) {
            sb.append(varName).append(".").append(fname).append(" = \"\"\n");
        } else {
            sb.append(varName).append(".").append(fname).append(" = ?\n");
        }
    }
    sb.append(varName).append(".process()\n");
    // }
    return sb.toString();
}
Also used : Field(java.lang.reflect.Field) Field(java.lang.reflect.Field) Out(oms3.annotations.Out)

Example 4 with Name

use of oms3.annotations.Name 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 5 with Name

use of oms3.annotations.Name in project hortonmachine by TheHortonMachine.

the class SpatialToolboxUtils method collectParameters.

// /**
// * Get the path for a module doc html.
// *
// * @param moduleClassName the name of the module to get the path for.
// * @return the path to the html file.
// * @throws Exception
// */
// public static String getModuleDocumentationPath( String moduleClassName ) throws Exception {
// File configurationsFolder = SpatialToolboxSessionPluginSingleton.getInstance().getConfigurationsFolder();
// File htmlDocsFolder = new File(configurationsFolder, STAGEHTMLDOCS);
// File htmlDocs = new File(htmlDocsFolder, moduleClassName + ".html");
// if (!htmlDocs.exists()) {
// htmlDocs = new File(htmlDocsFolder, "Oms" + moduleClassName + ".html");
// if (!htmlDocs.exists()) {
// return NO_DOCUMENTATION_AVAILABLE;
// }
// }
// return htmlDocs.getAbsolutePath();
// }
// /**
// * Porge the html docs folder.
// *
// * @throws Exception
// */
// public static void cleanModuleDocumentation() throws Exception {
// File configurationsFolder = SpatialToolboxSessionPluginSingleton.getInstance().getConfigurationsFolder();
// File htmlDocsFolder = new File(configurationsFolder, STAGEHTMLDOCS);
// if (htmlDocsFolder.exists()) {
// FileUtilities.deleteFileOrDir(htmlDocsFolder);
// }
// }
// 
// /**
// * Generate the module documentation in the configuration area.
// *
// * @param moduleClassName the class for which to generate the doc.
// * @throws Exception
// */
// @SuppressWarnings("nls")
// public static void generateModuleDocumentation( String moduleClassName ) throws Exception {
// 
// Class< ? > moduleClass = StageModulesManager.getInstance().getModulesClass(moduleClassName);
// 
// StringBuilder sb = new StringBuilder();
// sb.append("<html><body>\n");
// 
// // modules documentation
// Documentation documentation = moduleClass.getAnnotation(Documentation.class);
// String documentationStr = null;
// if (documentation != null) {
// documentationStr = AnnotationUtilities.getLocalizedDocumentation(documentation);
// if (documentationStr.length() == 0) {
// documentationStr = null;
// } else if (documentationStr.equals(" - ")) {
// documentationStr = null;
// }
// }
// if (documentation != null && documentationStr != null) {
// if (documentationStr.endsWith(DOCSSUFFIX)) {
// // have to get the file
// String modulePackage = moduleClassName.substring(0, moduleClassName.lastIndexOf('.'));
// String path = modulePackage.replaceAll("\\.", "/") + "/" + documentationStr;
// InputStream inStream = StageModulesManager.getInstance().getResourceAsStream(path);
// // InputStream inStream = moduleClass.getResourceAsStream(documentationStr);
// if (inStream != null) {
// BufferedReader br = null;
// try {
// br = new BufferedReader(new InputStreamReader(inStream));
// StringBuilder tmpSb = new StringBuilder();
// String line = "";
// while( (line = br.readLine()) != null ) {
// tmpSb.append(line).append(NEWLINE);
// }
// documentationStr = tmpSb.toString();
// } finally {
// if (br != null)
// br.close();
// }
// }
// }
// sb.append("<h2>Description</h2>").append(NEWLINE);
// sb.append(NEWLINE);
// sb.append("<blockquote>");
// sb.append(documentationStr);
// sb.append("</blockquote>");
// sb.append(NEWLINE);
// sb.append(NEWLINE);
// } else {
// // try with module description
// Description description = moduleClass.getAnnotation(Description.class);
// String descriptionStr = AnnotationUtilities.getLocalizedDescription(description);
// if (description != null) {
// sb.append("<h2>Description</h2>").append(NEWLINE);
// sb.append(NEWLINE);
// sb.append("<blockquote>");
// sb.append(descriptionStr);
// sb.append("</blockquote>");
// sb.append(NEWLINE);
// sb.append(NEWLINE);
// }
// }
// // general info
// sb.append("<h2>General Information</h2>").append(NEWLINE);
// sb.append(NEWLINE);
// // general info: status
// Status status = moduleClass.getAnnotation(Status.class);
// if (status != null) {
// sb.append("<blockquote>");
// sb.append("Module status: " + getStatusString(status.value())).append(NEWLINE);
// sb.append("</blockquote>");
// sb.append(NEWLINE);
// }
// 
// // general info: script name
// Name name = moduleClass.getAnnotation(Name.class);
// String nameStr = AnnotationUtilities.getLocalizedName(name);
// if (name != null) {
// sb.append("<blockquote>");
// sb.append(" Name to use in a script: <b>" + nameStr + "</b>").append(NEWLINE);
// sb.append("</blockquote>");
// sb.append(NEWLINE);
// }
// // general info: authors
// Author author = moduleClass.getAnnotation(Author.class);
// if (author != null) {
// String authorNameStr = author.name();
// String[] authorNameSplit = authorNameStr.split(",");
// 
// String authorContactStr = author.contact();
// String[] authorContactSplit = authorContactStr.split(",");
// 
// sb.append("<blockquote>");
// sb.append(" Authors ").append(NEWLINE);
// sb.append(HTMLNEWLINE);
// sb.append("<ul>").append(NEWLINE);
// for( String authorName : authorNameSplit ) {
// sb.append("<li>").append(authorName.trim());
// }
// sb.append("</li>").append(NEWLINE);
// sb.append("</ul>").append(NEWLINE);
// sb.append(NEWLINE);
// sb.append(HTMLNEWLINE);
// sb.append(HTMLNEWLINE);
// // if (authorContactStr.startsWith("http")) {
// // authorContactStr = "<a href=\"" + authorContactStr + "\">" + authorContactStr +
// // "</a>";
// // }
// sb.append(" Contacts: ").append(NEWLINE);
// sb.append(HTMLNEWLINE);
// sb.append("<ul>").append(NEWLINE);
// for( String authorContact : authorContactSplit ) {
// sb.append("<li>").append(authorContact.trim());
// }
// sb.append("</li>").append(NEWLINE);
// sb.append("</ul>").append(NEWLINE);
// sb.append("</blockquote>");
// sb.append(NEWLINE);
// }
// // general info: license
// License license = moduleClass.getAnnotation(License.class);
// if (license != null) {
// String licenseStr = AnnotationUtilities.getLocalizedLicense(license);
// sb.append("<blockquote>");
// sb.append(" License: " + licenseStr).append(NEWLINE);
// sb.append("</blockquote>");
// sb.append(NEWLINE);
// }
// // general info: keywords
// Keywords keywords = moduleClass.getAnnotation(Keywords.class);
// if (keywords != null) {
// String keywordsStr = AnnotationUtilities.getLocalizedKeywords(keywords);
// sb.append("<blockquote>");
// sb.append(" Keywords: " + keywordsStr).append(NEWLINE);
// sb.append("</blockquote>");
// sb.append(NEWLINE);
// }
// sb.append(NEWLINE);
// 
// // gather input fields
// Object annotatedObject = moduleClass.newInstance();
// ComponentAccess cA = new ComponentAccess(annotatedObject);
// 
// // parameters
// sb.append("<h2>Parameters</h2>").append(NEWLINE);
// sb.append(NEWLINE);
// sb.append("<blockquote>");
// // parameters: fields
// Collection<Access> inputs = cA.inputs();
// StringBuilder sbTmp = new StringBuilder();
// collectParameters(sbTmp, inputs);
// toTable(sb, sbTmp, "Input parameters");
// sb.append(NEWLINE);
// Collection<Access> outputs = cA.outputs();
// sbTmp = new StringBuilder();
// collectParameters(sbTmp, outputs);
// toTable(sb, sbTmp, "Output parameters");
// sb.append("</blockquote>");
// sb.append(NEWLINE);
// sb.append(NEWLINE);
// 
// sb.append("</body></html>");
// 
// File configurationsFolder = SpatialToolboxSessionPluginSingleton.getInstance().getConfigurationsFolder();
// File htmlDocsFolder = new File(configurationsFolder, STAGEHTMLDOCS);
// if (!htmlDocsFolder.exists()) {
// if (!htmlDocsFolder.mkdir()) {
// throw new RuntimeException();
// }
// }
// 
// File htmlDocs = new File(htmlDocsFolder, moduleClassName + ".html");
// FileUtilities.writeFile(sb.toString(), htmlDocs);
// }
private static void collectParameters(StringBuilder sbTmp, Collection<Access> accessList) throws Exception {
    for (Access access : accessList) {
        Field field = access.getField();
        String fieldName = field.getName();
        Description descriptionAnnot = field.getAnnotation(Description.class);
        if (fieldName.equals("pm")) {
            // ignore progress monitor
            continue;
        }
        String fieldDescription = " - ";
        if (descriptionAnnot != null) {
            fieldDescription = AnnotationUtilities.getLocalizedDescription(descriptionAnnot);
            if (fieldDescription == null) {
                fieldDescription = " - ";
            }
            Unit unitAnn = field.getAnnotation(Unit.class);
            if (unitAnn != null) {
                fieldDescription = fieldDescription + " [" + unitAnn.value() + "]";
            }
        }
        sbTmp.append("<tr>").append(NEWLINE);
        sbTmp.append("<td width=\"40%\"> <b>").append(fieldName).append("</b> </td><td width=\"60%\"> ");
        sbTmp.append(fieldDescription).append(" </td>").append(NEWLINE);
        sbTmp.append("</tr>").append(NEWLINE);
    }
}
Also used : Field(java.lang.reflect.Field) Description(oms3.annotations.Description) Access(oms3.Access) Unit(oms3.annotations.Unit)

Aggregations

Execute (oms3.annotations.Execute)24 File (java.io.File)19 ArrayList (java.util.ArrayList)19 IOException (java.io.IOException)12 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)12 Entry (java.util.Map.Entry)11 Description (oms3.annotations.Description)11 Status (oms3.annotations.Status)9 ClassField (org.hortonmachine.gears.libs.modules.ClassField)9 FilenameFilter (java.io.FilenameFilter)8 List (java.util.List)7 Access (oms3.Access)7 ComponentAccess (oms3.ComponentAccess)6 DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)6 SimpleFeature (org.opengis.feature.simple.SimpleFeature)6 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)6 Field (java.lang.reflect.Field)5 Author (oms3.annotations.Author)5 Documentation (oms3.annotations.Documentation)5 Keywords (oms3.annotations.Keywords)5