Java Code Examples for java.awt.image.WritableRaster#getSampleDouble()

The following examples show how to use java.awt.image.WritableRaster#getSampleDouble() . 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
/**
 * Checks two rasters for equality of content.
 * 
 * @param wr1 the first raster.
 * @param wr2 the second raster.
 * @return <code>true</code>, if the rasters contain the same values.
 */
public static boolean equals( WritableRaster wr1, WritableRaster wr2 ) {
    int w1 = wr1.getWidth();
    int h1 = wr1.getHeight();
    int w2 = wr2.getWidth();
    int h2 = wr2.getHeight();

    if (w1 != w2 || h1 != h2) {
        return false;
    }
    for( int c = 0; c < w1; c++ ) {
        for( int r = 0; r < h1; r++ ) {
            double v1 = wr1.getSampleDouble(c, r, 0);
            double v2 = wr2.getSampleDouble(c, r, 0);
            if (isNovalue(v1) && isNovalue(v2)) {
                continue;
            }
            if (!NumericsUtilities.dEq(v1, v2)) {
                return false;
            }
        }
    }
    return true;
}
 
Example 2
Source File: SimpleAbstractMergeStrategy.java    From geowave with Apache License 2.0 6 votes vote down vote up
protected void mergeRasters(
    final RasterTile<T> thisTile,
    final RasterTile<T> nextTile,
    final WritableRaster thisRaster,
    final WritableRaster nextRaster) {
  final int maxX = nextRaster.getMinX() + nextRaster.getWidth();
  final int maxY = nextRaster.getMinY() + nextRaster.getHeight();
  for (int b = 0; b < nextRaster.getNumBands(); b++) {
    for (int x = nextRaster.getMinX(); x < maxX; x++) {
      for (int y = nextRaster.getMinY(); y < maxY; y++) {
        final double thisSample = thisRaster.getSampleDouble(x, y, b);

        final double nextSample = nextRaster.getSampleDouble(x, y, b);
        thisRaster.setSample(x, y, b, getSample(x, y, b, thisSample, nextSample));
      }
    }
  }
}
 
Example 3
Source File: OmsInsolation.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
private void calcRadiation( int i, int j, WritableRaster demWR, WritableRaster sOmbraWR, WritableRaster insolationWR,
        double[] sunVector, WritableRaster gradientWR, double mr ) {
    double z = demWR.getSampleDouble(i, j, 0);
    double pressure = ATM * Math.exp(-0.0001184 * z);
    double ma = mr * pressure / ATM;
    double temp = 273 + pLapse * (z - 4000);
    double vap_psat = Math.exp(26.23 - 5416.0 / temp);
    double wPrec = 0.493 * pRH * vap_psat / temp;
    double taur = Math.exp((-.09030 * Math.pow(ma, 0.84)) * (1.0 + ma - Math.pow(ma, 1.01)));
    double d = pCmO3 * mr;
    double tauo = 1 - (0.1611 * d * Math.pow(1.0 + 139.48 * d, -0.3035) - 0.002715 * d)
            / (1.0 + 0.044 * d + 0.0003 * Math.pow(d, 2));
    double taug = Math.exp(-0.0127 * Math.pow(ma, 0.26));
    double tauw = 1 - 2.4959 * (wPrec * mr) / (1.0 + 79.034 * (wPrec * mr) * 0.6828 + 6.385 * (wPrec * mr));
    double taua = Math.pow((0.97 - 1.265 * Math.pow(pVisibility, (-0.66))), Math.pow(ma, 0.9));

    double In = 0.9751 * SOLARCTE * taur * tauo * taug * tauw * taua;

    double cosinc = scalarProduct(sunVector, gradientWR.getPixel(i, j, new double[3]));

    if (cosinc < 0) {
        cosinc = 0;
    }
    double tmp = insolationWR.getSampleDouble(i, j, 0);
    insolationWR.setSample(i, j, 0, In * cosinc * sOmbraWR.getSampleDouble(i, j, 0) / 1000 + tmp);
}
 
Example 4
Source File: OmsHillshade.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
private void setNoValueBorder( WritableRaster pitWR, int width, int height, WritableRaster hillshadeWR ) {
    for( int y = 2; y < height - 2; y++ ) {
        for( int x = 2; x < width - 2; x++ ) {
            if (pitWR.getSampleDouble(x, y, 0) == -9999.0) {
                hillshadeWR.setSample(x, y, 0, doubleNoValue);
            }
        }
    }

    // maybe NaN instead of 0
    for( int y = 0; y < height; y++ ) {
        hillshadeWR.setSample(0, y, 0, 0);
        hillshadeWR.setSample(1, y, 0, 0);
        hillshadeWR.setSample(width - 2, y, 0, 0);
        hillshadeWR.setSample(width - 1, y, 0, 0);
    }

    for( int x = 2; x < width - 2; x++ ) {
        hillshadeWR.setSample(x, 0, 0, 0);
        hillshadeWR.setSample(x, 1, 0, 0);
        hillshadeWR.setSample(x, height - 2, 0, 0);
        hillshadeWR.setSample(x, height - 1, 0, 0);
    }
}
 
