use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsMosaic method process.
@Execute
public void process() throws Exception {
if (!concatOr(outRaster == null, doReset)) {
return;
}
if (inFiles == null) {
throw new ModelsIllegalargumentException("No input data have been provided.", this, pm);
}
if (inFiles != null && inFiles.size() < 2) {
throw new ModelsIllegalargumentException("The patching module needs at least two maps to be patched.", this, pm);
}
GridGeometry2D referenceGridGeometry = null;
double n = Double.MIN_VALUE;
double s = Double.MAX_VALUE;
double e = Double.MIN_VALUE;
double w = Double.MAX_VALUE;
int np = Integer.MIN_VALUE;
int sp = Integer.MAX_VALUE;
int ep = Integer.MIN_VALUE;
int wp = Integer.MAX_VALUE;
pm.beginTask("Calculating final bounds...", inFiles.size());
for (File coverageFile : inFiles) {
GridCoverage2D coverage = OmsRasterReader.readRaster(coverageFile.getAbsolutePath());
if (referenceGridGeometry == null) {
// take the first as reference
crs = coverage.getCoordinateReferenceSystem();
referenceGridGeometry = coverage.getGridGeometry();
pm.message("Using crs: " + CrsUtilities.getCodeFromCrs(crs));
}
Envelope2D worldEnv = coverage.getEnvelope2D();
GridEnvelope2D pixelEnv = referenceGridGeometry.worldToGrid(worldEnv);
int minPX = (int) pixelEnv.getMinX();
int minPY = (int) pixelEnv.getMinY();
int maxPX = (int) pixelEnv.getMaxX();
int maxPY = (int) pixelEnv.getMaxY();
if (minPX < wp)
wp = minPX;
if (minPY < sp)
sp = minPY;
if (maxPX > ep)
ep = maxPX;
if (maxPY > np)
np = maxPY;
double minWX = worldEnv.getMinX();
double minWY = worldEnv.getMinY();
double maxWX = worldEnv.getMaxX();
double maxWY = worldEnv.getMaxY();
if (minWX < w)
w = minWX;
if (minWY < s)
s = minWY;
if (maxWX > e)
e = maxWX;
if (maxWY > n)
n = maxWY;
pm.worked(1);
}
pm.done();
int endWidth = ep - wp;
int endHeight = np - sp;
pm.message(MessageFormat.format("Output raster will have {0} cols and {1} rows.", endWidth, endHeight));
WritableRaster outputWR = CoverageUtilities.createWritableRaster(endWidth, endHeight, null, null, HMConstants.doubleNovalue);
WritableRandomIter outputIter = RandomIterFactory.createWritable(outputWR, null);
int offestX = Math.abs(wp);
int offestY = Math.abs(sp);
int index = 1;
for (File coverageFile : inFiles) {
GridCoverage2D coverage = OmsRasterReader.readRaster(coverageFile.getAbsolutePath());
RenderedImage renderedImage = coverage.getRenderedImage();
RandomIter randomIter = RandomIterFactory.create(renderedImage, null);
Envelope2D env = coverage.getEnvelope2D();
GridEnvelope2D repEnv = referenceGridGeometry.worldToGrid(env);
GridGeometry2D tmpGG = coverage.getGridGeometry();
GridEnvelope2D tmpEnv = tmpGG.worldToGrid(env);
int startX = (int) (repEnv.getMinX() + offestX);
int startY = (int) (repEnv.getMinY() + offestY);
double tmpW = tmpEnv.getWidth();
double tmpH = tmpEnv.getHeight();
// $NON-NLS-1$
pm.beginTask("Patch map " + index++, (int) tmpW);
for (int y = 0; y < tmpH; y++) {
for (int x = 0; x < tmpW; x++) {
double value = randomIter.getSampleDouble(x, y, 0);
outputIter.setSample(x + startX, y + startY, 0, value);
}
pm.worked(1);
}
pm.done();
randomIter.done();
}
HashMap<String, Double> envelopeParams = new HashMap<String, Double>();
envelopeParams.put(NORTH, n);
envelopeParams.put(SOUTH, s);
envelopeParams.put(WEST, w);
envelopeParams.put(EAST, e);
// $NON-NLS-1$
outRaster = CoverageUtilities.buildCoverage("patch", outputWR, envelopeParams, crs);
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsPointsRasterizer method process.
@Execute
public void process() throws Exception {
checkNull(inGrid, inVector);
SimpleFeatureType schema = inVector.getSchema();
if (!EGeometryType.isPoint(schema.getGeometryDescriptor())) {
throw new ModelsRuntimeException("The module works only with point vectors.", this);
}
RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(inGrid);
double n = regionMap.getNorth();
double s = regionMap.getSouth();
double e = regionMap.getEast();
double w = regionMap.getWest();
WritableRaster outWR = CoverageUtilities.createWritableRaster(regionMap.getCols(), regionMap.getRows(), null, null, HMConstants.doubleNovalue);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
List<FeatureMate> matesList = FeatureUtilities.featureCollectionToMatesList(inVector);
double value = 0;
pm.beginTask("Rasterizing points...", matesList.size());
for (FeatureMate featureMate : matesList) {
Geometry geometry = featureMate.getGeometry();
if (fCat == null) {
Double cat = featureMate.getAttribute(fCat, Double.class);
if (cat != null) {
value = cat;
}
}
Coordinate[] coordinates = geometry.getCoordinates();
for (Coordinate coordinate : coordinates) {
if (!NumericsUtilities.isBetween(coordinate.x, w, e) || !NumericsUtilities.isBetween(coordinate.y, s, n)) {
continue;
}
GridCoordinates2D onGrid = inGrid.worldToGrid(new DirectPosition2D(coordinate.x, coordinate.y));
outIter.setSample(onGrid.x, onGrid.y, 0, value);
}
pm.worked(1);
}
pm.done();
outRaster = CoverageUtilities.buildCoverage("pointsraster", outWR, regionMap, inVector.getSchema().getCoordinateReferenceSystem());
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsCutOut method process.
@Execute
public void process() throws Exception {
if (!concatOr(outRaster == null, doReset)) {
return;
}
// do autoboxing only once
if (pMax != null) {
max = pMax;
doMax = true;
}
if (pMin != null) {
min = pMin;
doMin = true;
}
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
int nCols = regionMap.getCols();
int nRows = regionMap.getRows();
RenderedImage geodataRI = inRaster.getRenderedImage();
RandomIter geodataIter = RandomIterFactory.create(geodataRI, null);
double maskNv = 0;
if (inMask != null) {
RenderedImage maskRI = inMask.getRenderedImage();
maskNv = HMConstants.getNovalue(inMask);
maskIter = RandomIterFactory.create(maskRI, null);
}
WritableRaster outWR = CoverageUtilities.renderedImage2WritableRaster(geodataRI, false);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
try {
double _maskNv = maskNv;
pm.beginTask("Processing map...", nRows * nCols);
processGrid(nCols, nRows, false, (c, r) -> {
if (pm.isCanceled()) {
return;
}
pm.worked(1);
if (maskIter != null) {
double maskValue = maskIter.getSampleDouble(c, r, 0);
if (!doInverse) {
if (isNovalue(maskValue, _maskNv)) {
outIter.setSample(c, r, 0, HMConstants.doubleNovalue);
return;
}
} else {
if (!isNovalue(maskValue, _maskNv)) {
outIter.setSample(c, r, 0, HMConstants.doubleNovalue);
return;
}
}
}
double value = geodataIter.getSampleDouble(c, r, 0);
if (doMax && value > max) {
outIter.setSample(c, r, 0, HMConstants.doubleNovalue);
return;
}
if (doMin && value < min) {
outIter.setSample(c, r, 0, HMConstants.doubleNovalue);
}
});
pm.done();
} finally {
geodataIter.done();
outIter.done();
if (maskIter != null) {
maskIter.done();
}
}
outRaster = CoverageUtilities.buildCoverage("cutout", outWR, regionMap, inRaster.getCoordinateReferenceSystem());
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsBiasedSigmaFilter method process.
@Execute
public void process() throws Exception {
checkNull(inGeodata);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inGeodata);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
double xres = regionMap.getXres();
double yres = regionMap.getYres();
RandomIter inIter = CoverageUtilities.getRandomIterator(inGeodata);
double novalue = HMConstants.getNovalue(inGeodata);
WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, novalue);
WritableRandomIter outIter = CoverageUtilities.getWritableRandomIterator(outWR);
int part = (pWindow - 1) / 2;
pm.beginTask("Processing filter...", cols - (pWindow - 1));
for (int r = part; r < rows - part; r++) {
for (int c = part; c < cols - part; c++) {
GridNode node = new GridNode(inIter, cols, rows, xres, yres, c, r, novalue);
if (node.isValid() && !node.touchesBound()) {
double[][] window = node.getWindow(pWindow, false);
double elevation = node.elevation;
double sumUpper = 0;
double countUpper = 0;
double sumLower = 0;
double countLower = 0;
for (int i = 0; i < window.length; i++) {
for (int j = 0; j < window[0].length; j++) {
if (i == 1 && j == 1) {
continue;
}
if (window[i][j] >= elevation) {
sumUpper = sumUpper + window[i][j];
countUpper++;
} else {
sumLower = sumLower + window[i][j];
countLower++;
}
}
}
double avgUpper = sumUpper / countUpper;
double avgLower = sumLower / countLower;
double value = 0;
if (countUpper == 0) {
value = avgLower;
} else if (countLower == 0) {
value = avgUpper;
} else {
double deltaUpper = abs(elevation - avgUpper);
double deltaLower = abs(elevation - avgLower);
if (deltaUpper < deltaLower) {
value = avgUpper;
} else {
value = avgLower;
}
}
if (!isNovalue(value)) {
outIter.setSample(c, r, 0, value);
}
}
}
pm.worked(1);
}
pm.done();
outIter.done();
outGeodata = CoverageUtilities.buildCoverage("sigma", outWR, regionMap, inGeodata.getCoordinateReferenceSystem());
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsKernelFilter method process.
public void process() throws Exception {
checkNull(inRaster);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
ValueType type = getKernelType(pKernel);
KernelJAI kernel = KernelFactory.createCircle(pRadius, type);
RenderedImage inImg = inRaster.getRenderedImage();
RandomIter inIter = RandomIterFactory.create(inImg, null);
WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
float[] kernelData = kernel.getKernelData();
pm.beginTask("Processing...", cols - 2 * pRadius);
for (int r = pRadius; r < rows - pRadius; r++) {
for (int c = pRadius; c < cols - pRadius; c++) {
double kernelSum = 0.0;
int k = 0;
double outputValue = 0.0;
for (int kr = -pRadius; kr <= pRadius; kr++, k++) {
for (int kc = -pRadius; kc <= pRadius; kc++) {
double value = inIter.getSampleDouble(c + kc, r + kr, 0);
if (!isNovalue(value)) {
outputValue = outputValue + value * kernelData[k];
kernelSum = kernelSum + kernelData[k];
}
}
}
outIter.setSample(c, r, 0, outputValue / kernelSum);
}
pm.worked(1);
}
pm.done();
outRaster = CoverageUtilities.buildCoverage("filtered", outWR, regionMap, inRaster.getCoordinateReferenceSystem());
}
Aggregations