use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsDrainDir method orlandiniD8LAD.
private void orlandiniD8LAD(int[] indexes, WritableRaster deviationsWR, BitMatrix analizedMatrix, WritableRaster pitWR, WritableRaster flowWR, WritableRaster tcaWR, WritableRaster dirWR, int nelev) {
RandomIter pitRandomIter = RandomIterFactory.create(pitWR, null);
RandomIter flowRandomIter = RandomIterFactory.create(flowWR, null);
WritableRandomIter tcaRandomIter = RandomIterFactory.createWritable(tcaWR, null);
WritableRandomIter deviationRandomIter = RandomIterFactory.createWritable(deviationsWR, null);
WritableRandomIter dirRandomIter = RandomIterFactory.createWritable(dirWR, null);
try {
int ncelle = 0;
pm.beginTask(msg.message("draindir.orlandinilad"), rows * cols);
for (int i = rows * cols - 1; i >= 0; i--) {
if (pm.isCanceled()) {
return;
}
double count = indexes[i] - 1;
int row = (int) Math.floor(count / cols);
int col = (int) (count % cols);
try {
if (!isNovalue(pitRandomIter.getSampleDouble(col, row, 0)) && !isNovalue(flowRandomIter.getSample(col, row, 0))) {
ncelle = ncelle + 1;
double[] maxSlopeData = calculateMaximumSlope(analizedMatrix, pitRandomIter, tcaRandomIter, col, row);
if (maxSlopeData[1] > 0) {
double dev1 = maxSlopeData[2];
double dev2 = ((PI / 4) - maxSlopeData[2]);
if (maxSlopeData[9] == 1) {
dev2 = -dev2;
} else {
dev1 = -dev1;
}
calculateDrainageArea(row, col, maxSlopeData, analizedMatrix, deviationRandomIter, pitRandomIter, tcaRandomIter, dirRandomIter, i, i);
double sumdev = maxSlopeData[6];
double sumdev1 = dev1 + (pLambda * sumdev);
double sumdev2 = dev2 + (pLambda * sumdev);
if ((Math.abs(sumdev1) <= Math.abs(sumdev2)) && ((maxSlopeData[3] - maxSlopeData[4]) > 0.0)) {
dirRandomIter.setSample(col, row, 0, maxSlopeData[7]);
deviationRandomIter.setSample(col, row, 0, sumdev1);
} else if (Math.abs(sumdev1) > Math.abs(sumdev2) || (maxSlopeData[3] - maxSlopeData[5]) > 0.0) {
dirRandomIter.setSample(col, row, 0, maxSlopeData[8]);
deviationRandomIter.setSample(col, row, 0, sumdev2);
} else {
break;
}
} else if (maxSlopeData[1] == 0) {
if (ncelle == nelev) {
/* sono all'uscita */
calculateDrainageArea(row, col, maxSlopeData, analizedMatrix, deviationRandomIter, pitRandomIter, tcaRandomIter, dirRandomIter, cols, rows);
dirRandomIter.setSample(col, row, 0, FlowNode.OUTLET);
deviationRandomIter.setSample(col, row, 0, pLambda * maxSlopeData[6]);
pm.done();
return;
} else {
calculateDrainageArea(row, col, maxSlopeData, analizedMatrix, deviationRandomIter, pitRandomIter, tcaRandomIter, dirRandomIter, cols, rows);
double sumdev = pLambda * maxSlopeData[6];
dirRandomIter.setSample(col, row, 0, flowRandomIter.getSample(col, row, 0));
int flow = dirRandomIter.getSample(col, row, 0);
int nr = row + order[flow][0];
int nc = col + order[flow][1];
while (analizedMatrix.isMarked(nc, nr)) {
tcaRandomIter.setSample(nc, nr, 0, tcaRandomIter.getSample(nc, nr, 0) + tcaRandomIter.getSample(col, row, 0));
flow = dirRandomIter.getSample(nc, nr, 0);
nr = nr + order[(int) flow][0];
nc = nc + order[(int) flow][1];
}
deviationRandomIter.setSample(col, row, 0, sumdev);
}
}
}
} catch (Exception e) {
pm.errorMessage("Error in col: " + col + " row: " + row);
e.printStackTrace();
}
pm.worked(1);
}
pm.done();
} finally {
dirRandomIter.done();
pitRandomIter.done();
flowRandomIter.done();
deviationRandomIter.done();
tcaRandomIter.done();
}
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsDrainDir method process.
/**
* Calculates new drainage directions
*
* @throws Exception
*/
@Execute
public void process() throws Exception {
if (!concatOr(outFlow == null, doReset)) {
return;
}
checkNull(inFlow, inPit);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inPit);
cols = regionMap.getCols();
rows = regionMap.getRows();
xRes = regionMap.getXres();
yRes = regionMap.getYres();
dxySqrt = Math.sqrt(xRes * xRes + yRes * yRes);
RenderedImage pitfillerRI = inPit.getRenderedImage();
WritableRaster pitfillerWR = CoverageUtilities.renderedImage2DoubleWritableRaster(pitfillerRI, true);
RenderedImage flowRI = inFlow.getRenderedImage();
WritableRaster flowWR = CoverageUtilities.renderedImage2ShortWritableRaster(flowRI, true);
RandomIter pitRandomIter = RandomIterFactory.create(pitfillerWR, null);
// create new matrix
double[] orderedelev = new double[cols * rows];
int[] indexes = new int[cols * rows];
int nelev = 0;
for (int r = 0; r < rows; r++) {
if (pm.isCanceled()) {
return;
}
for (int c = 0; c < cols; c++) {
double pitValue = pitRandomIter.getSampleDouble(c, r, 0);
int pos = (r * cols) + c;
orderedelev[pos] = pitValue;
indexes[pos] = pos + 1;
if (!isNovalue(pitValue)) {
nelev = nelev + 1;
}
}
}
QuickSortAlgorithm t = new QuickSortAlgorithm(pm);
t.sort(orderedelev, indexes);
pm.message(msg.message("draindir.initializematrix"));
// Initialize new RasterData and set value
WritableRaster tcaWR = CoverageUtilities.createWritableRaster(cols, rows, Integer.class, null, HMConstants.intNovalue);
WritableRaster dirWR = CoverageUtilities.createWritableRaster(cols, rows, Short.class, null, HMConstants.shortNovalue);
// it contains the analyzed cells
WritableRaster deviationsWR = CoverageUtilities.createWritableRaster(cols, rows, Double.class, null, null);
BitMatrix analizedMatrix = new BitMatrix(cols, rows);
if (doLad) {
orlandiniD8LAD(indexes, deviationsWR, analizedMatrix, pitfillerWR, flowWR, tcaWR, dirWR, nelev);
} else {
orlandiniD8LTD(indexes, deviationsWR, analizedMatrix, pitfillerWR, flowWR, tcaWR, dirWR, nelev);
if (pm.isCanceled()) {
return;
}
// only if required executes this method
if (inFlownet != null) {
newDirections(pitfillerWR, dirWR);
}
}
if (pm.isCanceled()) {
return;
}
outFlow = CoverageUtilities.buildCoverageWithNovalue("draindir", dirWR, regionMap, inPit.getCoordinateReferenceSystem(), HMConstants.intNovalue);
outTca = CoverageUtilities.buildCoverageWithNovalue("tca", tcaWR, regionMap, inPit.getCoordinateReferenceSystem(), HMConstants.shortNovalue);
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsDrainDir method newDirections.
/**
* The fixed network method allows you to assign a known channel network and
* to then correct the drainage directions.
*/
private void newDirections(WritableRaster pitWR, WritableRaster dirWR) {
int[][] odir = { { 0, 0, 0 }, { 0, 1, 1 }, { -1, 1, 2 }, { -1, 0, 3 }, { -1, -1, 4 }, { 0, -1, 5 }, { 1, -1, 6 }, { 1, 0, 7 }, { 1, 1, 8 }, { 0, 0, 9 }, { 0, 0, 10 } };
int[] flow = new int[2], nflow = new int[2];
RandomIter pitRandomIter = RandomIterFactory.create(pitWR, null);
RandomIter flowFixedIter = CoverageUtilities.getRandomIterator(inFlownet);
WritableRandomIter dirRandomIter = RandomIterFactory.createWritable(dirWR, null);
WritableRaster modifiedFlowWR = CoverageUtilities.createWritableRaster(pitWR.getWidth(), pitWR.getHeight(), Integer.class, null, null);
WritableRandomIter modflowRandomIter = RandomIterFactory.createWritable(modifiedFlowWR, null);
try {
pm.beginTask("Correcting drainage directions...", rows);
for (int j = 0; j < rows; j++) {
if (pm.isCanceled()) {
return;
}
for (int i = 0; i < cols; i++) {
if (!isNovalue(flowFixedIter.getSample(i, j, 0))) {
flow[0] = i;
flow[1] = j;
for (int k = 1; k <= 8; k++) {
nflow[0] = flow[0] + odir[k][1];
nflow[1] = flow[1] + odir[k][0];
if (modflowRandomIter.getSample(nflow[0], nflow[1], 0) == 0 && isNovalue(flowFixedIter.getSample(nflow[0], nflow[1], 0))) {
double elev = pitRandomIter.getSampleDouble(nflow[0] + odir[1][1], nflow[1] + odir[1][0], 0);
for (int n = 2; n <= 8; n++) {
if (nflow[0] + odir[n][0] >= 0 && nflow[0] + odir[n][1] < rows && nflow[1] + odir[n][0] >= 0 && nflow[1] + odir[n][0] < cols) {
double tmpElev = pitRandomIter.getSampleDouble(nflow[0] + odir[n][1], nflow[1] + odir[n][0], 0);
if (tmpElev >= elev) {
elev = tmpElev;
dirRandomIter.setSample(nflow[0], nflow[1], 0, odir[n][2]);
}
}
}
for (int s = 1; s <= 8; s++) {
if (nflow[0] + odir[s][0] >= 0 && nflow[0] + odir[s][0] < rows && nflow[1] + odir[s][1] >= 0 && nflow[1] + odir[s][1] < cols) {
if (!isNovalue(flowFixedIter.getSample(nflow[0] + odir[s][1], nflow[1] + odir[s][0], 0))) {
double tmpElev = pitRandomIter.getSampleDouble(nflow[0] + odir[s][1], nflow[1] + odir[s][0], 0);
if (tmpElev <= elev) {
elev = tmpElev;
dirRandomIter.setSample(nflow[0], nflow[1], 0, odir[s][2]);
}
}
}
}
modflowRandomIter.setSample(nflow[0], nflow[1], 0, 1);
}
}
}
if (!isNovalue(flowFixedIter.getSample(i, j, 0))) {
dirRandomIter.setSample(i, j, 0, flowFixedIter.getSample(i, j, 0));
}
}
pm.worked(1);
}
pm.done();
} finally {
dirRandomIter.done();
pitRandomIter.done();
modflowRandomIter.done();
flowFixedIter.done();
}
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsLeastCostFlowDirections method process.
@Execute
public void process() throws Exception {
if (!concatOr(outFlow == null, doReset)) {
return;
}
checkNull(inElev);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
cols = regionMap.getCols();
rows = regionMap.getRows();
double xRes = regionMap.getXres();
double yRes = regionMap.getYres();
RandomIter elevationIter = CoverageUtilities.getRandomIterator(inElev);
WritableRaster flowWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
flowIter = CoverageUtilities.getWritableRandomIterator(flowWR);
WritableRaster tcaWR = null;
if (doTca) {
tcaWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
tcaIter = CoverageUtilities.getWritableRandomIterator(tcaWR);
}
WritableRaster slopeWR = null;
if (doSlope) {
slopeWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
slopeIter = CoverageUtilities.getWritableRandomIterator(slopeWR);
}
WritableRaster aspectWR = null;
if (doAspect) {
aspectWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
aspectIter = CoverageUtilities.getWritableRandomIterator(aspectWR);
}
orderedNodes = new TreeSet<GridNode>(new GridNodeElevationToLeastComparator());
assignedFlowsMap = new BitMatrix(cols, rows);
double novalue = HMConstants.getNovalue(inElev);
pm.beginTask("Check for potential outlets...", rows);
int nonValidCellsNum = 0;
for (int r = 0; r < rows; r++) {
if (pm.isCanceled()) {
return;
}
for (int c = 0; c < cols; c++) {
GridNode node = new GridNode(elevationIter, cols, rows, xRes, yRes, c, r, novalue);
if (!node.isValid()) {
nonValidCellsNum++;
assignedFlowsMap.mark(c, r);
continue;
}
if (node.touchesBound()) {
orderedNodes.add(node);
if (doExcludeBorder) {
assignedFlowsMap.mark(c, r);
} else {
flowIter.setSample(c, r, 0, Direction.getOutletValue());
}
}
}
pm.worked(1);
}
pm.done();
pm.beginTask("Extract flowdirections...", (rows * cols - nonValidCellsNum));
GridNode lowestNode = null;
while ((lowestNode = orderedNodes.pollFirst()) != null) {
/*
* set the current cell as marked. If it is an alone one,
* it will stay put as an outlet (if we do not mark it, it
* might get overwritten. Else il will be redundantly set
* later again.
*/
assignedFlowsMap.mark(lowestNode.col, lowestNode.row);
List<GridNode> surroundingNodes = lowestNode.getSurroundingNodes();
/*
* vertical and horiz cells, if they exist, are
* set to flow inside the current cell and added to the
* list of cells to process.
*/
GridNode e = surroundingNodes.get(0);
if (nodeOk(e)) {
// flow in current and get added to the list of nodes to process by elevation
// order
setNodeValues(e, E.getEnteringFlow());
}
GridNode n = surroundingNodes.get(2);
if (nodeOk(n)) {
setNodeValues(n, N.getEnteringFlow());
}
GridNode w = surroundingNodes.get(4);
if (nodeOk(w)) {
setNodeValues(w, W.getEnteringFlow());
}
GridNode s = surroundingNodes.get(6);
if (nodeOk(s)) {
setNodeValues(s, S.getEnteringFlow());
}
/*
* diagonal cells are processed only if they are valid and
* they are not steeper than their attached vertical and horiz cells.
*/
GridNode en = surroundingNodes.get(1);
if (nodeOk(en) && assignFlowDirection(lowestNode, en, e, n)) {
setNodeValues(en, EN.getEnteringFlow());
}
GridNode nw = surroundingNodes.get(3);
if (nodeOk(nw) && assignFlowDirection(lowestNode, nw, n, w)) {
setNodeValues(nw, NW.getEnteringFlow());
}
GridNode ws = surroundingNodes.get(5);
if (nodeOk(ws) && assignFlowDirection(lowestNode, ws, w, s)) {
setNodeValues(ws, WS.getEnteringFlow());
}
GridNode se = surroundingNodes.get(7);
if (nodeOk(se) && assignFlowDirection(lowestNode, se, s, e)) {
setNodeValues(se, SE.getEnteringFlow());
}
}
pm.done();
CoordinateReferenceSystem crs = inElev.getCoordinateReferenceSystem();
outFlow = CoverageUtilities.buildCoverageWithNovalue("flowdirections", flowWR, regionMap, crs, doubleNovalue);
if (doTca)
outTca = CoverageUtilities.buildCoverageWithNovalue("tca", tcaWR, regionMap, crs, doubleNovalue);
if (doSlope)
outSlope = CoverageUtilities.buildCoverageWithNovalue("slope", slopeWR, regionMap, crs, doubleNovalue);
if (doAspect)
outAspect = CoverageUtilities.buildCoverageWithNovalue("aspect", aspectWR, regionMap, crs, doubleNovalue);
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsGc method createGCRaster.
private WritableRaster[] createGCRaster() {
// get rows and cols from the active region
int cols = regionMap.get(CoverageUtilities.COLS).intValue();
int rows = regionMap.get(CoverageUtilities.ROWS).intValue();
RandomIter slopeIter = CoverageUtilities.getRandomIterator(inSlope);
RandomIter netIter = CoverageUtilities.getRandomIterator(inNetwork);
RandomIter cp9Iter = CoverageUtilities.getRandomIterator(inCp9);
WritableRaster cpClassWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
WritableRandomIter cpClassIter = RandomIterFactory.createWritable(cpClassWR, null);
WritableRaster cpAggClassWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
WritableRandomIter cpAggClassIter = RandomIterFactory.createWritable(cpAggClassWR, null);
// calculate ...
pm.beginTask(msg.message("working") + "gc... (1/2)", rows);
for (int j = 0; j < rows; j++) {
for (int i = 0; i < cols; i++) {
// threshold
if (slopeIter.getSampleDouble(i, j, 0) >= pTh) {
cpClassIter.setSample(i, j, 0, 110);
} else // individuates the network
if (netIter.getSampleDouble(i, j, 0) == 2) {
cpClassIter.setSample(i, j, 0, 100);
} else {
cpClassIter.setSample(i, j, 0, cp9Iter.getSampleDouble(i, j, 0));
}
if (isNovalue(slopeIter.getSampleDouble(i, j, 0))) {
cpClassIter.setSample(i, j, 0, doubleNovalue);
}
}
pm.worked(1);
}
pm.done();
netIter = null;
slopeIter = null;
pm.beginTask(msg.message("working") + "gc... (2/2)", rows);
// 55 ? ravine sites (slope > critic value) (class 110).
for (int j = 0; j < rows; j++) {
for (int i = 0; i < cols; i++) {
if (cpClassIter.getSample(i, j, 0) == 70 || cpClassIter.getSampleDouble(i, j, 0) == 90 || cpClassIter.getSampleDouble(i, j, 0) == 30) {
cpAggClassIter.setSample(i, j, 0, 15);
} else if (cpClassIter.getSampleDouble(i, j, 0) == 10) {
cpAggClassIter.setSample(i, j, 0, 25);
} else if (cpClassIter.getSampleDouble(i, j, 0) == 100) {
cpAggClassIter.setSample(i, j, 0, 35);
} else if (cpClassIter.getSampleDouble(i, j, 0) == 110) {
cpAggClassIter.setSample(i, j, 0, 55);
} else if (!isNovalue(cpClassIter.getSampleDouble(i, j, 0))) {
cpAggClassIter.setSample(i, j, 0, 45);
} else if (isNovalue(cpClassIter.getSampleDouble(i, j, 0))) {
cpAggClassIter.setSample(i, j, 0, doubleNovalue);
}
}
pm.worked(1);
}
pm.done();
return new WritableRaster[] { cpClassWR, cpAggClassWR };
}
Aggregations