Example 5
Source File: GeoWaveBasicRasterIT.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void merge(
    final RasterTile<MergeCounter> thisTile,
    final RasterTile<MergeCounter> nextTile,
    final SampleModel sampleModel) {
  if (nextTile instanceof ServerMergeableRasterTile) {
    final WritableRaster nextRaster =
        Raster.createWritableRaster(sampleModel, nextTile.getDataBuffer(), null);
    final WritableRaster thisRaster =
        Raster.createWritableRaster(sampleModel, thisTile.getDataBuffer(), null);
    final MergeCounter mergeCounter = thisTile.getMetadata();
    // we're merging, this is the incremented new number of merges
    final int newNumMerges =
        mergeCounter.getNumMerges() + nextTile.getMetadata().getNumMerges() + 1;

    // we've merged 1 more tile than the total number of merges (ie.
    // if we've performed 1 merge, we've seen 2 tiles)
    final int totalTiles = newNumMerges + 1;
    final int maxX = nextRaster.getMinX() + nextRaster.getWidth();
    final int maxY = nextRaster.getMinY() + nextRaster.getHeight();
    for (int x = nextRaster.getMinX(); x < maxX; x++) {
      for (int y = nextRaster.getMinY(); y < maxY; y++) {
        for (int b = 0; (b + 1) < nextRaster.getNumBands(); b += 2) {
          final double thisSample = thisRaster.getSampleDouble(x, y, b);
          final double nextSample = nextRaster.getSampleDouble(x, y, b);

          final double sum = thisSample + nextSample;
          final double average = sum / totalTiles;
          thisRaster.setSample(x, y, b, sum);
          thisRaster.setSample(x, y, b + 1, average);
        }
      }
    }
    thisTile.setMetadata(new MergeCounter(newNumMerges));
  }
}
 
Example 6
Source File: RasterReader.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates the rgb image symbol.
 *
 * @param sym the sym
 * @param cov the cov
 * @param raster the raster
 */
private void createRGBImageSymbol(
        RasterSymbolizer sym, GridCoverage2D cov, WritableRaster raster) {
    double dest;
    List<Double> valueList = new ArrayList<>();

    GridEnvelope2D gridRange2D = cov.getGridGeometry().getGridRange2D();
    for (int x = 0; x < gridRange2D.getWidth(); x++) {
        for (int y = 0; y < gridRange2D.getHeight(); y++) {
            try {
                dest = raster.getSampleDouble(x, y, 0);

                if (!valueList.contains(dest)) {
                    valueList.add(dest);
                }
            } catch (Exception e) {
                ConsoleManager.getInstance().exception(this, e);
            }
        }
    }

    ColorMapImpl colourMap = new ColorMapImpl();

    // Sort the unique sample values in ascending order
    Collections.sort(valueList);

    // Create colour amp entries in the colour map for all the sample values
    for (Double value : valueList) {
        ColorMapEntry entry = new ColorMapEntryImpl();
        Literal colourExpression =
                ff.literal(ColourUtils.fromColour(ColourUtils.createRandomColour()));
        entry.setColor(colourExpression);
        entry.setQuantity(ff.literal(value.doubleValue()));

        colourMap.addColorMapEntry(entry);
    }

    colourMap.setType(ColorMap.TYPE_VALUES);
    sym.setColorMap(colourMap);
}
 
Example 7
Source File: OmsSkyview.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculate the angle.
 * 
 * @param x the x index.
 * @param y the y index.
 * @param tmpWR the sky map.
 * @param pitWR the elevation map.
 * @param res the resolution of the map.
 * @param normalSunVector
 * @param inverseSunVector
 * @param sunVector
 */
protected WritableRaster shadow( int x, int y, WritableRaster tmpWR, WritableRaster pitWR, double res,
        double[] normalSunVector, double[] inverseSunVector, double[] sunVector ) {
    int n = 0;
    double zcompare = -Double.MAX_VALUE;
    double dx = (inverseSunVector[0] * n);
    double dy = (inverseSunVector[1] * n);
    int nCols = tmpWR.getWidth();
    int nRows = tmpWR.getHeight();
    int idx = (int) (x + dx);
    int jdy = (int) (y + dy);
    double vectorToOrigin[] = new double[3];
    while( idx >= 0 && idx <= nCols - 1 && jdy >= 0 && jdy <= nRows - 1 ) {
        vectorToOrigin[0] = dx * res;
        vectorToOrigin[1] = dy * res;
        vectorToOrigin[2] = pitWR.getSampleDouble(idx, jdy, 0);
        double zprojection = scalarProduct(vectorToOrigin, normalSunVector);
        double nGrad[] = normalVectorWR.getPixel(idx, jdy, new double[3]);
        double cosinc = scalarProduct(sunVector, nGrad);
        double elevRad = elevation;
        if ((cosinc >= 0) && (zprojection > zcompare)) {
            tmpWR.setSample(idx, jdy, 0, elevRad);
            zcompare = zprojection;
        }
        n = n + 1;
        dy = (inverseSunVector[1] * n);
        dx = (inverseSunVector[0] * n);
        idx = (int) Math.round(x + dx);
        jdy = (int) Math.round(y + dy);
    }
    return tmpWR;

}
 
