use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class MultiRasterLoopProcessor method loop.
public GridCoverage2D loop(IDataLoopFunction function, Double noValue, GridCoverage2D... rasters) {
GridCoverage2D refRaster = null;
for (GridCoverage2D raster : rasters) {
if (raster != null) {
refRaster = raster;
break;
}
}
if (refRaster == null) {
throw new IllegalArgumentException("At least one raster needs to be non null.");
}
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(rasters[0]);
int rows = regionMap.getRows();
int cols = regionMap.getCols();
double nv = HMConstants.doubleNovalue;
if (noValue != null) {
nv = noValue;
}
WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, nv);
WritableRandomIter outIter = CoverageUtilities.getWritableRandomIterator(outWR);
RandomIter[] iters = new RandomIter[rasters.length];
for (int i = 0; i < iters.length; i++) {
if (rasters[i] != null) {
iters[i] = CoverageUtilities.getRandomIterator(rasters[i]);
}
}
try {
pm.beginTask(taskName, rows);
double[] values = new double[rasters.length];
for (int r = 0; r < rows; r++) {
if (pm.isCanceled()) {
return null;
}
for (int c = 0; c < cols; c++) {
for (int i = 0; i < iters.length; i++) {
if (iters[i] != null) {
values[i] = iters[i].getSampleDouble(c, r, 0);
} else {
values[i] = nv;
}
}
double result = function.process(values);
outIter.setSample(c, r, 0, result);
}
pm.worked(1);
}
pm.done();
} finally {
for (RandomIter randomIter : iters) {
if (randomIter != null) {
randomIter.done();
}
}
outIter.done();
}
return CoverageUtilities.buildCoverageWithNovalue("raster", outWR, regionMap, rasters[0].getCoordinateReferenceSystem(), noValue);
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsLinesRasterizer method process.
@Execute
public void process() throws Exception {
checkNull(inVector);
if (pNorth == null || pSouth == null || pWest == null || pEast == null || pRows == null || pCols == null) {
throw new ModelsIllegalargumentException("It is necessary to supply all the information about the processing region. Did you set the boundaries and rows/cols?", this, pm);
}
SimpleFeatureType schema = inVector.getSchema();
CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
GridGeometry2D inGrid = gridGeometryFromRegionValues(pNorth, pSouth, pEast, pWest, pCols, pRows, crs);
if (!EGeometryType.isLine(schema.getGeometryDescriptor())) {
throw new ModelsRuntimeException("The module works only with line vectors.", this);
}
RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(inGrid);
double n = regionMap.getNorth();
double s = regionMap.getSouth();
double e = regionMap.getEast();
double w = regionMap.getWest();
double xRes = regionMap.getXres();
double yRes = regionMap.getYres();
double step = Math.min(xRes, yRes);
WritableRaster outWR = CoverageUtilities.createWritableRaster(regionMap.getCols(), regionMap.getRows(), null, null, HMConstants.doubleNovalue);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
List<FeatureMate> matesList = FeatureUtilities.featureCollectionToMatesList(inVector);
pm.beginTask("Rasterizing lines...", matesList.size());
String fCatChecked = null;
for (FeatureMate featureMate : matesList) {
Geometry geometry = featureMate.getGeometry();
for (int i = 0; i < geometry.getNumGeometries(); i++) {
Geometry geometryN = geometry.getGeometryN(i);
List<Coordinate> lineCoordinatesAtStep = GeometryUtilities.getCoordinatesAtInterval((LineString) geometryN, step, true, -1, -1);
double cat;
if (fCat == null) {
cat = pCat;
} else {
if (fCatChecked == null) {
fCatChecked = FeatureUtilities.findAttributeName(featureMate.getFeature().getFeatureType(), fCat);
if (fCatChecked == null) {
throw new ModelsIllegalargumentException("Could not find an attribute named: " + fCat, this, pm);
}
}
cat = featureMate.getAttribute(fCat, Double.class);
}
for (Coordinate lineCoordinate : lineCoordinatesAtStep) {
if (!NumericsUtilities.isBetween(lineCoordinate.x, w, e) || !NumericsUtilities.isBetween(lineCoordinate.y, s, n)) {
continue;
}
GridCoordinates2D onGrid = inGrid.worldToGrid(new DirectPosition2D(lineCoordinate.x, lineCoordinate.y));
outIter.setSample(onGrid.x, onGrid.y, 0, cat);
}
}
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 OmsCarver method simpleMode.
private void simpleMode() throws Exception {
int cols = regionMap.getCols();
int rows = regionMap.getRows();
GridCoverage2D depthLinesRaster = null;
GridCoverage2D depthPolygonsRaster = null;
if (inCarverLines != null) {
OmsLinesRasterizer lr = new OmsLinesRasterizer();
lr.fCat = fDepthLines;
lr.pCat = pDepthLines;
lr.inVector = inCarverLines;
lr.pNorth = regionMap.getNorth();
lr.pSouth = regionMap.getSouth();
lr.pEast = regionMap.getEast();
lr.pWest = regionMap.getWest();
lr.pRows = rows;
lr.pCols = cols;
lr.pm = pm;
lr.process();
depthLinesRaster = lr.outRaster;
}
if (inCarverPolygons != null) {
OmsScanLineRasterizer raster = new OmsScanLineRasterizer();
raster.inVector = inCarverPolygons;
raster.pNorth = regionMap.getNorth();
raster.pSouth = regionMap.getSouth();
raster.pEast = regionMap.getEast();
raster.pWest = regionMap.getWest();
raster.pRows = rows;
raster.pCols = cols;
raster.fCat = fDepthLines;
raster.pValue = pDepthLines;
raster.pm = pm;
raster.process();
depthPolygonsRaster = raster.outRaster;
}
GridCoverage2D finalCarveGC = null;
if (depthLinesRaster != null && depthPolygonsRaster != null) {
finalCarveGC = CoverageUtilities.mergeCoverages(depthPolygonsRaster, depthLinesRaster);
} else if (depthLinesRaster == null && depthPolygonsRaster != null) {
finalCarveGC = depthPolygonsRaster;
} else if (depthLinesRaster != null && depthPolygonsRaster == null) {
finalCarveGC = depthLinesRaster;
} else {
throw new ModelsIllegalargumentException("At least one lines or one polygon carving layer needs to be supplied.", this, pm);
}
RandomIter dtmIter = CoverageUtilities.getRandomIterator(inRaster);
RandomIter depthIter = CoverageUtilities.getRandomIterator(finalCarveGC);
WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
try {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
double dtmValue = dtmIter.getSampleDouble(x, y, 0);
if (HMConstants.isNovalue(dtmValue)) {
continue;
}
double depthValue = depthIter.getSampleDouble(x, y, 0);
double newValue;
if (HMConstants.isNovalue(depthValue)) {
newValue = dtmValue;
} else {
newValue = dtmValue - depthValue;
}
outIter.setSample(x, y, 0, newValue);
}
}
} finally {
dtmIter.done();
depthIter.done();
outIter.done();
}
outRaster = CoverageUtilities.buildCoverage("outraster", outWR, regionMap, inRaster.getCoordinateReferenceSystem());
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsDownSlopeConnectivity method process.
// VARS DOC END
@Execute
public void process() throws Exception {
checkNull(inFlow, inNet, inSlope);
if (pWeight == null && inWeights == null) {
throw new ModelsIllegalargumentException("At lest one weight definition needs to be supplied.", this);
}
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
int nCols = regionMap.getCols();
int nRows = regionMap.getRows();
double xres = regionMap.getXres();
double yres = regionMap.getYres();
RandomIter flowIter = CoverageUtilities.getRandomIterator(inFlow);
RandomIter netIter = CoverageUtilities.getRandomIterator(inNet);
RandomIter slopeIter = CoverageUtilities.getRandomIterator(inSlope);
int flowNv = HMConstants.getIntNovalue(inFlow);
double netNv = HMConstants.getNovalue(inNet);
RandomIter weightsIter;
if (inWeights != null) {
weightsIter = CoverageUtilities.getRandomIterator(inWeights);
} else {
weightsIter = new ConstantRandomIter(pWeight);
}
WritableRaster[] connectivityRasterHolder = new WritableRaster[1];
outConnectivity = CoverageUtilities.createCoverageFromTemplate(inFlow, HMConstants.doubleNovalue, connectivityRasterHolder);
WritableRandomIter connectivityIter = CoverageUtilities.getWritableRandomIterator(connectivityRasterHolder[0]);
pm.beginTask("Calculate downslope connectivity...", nRows);
for (int r = 0; r < nRows; r++) {
if (pm.isCanceled()) {
return;
}
for (int c = 0; c < nCols; c++) {
FlowNode flowNode = new FlowNode(flowIter, nCols, nRows, c, r, flowNv);
if (!flowNode.isValid()) {
continue;
}
GridNode netNode = new GridNode(netIter, nCols, nRows, xres, yres, c, r, netNv);
double connectivitySum = 0;
while (flowNode.isValid() && !netNode.isValid()) {
FlowNode nextFlowNode = flowNode.goDownstream();
if (nextFlowNode == null) {
throw new ModelsRuntimeException("Could not properly navigate the flowdirections. Are you using an extracted basin?", this);
}
int col = flowNode.col;
int nextCol = nextFlowNode.col;
int row = flowNode.row;
int nextRow = nextFlowNode.row;
double distance = sqrt(pow((nextCol - col) * xres, 2.0) + pow((nextRow - row) * yres, 2.0));
double weight = weightsIter.getSampleDouble(flowNode.col, flowNode.row, 0);
double slope = slopeIter.getSampleDouble(flowNode.col, flowNode.row, 0);
if (slope == 0.0) {
slope = 0.005;
}
double Di = distance / weight / slope;
connectivitySum = connectivitySum + Di;
flowNode = nextFlowNode;
netNode = new GridNode(netIter, nCols, nRows, xres, yres, nextFlowNode.col, nextFlowNode.row, netNv);
}
connectivityIter.setSample(c, r, 0, connectivitySum);
}
pm.worked(1);
}
pm.done();
}
Aggregations