1

我正在尝试编写一个使用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给了我一个IllegalArgumentExceptionwith the message ImageRead: No OperationDescriptor is registered in the current operation registry under this name.

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

我简直无法理解这是怎么发生的!当我查看罐子时,文件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>

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

4

1 回答 1

0

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

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

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

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

于 2015-03-02T19:19:08.137 回答