Example 8
Source File: GeomorphUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void orizzonte4( double delta, int quadrata, double beta, double alfa, RandomIter elevImageIterator,
        WritableRaster curvatureImage, int[][] shadow ) {
    int rows = curvatureImage.getHeight();
    int cols = curvatureImage.getWidth();
    /*=====================*/

    int y, I, J;
    double zenith;

    /*======================*/

    if (beta != 0) {
        for( int j = 0; j < quadrata; j++ ) {
            I = -1;
            J = -1;
            y = 0;
            for( int jj = j; jj >= 0; jj-- ) {
                for( int i = rows - (int) floor(1 / tan(beta) * (j - jj)) - 1; i >= rows
                        - (int) floor(1 / tan(beta) * (j - jj + 1)) - 1
                        && i >= 0; i-- ) {
                    if (jj < cols && !isNovalue(elevImageIterator.getSampleDouble(jj, i, 0))) {
                        /*shadow.element[i][jj]=j;}}}}}*/
                        if (curvatureImage.getSampleDouble(jj, i, 0) == 1 && I == -1) {
                            I = i;
                            J = jj;
                            y = 1;
                        } else if (curvatureImage.getSampleDouble(jj, i, 0) == 1 && I != -1) {
                            zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator
                                    .getSampleDouble(jj, i, 0))
                                    / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                            + pow((double) (J - jj) * (double) delta, (double) 2));
                            if (zenith <= tan(alfa)) {
                                shadow[i][jj] = 0;
                                I = i;
                                J = jj;
                            } else {
                                shadow[i][jj] = 1;
                            }
                        } else if (curvatureImage.getSampleDouble(jj, i, 0) == 0 && y == 1) {
                            zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator
                                    .getSampleDouble(jj, i, 0))
                                    / sqrt((double) pow((I - i) * (double) delta, (double) 2)
                                            + pow((double) (J - jj) * (double) delta, (double) 2));
                            if (zenith <= tan(alfa)) {
                                shadow[i][jj] = 0;
                                y = 0;
                            } else {
                                shadow[i][jj] = 1;
                            }
                        }
                    }
                }
            }
        }
    } else {
        for( int j = 0; j < cols; j++ ) {
            I = -1;
            J = -1;
            y = 0;
            for( int i = rows - 1; i >= 0; i-- ) {
                if (!isNovalue(elevImageIterator.getSampleDouble(j, i, 0))) {
                    if (curvatureImage.getSampleDouble(j, i, 0) == 1 && I == -1) {
                        I = i;
                        J = j;
                        y = 1;
                    } else if (curvatureImage.getSampleDouble(j, i, 0) == 1 && I != -1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][j] = 0;
                            I = i;
                            J = j;
                        } else {
                            shadow[i][j] = 1;
                        }
                    } else if (curvatureImage.getSampleDouble(j, i, 0) == 0 && y == 1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][j] = 0;
                            y = 0;
                        } else {
                            shadow[i][j] = 1;
                        }
                    }
                }
            }
        }
    }

}
 
Example 9
Source File: GeomorphUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void orizzonte5( double delta, int quadrata, double beta, double alfa, RandomIter elevImageIterator,
        WritableRaster curvatureImage, int[][] shadow ) {
    int rows = curvatureImage.getHeight();
    int cols = curvatureImage.getWidth();
    /*=====================*/

    int y, I, J;
    double zenith;

    /*======================*/

    for( int j = quadrata; j >= 0; j-- ) {
        I = -1;
        J = -1;
        y = 0;
        for( int jj = j; jj < quadrata; jj++ ) {
            for( int i = rows - (int) floor(1 / tan(beta) * (jj - j)) - 1; i >= rows
                    - (int) floor(1 / tan(beta) * (jj - j + 1)) - 1
                    && i >= 0; i-- ) {
                if (jj >= quadrata - cols && !isNovalue(elevImageIterator.getSampleDouble(jj - (quadrata - cols), i, 0))) {
                    /*shadow.element[i][jj-(quadrata-Z0.nch)]=j;}}}}*/
                    if (curvatureImage.getSampleDouble(jj - (quadrata - cols), i, 0) == 1 && I == -1) {
                        I = i;
                        J = jj - (quadrata - cols);
                        y = 1;
                    } else if (curvatureImage.getSampleDouble(jj - (quadrata - cols), i, 0) == 1 && I != -1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(jj
                                - (quadrata - cols), i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - (jj - (quadrata - cols))) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][jj - (quadrata - cols)] = 0;
                            I = i;
                            J = jj - (quadrata - cols);
                        } else {
                            shadow[i][jj - (quadrata - cols)] = 1;
                        }
                    } else if (curvatureImage.getSampleDouble(jj - (quadrata - cols), i, 0) == 0 && y == 1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(jj
                                - (quadrata - cols), i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - (jj - (quadrata - cols))) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][jj - (quadrata - cols)] = 0;
                            y = 0;
                        } else {
                            shadow[i][jj - (quadrata - cols)] = 1;
                        }
                    }
                }
            }
        }
    }

}
 
