Created
March 29, 2018 15:18
-
-
Save martin-mfg/0fa84bd8e0c44dbab7b9df31e0965a11 to your computer and use it in GitHub Desktop.
GeoTools PolygonExtractionProcess demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Version 19.0 of GeoTools was used for this demo.