【问题标题】:JAI ImageRead module getting lostJAI ImageRead 模块丢失
【发布时间】:2015-02-27 18:58:36
【问题描述】:

我正在尝试编写一个使用 GeoTools 并读取 GeoTiff 图像的 OSMOSIS 扩展。

我已经写了一个最小的工作示例来说明它的作用:

package that.is.my.test;

import java.io.File;
import java.io.IOException;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.Envelope2D;
import org.opengis.coverage.grid.GridEnvelope;

public class ExampleClass {
    public static void main(String[] args) {
        new ExampleClass();
    }

    public ExampleClass() {
        try {
            File file = new File("<GEOTIFFFILE>");
            GeoTiffReader reader = new GeoTiffReader(file);
            GridCoverage2D coverage = (GridCoverage2D) reader.read(null);
            GridEnvelope gridBounds = coverage.getGridGeometry().getGridRange();
            System.out.println("grid bounds: " + gridBounds);

            Envelope2D worldBounds = coverage.getEnvelope2D();
            System.out.println("world bounds: " + worldBounds);

            int numBands = coverage.getNumSampleDimensions();
            System.out.println("num bands: " + numBands);

            System.out.println("Goodbye.");
        } catch (IllegalArgumentException | IOException e) {
            e.printStackTrace();
        }
    }
}

注意:这是一个最小的示例类,但 OSMOSIS 插件中的代码还没有做任何其他事情。

我可以从 NetBeans 运行这个示例类,它运行良好。我可以将它打包成一个可运行的 jar,也可以正常工作。

OSMOSIS 插件不能从 NetBeans 运行,因为它要被编译成一个 jar,然后由 OSMOSIS 自己调用。但是当我这样做时,以GridCoverage2D 开头的行会给我一个IllegalArgumentExceptionImageRead: No OperationDescriptor is registered in the current operation registry under this name. 的消息。

当我让两个类都打印出完整的 JAI 注册表列表时,我可以看到在 OSMOSIS 案例中,ImageReadImageWrite 和其他几个根本就不见了。

我简直无法理解这怎么会发生!当我查看 jars 时,META-INF\services\javax.imageio.spi.ImageReaderSpi 文件存在于它们中,内容完全相同。

这是我来自插件的 POM.xml,Example 类具有相同的依赖项和 repos 和构建指令:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>hello.OSMOSIS</groupId>
    <artifactId>my-osmosis-plugin</artifactId>
    <version>1.0-SNAPSHOT${jarWarning}</version>
    <packaging>jar</packaging>
    <name>OSMOSIS TEST plugin</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <osmosisScope>provided</osmosisScope> <!-- configure this by using the profiles, this is fallback only. -->
        <geotools.version>12-RC1</geotools.version>
    </properties>
    <!-- We define two different profiles: 
        debugWithNetbeans includes all the OSMOSIS jars into the plugin, 
            so that we can test-run the plugin with Netbeans (or anything else). 
            Note: This does not work yet...
        pluginProductions doesn't include the OSMOSIS sources, because later on 
            the plugin will be called BY osmosis and will not need the jars anymore. 
    DO NOT FORGET to set this correctly :-) -->
    <profiles>
        <profile>
            <id>debugWithNetbeans</id>
            <properties>
                <osmosisScope>compile</osmosisScope>
                <jarWarning>-DEBUG-BUILD</jarWarning>
            </properties>
        </profile>
        <profile>
            <id>pluginProduction</id>
            <properties>
                <osmosisScope>provided</osmosisScope>
                <jarWarning></jarWarning>
            </properties>
        </profile>
    </profiles>
    <repositories>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.openstreetmap.osmosis</groupId>
            <artifactId>osmosis-core</artifactId>
            <version>0.43-RELEASE</version>
            <scope>${osmosisScope}</scope>
        </dependency>
        <dependency>
            <groupId>org.openstreetmap.osmosis</groupId>
            <artifactId>osmosis-xml</artifactId>
            <version>0.43.1</version>
            <scope>${osmosisScope}</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geotiff</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>                 
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.3.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>hello.OSMOSIS.DoNothingBecauseImAPluginOnly</Main-Class>
                                        <Implementation-Vendor>Blapfi</Implementation-Vendor>
                                        <Implementation-Vendor-Id>huiuiui</Implementation-Vendor-Id>
                                        <Implementation-Version>bapfi</Implementation-Version>
                                    </manifestEntries>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

也许有人可以提示我做错了什么?会非常棒。 :-)

【问题讨论】:

    标签: maven jai geotools geotiff osmosis


    【解决方案1】:

    我现在可能找到了一个解释:当 JAI jar 或包含它的 jar 被添加到类路径时,JAI 注册表似乎已初始化。遗憾的是,OSMOSIS 的plugins 目录中的 jars 从未添加到任何类路径中,而是由 OSMOSIS 本身读取。

    所以,我的解决方案是将插件构建为一个 jar,与一个包含所有依赖项的 lib 文件夹和一个指示用户将 /plugins/lib/* 添加到 OSMOSIS 的类路径的 README 捆绑在一起。

    osmosis.bat 中,这可以通过将-cp "%PLEXUS_CP%" 更改为-cp "%PLEXUS_CP%";plugins/lib/* 来完成。在 linux 版本中,你必须使用 : 而不是 ;,我认为 (https://stackoverflow.com/a/219801/4249849)。

    Edit1:仅当您当前的工作目录为osmosis/bin 时才有效。如果你想从任何地方(路径)使用它,你必须添加pushdpopdcommands(Windows)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-23
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多