Example 10
Source File: GeomorphUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void orizzonte6( double delta, int quadrata, double beta, double alfa, RandomIter elevImageIterator,
        WritableRaster curvatureImage, int[][] shadow ) {
    int rows = curvatureImage.getHeight();
    int cols = curvatureImage.getWidth();
    /*=====================*/

    int y, I, J;
    double zenith;

    /*======================*/

    if (beta != 0) {
        for( int i = 0; i < quadrata; i++ ) {
            I = -1;
            J = -1;
            y = 0;
            for( int ii = i; ii >= 0; ii-- ) {
                for( int j = (int) floor(1 / tan(beta) * (i - ii)); j <= (int) floor(1 / tan(beta) * (i - ii + 1)) - 1
                        && j < cols; j++ ) {
                    if (ii < rows && !isNovalue(elevImageIterator.getSampleDouble(j, ii, 0))) {
                        /*shadow.element[ii][j]=i;}}}}}*/
                        if (curvatureImage.getSampleDouble(j, ii, 0) == 1 && I == -1) {
                            I = ii;
                            J = j;
                            y = 1;
                        } else if (curvatureImage.getSampleDouble(j, ii, 0) == 1 && I != -1) {
                            zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator
                                    .getSampleDouble(j, ii, 0))
                                    / sqrt(pow((double) (I - ii) * (double) delta, (double) 2)
                                            + pow((double) (J - j) * (double) delta, (double) 2));
                            if (zenith <= tan(alfa)) {
                                shadow[ii][j] = 0;
                                I = ii;
                                J = j;
                            } else {
                                shadow[ii][j] = 1;
                            }
                        } else if (curvatureImage.getSampleDouble(j, ii, 0) == 0 && y == 1) {
                            zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator
                                    .getSampleDouble(j, ii, 0))
                                    / sqrt((double) pow((I - ii) * (double) delta, (double) 2)
                                            + pow((double) (J - j) * (double) delta, (double) 2));
                            if (zenith <= tan(alfa)) {
                                shadow[ii][j] = 0;
                                y = 0;
                            } else {
                                shadow[ii][j] = 1;
                            }
                        }
                    }
                }
            }
        }
    } else {
        for( int i = 0; i < rows; i++ ) {
            I = -1;
            J = -1;
            y = 0;
            for( int j = 0; j < cols; j++ ) {
                if (!isNovalue(elevImageIterator.getSampleDouble(j, i, 0))) {
                    if (curvatureImage.getSampleDouble(j, i, 0) == 1 && I == -1) {
                        I = i;
                        J = j;
                        y = 1;
                    } else if (curvatureImage.getSampleDouble(j, i, 0) == 1 && I != -1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][j] = 0;
                            I = i;
                            J = j;
                        } else {
                            shadow[i][j] = 1;
                        }
                    } else if (curvatureImage.getSampleDouble(j, i, 0) == 0 && y == 1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][j] = 0;
                            y = 0;
                        } else {
                            shadow[i][j] = 1;
                        }
                    }
                }
            }
        }
    }

}
 
Example 11
Source File: GeomorphUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void orizzonte7( double delta, int quadrata, double beta, double alfa, RandomIter elevImageIterator,
        WritableRaster curvatureImage, int[][] shadow ) {
    int rows = curvatureImage.getHeight();
    int cols = curvatureImage.getWidth();
    /*=====================*/

    int y, I, J;
    double zenith;

    /*======================*/

    for( int i = quadrata - 1; i >= 0; i-- ) {
        I = -1;
        J = -1;
        y = 0;
        for( int ii = i; ii < quadrata - 1; ii++ ) {
            for( int j = (int) floor(1 / tan(beta) * (ii - i)); j <= (int) floor(1 / tan(beta) * (ii - i + 1)) - 1
                    && j < cols; j++ ) {
                if (ii >= (rows + 2 * cols) && !isNovalue(elevImageIterator.getSampleDouble(j, ii - (rows + 2 * cols), 0))) {
                    /*shadow.element[ii-(Z0.nrh+2*Z0.nch)][j]=i;}}}}*/
                    if (curvatureImage.getSampleDouble(j, ii - (rows + 2 * cols), 0) == 1 && I == -1) {
                        I = ii - (rows + 2 * cols);
                        J = j;
                        y = 1;
                    } else if (curvatureImage.getSampleDouble(j, ii - (rows + 2 * cols), 0) == 1 && I != -1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, ii
                                - (rows + 2 * cols), 0))
                                / sqrt(pow((double) (I - (ii - (rows + 2 * cols))) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[ii - (rows + 2 * cols)][j] = 0;
                            I = ii - (rows + 2 * cols);
                            J = j;
                        } else {
                            shadow[ii - (rows + 2 * cols)][j] = 1;
                        }
                    } else if (curvatureImage.getSampleDouble(j, ii - (rows + 2 * cols), 0) == 0 && y == 1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, ii
                                - (rows + 2 * cols), 0))
                                / sqrt(pow((double) (I - (ii - (rows + 2 * cols))) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[ii - (rows + 2 * cols)][j] = 0;
                            y = 0;
                        } else {
                            shadow[ii - (rows + 2 * cols)][j] = 1;
                        }
                    }
                }
            }
        }
    }

}
 
