1

I want to convert a raster file into a shapefile. I am creating a GridCoverage2D object and want to use the execute method of Geotools PolygonExtractionProcess class but this method is not executing. I sadly cannot find any useful example usages of this class. This is my code:

try {
    File rasterFile = new File("C:\\Data\\mytif.tif");
    AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
    Hints hints = new Hints();
    if (format instanceof GeoTiffFormat) {
        hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
    }
    reader = format.getReader(rasterFile, hints);
    GridCoverage2D coverage = reader.read(null); // It all works until this point
    final PolygonExtractionProcess process = new PolygonExtractionProcess();
    //System.out.println("This gets printed");
    SimpleFeatureCollection sfColl = process.execute(coverage, null, Boolean.TRUE, null, null, null, null);
    //System.out.println("This does not get printed anymore");
    Style style = SLD.createPolygonStyle(Color.RED, null, 0.0f);
    Layer layer = new FeatureLayer(sfColl, style);
    map.addLayer(layer);
} catch (Exception e) {
    System.err.println(e);
}
sLicK
  • 25
  • 3

1 Answers1

0

First, if all you want to do is extract vectors from a raster you could probably save using the process API completely by looking inside PolygonExtractionProcess to see how it works using the JAI.

But if you want to run a process then you should read the process tutorial which describes how they work and how to call them from your own code (this is usually used for testing). Essentially, the issue you are having comes from not understanding that processes are called from the processing engine (ProcessExecutor) which manages the input and output, and threading etc for you.

So your code should look something like this:

public class RasterToVector {

  public static void main(String[] args)
      throws IllegalArgumentException, IOException, InterruptedException, ExecutionException {
    RasterToVector rtv = new RasterToVector();

    SimpleFeatureCollection features = rtv.extract(args[0]);
    Style style = SLD.createPolygonStyle(Color.RED, null, 0.0f);
    Layer layer = new FeatureLayer(features, style);
    MapContent map = new MapContent();
    map.addLayer(layer);
    JMapFrame.showMap(map);
  }

  org.geotools.process.Process process;

  public RasterToVector() {
    Name name = new NameImpl("ras", "PolygonExtraction");
    process = Processors.createProcess(name);
  }

  private SimpleFeatureCollection extract(String filename)
      throws IllegalArgumentException, IOException, InterruptedException, ExecutionException {
    File rasterFile = new File(filename);
    AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
    Hints hints = new Hints();
    if (format instanceof GeoTiffFormat) {
      hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
    }
    AbstractGridCoverage2DReader reader = format.getReader(rasterFile, hints);
    GridCoverage2D coverage = reader.read(null);
    ProcessExecutor engine = Processors.newProcessExecutor(2);
    Map<String, Object> input = new KVP("data", coverage);
    Progress working = engine.submit(process, input);

    Map<String, Object> result = working.get();
    SimpleFeatureCollection features = (SimpleFeatureCollection) result.get("result");
    return features;
  }

}

For a local terrain file I get the following result:

enter image description here

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • Thank you for your response. I tried running your code but I get the following error: "org.geotools.image.ImageWorker INFORMATION: Warp/affine reduction enabled: true". Is this error maybe caused by an incorrect pom.xml? – sLicK Nov 03 '17 at 09:48
  • that is not an error it is an INFORMATION message, it is just telling you something. – Ian Turton Nov 03 '17 at 09:49
  • Okay lets call it information then. But the program is not running either – sLicK Nov 03 '17 at 10:03
  • can you share your tiff? – Ian Turton Nov 03 '17 at 10:05
  • I just went shopping and when i came back the result was on my monitor. The program just needs 10 or more minutes to execute but it´s working. – sLicK Nov 03 '17 at 10:57
  • I did. Thanks for your effort! – sLicK Nov 03 '17 at 11:04