java – 使用带有范围查找的JTS向量化多边形

我尝试使用Java(即带有JAI的geotools)对图像进行矢量化.

代码很小,在intelliJ中运行得很好

InputStream stringAsStream = new ByteArrayInputStream(inputAsciiGrid.getBytes(StandardCharsets.UTF_8));
SimpleFeatureIterator vectorizedFeatures = extractor.execute(input, 0, true, null,
                null, classificationRanges, null).features();

我的问题是通过maven执行此操作时:

mvn clean package && \
    java -jar target/quickstart-1.0.jar

它失败了,除了

RangeLookup: No OperationDescriptor is registered in the current operation registry under this name

示例代码可以在https://github.com/geoHeil/jts-vectorize找到

注意,jar包含类,但不包含META-INF / registry.jai文件中的OperationDescriptors.

jar -tf target/quickstart-1.0.jar| grep RangeLookupProcess                                                            
org/geotools/process/raster/RangeLookupProcess.class

编辑

我试图遵循java.lang.ClassNotFoundException: it.geosolutions.jaiext.range.Range
并补充说

<dependency>
            <groupId>it.geosolutions.jaiext.utilities</groupId>
            <artifactId>jt-utilities</artifactId>
            <version>1.1.8</version>
        </dependency>
        <dependency>
            <groupId>it.geosolutions.jaiext.iterators</groupId>
            <artifactId>jt-iterators</artifactId>
            <version>1.1.8</version>
        </dependency>

这会将异常更改为:

Exception in thread "main" java.lang.NoClassDefFoundError: it/geosolutions/jaiext/utilities/ImageLayout2
    at FooJava.getAsciiGridFromstring(FooJava.java:49)
    at FooJava.main(FooJava.java:35)
Caused by: java.lang.ClassNotFoundException: it.geosolutions.jaiext.utilities.ImageLayout2
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more
make: *** [run-java] Error 1

尽管班级还在那里:

geoheil@geoheilsMacBook ~/Downloads/vectorize/jts-vectorize                                                                 [20:36:45]
> $jar -tf target/quickstart-1.0.jar| grep ImageLayout2                                                                  [±master ●●]
org/jaitools/imageutils/ImageLayout2.class

这似乎是一个版本不匹配.
还原和添加(https://github.com/geotools/geotools/blob/18.4/pom.xml#L97)中列出的版本时:

<properties>
    <geotools.version>18.4</geotools.version>
    <jaiext.version>1.0.16</jaiext.version>
</properties>
<dependencies>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-arcgrid</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-process-raster</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>it.geosolutions.jaiext.utilities</groupId>
            <artifactId>jt-utilities</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>it.geosolutions.jaiext.rlookup</groupId>
            <artifactId>jt-rlookup</artifactId>
            <version>${jaiext.version}</version>
        </dependency>

    </dependencies>

错误也是最初的错误.

编辑2

手动注册缺少的操作描述符:

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/javax.media.jai.registryFile.jai</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/registryFile.jaiext</resource>
                                </transformer>

并添加两个文件(见上面的名称),内容为:

# --- JAI ext manual re-registration ---
descriptor  it.geosolutions.jaiext.rlookup.RangeLookupDescriptor

给了我一个新的例外:

java.lang.RuntimeException: - Unable to render RenderedOp for this operation.
        at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:827)
        at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
        at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:888)
        at javax.media.jai.RenderedOp$1.getPropertyNames(RenderedOp.java:1791)
        at javax.media.jai.PropertyEnvironment.mapDefaults(PropertyEnvironment.java:270)
        at javax.media.jai.PropertyEnvironment.getPropertyNames(PropertyEnvironment.java:125)
        at javax.media.jai.WritablePropertySourceImpl.addProperties(WritablePropertySourceImpl.java:298)
        at javax.media.jai.RenderedOp.createPropertySource(RenderedOp.java:1817)
        at javax.media.jai.RenderedOp.getPropertyNames(RenderedOp.java:1851)
        at javax.media.jai.PropertySourceImpl.<init>(PropertySourceImpl.java:143)
        at org.geotools.coverage.AbstractCoverage.<init>(AbstractCoverage.java:139)
        at org.geotools.coverage.grid.AbstractGridCoverage.<init>(AbstractGridCoverage.java:102)
        at org.geotools.coverage.grid.GridCoverage2D.<init>(GridCoverage2D.java:186)
        at org.geotools.coverage.grid.GridCoverageFactory.create(GridCoverageFactory.java:589)
        at org.geotools.process.raster.RangeLookupProcess.execute(RangeLookupProcess.java:208)
        at org.geotools.process.raster.RangeLookupProcess.execute(RangeLookupProcess.java:234)
        at org.geotools.process.raster.PolygonExtractionProcess.execute(PolygonExtractionProcess.java:167)
        at FooJava.getWktForDbRangeFromRaster(FooJava.java:57)
        at FooJava.main(FooJava.java:36)

然而,这不一定被视为进步,即现在即使IntelliJ也无法启动.由于空指针异常,它失败了:ExceptionInInitializerError

解决方法:

我认为描述符的配置是从registryFile.jai文件中读取的.(*)

当创建具有依赖项的jar时,registryFile.jai文件将被最后一个jar的内容覆盖.
将registryFile.jai内容与添加到shade插件的以下配置合并可以解决此问题. (这是添加到你提供的github源码)

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
   <resource>META-INF/registryFile.jai</resource>
</transformer>

(无需在项目资源/ META-INF文件夹中创建registryFile.jai.)

(*)我尝试将configuration添加到不同的命名文件,但没有找到描述符.
 即使尝试OperationRegistry.updateFromStream(是);命令没有用.

上一篇:AWS使用JAVA从s3对象创建新文件获取错误


下一篇:web 性能忧化(IIS篇)