Example 12
Source File: OmsSkyview.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void process() throws Exception {
    checkNull(inElev);
    // extract some attributes of the map
    HashMap<String, Double> attribute = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    double dx = attribute.get(CoverageUtilities.XRES);
    CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    // extract the raster.
    RenderedImage pitTmpRI = inElev.getRenderedImage();
    WritableRaster pitWR = CoverageUtilities.replaceNovalue(pitTmpRI, -9999.0);
    pitTmpRI = null;
    minX = pitWR.getMinX();
    minY = pitWR.getMinY();
    rows = pitWR.getHeight();
    cols = pitWR.getWidth();

    WritableRaster skyWR = skyviewfactor(pitWR, dx);

    int maxY = minY + rows;
    int maxX = minX + cols;
    for( int y = minY + 2; y < maxY - 2; y++ ) {
        for( int x = minX + 2; x < maxX - 2; x++ ) {
            if (pitWR.getSampleDouble(x, y, 0) == -9999.0) {
                skyWR.setSample(x, y, 0, doubleNovalue);
            }
        }
    }
    for( int y = minY; y < maxY; y++ ) {
        skyWR.setSample(0, y, 0, doubleNovalue);
        skyWR.setSample(1, y, 0, doubleNovalue);
        skyWR.setSample(cols - 2, y, 0, doubleNovalue);
        skyWR.setSample(cols - 1, y, 0, doubleNovalue);
    }

    for( int x = minX + 2; x < maxX - 2; x++ ) {
        skyWR.setSample(x, 0, 0, doubleNovalue);
        skyWR.setSample(x, 1, 0, doubleNovalue);
        skyWR.setSample(x, rows - 2, 0, doubleNovalue);
        skyWR.setSample(x, rows - 1, 0, doubleNovalue);
    }
    outSky = CoverageUtilities.buildCoverage("skyview factor", skyWR, attribute, inElev.getCoordinateReferenceSystem());

}
 
Example 13
Source File: OmsSkyview.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
protected WritableRaster normalVector( WritableRaster pitWR, double res ) {

        /*
         * 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);
        /*
         * apply the corripio's formula (is the formula (3) in the article)
         */
        int maxY = minX + rows;
        int maxX = minX + cols;
        for( int y = minY; y < maxY - 1; y++ ) {
            for( int x = minX; x < maxX - 1; x++ ) {
                double zij = pitWR.getSampleDouble(x, y, 0);
                double zidxj = pitWR.getSampleDouble(x + 1, y, 0);
                double zijdy = pitWR.getSampleDouble(x, y + 1, 0);
                double zidxjdy = pitWR.getSampleDouble(x + 1, y + 1, 0);
                double firstComponent = 0.5 * res * (zij - zidxj + zijdy - zidxjdy);
                double secondComponent = 0.5 * res * (zij + zidxj - zijdy - zidxjdy);
                double thirthComponent = res * res;
                tmpNormalVectorWR.setPixel(x, y, new double[]{firstComponent, secondComponent, thirthComponent});

            }
        }

        /*
         * Evaluate the value of the normal vector at the node as the mean of
         * the four value around, and normalize it.
         */
        WritableRaster normalVectorWR = CoverageUtilities.createWritableRaster(cols, rows, null, sm, 0.0);
        maxSlope = 3.13 / 2.0;
        for( int y = minY; y < maxY; y++ ) {
            for( int x = minX; x < maxX; x++ ) {
                normalVectorWR.setSample(x, y, 0, 1.0);
                normalVectorWR.setSample(x, y, 1, 1.0);
                normalVectorWR.setSample(x, y, 2, 1.0);

            }
        }
        for( int y = minY; y < maxY; y++ ) {
            for( int x = minX; x < maxX; x++ ) {
                double area = 0;
                double mean[] = new double[3];
                boolean isValidValue = true;
                for( int k = 0; k < 3; k++ ) {
                    double g00 = 1;
                    double g10 = 1;
                    double g01 = 1;
                    double g11 = 1;
                    if (y > 0 && x > 0) {
                        g00 = tmpNormalVectorWR.getSampleDouble(x - 1, y - 1, k);
                        g10 = tmpNormalVectorWR.getSampleDouble(x, y - 1, k);
                        g01 = tmpNormalVectorWR.getSampleDouble(x - 1, y, k);
                        g11 = tmpNormalVectorWR.getSampleDouble(x, y, k);
                    }

                    if (!isNovalue(g00) && !isNovalue(g01) && !isNovalue(g10) && !isNovalue(g11)) {
                        mean[k] = 1. / 4. * (g00 + g01 + g10 + g11);
                    } else {
                        isValidValue = false;
                        break;
                    }
                    area = area + mean[k] * mean[k];

                }
                if (isValidValue) {
                    area = Math.sqrt(area);
                    for( int k = 0; k < 3; k++ ) {
                        normalVectorWR.setSample(x, y, k, mean[k] / area);
                        if (x > minX + 1 && x < cols - 2 && y > minY + 1 && y < rows - 2 && k == 2) {
                            if (mean[k] / area < maxSlope)
                                maxSlope = mean[k] / area;
                        }
                    }

                }
            }
        }

        maxSlope = (int) (Math.acos(maxSlope) * 180.0 / Math.PI);

        return normalVectorWR;

    }
 
