Geotools PolygonExtractionProcess 不工作

Geotools PolygonExtractionProcess is not working

我想将光栅文件转换为 shapefile。我正在创建一个 GridCoverage2D 对象并想使用 Geotools PolygonExtractionProcess class 的执行方法,但此方法未执行。遗憾的是,我找不到此 class 的任何有用示例用法。这是我的代码:

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);
}

首先,如果您想做的只是从栅格中提取矢量,您可能可以使用过程 API 完全保存,方法是查看内部 PolygonExtractionProcess 以查看其使用 JAI 的工作原理。

但是如果你想要 运行 一个进程,那么你应该阅读 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),它为你管理输入和输出,以及线程等。

因此您的代码应如下所示:

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;
  }

}

对于本地地形文件,我得到以下结果: