javax.media.jai.iterator.RandomIter Java Examples

The following examples show how to use javax.media.jai.iterator.RandomIter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Transform a double values rendered image in its integer array representation by scaling the values.
 * 
 * @param renderedImage the rendered image to transform.
 * @param multiply value by which to multiply the values before casting to integer.
 * @return the array holding the data.
 */
public static int[] renderedImage2IntegerArray( RenderedImage renderedImage, double multiply ) {
    int width = renderedImage.getWidth();
    int height = renderedImage.getHeight();

    int[] values = new int[width * height];
    RandomIter imageIter = RandomIterFactory.create(renderedImage, null);
    int index = 0;;
    for( int x = 0; x < width; x++ ) {
        for( int y = 0; y < height; y++ ) {
            double sample = imageIter.getSampleDouble(x, y, 0);
            sample = sample * multiply;
            values[index++] = (int) sample;
        }
    }
    imageIter.done();
    return values;
}
 
Example #2
Source File: BinaryFast.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public BinaryFast( RenderedImage renderedImage ) {
    width = renderedImage.getWidth();
    height = renderedImage.getHeight();
    pixels = new int[width][height]; // take care, this matrix is in cols/rows mode
    size = width * height;

    RandomIter iter = RandomIterFactory.create(renderedImage, null);
    for( int i = 0; i < pixels.length; i++ ) {
        for( int j = 0; j < pixels[0].length; j++ ) {
            double sample = iter.getSampleDouble(i, j, 0);
            if (sample == 1) {
                pixels[i][j] = 1;
            } else {
                pixels[i][j] = 0;
            }
        }
    }

    generateForegroundEdge();
    generateBackgroundEdgeFromForegroundEdge();
}
 
Example #3
Source File: OmsDebrisVandre.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Moves the flowDirColRow variable to the next trigger point.
 * 
 * @param triggerIter
 * @param flowDirColRow
 * @return <code>true</code> if a new trigger was found, <code>false</code> if the exit was reached.
 */
private boolean moveToNextTriggerpoint( RandomIter triggerIter, RandomIter flowIter, int[] flowDirColRow ) {
    double tmpFlowValue = flowIter.getSampleDouble(flowDirColRow[0], flowDirColRow[1], 0);
    if (tmpFlowValue == 10) {
        return false;
    }
    if (!ModelsEngine.go_downstream(flowDirColRow, tmpFlowValue))
        throw new ModelsIllegalargumentException("Unable to go downstream: " + flowDirColRow[0] + "/" + flowDirColRow[1],
                this, pm);
    while( isNovalue(triggerIter.getSampleDouble(flowDirColRow[0], flowDirColRow[1], 0)) ) {
        tmpFlowValue = flowIter.getSampleDouble(flowDirColRow[0], flowDirColRow[1], 0);
        if (tmpFlowValue == 10) {
            return false;
        }
        if (!ModelsEngine.go_downstream(flowDirColRow, tmpFlowValue))
            throw new ModelsIllegalargumentException("Unable to go downstream: " + flowDirColRow[0] + "/" + flowDirColRow[1],
                    this, pm);
    }
    return true;
}
 
Example #4
Source File: HMTestCase.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
protected void checkMatrixEqual( Raster image, double[][] matrix ) {
    assertEquals("different dimension", image.getHeight(), matrix.length);
    assertEquals("different dimension", image.getWidth(), matrix[0].length);

    RandomIter randomIter = RandomIterFactory.create(image, null);
    int minX = image.getMinX();
    int minY = image.getMinY();

    for( int j = minY; j < minY + image.getHeight(); j++ ) {
        for( int i = minX; i < minX + image.getWidth(); i++ ) {
            double expectedResult = matrix[i - minX][j - minY];
            double value = randomIter.getSampleDouble(i, j, 0);
            if (isNovalue(value)) {
                assertTrue("Difference at position: " + i + " " + j, isNovalue(expectedResult));
            } else {
                assertEquals("Difference at position: " + i + " " + j, expectedResult, value);
            }
        }
    }

}
 
Example #5
Source File: ModelsEngine.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Takes a input raster and vectorializes it.
 *
 * @param input
 * @return
 */