Example 14
Source File: GeomorphUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void orizzonte3( double delta, int quadrata, double beta, double alfa, RandomIter elevImageIterator,
        WritableRaster curvatureImage, int[][] shadow ) {
    int rows = curvatureImage.getHeight();
    int cols = curvatureImage.getWidth();
    /*=====================*/

    int y, I, J;
    double zenith;

    /*======================*/

    for( int i = 0; i < quadrata; i++ ) {
        I = -1;
        J = -1;
        y = 0;
        for( int ii = i; ii >= 0; ii-- ) {
            for( int j = cols - (int) floor(1.0 / tan(beta) * (i - ii)) - 1; j >= cols
                    - (int) floor(1.0 / tan(beta) * (i - ii + 1)) - 1
                    && j >= 0; j-- ) {
                if (ii < rows && !isNovalue(elevImageIterator.getSampleDouble(j, ii, 0))) {
                    /*shadow->element[ii][j]=i;}}}}*/
                    if (curvatureImage.getSampleDouble(j, ii, 0) == 1 && I == -1) {
                        I = ii;
                        J = j;
                        y = 1;
                    } else if (curvatureImage.getSampleDouble(j, ii, 0) == 1 && I != -1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, ii, 0))
                                / sqrt(pow((double) (I - ii) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[ii][j] = 0;
                            I = ii;
                            J = j;
                        } else {
                            shadow[ii][j] = 1;
                        }
                    } else if (curvatureImage.getSampleDouble(j, ii, 0) == 0 && I != -1 && y == 1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, ii, 0))
                                / sqrt(pow((double) (I - ii) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[ii][j] = 0;
                            y = 0;
                        } else {
                            shadow[ii][j] = 1;
                        }
                    }
                    // System.out.println(ii + " " + j + "       " + shadow[ii][j]);
                }
            }
        }
    }

}
 
Example 15
Source File: GeomorphUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void orizzonte2( double delta, int quadrata, double beta, double alfa, RandomIter elevImageIterator,
        WritableRaster curvatureImage, int[][] shadow ) {
    int rows = curvatureImage.getHeight();
    int cols = curvatureImage.getWidth();
    /*=====================*/

    int y, I, J;
    double zenith;

    /*======================*/

    if (beta != 0) {
        for( int i = quadrata; i >= 0; i-- ) {
            I = -1;
            J = -1;
            y = 0;
            for( int ii = i; ii < quadrata; ii++ ) {
                for( int j = cols - (int) floor(1 / tan(beta) * (ii - i)) - 1; j >= cols
                        - (int) floor(1 / tan(beta) * (ii - i + 1)) - 1
                        && j >= 0; j-- ) {
                    if (ii >= (rows + 2 * cols)
                            && !isNovalue(elevImageIterator.getSampleDouble(j, ii - (rows + 2 * cols), 0))) {
                        /*shadow->element[ii-(Z0->nrh+2*Z0->nch)][j]=i}}}}}*/
                        if (curvatureImage.getSampleDouble(j, ii - (rows + 2 * cols), 0) == 1 && I == -1) {
                            I = ii - (rows + 2 * cols);
                            J = j;
                            y = 1;
                        } else if (curvatureImage.getSampleDouble(j, ii - (rows + 2 * cols), 0) == 1 && I != -1) {
                            zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, ii
                                    - (rows + 2 * cols), 0))
                                    / sqrt(Math.pow((double) (I - (ii - (rows + 2 * cols))) * (double) delta, (double) 2)
                                            + pow((double) (J - j) * (double) delta, (double) 2));
                            if (zenith <= tan(alfa)) {
                                shadow[ii - (rows + 2 * cols)][j] = 0;
                                I = ii - (rows + 2 * cols);
                                J = j;
                            } else {
                                shadow[ii - (rows + 2 * cols)][j] = 1;
                            }
                        } else if (curvatureImage.getSampleDouble(j, ii - (rows + 2 * cols), 0) == 0 && y == 1) {
                            zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, ii
                                    - (rows + 2 * cols), 0))
                                    / sqrt(Math.pow((double) (I - (ii - (rows + 2 * cols))) * (double) delta, (double) 2)
                                            + pow((double) (J - j) * (double) delta, (double) 2));
                            if (zenith <= tan(alfa)) {
                                shadow[ii - (rows + 2 * cols)][j] = 0;
                                y = 0;
                            } else {
                                shadow[ii - (rows + 2 * cols)][j] = 1;
                            }
                        }
                    }
                }
            }
        }
    } else {
        for( int i = 0; i < rows; i++ ) {
            I = -1;
            J = -1;
            y = 0;
            for( int j = cols - 1; j >= 0; j-- ) {
                if (!isNovalue(elevImageIterator.getSampleDouble(j, i, 0))) {
                    if (curvatureImage.getSampleDouble(j, i, 0) == 1 && I == -1) {
                        I = i;
                        J = j;
                        y = 1;
                    } else if (curvatureImage.getSampleDouble(j, i, 0) == 1 && I != -1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][j] = 0;
                            I = i;
                            J = j;
                        } else {
                            shadow[i][j] = 1;
                        }
                    } else if (curvatureImage.getSampleDouble(j, i, 0) == 0 && y == 1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][j] = 0;
                            y = 0;
                        } else {
                            shadow[i][j] = 1;
                        }
                    }
                }
            }
        }
    }

}
 
