use of org.hortonmachine.gears.modules.v.vectorize.OmsVectorizer in project hortonmachine by TheHortonMachine.
the class LasInfoController method findCircles.
public static List<Geometry> findCircles(GridCoverage2D inRaster, int pixelsThreshold) throws Exception {
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
int nCols = regionMap.getCols();
int nRows = regionMap.getRows();
RandomIter rasterIter = CoverageUtilities.getRandomIterator(inRaster);
WritableRaster[] holder = new WritableRaster[1];
GridCoverage2D outGC = CoverageUtilities.createCoverageFromTemplate(inRaster, HMConstants.doubleNovalue, holder);
WritableRandomIter outIter = RandomIterFactory.createWritable(holder[0], null);
for (int r = 0; r < nRows; r++) {
for (int c = 0; c < nCols; c++) {
double value = rasterIter.getSampleDouble(c, r, 0);
if (value != 255) {
outIter.setSample(c, r, 0, 1);
} else {
outIter.setSample(c, r, 0, -9999.0);
}
}
}
OmsVectorizer v = new OmsVectorizer();
v.inRaster = outGC;
v.doRemoveHoles = false;
v.pThres = pixelsThreshold;
v.pValue = 1.0;
v.process();
SimpleFeatureCollection outVector = v.outVector;
List<SimpleFeature> featuresList = FeatureUtilities.featureCollectionToList(outVector).stream().filter(f -> {
Object attribute = f.getAttribute(v.fDefault);
if (attribute instanceof Number) {
double value = ((Number) attribute).doubleValue();
if (value != -9999.0) {
Geometry geom = (Geometry) f.getDefaultGeometry();
// assume no centroid intersection
Point centroid = geom.getCentroid();
Envelope env = geom.getEnvelopeInternal();
double buffer = env.getWidth() * 0.01;
Geometry centroidB = centroid.buffer(buffer);
if (geom.intersects(centroidB)) {
return false;
}
return true;
}
}
return false;
}).collect(Collectors.toList());
// DefaultFeatureCollection fc = new DefaultFeatureCollection();
// fc.addAll(featuresList);
// HMMapframe mf = HMMapframe.openFrame(false);
// mf.addLayer(fc);
List<Geometry> geomsList = featuresList.stream().map(f -> {
Geometry geom = (Geometry) f.getDefaultGeometry();
Envelope env = geom.getEnvelopeInternal();
Coordinate centre = env.centre();
Point centerPoint = GeometryUtilities.gf().createPoint(centre);
double width = env.getWidth();
double height = env.getHeight();
double radius = Math.max(width, height) / 2.0;
Geometry finalBuffer = centerPoint.buffer(radius);
return finalBuffer;
}).collect(Collectors.toList());
return geomsList;
}
use of org.hortonmachine.gears.modules.v.vectorize.OmsVectorizer in project hortonmachine by TheHortonMachine.
the class LasShapeVectorizer method process.
@Execute
public void process() throws Exception {
checkNull(inLas);
CoordinateReferenceSystem crs = null;
double west = Double.POSITIVE_INFINITY;
double south = Double.POSITIVE_INFINITY;
double east = Double.NEGATIVE_INFINITY;
double north = Double.NEGATIVE_INFINITY;
// check the real minimum bounds
pm.beginTask("Reading real bounds...", IHMProgressMonitor.UNKNOWN);
long recordsCount = 0;
try (ALasReader reader = ALasReader.getReader(new File(inLas), null)) {
reader.open();
ILasHeader header = reader.getHeader();
crs = header.getCrs();
recordsCount = header.getRecordsCount();
while (reader.hasNextPoint()) {
LasRecord dot = reader.getNextPoint();
west = Math.min(west, dot.x);
south = Math.min(south, dot.y);
east = Math.max(east, dot.x);
north = Math.max(north, dot.y);
}
}
pm.done();
// buffer by one resolution
north = north + pYres;
south = south - pYres;
west = west - pXres;
east = east + pXres;
int rows = (int) round((north - south) / pYres);
int cols = (int) round((east - west) / pXres);
// bounds snapped on grid
east = west + cols * pXres;
north = south + rows * pYres;
final GridGeometry2D gridGeometry2D = CoverageUtilities.gridGeometryFromRegionValues(north, south, east, west, cols, rows, crs);
RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(gridGeometry2D);
final WritableRaster wr = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
// now map the points on the raster
pm.beginTask("Mapping points on raster...", (int) recordsCount);
try (ALasReader reader = ALasReader.getReader(new File(inLas), null)) {
reader.open();
final Point gridPoint = new Point();
while (reader.hasNextPoint()) {
LasRecord dot = reader.getNextPoint();
double dotZ = dot.z;
Coordinate coordinate = new Coordinate(dot.x, dot.y, dotZ);
CoverageUtilities.colRowFromCoordinate(coordinate, gridGeometry2D, gridPoint);
wr.setSample(gridPoint.x, gridPoint.y, 0, 1);
pm.worked(1);
}
}
pm.done();
GridCoverage2D gridCoverage2D = CoverageUtilities.buildCoverage("mapped", wr, regionMap, crs);
OmsVectorizer vectorizer = new OmsVectorizer();
vectorizer.inRaster = gridCoverage2D;
vectorizer.pm = pm;
vectorizer.process();
SimpleFeatureCollection outVector = vectorizer.outVector;
dumpVector(outVector, outShp);
}
use of org.hortonmachine.gears.modules.v.vectorize.OmsVectorizer in project hortonmachine by TheHortonMachine.
the class Vectorizer method process.
@Execute
public void process() throws Exception {
OmsVectorizer vectorizer = new OmsVectorizer();
vectorizer.inRaster = getRaster(inRaster);
vectorizer.pValue = pValue;
vectorizer.fDefault = fDefault;
vectorizer.pThres = pThres;
vectorizer.doRegioncheck = doRegioncheck;
vectorizer.pm = pm;
vectorizer.doProcess = doProcess;
vectorizer.doReset = doReset;
vectorizer.process();
dumpVector(vectorizer.outVector, outVector);
}
use of org.hortonmachine.gears.modules.v.vectorize.OmsVectorizer in project hortonmachine by TheHortonMachine.
the class OmsBasinShape method process.
@Execute
public void process() throws Exception {
if (!concatOr(outBasins == null, doReset)) {
return;
}
checkNull(inBasins);
HashMap<String, Double> regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inBasins);
nCols = regionMap.get(CoverageUtilities.COLS).intValue();
nRows = regionMap.get(CoverageUtilities.ROWS).intValue();
// double xRes = regionMap.get(CoverageUtilities.XRES);
// double yRes = regionMap.get(CoverageUtilities.YRES);
RenderedImage basinsRI = inBasins.getRenderedImage();
RenderedImage pitRI = null;
if (inElev != null)
pitRI = inElev.getRenderedImage();
int[] nstream = new int[1];
// nstream[0] = 1508;
WritableRaster basinsWR = CoverageUtilities.renderedImage2WritableRaster(basinsRI, true);
RandomIter basinsRandomIter = RandomIterFactory.create(basinsWR, null);
for (int j = 0; j < nRows; j++) {
for (int i = 0; i < nCols; i++) {
if (!isNovalue(basinsRandomIter.getSampleDouble(i, j, 0)) && basinsRandomIter.getSampleDouble(i, j, 0) > (double) nstream[0]) {
nstream[0] = (int) basinsRandomIter.getSampleDouble(i, j, 0);
}
}
}
WritableRaster subbasinsWR = CoverageUtilities.createWritableRaster(basinsRI.getWidth(), basinsRI.getHeight(), null, basinsRI.getSampleModel(), doubleNovalue);
// create the feature type
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
// set the name
// $NON-NLS-1$
b.setName("basinshape");
// add a geometry property
// $NON-NLS-1$
String defaultGeometryName = "the_geom";
b.setCRS(inBasins.getCoordinateReferenceSystem());
b.add(defaultGeometryName, MultiPolygon.class);
// add some properties
// $NON-NLS-1$
b.add("area", Float.class);
// $NON-NLS-1$
b.add("perimeter", Float.class);
// $NON-NLS-1$
b.add(NetworkChannel.NETNUMNAME, Integer.class);
// $NON-NLS-1$
b.add("maxelev", Float.class);
// $NON-NLS-1$
b.add("minelev", Float.class);
// $NON-NLS-1$
b.add("avgelev", Float.class);
// $NON-NLS-1$
b.add(NetworkChannel.BARICENTERELEVNAME, Float.class);
// build the type
SimpleFeatureType type = b.buildFeatureType();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
outBasins = new DefaultFeatureCollection();
// for each stream correct problems with basins and create geometries
pm.beginTask("Extracting basins...", nstream[0]);
for (int num = 1; num <= nstream[0]; num++) {
Object[] values = new Object[8];
int nordRow = -1;
int southRow = 0;
int eastCol = -1;
int westCol = nCols;
int numPixel = 0;
double minZ = Double.MAX_VALUE;
double maxZ = Double.MIN_VALUE;
double averageZ = 0.0;
if (pitRI != null)
pitRandomIter = RandomIterFactory.create(pitRI, null);
WritableRandomIter subbasinIter = RandomIterFactory.createWritable(subbasinsWR, null);
for (int j = 0; j < nRows; j++) {
for (int i = 0; i < nCols; i++) {
double basinId = basinsRandomIter.getSampleDouble(i, j, 0);
if (isNovalue(basinId)) {
continue;
}
int basinNum = (int) basinId;
if (basinNum == num) {
if (nordRow == -1) {
nordRow = i;
}
if (i > nordRow) {
southRow = i;
}
if (westCol > j) {
westCol = j;
}
if (eastCol < j) {
eastCol = j;
}
subbasinIter.setSample(i, j, 0, basinNum);
if (pitRI != null) {
double elevation = pitRandomIter.getSampleDouble(i, j, 0);
if (!isNovalue(elevation)) {
minZ = elevation < minZ ? elevation : minZ;
maxZ = elevation > maxZ ? elevation : maxZ;
averageZ = averageZ + elevation;
} else {
minZ = -1;
maxZ = -1;
averageZ = 0;
}
}
numPixel++;
}
}
}
if (numPixel != 0) {
// min, max and average
values[3] = num;
values[4] = maxZ;
values[5] = minZ;
values[6] = averageZ / numPixel;
numPixel = 0;
for (int i = nordRow; i < southRow + 1; i++) {
for (int j = westCol; j < eastCol + 1; j++) {
if (isNovalue(subbasinIter.getSampleDouble(i, j, 0))) {
for (int k = 1; k <= 8; k++) {
// index.setFlow(k);
// index.getParameters()[
int indexI = i + ModelsSupporter.DIR[k][1];
// 0];
// index.getParameters()[
int indexJ = j + ModelsSupporter.DIR[k][0];
// 1];
if (!isNovalue(subbasinIter.getSampleDouble(indexI, indexJ, 0))) {
numPixel++;
}
k++;
}
if (numPixel == 4) {
subbasinIter.setSample(i, j, 0, num);
}
}
numPixel = 0;
}
}
// extract the feature polygon of that basin number
OmsVectorizer vectorizer = new OmsVectorizer();
try {
vectorizer.inRaster = inBasins;
vectorizer.pm = pm;
vectorizer.doReset = true;
vectorizer.pValue = (double) num;
vectorizer.process();
} catch (Exception e) {
pm.errorMessage(e.getLocalizedMessage());
continue;
}
SimpleFeatureCollection outGeodata = vectorizer.outVector;
FeatureIterator<SimpleFeature> outGeodataIterator = outGeodata.features();
List<Polygon> polygons = new ArrayList<Polygon>();
while (outGeodataIterator.hasNext()) {
SimpleFeature feature = outGeodataIterator.next();
polygons.add((Polygon) feature.getDefaultGeometry());
}
outGeodataIterator.close();
MultiPolygon geometry = GeometryUtilities.gf().createMultiPolygon((Polygon[]) polygons.toArray(new Polygon[polygons.size()]));
values[0] = geometry;
values[1] = geometry.getArea();
values[2] = geometry.getLength();
Point centroid = geometry.getCentroid();
if (centroid == null || centroid.isEmpty()) {
pm.errorMessage("Unable to extract basin: " + num);
continue;
}
Coordinate centroidCoords = centroid.getCoordinate();
GridGeometry2D gridGeometry = inBasins.getGridGeometry();
GridCoordinates2D worldToGrid = gridGeometry.worldToGrid(new DirectPosition2D(centroidCoords.x, centroidCoords.y));
int[] rowColPoint = new int[] { worldToGrid.y, worldToGrid.x };
double centroidElevation = -1;
;
if (pitRI != null) {
double elev = pitRandomIter.getSampleDouble(rowColPoint[1], rowColPoint[0], 0);
if (!isNovalue(elev)) {
centroidElevation = elev;
}
}
values[7] = centroidElevation;
subbasinIter.done();
subbasinsWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
// add the values
builder.addAll(values);
// build the feature with provided ID
SimpleFeature feature = builder.buildFeature(type.getTypeName() + "." + num);
((DefaultFeatureCollection) outBasins).add(feature);
}
pm.worked(1);
}
pm.done();
basinsRandomIter.done();
basinsWR = null;
}
use of org.hortonmachine.gears.modules.v.vectorize.OmsVectorizer in project hortonmachine by TheHortonMachine.
the class TestVectorizer method testVectorizer2.
public void testVectorizer2() throws Exception {
double[][] inData = HMTestMaps.extractNet0Data;
HashMap<String, Double> envelopeParams = HMTestMaps.getEnvelopeparams();
CoordinateReferenceSystem crs = HMTestMaps.getCrs();
GridCoverage2D inCoverage = CoverageUtilities.buildCoverage("data", inData, envelopeParams, crs, true);
OmsVectorizer vectorizer = new OmsVectorizer();
vectorizer.pm = pm;
vectorizer.inRaster = inCoverage;
vectorizer.pValue = null;
vectorizer.pThres = 1;
vectorizer.fDefault = "rast";
vectorizer.process();
SimpleFeatureCollection outGeodata = vectorizer.outVector;
assertEquals(2, outGeodata.size());
List<SimpleFeature> features = FeatureUtilities.featureCollectionToList(outGeodata);
SimpleFeature f1 = features.get(0);
SimpleFeature f2 = features.get(1);
Geometry g1 = (Geometry) f1.getDefaultGeometry();
Geometry g2 = (Geometry) f2.getDefaultGeometry();
// SimpleFeature nvFeature = f1;
SimpleFeature valuesFeature = f2;
if (g1.getArea() < g2.getArea()) {
// nvFeature = f2;
valuesFeature = f1;
}
double value = ((Number) valuesFeature.getAttribute("rast")).doubleValue();
assertEquals(2.0, value, 0.0000001);
Geometry geometry = (Geometry) valuesFeature.getDefaultGeometry();
double area = geometry.getArea();
assertEquals(6300.0, area, 0.0000001);
}
Aggregations