public static double[] vectorizeDoubleMatrix( RenderedImage input ) {
    double[] U = new double[input.getWidth() * input.getHeight()];
    RandomIter inputRandomIter = RandomIterFactory.create(input, null);

    int j = 0;
    for( int i = 0; i < input.getHeight() * input.getWidth(); i = i + input.getWidth() ) {
        double tmp[] = new double[input.getWidth()];
        for( int k = 0; k < input.getWidth(); k++ ) {
            tmp[k] = inputRandomIter.getSampleDouble(k, j, 0);
        }

        System.arraycopy(tmp, 0, U, i, input.getWidth());
        j++;
    }

    return U;
}
 
Example #6
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Transform a rendered image in its array representation.
 * 
 * @param renderedImage the rendered image to transform.
 * @param bandNumber the the number of the band to consider..
 * @return the array holding the data.
 */
public static double[] renderedImage2DoubleArray( RenderedImage renderedImage, int bandNumber ) {
    int width = renderedImage.getWidth();
    int height = renderedImage.getHeight();

    double[] values = new double[width * height];
    RandomIter imageIter = RandomIterFactory.create(renderedImage, null);
    int index = 0;;
    for( int x = 0; x < width; x++ ) {
        for( int y = 0; y < height; y++ ) {
            double sample = imageIter.getSampleDouble(x, y, bandNumber);
            values[index++] = sample;
        }
    }
    imageIter.done();
    return values;
}
 
Example #7
Source File: RiverSectionsFromDtmExtractor.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Extract a {@link RiverPoint}.
 * 
 * @param riverLine the geometry of the main river.
 * @param elevIter the elevation raster.
 * @param gridGeometry the raster geometry.
 * @param progressiveDistance the progressive distance along the main river.
 * @param ks the KS for the section.
 * @param leftPoint the left point of the section.
 * @param rightPoint the right point of the section.
 * @return the created {@link RiverPoint}.
 * @throws TransformException
 */
private RiverPoint getNetworkPoint( LineString riverLine, RandomIter elevIter, GridGeometry2D gridGeometry,
        double progressiveDistance, Double ks, Coordinate leftPoint, Coordinate rightPoint ) throws TransformException {
    List<ProfilePoint> sectionPoints = CoverageUtilities.doProfile(elevIter, gridGeometry, rightPoint, leftPoint);
    List<Coordinate> coordinate3dList = new ArrayList<Coordinate>();
    for( ProfilePoint sectionPoint : sectionPoints ) {
        Coordinate position = sectionPoint.getPosition();
        position.z = sectionPoint.getElevation();
        coordinate3dList.add(position);
    }
    LineString sectionLine3d = gf.createLineString(coordinate3dList.toArray(new Coordinate[0]));
    Geometry crossPoint = sectionLine3d.intersection(riverLine);
    Coordinate coordinate = crossPoint.getCoordinate();
    if (coordinate == null) {
        return null;
    }
    int[] colRow = CoverageUtilities.colRowFromCoordinate(coordinate, gridGeometry, null);
    double elev = elevIter.getSampleDouble(colRow[0], colRow[1], 0);
    coordinate.z = elev;
    RiverPoint netPoint = new RiverPoint(coordinate, progressiveDistance, sectionLine3d, ks);
    return netPoint;
}
 
Example #8
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Replace the current internal novalue with a given value.
 * 
 * @param renderedImage a {@link RenderedImage}.
 * @param newValue the value to put in instead of the novalue.
 * @return the rendered image with the substituted novalue. 
 */
public static WritableRaster replaceNovalue( RenderedImage renderedImage, double newValue ) {
    WritableRaster tmpWR = (WritableRaster) renderedImage.getData();
    RandomIter pitTmpIterator = RandomIterFactory.create(renderedImage, null);

    int height = renderedImage.getHeight();
    int width = renderedImage.getWidth();
    for( int y = 0; y < height; y++ ) {
        for( int x = 0; x < width; x++ ) {
            if (isNovalue(pitTmpIterator.getSampleDouble(x, y, 0))) {
                tmpWR.setSample(x, y, 0, newValue);
            }
        }
    }
    pitTmpIterator.done();
    return tmpWR;
}
 
Example #9
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public static GridCoverage2D invert( GridCoverage2D raster, double max ) {
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(raster);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();

    RandomIter rasterIter = CoverageUtilities.getRandomIterator(raster);

    WritableRaster outWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    for( int r = 0; r < nRows; r++ ) {
        for( int c = 0; c < nCols; c++ ) {
            double value = rasterIter.getSampleDouble(c, r, 0);
            if (!isNovalue(value)) {
                outIter.setSample(c, r, 0, max - value);
            }
        }
    }
    GridCoverage2D invertedRaster = CoverageUtilities.buildCoverage("inverted", outWR, regionMap,
            raster.getCoordinateReferenceSystem());
    return invertedRaster;
}
 
Example #10
Source File: PrintUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Print data from a {@link RenderedImage}.
 * 
 * @param renderedImage
 *            the image.
 */
public static void printRenderedImageData( RenderedImage renderedImage ) {
    RandomIter iter = RandomIterFactory.create(renderedImage, null);
    int cols = renderedImage.getWidth();
    int rows = renderedImage.getHeight();
    int numBands = renderedImage.getSampleModel().getNumBands();
    for( int r = 0; r < rows; r++ ) {
        for( int c = 0; c < cols; c++ ) {
            for( int b = 0; b < numBands; b++ ) {
                if (b > 0) {
                    printer.print("/");
                }
                printer.print(iter.getSampleDouble(c, r, b));
            }
            printer.print(separator);
        }
        printer.println();
    }
    iter.done();
}
 
Example #11
Source File: OmsRescaledDistance.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
private void calculateRescaledDistance( FlowNode runningNode, float distance, WritableRandomIter rescaledIter,
        RandomIter elevIter, RandomIter netIter ) {
    runningNode.setFloatValueInMap(rescaledIter, distance);
    if (runningNode.getEnteringNodes().size() > 0) {
        List<FlowNode> enteringNodes = runningNode.getEnteringNodes();
        for( FlowNode enteringNode : enteringNodes ) {
            double tmpDistance = Direction.forFlow((int) enteringNode.flow).getDistance(xRes, yRes);
            if (elevIter != null) {
                double fromElev = enteringNode.getValueFromMap(elevIter);
                double toElev = runningNode.getValueFromMap(elevIter);
                tmpDistance = NumericsUtilities.pythagoras(tmpDistance, abs(toElev - fromElev));
            }

            int netValue = enteringNode.getIntValueFromMap(netIter);
            double newDistance = 0.0;
            if (isNovalue(netValue)) {
                newDistance = distance + tmpDistance * pRatio;
            } else {
                newDistance = distance + tmpDistance;
            }
            calculateRescaledDistance(enteringNode, (float) newDistance, rescaledIter, elevIter, netIter);
        }
    }

}
 
Example #12
Source File: OmsNabla.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
@Execute
public void process() {
    if (!concatOr(outNabla == null, doReset)) {
        return;
    }
    checkNull(inElev);
    HashMap<String, Double> regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    nCols = regionMap.get(CoverageUtilities.COLS).intValue();
    nRows = regionMap.get(CoverageUtilities.ROWS).intValue();
    xRes = regionMap.get(CoverageUtilities.XRES);
    yRes = regionMap.get(CoverageUtilities.YRES);

    RenderedImage elevationRI = inElev.getRenderedImage();
    RandomIter elevationIter = RandomIterFactory.create(elevationRI, null);

    WritableRaster gradientWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    if (pThreshold == null) {
        nabla(elevationIter, gradientWR);
    } else {
        nabla_mask(elevationIter, gradientWR, pThreshold);
    }

    outNabla = CoverageUtilities.buildCoverage("nabla", gradientWR, regionMap, inElev.getCoordinateReferenceSystem());
}
 
Example #13
Source File: OmsHackLength.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Compare two value of tca and distance.
 * 
 * <p>
 * It's used to evaluate some special distance (as hacklength). 
 * In these case, the value of the distance is a property of 
 * the path, and so when two pixel drain in a same pixel the 
 * actual value is calculate from the pixel that have the 
 * maximum value. So this method evaluate if the distance is 
 * already evaluate, throghout another path, and 
 * if the value of the old path is greater than the next path.
 * </p>
 */