Example 16
Source File: GeomorphUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void orizzonte1( double delta, int quadrata, double beta, double alfa, RandomIter elevImageIterator,
        WritableRaster curvatureImage, int[][] shadow ) {
    int rows = curvatureImage.getHeight();
    int cols = curvatureImage.getWidth();

    /*=====================*/
    int y, I, J;
    double zenith;
    /*======================*/

    if (beta != 0) {
        for( int j = 0; j < quadrata; j++ ) {
            I = -1;
            J = -1;
            y = 0;
            for( int jj = j; jj >= 0; jj-- ) {
                for( int i = (int) floor(1 / tan(beta) * (j - jj)); i <= (int) floor(1 / tan(beta) * (j - jj + 1)) - 1
                        && i < rows; i++ ) {
                    if (jj < cols && !isNovalue(elevImageIterator.getSampleDouble(jj, i, 0))) {
                        /*shadow->element[i][jj]=j;}}}}}*/
                        if (curvatureImage.getSampleDouble(jj, i, 0) == 1 && I == -1) {
                            I = i;
                            J = jj;
                            y = 1;
                        } else if (curvatureImage.getSampleDouble(jj, i, 0) == 1 && I != -1) {
                            zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator
                                    .getSampleDouble(jj, i, 0))
                                    / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                            + pow((double) (J - jj) * (double) delta, (double) 2));
                            if (zenith <= tan(alfa)) {
                                shadow[i][jj] = 0;
                                I = i;
                                J = jj;
                            } else {
                                shadow[i][jj] = 1;
                            }
                        } else if (curvatureImage.getSampleDouble(jj, i, 0) == 0 && y == 1) {
                            zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator
                                    .getSampleDouble(jj, i, 0))
                                    / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                            + pow((double) (J - jj) * (double) delta, (double) 2));
                            if (zenith <= tan(alfa)) {
                                shadow[i][jj] = 0;
                                y = 0;
                            } else {
                                shadow[i][jj] = 1;
                            }
                        }
                    }
                }
            }
        }
    } else {
        for( int j = 0; j < cols; j++ ) {
            I = -1;
            J = -1;
            y = 0;
            for( int i = 0; i < rows; i++ ) {
                if (!isNovalue(elevImageIterator.getSampleDouble(j, i, 0))) {
                    if (curvatureImage.getSampleDouble(j, i, 0) == 1 && I == -1) {
                        I = i;
                        J = j;
                        y = 1;
                    } else if (curvatureImage.getSampleDouble(j, i, 0) == 1 && I != -1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][j] = 0;
                            I = i;
                            J = j;
                        } else {
                            shadow[i][j] = 1;
                        }
                    } else if (curvatureImage.getSampleDouble(j, i, 0) == 0 && y == 1) {
                        zenith = (elevImageIterator.getSampleDouble(J, I, 0) - elevImageIterator.getSampleDouble(j, i, 0))
                                / sqrt(pow((double) (I - i) * (double) delta, (double) 2)
                                        + pow((double) (J - j) * (double) delta, (double) 2));
                        if (zenith <= tan(alfa)) {
                            shadow[i][j] = 0;
                            y = 0;
                        } else {
                            shadow[i][j] = 1;
                        }
                    }
                }
            }
        }
    }
}
 
Example 17
Source File: OmsNabla.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
private void nabla( RandomIter elevationIter, WritableRaster nablaRaster ) {
    int y;
    double nablaT, n;
    double[] z = new double[9];
    int[][] v = ModelsEngine.DIR;

    WritableRaster segnWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);

    // grid contains the dimension of pixels according with flow directions
    double[] grid = new double[9];
    grid[0] = 0;
    grid[1] = grid[5] = xRes;
    grid[3] = grid[7] = yRes;
    grid[2] = grid[4] = grid[6] = grid[8] = Math.sqrt(grid[1] * grid[1] + grid[3] * grid[3]);

    pm.beginTask("Processing nabla...", nCols * 2);
    for( int r = 1; r < nRows - 1; r++ ) {
        for( int c = 1; c < nCols - 1; c++ ) {
            z[0] = elevationIter.getSampleDouble(c, r, 0);
            if (!isNovalue((z[0]))) {
                y = 1;
                for( int h = 1; h <= 8; h++ ) {
                    z[h] = elevationIter.getSample(c + v[h][0], r + v[h][1], 0);
                    if (isNovalue(z[h])) {
                        y = 0;
                        segnWR.setSample(c, r, 0, 1);
                        break;
                    }
                }
                if (y == 0) {
                    nablaRaster.setSample(c, r, 0, doubleNovalue);
                } else {
                    double derivata = 0.5 * ((z[1] + z[5] - 2 * z[0]) / (grid[1] * grid[1])
                            + (z[3] + z[7] - 2 * z[0]) / (grid[3] * grid[3]));
                    double derivata2 = derivata + 0.5 * ((z[2] + z[4] + z[6] + z[8] - 4 * z[0]) / (grid[6] * grid[6]));
                    nablaRaster.setSample(c, r, 0, derivata2);
                }
            } else {
                nablaRaster.setSample(c, r, 0, doubleNovalue);
            }
        }
        pm.worked(1);
    }

    for( int r = 1; r < nRows - 1; r++ ) {
        for( int c = 1; c < nCols - 1; c++ ) {
            if (segnWR.getSampleDouble(c, r, 0) == 1) {
                n = 0.0;
                nablaT = 0.0;
                y = 0;
                for( int h = 1; h <= 8; h++ ) {
                    z[h] = elevationIter.getSampleDouble(c + v[h][0], r + v[h][1], 0);
                    y = 0;
                    double nablaSample = nablaRaster.getSampleDouble(c + v[h][0], r + v[h][1], 0);
                    if (isNovalue(z[h]) || !isNovalue(nablaSample))
                        y = 1;
                    if (y == 0) {
                        n += 1;
                        nablaT += nablaSample;
                    }
                }
                if (n == 0)
                    n = 1;
                nablaRaster.setSample(c, r, 0, nablaT / (float) n);
            }
        }
        pm.worked(1);
    }
    pm.done();

}
 
