use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsGeomorphonIM method processCell.
@Override
protected void processCell(int readCol, int readRow, int writeCol, int writeRow, int readCols, int readRows, int writeCols, int writeRows) {
try {
RandomIter elevIter = inRasterIterators.get(0);
double classification = OmsGeomorphon.calculateGeomorphon(elevIter, readGridGeometry, pRadius, pThreshold, diagonalDelta, readCol, readRow);
WritableRandomIter outDataIter = outRasterIterators.get(0);
outDataIter.setSample(writeCol, writeRow, 0, classification);
} catch (TransformException e) {
e.printStackTrace();
}
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsGradient method process.
@Execute
public void process() throws Exception {
if (!concatOr(outSlope == null, doReset)) {
return;
}
checkNull(inElev);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
nCols = regionMap.getCols();
nRows = regionMap.getRows();
xRes = regionMap.getXres();
yRes = regionMap.getYres();
RenderedImage elevationRI = inElev.getRenderedImage();
RandomIter elevationIter = RandomIterFactory.create(elevationRI, null);
WritableRaster gradientWR;
try {
gradientWR = null;
if (pMode.equals(HORN)) {
gradientWR = gradientHorn(elevationIter);
} else if (pMode.equals(EVANS)) {
gradientWR = gradientEvans(elevationIter);
} else {
gradientWR = gradientDiff(elevationIter);
}
outSlope = CoverageUtilities.buildCoverage("gradient", gradientWR, regionMap, inElev.getCoordinateReferenceSystem());
} finally {
elevationIter.done();
}
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsGradientIM method processCell.
@Override
protected void processCell(int readCol, int readRow, int writeCol, int writeRow, int readCols, int readRows, int writeCols, int writeRows) {
RandomIter elevIter = inRasterIterators.get(0);
double gradient;
switch(pMode) {
case 1:
gradient = OmsGradient.doGradientHornOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
break;
case 2:
gradient = OmsGradient.doGradientEvansOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
break;
default:
gradient = OmsGradient.doGradientDiffOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
break;
}
WritableRandomIter outDataIter = outRasterIterators.get(0);
outDataIter.setSample(writeCol, writeRow, 0, gradient);
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class RegionGrowing method process.
public void process() throws Exception {
checkNull(inMaxima, inDsm, outRaster);
GridCoverage2D inDsmGC = getRaster(inDsm);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inDsmGC);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
double xRes = regionMap.getXres();
double yRes = regionMap.getYres();
GridGeometry2D gridGeometry = inDsmGC.getGridGeometry();
double dsmNv = HMConstants.getNovalue(inDsmGC);
double dtmNv;
RandomIter dsmIter = CoverageUtilities.getRandomIterator(inDsmGC);
RandomIter dtmIter;
if (inDtm != null) {
GridCoverage2D inDtmGC = getRaster(inDtm);
dtmIter = CoverageUtilities.getRandomIterator(inDtmGC);
dtmNv = HMConstants.getNovalue(inDtmGC);
} else {
WritableRaster dtmWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, 0.0);
dtmIter = RandomIterFactory.createWritable(dtmWR, null);
dtmNv = HMConstants.doubleNovalue;
}
WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
outIter = RandomIterFactory.createWritable(outWR, null);
SimpleFeatureCollection inMaximaFC = getVector(inMaxima);
List<SimpleFeature> maximaList = FeatureUtilities.featureCollectionToList(inMaximaFC);
pm.beginTask("Processing region growing...", maximaList.size());
int index = 0;
for (SimpleFeature maximaFeature : maximaList) {
Coordinate coordinate = ((Geometry) maximaFeature.getDefaultGeometry()).getCoordinate();
int[] colRow = CoverageUtilities.colRowFromCoordinate(coordinate, gridGeometry, null);
GridNode startDsmNode = new GridNode(dsmIter, cols, rows, xRes, yRes, colRow[0], colRow[1], dsmNv);
GridNode startDtmNode = new GridNode(dtmIter, cols, rows, xRes, yRes, colRow[0], colRow[1], dtmNv);
growRegion(startDsmNode, startDtmNode, index, startDsmNode, startDtmNode);
index++;
pm.worked(1);
}
pm.done();
dtmIter.done();
outIter.done();
GridCoverage2D outRasterGC = CoverageUtilities.buildCoverage("filtered", outWR, regionMap, inDsmGC.getCoordinateReferenceSystem());
dumpRaster(outRasterGC, outRaster);
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsRasterMaximaFinder method process.
@Execute
public void process() throws Exception {
checkNull(inDsmDtmDiff, pMode);
int mode = 0;
if (pMode.equals(CUSTOM)) {
mode = 0;
} else if (pMode.equals(MIXED_PINES_AND_DECIDUOUS)) {
mode = 1;
} else if (pMode.equals(DECIDUOUS)) {
mode = 2;
} else if (pMode.equals(CONIFER)) {
mode = 3;
} else {
throw new ModelsIllegalargumentException("Processing mode not recognized: " + pMode, this);
}
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inDsmDtmDiff);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
double xRes = regionMap.getXres();
double yRes = regionMap.getYres();
GridGeometry2D gridGeometry = inDsmDtmDiff.getGridGeometry();
GeometryFactory gf = GeometryUtilities.gf();
SimpleFeatureTypeBuilder maximaTypeBuilder = new SimpleFeatureTypeBuilder();
maximaTypeBuilder.setName("pointtype");
maximaTypeBuilder.setCRS(inDsmDtmDiff.getCoordinateReferenceSystem());
maximaTypeBuilder.add("the_geom", Point.class);
maximaTypeBuilder.add("id", Integer.class);
maximaTypeBuilder.add("elev", Double.class);
maximaTypeBuilder.add(NOTE, String.class);
SimpleFeatureType maximaType = maximaTypeBuilder.buildFeatureType();
SimpleFeatureBuilder maximaBuilder = new SimpleFeatureBuilder(maximaType);
outMaxima = new DefaultFeatureCollection();
SimpleFeatureTypeBuilder circleTypeBuilder = new SimpleFeatureTypeBuilder();
circleTypeBuilder.setName("pointtype");
circleTypeBuilder.setCRS(inDsmDtmDiff.getCoordinateReferenceSystem());
circleTypeBuilder.add("the_geom", Polygon.class);
circleTypeBuilder.add("id_maxima", Integer.class);
circleTypeBuilder.add("area", Double.class);
SimpleFeatureType circleType = circleTypeBuilder.buildFeatureType();
SimpleFeatureBuilder circleBuilder = new SimpleFeatureBuilder(circleType);
outCircles = new DefaultFeatureCollection();
RandomIter elevIter = CoverageUtilities.getRandomIterator(inDsmDtmDiff);
double novalue = HMConstants.getNovalue(inDsmDtmDiff);
int id = 1;
pm.beginTask("Finding maxima...", rows);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
GridNode node = new GridNode(elevIter, cols, rows, xRes, yRes, c, r, novalue);
if (!node.isValid()) {
continue;
}
double elevation = node.elevation;
int size = 3;
switch(mode) {
case 1:
{
/*
* Popescu & Kini 2004 for mixed pines and deciduous trees
*/
double windowWidth = 2.51503 + 0.00901 * pow(elevation, 2.0);
windowWidth = windowWidth * pPercent / 100.0;
if (windowWidth > 2 * pMaxRadius) {
windowWidth = 2 * pMaxRadius;
}
size = (int) ceil(windowWidth / xRes);
break;
}
case 2:
{
double windowWidth = 3.09632 + 0.00895 * pow(elevation, 2.0);
windowWidth = windowWidth * pPercent / 100.0;
if (windowWidth > 2 * pMaxRadius) {
windowWidth = 2 * pMaxRadius;
}
size = (int) ceil(windowWidth / xRes);
break;
}
case 3:
{
double windowWidth = 3.75105 + 0.17919 * elevation + 0.01241 * pow(elevation, 2.0);
windowWidth = windowWidth * pPercent / 100.0;
if (windowWidth > 2 * pMaxRadius) {
windowWidth = 2 * pMaxRadius;
}
size = (int) ceil(windowWidth / xRes);
break;
}
default:
size = pSize;
break;
}
if (size > cols / 2) {
throw new ModelsIllegalargumentException("The windows width is larger than half the processing region for elevation = " + elevation, this);
}
boolean tmpDoCircular = doCircular;
if (size <= 3) {
size = 3;
tmpDoCircular = false;
}
double[][] window = node.getWindow(size, tmpDoCircular);
// int center = (int) ((window.length - 1.0) / 2.0);
String note = "";
boolean isMax = true;
for (int mrow = 0; mrow < window.length; mrow++) {
for (int mcol = 0; mcol < window[0].length; mcol++) {
if (!isNovalue(window[mrow][mcol])) {
// valid value
if (window[mrow][mcol] > elevation) {
isMax = false;
break;
}
}
}
if (!isMax)
break;
}
if (isMax) {
int nonValidCells = 0;
List<GridNode> surroundingNodes = node.getSurroundingNodes();
int topBufferThresViolationCount = 0;
for (GridNode gridNode : surroundingNodes) {
if (gridNode != null) {
double elev = gridNode.elevation;
double elevDiff = elevation - elev;
if (elevDiff > pTopBufferThres) {
topBufferThresViolationCount++;
}
} else {
nonValidCells++;
}
if (nonValidCells > 1 && !doAllowNovalues) {
note = "exclude: found invalid neighbor cells = " + nonValidCells;
isMax = false;
break;
}
}
if (isMax && pTopBufferThresCellCount > 0 && topBufferThresViolationCount >= pTopBufferThresCellCount) {
isMax = false;
note = "exclude: elevation diff of neighbors from top violates thres (" + pTopBufferThres + "/" + topBufferThresViolationCount + ")";
}
if (isMax) {
if (elevation >= pThreshold) {
Coordinate coordinate = CoverageUtilities.coordinateFromColRow(c, r, gridGeometry);
Point point = gf.createPoint(coordinate);
String elevStr = formatter.format(elevation);
elevStr = elevStr.replace(',', '.');
elevation = Double.parseDouble(elevStr);
Object[] values = new Object[] { point, id, elevation, note };
maximaBuilder.addAll(values);
SimpleFeature feature = maximaBuilder.buildFeature(null);
((DefaultFeatureCollection) outMaxima).add(feature);
double radius = (size * xRes) / 2.0;
Geometry buffer = point.buffer(radius);
values = new Object[] { buffer, id, buffer.getArea() };
circleBuilder.addAll(values);
feature = circleBuilder.buildFeature(null);
((DefaultFeatureCollection) outCircles).add(feature);
id++;
}
}
}
}
pm.worked(1);
}
pm.done();
elevIter.done();
if (pBorderDistanceThres > 0) {
OmsVectorizer vectorizer = new OmsVectorizer();
vectorizer.pm = pm;
vectorizer.inRaster = inDsmDtmDiff;
vectorizer.pValue = null;
vectorizer.pThres = 0;
vectorizer.doMask = true;
vectorizer.pMaskThreshold = pThreshold;
vectorizer.fDefault = "rast";
vectorizer.process();
SimpleFeatureCollection diffPolygons = vectorizer.outVector;
List<Geometry> diffGeoms = FeatureUtilities.featureCollectionToGeometriesList(diffPolygons, true, null);
List<LineString> bordersGeoms = new ArrayList<LineString>();
for (Geometry geometry : diffGeoms) {
if (geometry instanceof Polygon) {
Polygon polygon = (Polygon) geometry;
LineString exteriorRing = polygon.getExteriorRing();
bordersGeoms.add(exteriorRing);
int numInteriorRing = polygon.getNumInteriorRing();
for (int i = 0; i < numInteriorRing; i++) {
LineString interiorRingN = polygon.getInteriorRingN(i);
bordersGeoms.add(interiorRingN);
}
}
}
MultiLineString allBorders = gf.createMultiLineString(bordersGeoms.toArray(GeometryUtilities.TYPE_LINESTRING));
outBorders = FeatureUtilities.featureCollectionFromGeometry(inDsmDtmDiff.getCoordinateReferenceSystem(), allBorders);
SimpleFeatureIterator maximaIter = outMaxima.features();
while (maximaIter.hasNext()) {
SimpleFeature maxima = maximaIter.next();
Geometry maximaGeometry = (Geometry) maxima.getDefaultGeometry();
double distance = DistanceOp.distance(allBorders, maximaGeometry);
if (distance < pBorderDistanceThres) {
maxima.setAttribute(NOTE, "exclude: near border: " + distance);
}
}
maximaIter.close();
}
}
Aggregations