public static boolean tcaMax( FlowNode flowNode, RandomIter tcaIter, RandomIter hacklengthIter, double maxTca,
        double maxDistance ) {

    List<FlowNode> enteringNodes = flowNode.getEnteringNodes();
    for( Node node : enteringNodes ) {
        double tca = node.getValueFromMap(tcaIter);
        if (tca >= maxTca) {
            if (NumericsUtilities.dEq(tca, maxTca)) {
                if (node.getValueFromMap(hacklengthIter) > maxDistance)
                    return false;
            } else
                return false;
        }

    }
    return true;
}
 
Example #14
Source File: OmsGeomorphon.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
private static void calculateCount( RandomIter elevIter, GridGeometry2D gridGeometry, int[] plusCount, int[] minusCount,
        double elevation, Coordinate center, Coordinate otherCoordinate, double angleThreshold ) throws TransformException {
    List<ProfilePoint> profile = CoverageUtilities.doProfile(elevIter, gridGeometry, center, otherCoordinate);
    double[] lastVisiblePointData = ProfilePoint.getLastVisiblePointData(profile);

    if (lastVisiblePointData != null) {
        double zenithAngle = lastVisiblePointData[4];
        double nadirAngle = 180 - lastVisiblePointData[9];

        double diff = nadirAngle - zenithAngle;
        int zeroCount = 0;
        if (diff > angleThreshold) {
            plusCount[0] = plusCount[0] + 1;
        } else if (diff < -angleThreshold) {
            minusCount[0] = minusCount[0] + 1;
        } else if (abs(diff) < angleThreshold) {
            zeroCount++;
        } else {
            throw new IllegalArgumentException();
        }
    }
}
 
Example #15
Source File: OmsGradient.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public static double doGradientDiffOnCell( RandomIter elevationIter, int x, int y, double xRes, double yRes,
        boolean doDegrees ) {
    // extract the value to use for the algoritm. It is the finite difference approach.
    double elevIJ = elevationIter.getSampleDouble(x, y, 0);
    double elevIJipre = elevationIter.getSampleDouble(x - 1, y, 0);
    double elevIJipost = elevationIter.getSampleDouble(x + 1, y, 0);
    double elevIJjpre = elevationIter.getSampleDouble(x, y - 1, 0);
    double elevIJjpost = elevationIter.getSampleDouble(x, y + 1, 0);
    if (isNovalue(elevIJ) || isNovalue(elevIJipre) || isNovalue(elevIJipost) || isNovalue(elevIJjpre)
            || isNovalue(elevIJjpost)) {
        return doubleNovalue;
    } else if (!isNovalue(elevIJ) && !isNovalue(elevIJipre) && !isNovalue(elevIJipost) && !isNovalue(elevIJjpre)
            && !isNovalue(elevIJjpost)) {
        double xGrad = 0.5 * (elevIJipost - elevIJipre) / xRes;
        double yGrad = 0.5 * (elevIJjpre - elevIJjpost) / yRes;
        double grad = sqrt(pow(xGrad, 2) + pow(yGrad, 2));
        if (doDegrees) {
            grad = transform(grad);
        }
        return grad;
    } else {
        throw new ModelsIllegalargumentException("Error in gradient", "GRADIENT");
    }
}
 
Example #16
Source File: OmsCurvaturesIM.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void processCell( int readCol, int readRow, int writeCol, int writeRow, int readCols, int readRows, int writeCols,
        int writeRows ) {

    RandomIter elevIter = inRasterIterators.get(0);

    GridNode node = new GridNode(elevIter, readCols, readRows, xRes, yRes, readCol, readRow);
    OmsCurvatures.calculateCurvatures2(node, planTangProf);
    if (outPlan != null)
        outRasterIterators.get(0).setSample(writeCol, writeRow, 0, planTangProf[0]);
    if (outTang != null)
        outRasterIterators.get(1).setSample(writeCol, writeRow, 0, planTangProf[1]);
    if (outProf != null)
        outRasterIterators.get(2).setSample(writeCol, writeRow, 0, planTangProf[2]);

}
 
