use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsGeomorphonMaximaFinder method process.
@Execute
public void process() throws Exception {
checkNull(inDTM, inDSM);
GridGeometry2D gridGeometry = inDSM.getGridGeometry();
DummyProgressMonitor pm = new DummyProgressMonitor();
OmsGeomorphon g = new OmsGeomorphon();
g.pm = pm;
g.inElev = inDSM;
g.pRadius = pRadius;
g.pThreshold = pThreshold;
g.process();
GridCoverage2D geomorphonGC = g.outRaster;
OmsRasterDiff rasterDiff = new OmsRasterDiff();
rasterDiff.inRaster1 = inDSM;
rasterDiff.inRaster2 = inDTM;
rasterDiff.pm = pm;
rasterDiff.process();
GridCoverage2D dsmDtmThresDiff = rasterDiff.outRaster;
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inDSM);
int rows = regionMap.getRows();
int cols = regionMap.getCols();
TreeSet<GridNode> topNodes = new TreeSet<GridNode>(new GridNodePositionComparator());
peakCode = GeomorphonClassification.PEAK.getCode();
hollowCode = GeomorphonClassification.HOLLOW.getCode();
valleyCode = GeomorphonClassification.VALLEY.getCode();
pitCode = GeomorphonClassification.PIT.getCode();
spurCode = GeomorphonClassification.SPUR.getCode();
double geomorphNv = HMConstants.getNovalue(geomorphonGC);
double elevNv = HMConstants.getNovalue(dsmDtmThresDiff);
RandomIter geomorphIter = CoverageUtilities.getRandomIterator(geomorphonGC);
RandomIter elevIter = CoverageUtilities.getRandomIterator(dsmDtmThresDiff);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
GridNode geomorphNode = new GridNode(geomorphIter, cols, rows, 1, -1, c, r, geomorphNv);
GridNode elevNode = new GridNode(elevIter, cols, rows, 1, -1, c, r, elevNv);
if (geomorphNode.elevation == peakCode && !elevNode.touchesBound()) {
// found peak
boolean isLocalMaxima = true;
TreeSet<GridNode> peakNodes = new TreeSet<GridNode>(new GridNodePositionComparator());
peakNodes.add(geomorphNode);
gatherNodes(peakNodes, geomorphNode);
if (peakNodes.size() == 1) {
GridNode topNode = peakNodes.first();
GridNode topElevNode = new GridNode(elevIter, cols, rows, 1, -1, topNode.col, topNode.row, elevNv);
List<GridNode> validSurroundingNodes = topElevNode.getValidSurroundingNodes();
if (validSurroundingNodes.size() < 6) {
// no more than 2 invalid permitted
isLocalMaxima = false;
} else {
if (!analyzeNeighbors(topNode)) {
isLocalMaxima = false;
}
}
}
GridNode topNode = null;
if (isLocalMaxima) {
double maxElev = Double.NEGATIVE_INFINITY;
for (GridNode peakNode : peakNodes) {
double elev = peakNode.getValueFromMap(elevIter);
if (elev > maxElev) {
maxElev = elev;
topNode = peakNode;
}
}
if (topNode != null) {
// check
GridNode topElevNode = new GridNode(elevIter, cols, rows, 1, -1, topNode.col, topNode.row, elevNv);
double[][] windowValues = topElevNode.getWindow(3, false);
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (double[] windowRow : windowValues) {
for (double windowValue : windowRow) {
if (HMConstants.isNovalue(windowValue)) {
isLocalMaxima = false;
break;
} else {
min = Math.min(min, windowValue);
max = Math.max(max, windowValue);
}
}
if (!isLocalMaxima) {
break;
}
}
if (max - min > pElevDiffThres) {
isLocalMaxima = false;
}
}
}
if (isLocalMaxima && topNode != null) {
topNodes.add(topNode);
}
}
}
}
outMaxima = new DefaultFeatureCollection();
SimpleFeatureBuilder builder = getOutBuilder();
int id = 0;
for (GridNode topNode : topNodes) {
Coordinate coordinate = CoverageUtilities.coordinateFromColRow(topNode.col, topNode.row, gridGeometry);
Point point = gf.createPoint(coordinate);
double elev = topNode.getValueFromMap(elevIter);
Object[] values = new Object[] { point, id++, elev };
try {
builder.addAll(values);
} catch (Exception e) {
e.printStackTrace();
}
SimpleFeature newFeature = builder.buildFeature(null);
((DefaultFeatureCollection) outMaxima).add(newFeature);
}
geomorphIter.done();
elevIter.done();
int size = outMaxima.size();
if (size == 0) {
pm.message("No tops extracted...");
} else {
pm.message("Extracted tops = " + outMaxima.size());
}
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class LasHeightDistribution method overlapForPercentage.
private boolean overlapForPercentage(GridCoverage2D cov1, GridCoverage2D cov2, double forPercentage) {
RandomIter cov1Iter = CoverageUtilities.getRandomIterator(cov1);
RandomIter cov2Iter = CoverageUtilities.getRandomIterator(cov2);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(cov1);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
int valid1 = 0;
int valid2 = 0;
int overlapping = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
double v1 = cov1Iter.getSampleDouble(c, r, 0);
double v2 = cov2Iter.getSampleDouble(c, r, 0);
if (!isNovalue(v1)) {
valid1++;
}
if (!isNovalue(v2)) {
valid2++;
}
if (!isNovalue(v1) && !isNovalue(v2)) {
overlapping++;
}
}
}
cov1Iter.done();
cov2Iter.done();
if (overlapping == 0) {
return false;
}
double perc1 = overlapping / valid1;
if (perc1 > forPercentage) {
return true;
}
double perc2 = overlapping / valid2;
if (perc2 > forPercentage) {
return true;
}
return false;
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class TestGeomorphon method testLastVisible2.
public void testLastVisible2() throws Exception {
double[][] mapData = new double[][] { //
{ 500, 500, 500, 500, 500, 500, 500, 500, 500, 500 }, //
{ 500, 600, 600, 600, 600, 700, 800, 1100, 1500, 1500 }, //
{ 500, 600, 700, 600, 600, 700, 800, 1100, 1500, 1500 }, //
{ 500, 600, 800, 600, 600, 700, 800, 1100, 1500, 1500 }, //
{ 500, 600, 800, 600, 600, 700, 800, 1100, 1500, 1500 }, //
{ 500, 600, 700, 600, 600, 700, 800, 1100, 1500, 2500 }, //
{ 500, 600, 600, 600, 600, 700, 800, 1100, 1500, 1500 }, { 500, 500, 500, 500, 500, 500, 500, 500, 500, 500 } };
HashMap<String, Double> eP = HMTestMaps.getEnvelopeparams();
CoordinateReferenceSystem crs = HMTestMaps.getCrs();
GridCoverage2D inElev = CoverageUtilities.buildCoverage("elevation", mapData, eP, crs, true);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
RandomIter elevIter = CoverageUtilities.getRandomIterator(inElev);
GridGeometry2D gridGeometry = inElev.getGridGeometry();
// up
DirectPosition startPosition = gridGeometry.gridToWorld(new GridCoordinates2D(0, 3));
double[] coordinateArray = startPosition.getCoordinate();
Coordinate startCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
DirectPosition endPosition = gridGeometry.gridToWorld(new GridCoordinates2D(9, 5));
coordinateArray = endPosition.getCoordinate();
Coordinate endCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
ProfilePoint lastVisiblePoint = OmsGeomorphon.getLastVisiblePoint(regionMap, elevIter, gridGeometry, startCoord, endCoord);
assertEquals(2500.0, lastVisiblePoint.getElevation(), DELTA);
// down
startPosition = gridGeometry.gridToWorld(new GridCoordinates2D(9, 5));
coordinateArray = startPosition.getCoordinate();
startCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
endPosition = gridGeometry.gridToWorld(new GridCoordinates2D(0, 3));
coordinateArray = endPosition.getCoordinate();
endCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
lastVisiblePoint = OmsGeomorphon.getLastVisiblePoint(regionMap, elevIter, gridGeometry, startCoord, endCoord);
assertEquals(500.0, lastVisiblePoint.getElevation(), DELTA);
// middle
startPosition = gridGeometry.gridToWorld(new GridCoordinates2D(5, 3));
coordinateArray = startPosition.getCoordinate();
startCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
endPosition = gridGeometry.gridToWorld(new GridCoordinates2D(0, 3));
coordinateArray = endPosition.getCoordinate();
endCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
lastVisiblePoint = OmsGeomorphon.getLastVisiblePoint(regionMap, elevIter, gridGeometry, startCoord, endCoord);
assertEquals(800.0, lastVisiblePoint.getElevation(), DELTA);
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class TestScanLineRasterizer method testScanLineRasterizerUtilsMethod.
public void testScanLineRasterizerUtilsMethod() throws Exception {
RandomIter rasterIter = CoverageUtilities.getRandomIterator(elevationCoverage);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(elevationCoverage);
List<Coordinate> coordinates = CoverageUtilities.extractPolygonOnCoverage(rasterIter, regionMap.getCols(), regionMap.getRows(), regionMap.getXres(), elevationCoverage.getGridGeometry(), polygon, 0);
int index = 0;
for (int r = 0; r < elevationData.length; r++) {
for (int c = 0; c < elevationData[0].length; c++) {
Coordinate coordinate = coordinates.get(index++);
assertEquals(elevationData[r][c], coordinate.z, DELTA);
}
}
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class OmsKernelDensity method process.
@SuppressWarnings("nls")
@Execute
public void process() throws Exception {
checkNull(inMap);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inMap);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
ValueType type = KernelFactory.ValueType.EPANECHNIKOV;
switch(pKernel) {
case 0:
type = KernelFactory.ValueType.BINARY;
break;
case 1:
type = KernelFactory.ValueType.COSINE;
break;
case 2:
type = KernelFactory.ValueType.DISTANCE;
break;
case 4:
type = KernelFactory.ValueType.GAUSSIAN;
break;
case 5:
type = KernelFactory.ValueType.INVERSE_DISTANCE;
break;
case 6:
type = KernelFactory.ValueType.QUARTIC;
break;
case 7:
type = KernelFactory.ValueType.TRIANGULAR;
break;
case 8:
type = KernelFactory.ValueType.TRIWEIGHT;
break;
}
KernelJAI kernel = KernelFactory.createCircle(pRadius, type);
RenderedImage inImg = inMap.getRenderedImage();
RandomIter inIter = RandomIterFactory.create(inImg, null);
WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
float[] kernelData = kernel.getKernelData();
pm.beginTask("Estimating kernel density...", cols - 2 * pRadius);
for (int r = pRadius; r < rows - pRadius; r++) {
for (int c = pRadius; c < cols - pRadius; c++) {
double inputValue = inIter.getSampleDouble(c, r, 0);
if (isNovalue(inputValue)) {
continue;
}
if (doConstant)
inputValue = 1.0;
int k = 0;
for (int kr = -pRadius; kr <= pRadius; kr++, k++) {
for (int kc = -pRadius; kc <= pRadius; kc++) {
// data[gridCoords.y + j][gridCoords.x + i] += cdata[k] * centreValue;
double outputValue = outIter.getSampleDouble(c + kc, r + kr, 0);
if (isNovalue(outputValue)) {
outputValue = 0;
}
outputValue = outputValue + kernelData[k] * inputValue;
outIter.setSample(c + kc, r + kr, 0, outputValue);
}
}
}
pm.worked(1);
}
pm.done();
pm.beginTask("Finalizing...", cols);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
double outputValue = outIter.getSampleDouble(c, r, 0);
if (isNovalue(outputValue)) {
outIter.setSample(c, r, 0, 0.0);
}
}
pm.worked(1);
}
pm.done();
outDensity = CoverageUtilities.buildCoverage("kerneldensity", outWR, regionMap, inMap.getCoordinateReferenceSystem());
}
Aggregations