Example 18
Source File: NoDataMergeStrategy.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void merge(
    final RasterTile<NoDataMetadata> thisTile,
    final RasterTile<NoDataMetadata> nextTile,
    final SampleModel sampleModel) {

  // this strategy aims for latest tile with data values, but where there
  // is no data in the latest and there is data in the earlier tile, it
  // fills the data from the earlier tile

  // if next tile is null or if this tile does not have metadata, just
  // keep this tile as is
  if ((nextTile != null) && (thisTile.getMetadata() != null)) {
    final NoDataMetadata thisTileMetadata = thisTile.getMetadata();
    final NoDataMetadata nextTileMetadata = nextTile.getMetadata();

    final WritableRaster thisRaster =
        Raster.createWritableRaster(sampleModel, thisTile.getDataBuffer(), null);
    final WritableRaster nextRaster =
        Raster.createWritableRaster(sampleModel, nextTile.getDataBuffer(), null);
    final int maxX = thisRaster.getMinX() + thisRaster.getWidth();
    final int maxY = thisRaster.getMinY() + thisRaster.getHeight();
    boolean recalculateMetadata = false;
    for (int b = 0; b < thisRaster.getNumBands(); b++) {
      for (int x = thisRaster.getMinX(); x < maxX; x++) {
        for (int y = thisRaster.getMinY(); y < maxY; y++) {
          if (thisTileMetadata.isNoData(
              new SampleIndex(x, y, b),
              thisRaster.getSampleDouble(x, y, b))) {
            final double sample = nextRaster.getSampleDouble(x, y, b);
            if ((nextTileMetadata == null)
                || !nextTileMetadata.isNoData(new SampleIndex(x, y, b), sample)) {
              // we only need to recalculate metadata if
              // the raster is overwritten,
              // otherwise just use this raster's
              // metadata
              recalculateMetadata = true;
              thisRaster.setSample(x, y, b, sample);
            }
          }
        }
      }
    }
    if (recalculateMetadata) {
      thisTile.setMetadata(
          NoDataMetadataFactory.mergeMetadata(
              thisTileMetadata,
              thisRaster,
              nextTileMetadata,
              nextRaster));
    }
  }
}
 
Example 19
Source File: ModelsEngine.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Evaluate the shadow map.
 *
 * @param i
 *            the x axis index.
 * @param j
 *            the y axis index.
 * @param tmpWR
 *            the output shadow map.
 * @param demWR
 *            the elevation map.
 * @param res
 *            the resolution of the elevation map.
 * @param normalSunVector
 * @param inverseSunVector
 * @return
 */
private static WritableRaster shadow( int i, int j, WritableRaster tmpWR, WritableRaster demWR, double res,
        double[] normalSunVector, double[] inverseSunVector ) {
    int n = 0;
    double zcompare = -Double.MAX_VALUE;
    double dx = (inverseSunVector[0] * n);
    double dy = (inverseSunVector[1] * n);
    int nCols = tmpWR.getWidth();
    int nRows = tmpWR.getHeight();
    int idx = (int) Math.round(i + dx);
    int jdy = (int) Math.round(j + dy);
    double vectorToOrigin[] = new double[3];
    while( idx >= 0 && idx <= nCols - 1 && jdy >= 0 && jdy <= nRows - 1 ) {
        vectorToOrigin[0] = dx * res;
        vectorToOrigin[1] = dy * res;

        int tmpY = (int) (j + dy);
        if (tmpY < 0) {
            tmpY = 0;
        } else if (tmpY > nRows) {
            tmpY = nRows - 1;
        }
        int tmpX = (int) (i + dx);
        if (tmpX < 0) {
            tmpX = 0;
        } else if (tmpY > nCols) {
            tmpX = nCols - 1;
        }
        vectorToOrigin[2] = demWR.getSampleDouble(idx, jdy, 0);
        // vectorToOrigin[2] = (pitRandomIter.getSampleDouble(idx, jdy, 0) +
        // pitRandomIter
        // .getSampleDouble(tmpX, tmpY, 0)) / 2;
        double zprojection = scalarProduct(vectorToOrigin, normalSunVector);
        if ((zprojection < zcompare)) {
            tmpWR.setSample(idx, jdy, 0, 0);
        } else {
            zcompare = zprojection;
        }
        n = n + 1;
        dy = (inverseSunVector[1] * n);
        dx = (inverseSunVector[0] * n);
        idx = (int) Math.round(i + dx);
        jdy = (int) Math.round(j + dy);
    }
    return tmpWR;
}