Example #17
Source File: OmsRasterNormalizer.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
@Execute
public void process() throws Exception {
    checkNull(inRaster);

    double[] minMax = OmsRasterSummary.getMinMax(inRaster);

    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();

    RandomIter rasterIter = CoverageUtilities.getRandomIterator(inRaster);

    WritableRaster outWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);

    pm.beginTask("Normalizing...", nRows);
    for( int r = 0; r < nRows; r++ ) {
        if (pm.isCanceled()) {
            return;
        }
        for( int c = 0; c < nCols; c++ ) {
            double value = rasterIter.getSampleDouble(c, r, 0);
            if (isNovalue(value)) {
                if (doSetnovalues) {
                    outIter.setSample(c, r, 0, 0.0);
                }
                continue;
            }
            double normalizedValue = NumericsUtilities.normalize(minMax[1], minMax[0], value, pNValue);
            outIter.setSample(c, r, 0, normalizedValue);
        }
        pm.worked(1);
    }
    pm.done();

    outRaster = CoverageUtilities.buildCoverage("normalized", outWR, regionMap, inRaster.getCoordinateReferenceSystem());

}
 
Example #18
Source File: Node.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the int value of another map in the current node position.
 * 
 * @param map the map from which to get the value. 
 * @return the int value or a novalue.
 */
public int getIntValueFromMap( RandomIter map ) {
    try {
        if (map == null) {
            return HMConstants.intNovalue;
        }
        int value = map.getSample(col, row, 0);
        return value;
    } catch (Exception e) {
        // ignore and return novalue
        return HMConstants.intNovalue;
    }
}
 
Example #19
Source File: OmsGeomorphonIM.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void processCell( int readCol, int readRow, int writeCol, int writeRow, int readCols, int readRows, int writeCols,
        int writeRows ) {
    try {
        RandomIter elevIter = inRasterIterators.get(0);
        double classification = OmsGeomorphon.calculateGeomorphon(elevIter, readGridGeometry, pRadius, pThreshold,
                diagonalDelta, readCol, readRow);
        WritableRandomIter outDataIter = outRasterIterators.get(0);
        outDataIter.setSample(writeCol, writeRow, 0, classification);
    } catch (TransformException e) {
        e.printStackTrace();
    }
}
 
Example #20
Source File: OmsExtractNetwork.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * this method calculates the network using a threshold value on the
 * contributing areas or on magnitudo
 * @throws Exception 
 */
private WritableRaster extractNetTcaThreshold( RenderedImage tcaRI ) throws Exception {
    RandomIter tcaIter = RandomIterFactory.create(tcaRI, null);
    WritableRaster netWR = CoverageUtilities.createWritableRaster(cols, rows, Short.class, null, shortNovalue);
    WritableRandomIter netIter = RandomIterFactory.createWritable(netWR, null);

    try {
        pm.beginTask(msg.message("extractnetwork.extracting"), rows * cols); //$NON-NLS-1$
        processGrid(cols, rows, false, ( c, r ) -> {
            if (pm.isCanceled()) {
                return;
            }
            int tcaValue = tcaIter.getSample(c, r, 0);
            if (!isNovalue(tcaValue)) {
                if (tcaValue >= pThres) { // FIXME needs power here?
                    netIter.setSample(c, r, 0, NETVALUE);
                }
            }
            pm.worked(1);
        });
        pm.done();
        return netWR;
    } finally {
        netIter.done();
        tcaIter.done();
    }
}
 
Example #21
Source File: OmsHackLength.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
@Execute
public void process() {
    if (!concatOr(outHacklength == null, doReset)) {
        return;
    }
    checkNull(inFlow, inTca);

    regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    nCols = regionMap.getCols();
    nRows = regionMap.getRows();
    xRes = regionMap.getXres();
    yRes = regionMap.getYres();

    RandomIter tcaIter = CoverageUtilities.getRandomIterator(inTca);

    RenderedImage flowRI = inFlow.getRenderedImage();
    WritableRaster flowWR = CoverageUtilities.renderedImage2WritableRaster(flowRI, true);
    WritableRandomIter flowIter = RandomIterFactory.createWritable(flowWR, null);

    // if inElevation isn't null then work in 3d.
    RandomIter elevIter = null;
    if (inElevation != null) {
        elevIter = CoverageUtilities.getRandomIterator(inElevation);
    }

    hacklength(flowIter, tcaIter, elevIter);

    tcaIter.done();
    flowIter.done();
    if (elevIter != null) {
        elevIter.done();
    }

}
 
