use of javax.media.jai.iterator.WritableRandomIter in project imageio-ext by geosolutions-it.
the class MercatorOceanConverter method Resampler.
private WritableRaster Resampler(final Array latData, final Array lonData, final int imageWidth, final int imageHeight, final int polyDegree, final Array data, final float fillValue) {
final Index latIndex = latData.getIndex();
final Index lonIndex = lonData.getIndex();
final int numCoeffs = (polyDegree + 1) * (polyDegree + 2) / 2;
final int XOFFSET = 0;
final int YOFFSET = 1;
final int stepX = 2;
final int stepY = 2;
int numNeededPoints = 0;
for (int xi = 0; xi < imageWidth; xi += stepX) {
for (int yi = 0; yi < imageHeight; yi += stepY) {
numNeededPoints++;
}
}
computeMatrixExtremes(latData, lonData, imageWidth, imageHeight, latIndex, lonIndex);
float[] destCoords = new float[2 * numNeededPoints];
float[] srcCoords = new float[2 * numNeededPoints];
/*
* Copy source and destination coordinates into float arrays. The
* destination coordinates are scaled in order to gets values similar to
* source coordinates (values will be identical if all "real world"
* coordinates are grid indices multiplied by a constant).
*/
int offset = 0;
for (int yi = 0; yi < imageHeight; yi += stepY) {
for (int xi = 0; xi < imageWidth; xi += stepX) {
srcCoords[offset] = xi;
srcCoords[offset + 1] = yi;
destCoords[offset] = (float) ((lonData.getFloat(lonIndex.set(xi)) - this.xmin) / this.periodX);
destCoords[offset + 1] = (float) ((this.ymax - latData.getFloat(latIndex.set(yi))) / this.periodY);
// destCoords[offset + 1] = ((latData.getFloat(latIndex.set(yi))
// - this.ymin) / this.periodY);
offset += 2;
}
}
GMatrix A = new GMatrix(numNeededPoints, numCoeffs);
for (int coord = 0; coord < numNeededPoints; coord++) {
int var = 0;
for (int i = 0; i <= polyDegree; i++) {
for (int j = 0; j <= i; j++) {
double value = Math.pow(destCoords[2 * coord + XOFFSET], (double) (i - j)) * Math.pow(destCoords[2 * coord + YOFFSET], (double) j);
A.setElement(coord, var++, value);
}
}
}
GMatrix AtAi = new GMatrix(numCoeffs, numCoeffs);
GMatrix Ap = new GMatrix(numCoeffs, numNeededPoints);
AtAi.mulTransposeLeft(A, A);
AtAi.invert();
Ap.mulTransposeRight(AtAi, A);
GMatrix xVector = new GMatrix(numNeededPoints, 1);
GMatrix yVector = new GMatrix(numNeededPoints, 1);
for (int idx = 0; idx < numNeededPoints; idx++) {
xVector.setElement(idx, 0, srcCoords[2 * idx + XOFFSET]);
yVector.setElement(idx, 0, srcCoords[2 * idx + YOFFSET]);
}
GMatrix xCoeffsG = new GMatrix(numCoeffs, 1);
GMatrix yCoeffsG = new GMatrix(numCoeffs, 1);
xCoeffsG.mul(Ap, xVector);
yCoeffsG.mul(Ap, yVector);
float[] xCoeffs = new float[numCoeffs];
float[] yCoeffs = new float[numCoeffs];
for (int ii = 0; ii < numCoeffs; ii++) {
xCoeffs[ii] = new Double(xCoeffsG.getElement(ii, 0)).floatValue();
yCoeffs[ii] = new Double(yCoeffsG.getElement(ii, 0)).floatValue();
}
WritableRaster outDataCube;
WritableRandomIter iteratorDataCube;
SampleModel outSampleModel = RasterFactory.createBandedSampleModel(// data type
DataBuffer.TYPE_FLOAT, // width
imageWidth, // height
imageHeight, // num bands
1);
outDataCube = Raster.createWritableRaster(outSampleModel, null);
iteratorDataCube = RandomIterFactory.createWritable(outDataCube, null);
// Transfering data in the WritableRaster structure
Index indexInputVar = data.getIndex();
for (int jj = 0; jj < outDataCube.getNumBands(); jj++) {
for (int kk = 0; kk < outDataCube.getWidth(); kk++) {
for (int ll = 0; ll < outDataCube.getHeight(); ll++) {
iteratorDataCube.setSample(kk, ll, jj, data.getFloat(indexInputVar.set(ll, kk)));
}
}
}
WritableRaster target = RasterFactory.createWritableRaster(outSampleModel, null);
for (int bi = 0; bi < outDataCube.getNumBands(); bi++) {
for (int yi = 0; yi < imageHeight; yi++) {
for (int xi = 0; xi < imageWidth; xi++) {
GMatrix regressionVec = new GMatrix(numCoeffs, 1);
int var = 0;
for (int i = 0; i <= polyDegree; i++) {
for (int j = 0; j <= i; j++) {
double value = Math.pow(xi, (double) (i - j)) * Math.pow(yi, (double) j);
regressionVec.setElement(var++, 0, value);
}
}
GMatrix xG = new GMatrix(1, 1);
GMatrix yG = new GMatrix(1, 1);
xG.mulTransposeLeft(regressionVec, xCoeffsG);
yG.mulTransposeLeft(regressionVec, yCoeffsG);
int X = (int) Math.round(xG.getElement(0, 0));
int Y = (int) Math.round(yG.getElement(0, 0));
if (X >= 0 && Y >= 0 && X < imageWidth && Y < imageHeight) {
target.setSample(xi, yi, bi, outDataCube.getSampleFloat(X, Y, bi));
} else {
// TODO: Change with fillvalue
// target.setSample(xi, yi, bi, Float.NaN);
target.setSample(xi, yi, bi, fillValue);
}
}
}
}
return target;
}
use of javax.media.jai.iterator.WritableRandomIter in project imageio-ext by geosolutions-it.
the class NetCDFCF_CLewis_Converter method Resampler.
private WritableRaster Resampler(final Array latData, final Array lonData, final int imageWidth, final int imageHeight, final int polyDegree, final Array data, final float fillValue) {
final Index latIndex = latData.getIndex();
final Index lonIndex = lonData.getIndex();
final int numCoeffs = (polyDegree + 1) * (polyDegree + 2) / 2;
final int XOFFSET = 0;
final int YOFFSET = 1;
final int stepX = 2;
final int stepY = 2;
int numNeededPoints = 0;
for (int xi = 0; xi < imageWidth; xi += stepX) {
for (int yi = 0; yi < imageHeight; yi += stepY) {
numNeededPoints++;
}
}
computeMatrixExtremes(latData, lonData, imageWidth, imageHeight, latIndex, lonIndex);
float[] destCoords = new float[2 * numNeededPoints];
float[] srcCoords = new float[2 * numNeededPoints];
/*
* Copy source and destination coordinates into float arrays. The
* destination coordinates are scaled in order to gets values similar to
* source coordinates (values will be identical if all "real world"
* coordinates are grid indices multiplied by a constant).
*/
int offset = 0;
for (int yi = 0; yi < imageHeight; yi += stepY) {
for (int xi = 0; xi < imageWidth; xi += stepX) {
srcCoords[offset] = xi;
srcCoords[offset + 1] = yi;
destCoords[offset] = (float) ((lonData.getFloat(lonIndex.set(xi)) - this.xmin) / this.periodX);
destCoords[offset + 1] = (float) ((this.ymax - latData.getFloat(latIndex.set(yi))) / this.periodY);
// destCoords[offset + 1] = ((latData.getFloat(latIndex.set(yi)) - this.ymin) / this.periodY);
offset += 2;
}
}
GMatrix A = new GMatrix(numNeededPoints, numCoeffs);
for (int coord = 0; coord < numNeededPoints; coord++) {
int var = 0;
for (int i = 0; i <= polyDegree; i++) {
for (int j = 0; j <= i; j++) {
double value = Math.pow(destCoords[2 * coord + XOFFSET], (double) (i - j)) * Math.pow(destCoords[2 * coord + YOFFSET], (double) j);
A.setElement(coord, var++, value);
}
}
}
GMatrix AtAi = new GMatrix(numCoeffs, numCoeffs);
GMatrix Ap = new GMatrix(numCoeffs, numNeededPoints);
AtAi.mulTransposeLeft(A, A);
AtAi.invert();
Ap.mulTransposeRight(AtAi, A);
GMatrix xVector = new GMatrix(numNeededPoints, 1);
GMatrix yVector = new GMatrix(numNeededPoints, 1);
for (int idx = 0; idx < numNeededPoints; idx++) {
xVector.setElement(idx, 0, srcCoords[2 * idx + XOFFSET]);
yVector.setElement(idx, 0, srcCoords[2 * idx + YOFFSET]);
}
GMatrix xCoeffsG = new GMatrix(numCoeffs, 1);
GMatrix yCoeffsG = new GMatrix(numCoeffs, 1);
xCoeffsG.mul(Ap, xVector);
yCoeffsG.mul(Ap, yVector);
float[] xCoeffs = new float[numCoeffs];
float[] yCoeffs = new float[numCoeffs];
for (int ii = 0; ii < numCoeffs; ii++) {
xCoeffs[ii] = new Double(xCoeffsG.getElement(ii, 0)).floatValue();
yCoeffs[ii] = new Double(yCoeffsG.getElement(ii, 0)).floatValue();
}
WritableRaster outDataCube;
WritableRandomIter iteratorDataCube;
SampleModel outSampleModel = RasterFactory.createBandedSampleModel(// data type
DataBuffer.TYPE_FLOAT, // width
imageWidth, // height
imageHeight, // num bands
1);
outDataCube = Raster.createWritableRaster(outSampleModel, null);
iteratorDataCube = RandomIterFactory.createWritable(outDataCube, null);
// Transfering data in the WritableRaster structure
Index indexInputVar = data.getIndex();
for (int jj = 0; jj < outDataCube.getNumBands(); jj++) {
for (int kk = 0; kk < outDataCube.getWidth(); kk++) {
for (int ll = 0; ll < outDataCube.getHeight(); ll++) {
iteratorDataCube.setSample(kk, ll, jj, data.getFloat(indexInputVar.set(ll, kk)));
}
}
}
WritableRaster target = RasterFactory.createWritableRaster(outSampleModel, null);
for (int bi = 0; bi < outDataCube.getNumBands(); bi++) {
for (int yi = 0; yi < imageHeight; yi++) {
for (int xi = 0; xi < imageWidth; xi++) {
float[] dstCoords = new float[2];
GMatrix regressionVec = new GMatrix(numCoeffs, 1);
int var = 0;
for (int i = 0; i <= polyDegree; i++) {
for (int j = 0; j <= i; j++) {
double value = Math.pow(xi, (double) (i - j)) * Math.pow(yi, (double) j);
regressionVec.setElement(var++, 0, value);
}
}
GMatrix xG = new GMatrix(1, 1);
GMatrix yG = new GMatrix(1, 1);
xG.mulTransposeLeft(regressionVec, xCoeffsG);
yG.mulTransposeLeft(regressionVec, yCoeffsG);
int X = (int) Math.round(xG.getElement(0, 0));
int Y = (int) Math.round(yG.getElement(0, 0));
if (X >= 0 && Y >= 0 && X < imageWidth && Y < imageHeight) {
target.setSample(xi, yi, bi, outDataCube.getSampleFloat(X, Y, bi));
} else {
// TODO: Change with fillvalue
// target.setSample(xi, yi, bi, Float.NaN);
target.setSample(xi, yi, bi, fillValue);
}
}
}
}
return target;
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsTca method process.
@Execute
public void process() throws Exception {
if (!concatOr(outTca == null, doReset)) {
return;
}
checkNull(inFlow);
if (doLoopCheck) {
// prepare the loop featurecollection
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
b.setName("loop");
b.setCRS(inFlow.getCoordinateReferenceSystem());
b.add("the_geom", LineString.class);
loopFT = b.buildFeatureType();
outLoop = new DefaultFeatureCollection();
}
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
int novalue = HMConstants.getIntNovalue(inFlow);
RenderedImage flowRI = inFlow.getRenderedImage();
WritableRaster tcaWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
RandomIter flowIter = RandomIterFactory.create(flowRI, null);
WritableRandomIter tcaIter = RandomIterFactory.createWritable(tcaWR, null);
boolean loopError = false;
TreeSet<CheckPoint> passedPoints = null;
// $NON-NLS-1$
pm.beginTask("Calculating tca...", rows);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
FlowNode flowNode = new FlowNode(flowIter, cols, rows, c, r, novalue);
if (flowNode.isSource()) {
double previousTcaValue = 0.0;
if (doLoopCheck)
passedPoints = new TreeSet<>();
int index = 0;
while (flowNode != null && flowNode.isValid()) {
int col = flowNode.col;
int row = flowNode.row;
if (doLoopCheck && !passedPoints.add(new CheckPoint(col, row, index++))) {
// create a shapefile with the loop performed
GridGeometry2D gridGeometry = inFlow.getGridGeometry();
Iterator<CheckPoint> iterator = passedPoints.iterator();
GeometryFactory gf = GeometryUtilities.gf();
List<Coordinate> coordinates = new ArrayList<Coordinate>();
while (iterator.hasNext()) {
CheckPoint checkPoint = (CheckPoint) iterator.next();
DirectPosition world = gridGeometry.gridToWorld(new GridCoordinates2D(checkPoint.col, checkPoint.row));
double[] coord = world.getCoordinate();
coordinates.add(new Coordinate(coord[0], coord[1]));
}
if (coordinates.size() == 1) {
Coordinate first = coordinates.get(0);
Coordinate dummy = new Coordinate(first.x + 0.000_000_1, first.y + 0.000_000_1);
coordinates.add(dummy);
}
LineString lineString = gf.createLineString(coordinates.toArray(new Coordinate[0]));
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(loopFT);
Object[] values = new Object[] { lineString };
builder.addAll(values);
SimpleFeature feature = builder.buildFeature(null);
((DefaultFeatureCollection) outLoop).add(feature);
pm.errorMessage(MessageFormat.format("The downstream sum passed twice through the same position, there might be an error in your flowdirections. col = {0} row = {1}", col, row));
loopError = true;
break;
}
double tmpTca = tcaIter.getSampleDouble(col, row, 0);
double newTcaValue;
/*
* cumulate only if first time passing, else just propagate
*/
if (isNovalue(tmpTca)) {
tmpTca = 1.0;
newTcaValue = tmpTca + previousTcaValue;
previousTcaValue = newTcaValue;
} else {
newTcaValue = tmpTca + previousTcaValue;
}
tcaIter.setSample(col, row, 0, newTcaValue);
flowNode = flowNode.goDownstream();
}
if (loopError) {
break;
}
}
}
if (loopError) {
break;
}
pm.worked(1);
}
pm.done();
flowIter.done();
tcaIter.done();
if (loopError) {
outTca = CoverageUtilities.buildDummyCoverage();
} else {
outLoop = null;
outTca = CoverageUtilities.buildCoverageWithNovalue("tca", tcaWR, regionMap, inFlow.getCoordinateReferenceSystem(), doubleNovalue);
}
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsH2cd method process.
@Execute
public void process() throws Exception {
checkNull(inFlow, inNet);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
RenderedImage flowRI = inFlow.getRenderedImage();
WritableRaster flowWR = CoverageUtilities.renderedImage2WritableRaster(flowRI, false);
WritableRandomIter flowIter = RandomIterFactory.createWritable(flowWR, null);
RenderedImage netRI = inNet.getRenderedImage();
RandomIter netIter = RandomIterFactory.create(netRI, null);
RandomIter elevIter = null;
if (inElev != null && pMode == 1) {
RenderedImage elevRI = inElev.getRenderedImage();
elevIter = RandomIterFactory.create(elevRI, null);
}
WritableRaster h2cdWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, 0.0);
WritableRandomIter h2cdIter = RandomIterFactory.createWritable(h2cdWR, null);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
double value = netIter.getSampleDouble(c, r, 0);
if (!isNovalue(value))
flowIter.setSample(c, r, 0, 10.0);
}
}
if (pMode == 1) {
ModelsEngine.topologicalOutletdistance(flowIter, elevIter, h2cdIter, regionMap, pm);
} else {
ModelsEngine.outletdistance(flowIter, h2cdIter, regionMap, pm);
}
for (int j = 0; j < rows; j++) {
for (int i = 0; i < cols; i++) {
if (!isNovalue(netIter.getSampleDouble(i, j, 0)) && !isNovalue(flowIter.getSampleDouble(i, j, 0))) {
h2cdIter.setSample(i, j, 0, 0.0);
} else if (isNovalue(flowIter.getSampleDouble(i, j, 0))) {
h2cdIter.setSample(i, j, 0, HMConstants.doubleNovalue);
}
}
}
// $NON-NLS-1$
outH2cd = CoverageUtilities.buildCoverage("h2cd", h2cdWR, regionMap, inFlow.getCoordinateReferenceSystem());
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsTc method process.
@Execute
public void process() throws Exception {
if (!concatOr(outTc3 == null, outTc9 == null, doReset)) {
return;
}
checkNull(inProf, inTan);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inProf);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
WritableRaster tc3WR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
WritableRaster tc9WR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
WritableRandomIter tc3Iter = RandomIterFactory.createWritable(tc3WR, null);
WritableRandomIter tc9Iter = RandomIterFactory.createWritable(tc9WR, null);
RandomIter profIter = CoverageUtilities.getRandomIterator(inProf);
RandomIter tangIter = CoverageUtilities.getRandomIterator(inTan);
// calculate ...
// $NON-NLS-1$ //$NON-NLS-2$
pm.beginTask(msg.message("working") + "tc9...", rows);
for (int j = 0; j < rows; j++) {
for (int i = 0; i < cols; i++) {
double tangValue = tangIter.getSampleDouble(i, j, 0);
if (isNovalue(tangValue)) {
tc9Iter.setSample(i, j, 0, HMConstants.doubleNovalue);
} else {
double profValue = profIter.getSampleDouble(i, j, 0);
if (Math.abs(tangValue) <= pTanthres) {
if (Math.abs(profValue) <= pProfthres) {
tc9Iter.setSample(i, j, 0, 10);
} else if (profValue < -pProfthres) {
tc9Iter.setSample(i, j, 0, 20);
} else if (profValue > pProfthres) {
tc9Iter.setSample(i, j, 0, 30);
}
} else if (tangValue < -pTanthres) {
if (Math.abs(profValue) <= pProfthres) {
tc9Iter.setSample(i, j, 0, 40);
} else if (profValue < -pProfthres) {
tc9Iter.setSample(i, j, 0, 50);
} else if (profValue > pProfthres) {
tc9Iter.setSample(i, j, 0, 60);
}
} else if (tangValue > pTanthres) {
if (Math.abs(profValue) <= pProfthres) {
tc9Iter.setSample(i, j, 0, 70);
} else if (profValue < -pProfthres) {
tc9Iter.setSample(i, j, 0, 80);
} else if (profValue > pProfthres) {
tc9Iter.setSample(i, j, 0, 90);
}
}
}
}
pm.worked(1);
}
pm.done();
profIter.done();
tangIter.done();
// $NON-NLS-1$ //$NON-NLS-2$
pm.beginTask(msg.message("working") + "tc3...", rows);
for (int j = 0; j < rows; j++) {
for (int i = 0; i < cols; i++) {
double cp9Value = tc9Iter.getSampleDouble(i, j, 0);
if (!isNovalue(cp9Value)) {
if (cp9Value == 70 || cp9Value == 90 || cp9Value == 30) {
tc3Iter.setSample(i, j, 0, 15);
} else if (cp9Value == 10) {
tc3Iter.setSample(i, j, 0, 25);
} else {
tc3Iter.setSample(i, j, 0, 35);
}
} else {
tc3Iter.setSample(i, j, 0, cp9Value);
}
}
pm.worked(1);
}
pm.done();
// $NON-NLS-1$
outTc3 = CoverageUtilities.buildCoverage("tc3", tc3WR, regionMap, inProf.getCoordinateReferenceSystem());
// $NON-NLS-1$
outTc9 = CoverageUtilities.buildCoverage("tc9", tc9WR, regionMap, inProf.getCoordinateReferenceSystem());
}
Aggregations