Search in sources :

Example 6 with Out

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

the class OmsRasterTransformer method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outRaster == null, doReset)) {
        return;
    }
    Interpolation interpolation = Interpolation.getInstance(Interpolation.INTERP_NEAREST);
    if (pInterpolation.equals(BILINEAR)) {
        interpolation = Interpolation.getInstance(Interpolation.INTERP_BILINEAR);
    } else if (pInterpolation.equals(BICUBIC)) {
        interpolation = Interpolation.getInstance(Interpolation.INTERP_BICUBIC);
    }
    RenderedImage inRasterRI = inRaster.getRenderedImage();
    RegionMap sourceRegion = CoverageUtilities.gridGeometry2RegionParamsMap(inRaster.getGridGeometry());
    Envelope2D envelope2d = inRaster.getEnvelope2D();
    Envelope targetEnvelope = new Envelope(envelope2d.getMinX(), envelope2d.getMaxX(), envelope2d.getMinY(), envelope2d.getMaxY());
    Geometry targetGeometry = null;
    GeometryFactory gf = GeometryUtilities.gf();
    RenderedOp finalImg = null;
    if (pAngle != null) {
        pm.beginTask("Rotate raster by angle: " + pAngle, IHMProgressMonitor.UNKNOWN);
        float centerX = 0f;
        float centerY = 0f;
        if (pEast == null) {
            centerX = (float) envelope2d.getCenterX();
        } else {
            centerX = pEast.floatValue();
        }
        if (pNorth == null) {
            centerY = (float) envelope2d.getCenterY();
        } else {
            centerY = pNorth.floatValue();
        }
        finalImg = RotateDescriptor.create(inRasterRI, centerX, centerY, (float) Math.toRadians(pAngle), interpolation, null, null);
        // also keep track of the transforming envelope
        AffineTransform rotationAT = new AffineTransform();
        rotationAT.translate(centerX, centerY);
        rotationAT.rotate(Math.toRadians(-pAngle));
        rotationAT.translate(-centerX, -centerY);
        MathTransform rotationTransform = new AffineTransform2D(rotationAT);
        Envelope jtsEnv = new Envelope(targetEnvelope.getMinX(), targetEnvelope.getMaxX(), targetEnvelope.getMinY(), targetEnvelope.getMaxY());
        targetEnvelope = JTS.transform(jtsEnv, rotationTransform);
        Geometry rotGeometry = gf.toGeometry(jtsEnv);
        targetGeometry = JTS.transform(rotGeometry, rotationTransform);
        pm.done();
    }
    if (doFlipHorizontal) {
        pm.beginTask("Flip horizontally...", IHMProgressMonitor.UNKNOWN);
        if (finalImg != null) {
            finalImg = TransposeDescriptor.create(finalImg, TransposeDescriptor.FLIP_HORIZONTAL, null);
        } else {
            finalImg = TransposeDescriptor.create(inRasterRI, TransposeDescriptor.FLIP_HORIZONTAL, null);
        }
        Envelope jtsEnv = new Envelope(targetEnvelope.getMinX(), targetEnvelope.getMaxX(), targetEnvelope.getMinY(), targetEnvelope.getMaxY());
        targetGeometry = gf.toGeometry(jtsEnv);
        pm.done();
    }
    if (doFlipVertical) {
        pm.beginTask("Flip vertically...", IHMProgressMonitor.UNKNOWN);
        if (finalImg != null) {
            finalImg = TransposeDescriptor.create(finalImg, TransposeDescriptor.FLIP_VERTICAL, null);
        } else {
            finalImg = TransposeDescriptor.create(inRasterRI, TransposeDescriptor.FLIP_VERTICAL, null);
        }
        Envelope jtsEnv = new Envelope(targetEnvelope.getMinX(), targetEnvelope.getMaxX(), targetEnvelope.getMinY(), targetEnvelope.getMaxY());
        targetGeometry = gf.toGeometry(jtsEnv);
        pm.done();
    }
    if (pScaleX != null || pScaleY != null) {
        float scaleX = 1f;
        float scaleY = 1f;
        if (pScaleX == null) {
            scaleX = 1f;
        } else {
            scaleX = pScaleX.floatValue();
        }
        if (pScaleY == null) {
            scaleY = 1f;
        } else {
            scaleY = pScaleY.floatValue();
        }
        pm.beginTask("Scale raster by: " + scaleX + " and " + scaleY, IHMProgressMonitor.UNKNOWN);
        // float centerY = (float) envelope2d.getCenterY();
        if (finalImg != null) {
            finalImg = ScaleDescriptor.create(finalImg, new Float(scaleX), new Float(scaleY), new Float(0.0f), new Float(0.0f), interpolation, null);
        } else {
            finalImg = ScaleDescriptor.create(inRasterRI, new Float(scaleX), new Float(scaleY), new Float(0.0f), new Float(0.0f), interpolation, null);
        }
        // also keep track of the transforming envelope
        AffineTransform scaleAT = new AffineTransform();
        // scaleAT.translate(centerX, centerY);
        scaleAT.scale(scaleX, scaleY);
        // scaleAT.translate(-centerX, -centerY);
        MathTransform scaleTransform = new AffineTransform2D(scaleAT);
        Envelope jtsEnv = new Envelope(targetEnvelope.getMinX(), targetEnvelope.getMaxX(), targetEnvelope.getMinY(), targetEnvelope.getMaxY());
        targetEnvelope = JTS.transform(jtsEnv, scaleTransform);
        Geometry scaledGeometry = gf.toGeometry(jtsEnv);
        targetGeometry = JTS.transform(scaledGeometry, scaleTransform);
        pm.done();
    }
    if (pTransX != null || pTransY != null) {
        float transX = 1f;
        float transY = 1f;
        if (pTransX == null) {
            transX = 1f;
        } else {
            transX = pTransX.floatValue();
        }
        if (pTransY == null) {
            transY = 1f;
        } else {
            transY = pTransY.floatValue();
        }
        pm.beginTask("Translate raster by: " + transX + " and " + transY, IHMProgressMonitor.UNKNOWN);
        if (finalImg != null) {
            finalImg = TranslateDescriptor.create(finalImg, transX, transY, interpolation, null);
        } else {
            finalImg = TranslateDescriptor.create(inRasterRI, transX, transY, interpolation, null);
        }
        // also keep track of the transforming envelope
        AffineTransform translationAT = new AffineTransform();
        translationAT.translate(transX, transY);
        MathTransform translateTransform = new AffineTransform2D(translationAT);
        if (targetGeometry == null) {
            targetGeometry = gf.toGeometry(targetEnvelope);
        }
        targetEnvelope = JTS.transform(targetEnvelope, translateTransform);
        targetGeometry = JTS.transform(targetGeometry, translateTransform);
        pm.done();
    }
    if (finalImg != null) {
        RegionMap targetRegion = new RegionMap();
        targetRegion.put(NORTH, targetEnvelope.getMaxY());
        targetRegion.put(SOUTH, targetEnvelope.getMinY());
        targetRegion.put(WEST, targetEnvelope.getMinX());
        targetRegion.put(EAST, targetEnvelope.getMaxX());
        targetRegion.put(XRES, sourceRegion.getXres());
        targetRegion.put(YRES, sourceRegion.getYres());
        // targetRegion.put(ROWS, (double) height);
        // targetRegion.put(COLS, (double) width);
        CoordinateReferenceSystem crs = inRaster.getCoordinateReferenceSystem();
        outRaster = CoverageUtilities.buildCoverage("out", finalImg, targetRegion, crs);
        outBounds = FeatureUtilities.featureCollectionFromGeometry(crs, targetGeometry);
    }
    pm.done();
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) MathTransform(org.opengis.referencing.operation.MathTransform) RegionMap(org.hortonmachine.gears.utils.RegionMap) Envelope(org.locationtech.jts.geom.Envelope) Envelope2D(org.geotools.geometry.Envelope2D) Geometry(org.locationtech.jts.geom.Geometry) Interpolation(javax.media.jai.Interpolation) RenderedOp(javax.media.jai.RenderedOp) AffineTransform(java.awt.geom.AffineTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RenderedImage(java.awt.image.RenderedImage) AffineTransform2D(org.geotools.referencing.operation.transform.AffineTransform2D) Execute(oms3.annotations.Execute)

Example 7 with Out

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

the class OmsEnergyBalance method writeSafePoint.

@Finalize
public void writeSafePoint() {
    if (pEndsafepoint != null && new File(pEndsafepoint).getParentFile() != null) {
        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        try {
            fos = new FileOutputStream(pEndsafepoint);
            out = new ObjectOutputStream(fos);
            out.writeObject(safePoint);
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) Finalize(oms3.annotations.Finalize)

Example 8 with Out

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

the class FieldAccess method out.

/**
 * a field is sending a new value (out)
 *
 * @return  the object value in out
 * @throws java.lang.Exception
 */
@Override
public void out() throws Exception {
    Object val = getFieldValue();
    if (ens.shouldFire()) {
        DataflowEvent e = new DataflowEvent(ens.getController(), this, val);
        // DataflowEvent e = new DataflowEvent(ens.getController(), this, access.toObject());
        ens.fireOut(e);
        // the value might be altered
        val = e.getValue();
    }
    // if data==null this unconsumed @Out, its OK but we do not want to set it.
    if (data != null) {
        data.setValue(val);
    }
}
Also used : DataflowEvent(oms3.Notification.DataflowEvent)

Example 9 with Out

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

the class Components method figureOutConnect.

/**
 * Figure out connectivity and generate Java statements.
 * @param comps
 */
public static void figureOutConnect(PrintStream w, Object... comps) {
    // add all the components via Proxy.
    List<ComponentAccess> l = new ArrayList<ComponentAccess>();
    for (Object c : comps) {
        l.add(new ComponentAccess(c));
    }
    // find all out slots
    for (ComponentAccess cp_out : l) {
        w.println("// connect " + objName(cp_out));
        // over all input slots.
        for (Access fout : cp_out.outputs()) {
            String s = "   out2in(" + objName(cp_out) + ", \"" + fout.getField().getName() + "\"";
            for (ComponentAccess cp_in : l) {
                // skip if it is the same component.
                if (cp_in == cp_out) {
                    continue;
                }
                // out points to in
                for (Access fin : cp_in.inputs()) {
                    // name equivalence enought for now.
                    if (fout.getField().getName().equals(fin.getField().getName())) {
                        s = s + ", " + objName(cp_in);
                    }
                }
            }
            w.println(s + ");");
        }
        w.println();
    }
}
Also used : ComponentAccess(oms3.ComponentAccess) ArrayList(java.util.ArrayList) Access(oms3.Access) ComponentAccess(oms3.ComponentAccess)

Example 10 with Out

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

the class Convert method param.

public static String param(String file) {
    File param = new File(file);
    if (!param.isAbsolute()) {
        String work = System.getProperty("oms3.work");
        if (work == null) {
            return "Error no $work property found, use full qualifies name instead.)";
        }
        param = new File(work + "/data", file);
    }
    String name = param.getName().substring(0, param.getName().indexOf('.'));
    File out = new File(param.getParentFile(), name + ".csv");
    try {
        MmsParamInfo info = MMSParameterAdapter.map(param);
        info.store(new FileOutputStream(out));
        return "  Converted: '" + file + "' -> '" + out + "'\n";
    } catch (IOException ex) {
        return "Error: " + ex.getMessage();
    }
}
Also used : MmsParamInfo(oms3.ngmf.ui.mms.MMSParameterAdapter.MmsParamInfo)

Aggregations

ArrayList (java.util.ArrayList)4 Execute (oms3.annotations.Execute)4 File (java.io.File)3 IOException (java.io.IOException)3 Envelope (org.locationtech.jts.geom.Envelope)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 FileOutputStream (java.io.FileOutputStream)2 DataflowEvent (oms3.Notification.DataflowEvent)2 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)2 ModelsIOException (org.hortonmachine.gears.libs.exceptions.ModelsIOException)2 RegionMap (org.hortonmachine.gears.utils.RegionMap)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 Geometry (org.locationtech.jts.geom.Geometry)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 AffineTransform (java.awt.geom.AffineTransform)1 RenderedImage (java.awt.image.RenderedImage)1 WritableRaster (java.awt.image.WritableRaster)1 InputStream (java.io.InputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1