Example #22
Source File: OmsGeomorphon.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
@Execute
public void process() throws Exception {
    checkNull(inElev);

    if (pRadius <= 0) {
        throw new ModelsIllegalargumentException("The search radius has to be > 0.", this, pm);
    }

    final double diagonalDelta = pRadius / sqrt(2.0);

    final RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    int cols = regionMap.getCols();
    final int rows = regionMap.getRows();

    final RandomIter elevIter = CoverageUtilities.getRandomIterator(inElev);
    final GridGeometry2D gridGeometry = inElev.getGridGeometry();

    WritableRaster[] outWRHolder = new WritableRaster[1];
    outRaster = CoverageUtilities.createCoverageFromTemplate(inElev, HMConstants.doubleNovalue, outWRHolder);
    final WritableRandomIter outIter = CoverageUtilities.getWritableRandomIterator(outWRHolder[0]);

    pm.beginTask("Calculate classes...", cols);
    for( int r = 0; r < rows; r++ ) {
        for( int c = 0; c < cols; c++ ) {
            try {
                double classification = calculateGeomorphon(elevIter, gridGeometry, pRadius, pThreshold, diagonalDelta, c, r);
                outIter.setSample(c, r, 0, classification);
            } catch (TransformException e) {
                e.printStackTrace();
            }
        }
        pm.worked(1);
    }
    pm.done();

}
 
Example #23
Source File: ModelsEngine.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Extract the subbasins of a raster map.
 *
 * @param flowIter the map of flowdirections.
 * @param netIter the network map.
 * @param netNumberIter the netnumber map.
 * @param rows rows of the region.
 * @param cols columns of the region.
 * @param pm
 * @return the map of extracted subbasins.
 */
public static WritableRaster extractSubbasins( WritableRandomIter flowIter, RandomIter netIter,
        WritableRandomIter netNumberIter, int rows, int cols, IHMProgressMonitor pm ) {

    for( int r = 0; r < rows; r++ ) {
        for( int c = 0; c < cols; c++ ) {
            if (!isNovalue(netIter.getSampleDouble(c, r, 0)))
                flowIter.setSample(c, r, 0, FlowNode.OUTLET);
        }
    }

    WritableRaster subbasinWR = CoverageUtilities.createWritableRaster(cols, rows, Integer.class, null, null);
    WritableRandomIter subbasinIter = RandomIterFactory.createWritable(subbasinWR, null);

    markHillSlopeWithLinkValue(flowIter, netNumberIter, subbasinIter, cols, rows, pm);

    try {
        for( int r = 0; r < rows; r++ ) {
            for( int c = 0; c < cols; c++ ) {
                int netValue = netIter.getSample(c, r, 0);
                int netNumberValue = netNumberIter.getSample(c, r, 0);
                if (!isNovalue(netValue)) {
                    subbasinIter.setSample(c, r, 0, netNumberValue);
                }
                if (NumericsUtilities.dEq(netNumberValue, 0)) {
                    netNumberIter.setSample(c, r, 0, HMConstants.intNovalue);
                }
                int subbValue = subbasinIter.getSample(c, r, 0);
                if (NumericsUtilities.dEq(subbValue, 0))
                    subbasinIter.setSample(c, r, 0, HMConstants.intNovalue);
            }
        }
    } finally {
        subbasinIter.done();
    }

    return subbasinWR;
}
 
Example #24
Source File: OmsCurvaturesBivariate.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculate curvatures for a single cell.
 * 
 * @param elevationIter the elevation map.
 * @param planTangProf the array into which to insert the resulting [plan, tang, prof] curvatures.
 * @param col the column to process.
 * @param row the row to process.
 * @param ncols the columns of the raster.
 * @param nrows the rows of the raster.
 * @param xRes 
 * @param yRes
 * @param disXX the diagonal size of the cell, x component.
 * @param disYY the diagonal size of the cell, y component.
 */
