Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martin-mfg/0fa84bd8e0c44dbab7b9df31e0965a11 to your computer and use it in GitHub Desktop.
Save martin-mfg/0fa84bd8e0c44dbab7b9df31e0965a11 to your computer and use it in GitHub Desktop.
GeoTools PolygonExtractionProcess demo
public static void main(String[] args) throws Exception
{
final int WIDTH = 5;
final int HEIGHT = 5;
GridCoverageBuilder builder = new GridCoverageBuilder();
builder.setEnvelope(0, 0, WIDTH, HEIGHT); // x_min, y_min, x_max, y_max
/*
According to the documentation, the min and max allowed values for the coordinates are to be specified here,
i.e. it should say WIDTH - 1 and HEIGHT - 1. However, this leads to incorrect results, while the above code does not.
So the documentation seems to be wrong.
*/
builder.setImageSize(WIDTH, HEIGHT);
// Will use sample value in the range x inclusive to y exclusive.
builder.setSampleRange(0, 42);
GridCoverageBuilder.Variable precipitation = builder.newVariable("precipitation", Unit.ONE);
builder.getBufferedImage().getRaster().setSample(2, (HEIGHT - 1) - 2, 0, -99);
GridCoverage2D coverage = builder.getGridCoverage2D();
System.out.println(coverage);
PolygonExtractionProcess extractor = new PolygonExtractionProcess();
LinkedList<Range> classificationRanges = new LinkedList<Range>();
classificationRanges.add(new Range(-0.5, true, 0.5, true));
classificationRanges.add(new Range(0.5, true, 1.5, true));
classificationRanges.add(new Range(1.5, true, 2.5, true));
SimpleFeatureCollection result = extractor.execute(coverage, 0, true, null, null, classificationRanges, null);
// As far as I can tell from the geotools source code, the progressListener is not used at all. So we can as well just pass null.
SimpleFeatureIterator iterator = result.features();
try{
while(iterator.hasNext()) {
SimpleFeature feature = iterator.next();
System.out.println(feature);
}
} finally {
iterator.close();
}
System.out.println(result);
}
@martin-mfg
Copy link
Author

A quick-and-dirty demo for the PolygonExtractionProcess class from the GeoTools project.
The code creates a 5x5 raster, sets the cell at (2, 2) to value 1 and then calls the PolygonExtractionProcess.

Here is the output of the code:

GridCoverage2D["null", GeneralEnvelope[(0.0, 0.0), (5.0, 5.0)], DefaultEngineeringCRS["Cartesian 2D"]]
│   RenderedSampleDimension("precipitation":[0.0 ... 42.0])
│     ‣ Category("precipitation":[0...41])
└ Image=WritableRenderedImageAdapter[]

Mär 29, 2018 5:16:42 PM org.geotools.image.ImageWorker <clinit>
INFORMATION: Warp/affine reduction enabled: true
SimpleFeatureImpl:reclassified=[SimpleFeatureImpl.Attribute: the_geom<the_geom id=0>=POLYGON ((0 5, 0 0, 5 0, 5 5, 0 5), (2 3, 3 3, 3 2, 2 2, 2 3)), SimpleFeatureImpl.Attribute: value<value id=0>=1.0]
SimpleFeatureImpl:reclassified=[SimpleFeatureImpl.Attribute: the_geom<the_geom id=1>=POLYGON ((3 2, 3 3, 2 3, 2 2, 3 2)), SimpleFeatureImpl.Attribute: value<value id=1>=157.0]
org.geotools.data.collection.ListFeatureCollection@42b3b079

Version 19.0 of GeoTools was used for this demo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment