use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsMorpher method lineendings.
public static void lineendings(WritableRaster inWR, RegionMap regionMap, WritableRaster outWR, int[][] kernels) {
int cols = regionMap.getCols();
int rows = regionMap.getRows();
WritableRandomIter inIter = RandomIterFactory.createWritable(inWR, null);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
BinaryFast binaryData = toBinaryFast(cols, rows, inIter);
new Thin().processLineendings(binaryData, kernels);
int[] values = binaryData.getValues();
int index = 0;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
double value = (double) values[index];
if (value == 0) {
value = doubleNovalue;
}
double origValue = inIter.getSampleDouble(x, y, 0);
if (!isNovalue(origValue) && isNovalue(value)) {
outIter.setSample(x, y, 0, 1);
}
index++;
}
}
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsMorpher method prune.
public static void prune(WritableRaster inWR, RegionMap regionMap, WritableRaster outWR, int[][] kernels, int iterations) {
int cols = regionMap.getCols();
int rows = regionMap.getRows();
WritableRandomIter inIter = RandomIterFactory.createWritable(inWR, null);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
BinaryFast binaryData = toBinaryFast(cols, rows, inIter);
new Thin().processPruning(binaryData, iterations, kernels);
fromBinaryFast(cols, rows, outIter, binaryData);
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsRasterNormalizer method process.
@Execute
public void process() throws Exception {
checkNull(inRaster);
double[] minMax = OmsRasterSummary.getMinMax(inRaster);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
int nCols = regionMap.getCols();
int nRows = regionMap.getRows();
RandomIter rasterIter = CoverageUtilities.getRandomIterator(inRaster);
WritableRaster outWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
pm.beginTask("Normalizing...", nRows);
for (int r = 0; r < nRows; r++) {
if (pm.isCanceled()) {
return;
}
for (int c = 0; c < nCols; c++) {
double value = rasterIter.getSampleDouble(c, r, 0);
if (isNovalue(value)) {
if (doSetnovalues) {
outIter.setSample(c, r, 0, 0.0);
}
continue;
}
double normalizedValue = NumericsUtilities.normalize(minMax[1], minMax[0], value, pNValue);
outIter.setSample(c, r, 0, normalizedValue);
}
pm.worked(1);
}
pm.done();
outRaster = CoverageUtilities.buildCoverage("normalized", outWR, regionMap, inRaster.getCoordinateReferenceSystem());
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsRasterConverter method process.
@Execute
public void process() throws Exception {
checkNull(inRaster);
if (pOutType == null || pOutType.length() == 0) {
outRaster = inRaster;
} else {
// convert type
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
int nCols = regionMap.getCols();
int nRows = regionMap.getRows();
RandomIter inIter = CoverageUtilities.getRandomIterator(inRaster);
WritableRaster outWritableRaster = null;
WritableRandomIter outIter = null;
try {
switch(pOutType) {
case TYPE_INT:
outWritableRaster = CoverageUtilities.createWritableRaster(nCols, nRows, Integer.class, null, null);
outIter = CoverageUtilities.getWritableRandomIterator(outWritableRaster);
pm.beginTask("Converting...", nRows);
for (int r = 0; r < nRows; r++) {
if (pm.isCanceled()) {
return;
}
for (int c = 0; c < nCols; c++) {
int value = inIter.getSample(c, r, 0);
outIter.setSample(c, r, 0, value);
}
pm.worked(1);
}
pm.done();
break;
case TYPE_FLOAT:
outWritableRaster = CoverageUtilities.createWritableRaster(nCols, nRows, Float.class, null, null);
outIter = CoverageUtilities.getWritableRandomIterator(outWritableRaster);
try {
pm.beginTask("Converting...", nRows);
for (int r = 0; r < nRows; r++) {
if (pm.isCanceled()) {
return;
}
for (int c = 0; c < nCols; c++) {
float value = inIter.getSampleFloat(c, r, 0);
outIter.setSample(c, r, 0, value);
}
pm.worked(1);
}
pm.done();
} finally {
outIter.done();
}
break;
case TYPE_DOUBLE:
default:
outWritableRaster = CoverageUtilities.createWritableRaster(nCols, nRows, Double.class, null, null);
outIter = CoverageUtilities.getWritableRandomIterator(outWritableRaster);
try {
pm.beginTask("Converting...", nRows);
for (int r = 0; r < nRows; r++) {
if (pm.isCanceled()) {
return;
}
for (int c = 0; c < nCols; c++) {
double value = inIter.getSampleDouble(c, r, 0);
outIter.setSample(c, r, 0, value);
}
pm.worked(1);
}
pm.done();
} finally {
outIter.done();
}
break;
}
outRaster = CoverageUtilities.buildCoverage("converted", outWritableRaster, regionMap, inRaster.getCoordinateReferenceSystem());
} finally {
outIter.done();
inIter.done();
}
}
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class OmsBobTheBuilder method process.
@Execute
public void process() throws Exception {
checkNull(inRaster, inArea, inElevations, fElevation);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
double west = regionMap.getWest();
double east = regionMap.getEast();
double south = regionMap.getSouth();
double north = regionMap.getNorth();
ReferencedEnvelope vectorBounds = inArea.getBounds();
if (!isBetween(vectorBounds.getMaxX(), west, east) || !isBetween(vectorBounds.getMinX(), west, east) || !isBetween(vectorBounds.getMaxY(), south, north) || !isBetween(vectorBounds.getMinY(), south, north)) {
throw new ModelsIllegalargumentException("The vector map has to be within the raster map boundaries.", this, pm);
}
List<FeatureMate> polygonMates = FeatureUtilities.featureCollectionToMatesList(inArea);
String polygonMessage = "This operation can be applied only to a single polygon.";
if (polygonMates.size() != 1) {
throw new ModelsIllegalargumentException(polygonMessage, this, pm);
}
FeatureMate polygonMate = polygonMates.get(0);
Geometry polygon = polygonMate.getGeometry();
if (polygon instanceof MultiPolygon) {
polygon = polygon.getGeometryN(0);
}
if (!(polygon instanceof Polygon)) {
throw new ModelsIllegalargumentException(polygonMessage, this, pm);
}
List<FeatureMate> pointsMates = FeatureUtilities.featureCollectionToMatesList(inElevations);
if (doUseOnlyInternal) {
PreparedGeometry preparedPolygon = PreparedGeometryFactory.prepare(polygon);
List<FeatureMate> tmpPointsMates = new ArrayList<FeatureMate>();
for (FeatureMate pointMate : pointsMates) {
Geometry geometry = pointMate.getGeometry();
if (preparedPolygon.covers(geometry)) {
tmpPointsMates.add(pointMate);
}
}
pointsMates = tmpPointsMates;
}
if (pointsMates.size() < 4) {
throw new ModelsIllegalargumentException("You need at least 4 elevation points (the more, the better) to gain a decent interpolation.", this, pm);
}
List<Coordinate> controlPointsList = new ArrayList<Coordinate>();
if (doPolygonborder) {
pm.beginTask("Extract polygon border...", IHMProgressMonitor.UNKNOWN);
Coordinate[] polygonCoordinates = polygon.getCoordinates();
List<ProfilePoint> profile = CoverageUtilities.doProfile(inRaster, polygonCoordinates);
for (ProfilePoint profilePoint : profile) {
Coordinate position = profilePoint.getPosition();
double elevation = profilePoint.getElevation();
Coordinate coord = new Coordinate(position.x, position.y, elevation);
controlPointsList.add(coord);
}
pm.done();
}
for (FeatureMate pointsMate : pointsMates) {
Coordinate coordinate = pointsMate.getGeometry().getCoordinate();
double elev = pointsMate.getAttribute(fElevation, Double.class);
Coordinate coord = new Coordinate(coordinate.x, coordinate.y, elev);
controlPointsList.add(coord);
}
Coordinate[] controlPoints = controlPointsList.toArray(new Coordinate[0]);
GridGeometry2D gridGeometry = inRaster.getGridGeometry();
RandomIter elevIter = CoverageUtilities.getRandomIterator(inRaster);
WritableRaster outputWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
WritableRandomIter outputIter = RandomIterFactory.createWritable(outputWR, null);
DefaultFeatureCollection newCollection = new DefaultFeatureCollection();
newCollection.add(polygonMate.getFeature());
OmsScanLineRasterizer slRasterizer = new OmsScanLineRasterizer();
slRasterizer.pm = pm;
slRasterizer.inVector = newCollection;
slRasterizer.pCols = cols;
slRasterizer.pRows = rows;
slRasterizer.pNorth = north;
slRasterizer.pSouth = south;
slRasterizer.pEast = east;
slRasterizer.pWest = west;
slRasterizer.pValue = 1.0;
slRasterizer.process();
GridCoverage2D outRasterized = slRasterizer.outRaster;
if (pMaxbuffer < 0)
pMaxbuffer = Math.max(vectorBounds.getWidth(), vectorBounds.getHeight());
IDWInterpolator interpolator = new IDWInterpolator(pMaxbuffer);
final GridCoordinates2D gridCoord = new GridCoordinates2D();
RandomIter rasterizedIter = CoverageUtilities.getRandomIterator(outRasterized);
pm.beginTask("Interpolating...", cols);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
double probValue = rasterizedIter.getSampleDouble(c, r, 0);
if (isNovalue(probValue)) {
continue;
}
gridCoord.setLocation(c, r);
DirectPosition world = gridGeometry.gridToWorld(gridCoord);
double[] coordinate = world.getCoordinate();
double interpolated = interpolator.getValue(controlPoints, new Coordinate(coordinate[0], coordinate[1]));
outputIter.setSample(c, r, 0, interpolated);
}
pm.worked(1);
}
pm.done();
pm.beginTask("Merging with original raster...", cols);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
double interpolatedValue = outputIter.getSampleDouble(c, r, 0);
double rasterValue = elevIter.getSampleDouble(c, r, 0);
if (isNovalue(interpolatedValue)) {
if (!isNovalue(rasterValue))
outputIter.setSample(c, r, 0, rasterValue);
} else {
if (doErode) {
// any value generated is ok
outputIter.setSample(c, r, 0, interpolatedValue);
} else {
// only values higher than the raster are ok
if (!isNovalue(rasterValue)) {
if (rasterValue < interpolatedValue) {
outputIter.setSample(c, r, 0, interpolatedValue);
} else {
outputIter.setSample(c, r, 0, rasterValue);
}
} else {
outputIter.setSample(c, r, 0, interpolatedValue);
}
}
}
}
pm.worked(1);
}
pm.done();
outRaster = CoverageUtilities.buildCoverage("raster", outputWR, regionMap, inRaster.getCoordinateReferenceSystem());
}
Aggregations