public static void calculateCurvatures( RandomIter elevationIter, final double[] planTangProf, int ncols, int nrows, int col,
        int row, double xRes, double yRes, double disXX, double disYY, int windowSize ) {

    GridNode node = new GridNode(elevationIter, ncols, nrows, xRes, yRes, col, row);
    double[][] window = node.getWindow(windowSize, false);
    if (!hasNovalues(window)) {
        double[] parameters = calculateParameters(window);
        double a = parameters[0];
        double b = parameters[1];
        double c = parameters[2];
        double d = parameters[3];
        double e = parameters[4];

        double slope = atan(sqrt(d * d + e * e));
        slope = toDegrees(slope);
        double aspect = atan(e / d);
        aspect = toDegrees(aspect);

        double profcNumerator = -200.0 * (a * d * d + b * e * e + c * d * e);
        double profcDenominator = (e * e + d * d) * pow((1 + e * e + d * d), 1.5);
        double profc = profcNumerator / profcDenominator;

        double plancNumerator = -200.0 * (b * d * d + a * e * e + c * d * e);
        double plancDenominator = pow((e * e + d * d), 1.5);
        double planc = plancNumerator / plancDenominator;

        planTangProf[0] = planc;
        planTangProf[1] = profc;
        planTangProf[2] = slope;
        planTangProf[3] = aspect;
    } else {
        planTangProf[0] = doubleNovalue;
        planTangProf[1] = doubleNovalue;
        planTangProf[2] = doubleNovalue;
        planTangProf[3] = doubleNovalue;
    }
}
 
Example #25
Source File: OmsHillshade.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
protected WritableRaster normalVector( WritableRaster pitWR, double res ) {
    int minX = pitWR.getMinX();
    int minY = pitWR.getMinY();
    int rows = pitWR.getHeight();
    int cols = pitWR.getWidth();

    RandomIter pitIter = RandomIterFactory.create(pitWR, null);
    /*
     * Initialize the Image of the normal vector in the central point of the
     * cells, which have 3 components so the Image have 3 bands..
     */
    SampleModel sm = RasterFactory.createBandedSampleModel(5, cols, rows, 3);
    WritableRaster tmpNormalVectorWR = CoverageUtilities.createWritableRaster(cols, rows, null, sm, 0.0);
    WritableRandomIter tmpNormaIter = RandomIterFactory.createWritable(tmpNormalVectorWR, null);
    /*
     * apply the corripio's formula (is the formula (3) in the article)
     */
    for( int j = minY; j < minX + rows - 1; j++ ) {
        for( int i = minX; i < minX + cols - 1; i++ ) {
            double zij = pitIter.getSampleDouble(i, j, 0);
            double zidxj = pitIter.getSampleDouble(i + 1, j, 0);
            double zijdy = pitIter.getSampleDouble(i, j + 1, 0);
            double zidxjdy = pitIter.getSampleDouble(i + 1, j + 1, 0);
            double firstComponent = 0.5 * res * (zij - zidxj + zijdy - zidxjdy);
            double secondComponent = 0.5 * res * (zij + zidxj - zijdy - zidxjdy);
            double thirthComponent = (res * res);
            double den = Math.sqrt(firstComponent * firstComponent + secondComponent * secondComponent + thirthComponent
                    * thirthComponent);
            tmpNormaIter.setPixel(i, j, new double[]{firstComponent / den, secondComponent / den, thirthComponent / den});

        }
    }
    pitIter.done();

    return tmpNormalVectorWR;

}
 
