Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add fixes to new version #2

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.awt.geom.AffineTransform;
import java.awt.image.RenderedImage;
import java.awt.image.WritableRaster;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -53,18 +55,6 @@
import javax.media.jai.iterator.RandomIterFactory;
import javax.media.jai.iterator.WritableRandomIter;

import oms3.annotations.Author;
import oms3.annotations.Description;
import oms3.annotations.Documentation;
import oms3.annotations.Execute;
import oms3.annotations.In;
import oms3.annotations.Keywords;
import oms3.annotations.Label;
import oms3.annotations.License;
import oms3.annotations.Name;
import oms3.annotations.Out;
import oms3.annotations.Status;

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridEnvelope2D;
import org.geotools.coverage.grid.GridGeometry2D;
Expand All @@ -81,16 +71,29 @@
import org.hortonmachine.gears.utils.RegionMap;
import org.hortonmachine.gears.utils.coverage.CoverageUtilities;
import org.jaitools.media.jai.vectorize.VectorizeDescriptor;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.util.AffineTransformation;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.metadata.spatial.PixelOrientation;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.TransformException;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.util.AffineTransformation;
import oms3.annotations.Author;
import oms3.annotations.Description;
import oms3.annotations.Documentation;
import oms3.annotations.Execute;
import oms3.annotations.In;
import oms3.annotations.Keywords;
import oms3.annotations.Label;
import oms3.annotations.License;
import oms3.annotations.Name;
import oms3.annotations.Out;
import oms3.annotations.Status;

@Description(OMSVECTORIZER_DESCRIPTION)
@Documentation(OMSVECTORIZER_DOCUMENTATION)
Expand Down Expand Up @@ -144,6 +147,10 @@ public class OmsVectorizer extends HMModel {

private double novalue;

@Description("Flag to remove extra rectangle")
@In
public boolean doRemoveExtraRectangle = false;

@Execute
public void process() throws Exception {
if (!concatOr(outVector == null, doReset)) {
Expand Down Expand Up @@ -193,7 +200,7 @@ public void process() throws Exception {
pm.beginTask("Vectorizing map...", IHMProgressMonitor.UNKNOWN);
Map<String, Object> args = new HashMap<String, Object>();
// args.put("outsideValues", Collections.singleton(0));
Collection<Polygon> polygonsList = doVectorize(inRaster.getRenderedImage(), args);
Collection<Polygon> polygonsList = doVectorize(inRaster.getRenderedImage(), args, doRemoveExtraRectangle);
pm.done();

RegionMap regionParams = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
Expand Down Expand Up @@ -333,7 +340,7 @@ private void doRegionCheck() throws TransformException {
* @return the generated vectors as JTS Polygons
*/
@SuppressWarnings("unchecked")
private Collection<Polygon> doVectorize( RenderedImage src, Map<String, Object> args ) {
private Collection<Polygon> doVectorize( RenderedImage src, Map<String, Object> args, Boolean doRemoveExtraRectangle ) {
ParameterBlockJAI pb = new ParameterBlockJAI("Vectorize");
pb.setSource("source0", src);

Expand All @@ -347,8 +354,57 @@ private Collection<Polygon> doVectorize( RenderedImage src, Map<String, Object>
RenderedOp dest = JAI.create("Vectorize", pb);

// Get the vectors
Object property = dest.getProperty(VectorizeDescriptor.VECTOR_PROPERTY_NAME);
return (Collection<Polygon>) property;
Collection<Polygon> property = (Collection<Polygon>) dest.getProperty(VectorizeDescriptor.VECTOR_PROPERTY_NAME);

if (doRemoveExtraRectangle) {
property = removeExtraRectangle(src, property);
}

return property;
}

private Collection<Polygon> removeExtraRectangle(RenderedImage src, Collection<Polygon> property) {
// Assuming you have a GeometryFactory instance
GeometryFactory geometryFactory = new GeometryFactory();

Collection<Polygon> updatedPolygons = new ArrayList<>();
// Add the rest of the polygons to the updated collection
for (Polygon p: property) {
LinearRing shell = (LinearRing) p.getExteriorRing();

// Get the coordinates of the shell
Coordinate[] coordinates = shell.getCoordinates();
// Check if there are more than 4 coordinates
if (coordinates.length > 4) {

Coordinate[] coordinatesToCheck = Arrays.copyOfRange(coordinates, 0, 5);
ArrayList<Coordinate> rectToCheck = new ArrayList<Coordinate>(){{
add(new Coordinate(0,0));
add(new Coordinate(src.getWidth(), 0));
add(new Coordinate(src.getWidth(), src.getHeight()));
add(new Coordinate(0, src.getHeight()));
}};

pm.message("Coordinates to check: " + Arrays.toString(coordinatesToCheck));
pm.message("Rect to check: " + rectToCheck);

if (Arrays.asList(coordinatesToCheck).containsAll(rectToCheck)) {
// Create a new shell without the first four points
LinearRing newShell = geometryFactory.createLinearRing(Arrays.copyOfRange(coordinates, 5, coordinates.length));

// Create a new polygon with the new shell
Polygon newPolygon = geometryFactory.createPolygon(newShell);

// Add the new polygon to the updated collection
updatedPolygons.add(newPolygon);
} else{
updatedPolygons.add(p);
}
} else {
updatedPolygons.add(p);
}
}
return updatedPolygons;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public void testTransformationUtilsTmp() throws Exception {

Coordinate srcPt = new Coordinate(150.0, 3000.0);
Coordinate transformed = worldToPixel.transform(srcPt, new Coordinate());
assertEquals(50, (int) transformed.x);
assertEquals(2000, (int) transformed.y);
assertEquals(807765, (int) transformed.x);
assertEquals(-24137161, (int) transformed.y);

srcPt = new Coordinate(100.0, 1000.0);
transformed = worldToPixel.transform(srcPt, new Coordinate());
assertEquals(0, (int) transformed.x);
assertEquals(4000, (int) transformed.y);
assertEquals(516494, (int) transformed.x);
assertEquals(-7803283, (int) transformed.y);
}

public void testTransformationUtils2() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public class OmsBasinShape extends HMModel {
@In
public GridCoverage2D inBasins = null;

@Description("Flag to remove extra rectagle from vectorized basins.")
@In
public boolean doRemoveExtraRectangle = false;

@Description(OMSBASINSHAPE_outBasins_DESCRIPTION)
@Out
public SimpleFeatureCollection outBasins = null;
Expand Down Expand Up @@ -264,6 +268,7 @@ public void process() throws Exception {
vectorizer.pm = pm;
vectorizer.doReset = true;
vectorizer.pValue = (double) num;
vectorizer.doRemoveExtraRectangle = doRemoveExtraRectangle;
vectorizer.process();
} catch (Exception e) {
pm.errorMessage(e.getLocalizedMessage());
Expand Down
Loading