Example #26
Source File: OmsDrainDir.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private void calculateDrainageArea( int row, int col, double[] dati, BitMatrix analizedMatrix, WritableRandomIter deviation,
        RandomIter pitRandomIter, WritableRandomIter tcaRandomIter, WritableRandomIter dirRandomIter, int nCols, int nRows ) {
    double[] dev = new double[8];
    int[] are = new int[8];

    int ninflow = 0;
    double sumdev = 0;
    for( int n = 1; n <= 8; n++ ) {
        int conta = (col + order[n][1] - 1) * nCols + row + order[n][0];
        /*
         * verifico se la cella che sto considerando e' stata gia' processata
         */
        if (analizedMatrix.isMarked(col + order[n][1], row + order[n][0])) {
            if (!isNovalue(pitRandomIter.getSampleDouble(col + order[n][1], row + order[n][0], 0))
                    || conta <= nRows * nCols) {
                int outdir = dirRandomIter.getSample(col + order[n][1], row + order[n][0], 0);
                /*
                 * verifico se la cella che sto considerando drena nel pixel
                 * centrale
                 */
                if (outdir - n == 4 || outdir - n == -4) {
                    ninflow = ninflow + 1;
                    tcaRandomIter.setSample(col, row, 0, tcaRandomIter.getSample(col, row, 0)
                            + tcaRandomIter.getSample(col + order[n][1], row + order[n][0], 0));
                    dev[ninflow] = deviation.getSampleDouble(col + order[n][1], row + order[n][0], 0);
                    are[ninflow] = tcaRandomIter.getSample(col + order[n][1], row + order[n][0], 0);
                }
            }
        }
    }

    for( int i = 1; i <= ninflow; i++ ) {
        sumdev = sumdev + are[i] * dev[i] / tcaRandomIter.getSample(col, row, 0);
    }
    dati[6] = sumdev;

}
 
Example #27
Source File: OmsGradient.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
@Execute
public void process() throws Exception {
    if (!concatOr(outSlope == null, doReset)) {
        return;
    }

    checkNull(inElev);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    nCols = regionMap.getCols();
    nRows = regionMap.getRows();
    xRes = regionMap.getXres();
    yRes = regionMap.getYres();

    RenderedImage elevationRI = inElev.getRenderedImage();
    RandomIter elevationIter = RandomIterFactory.create(elevationRI, null);

    WritableRaster gradientWR;
    try {
        gradientWR = null;
        if (pMode.equals(HORN)) {
            gradientWR = gradientHorn(elevationIter);
        } else if (pMode.equals(EVANS)) {
            gradientWR = gradientEvans(elevationIter);
        } else {
            gradientWR = gradientDiff(elevationIter);
        }
        outSlope = CoverageUtilities.buildCoverage("gradient", gradientWR, regionMap, inElev.getCoordinateReferenceSystem());
    } finally {
        elevationIter.done();
    }
}
 
Example #28
Source File: Node.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the double value of another map in the current node position.
 * 
 * @param map the map from which to get the value. 
 * @return the double value or a novalue.
 */
public double getDoubleValueFromMap( RandomIter map ) {
    try {
        if (map == null) {
            return HMConstants.doubleNovalue;
        }
        double value = map.getSampleDouble(col, row, 0);
        return value;
    } catch (Exception e) {
        // ignore and return novalue
        return HMConstants.doubleNovalue;
    }
}
 
Example #29
Source File: Node.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the float value of another map in the current node position.
 * 
 * @param map the map from which to get the value. 
 * @return the float value or a novalue.
 */
public float getFloatValueFromMap( RandomIter map ) {
    try {
        if (map == null) {
            return HMConstants.floatNovalue;
        }
        float value = map.getSampleFloat(col, row, 0);
        return value;
    } catch (Exception e) {
        // ignore and return novalue
        return HMConstants.floatNovalue;
    }
}
 
Example #30
Source File: ModelsEngine.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Verifies if the point is a source pixel in the supplied flow raster.
 *
 * @param flowIter the {@link RandomIter iterator} of the flowdirections.
 * @param col the col of the point to check.
 * @param row the row of the point to check.
 * @return true if the point identified by col and row is a source pixel.
 */
public static boolean isSourcePixel( RandomIter flowIter, int col, int row ) {
    double flowDirection = flowIter.getSampleDouble(col, row, 0);
    if (flowDirection < 9.0 && flowDirection > 0.0) {
        for( int k = 1; k <= 8; k++ ) {
            if (flowIter.getSampleDouble(col + dirIn[k][1], row + dirIn[k][0], 0) == dirIn[k][2]) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}