无法通过 tomcat 获得 CRS:org.opengis.referencing.NoSuchAuthorityCodeException

Impossible to get CRS with tomcat : org.opengis.referencing.NoSuchAuthorityCodeException

我遇到了一个奇怪的情况。 我使用 geotools 来投影栅格并且它可以工作,例如

CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");

GridCoverage2D projectedImage = (GridCoverage2D) Operations.DEFAULT.resample(gridCoverageImage,
                        targetCRS);

现在我使用完全相同的代码将我的进程移动到 Web 服务器控制器中:

public ResponseEntity<InputStreamResource> getProjectedImage(@RequestParam 
String filename, @RequestParam String targetCrs){
     File file = new File(filename);
     CoordinateReferenceSystem targetCRS = CRS.decode(targetCrs);
     /** Some process to return file /**
}

我有:

org.opengis.referencing.NoSuchAuthorityCodeException: No code "EPSG:3857"
from authority "EPSG" found for object of type "EngineeringCRS"

我的pom.xml

   <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-shapefile</artifactId>
        <version>18.2</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-epsg-hsql</artifactId>
        <version>18.2</version>
    </dependency>   
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-coverage</artifactId>
        <version>18.2</version>
    </dependency>     
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-geotiff</artifactId>
        <version>18.2</version>
    </dependency>   
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-image</artifactId>
        <version>18.2</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-wms</artifactId>
        <version>18.2</version>
    </dependency> 

当我查看 WEB-INF/lib 时,所有 jar 都在这里,包括依赖项(gt-referencing、gt-metadata.....)

Tomcat:8.0 Java 8 地理工具:18.2

当我不在 servlet 容器中时它工作正常。 此外,来自 geotools 的其他实用程序运行良好。例如,裁剪或 GridCoverage2D 转换在这个 servlet 容器中工作。

你能帮我理解这是怎么回事吗?

提前致谢

我找到了一个解决方法,即使它没有解决最初的问题(与 geotools 的依赖关系和部署到 Tomcat 相关)

不要使用 gt-epsg-hsql,它会导致您的应用搜索 EPSG 代码以解码到 HSQL 数据库中。

而不是使用 CRS.decode(targetCrs);

CRS.parseWKT("PROJCS[\"WGS 84 / Plate Carree (deprecated)\",\r\n" + 
                "    GEOGCS[\"WGS 84\",\r\n" + 
                "        DATUM[\"WGS_1984\",\r\n" + 
                "            SPHEROID[\"WGS 84\",6378137,298.257223563,\r\n" + 
                "                AUTHORITY[\"EPSG\",\"7030\"]],\r\n" + 
                "            AUTHORITY[\"EPSG\",\"6326\"]],\r\n" + 
                "        PRIMEM[\"Greenwich\",0,\r\n" + 
                "            AUTHORITY[\"EPSG\",\"8901\"]],\r\n" + 
                "        UNIT[\"degree\",0.0174532925199433,\r\n" + 
                "            AUTHORITY[\"EPSG\",\"9122\"]],\r\n" + 
                "        AUTHORITY[\"EPSG\",\"4326\"]],\r\n" + 
                "    PROJECTION[\"Equirectangular\"],\r\n" + 
                "    UNIT[\"metre\",1,\r\n" + 
                "        AUTHORITY[\"EPSG\",\"9001\"]],\r\n" + 
                "    AXIS[\"X\",EAST],\r\n" + 
                "    AXIS[\"Y\",NORTH],\r\n" + 
                "    AUTHORITY[\"EPSG\",\"32662\"]]");

您可以找到与每个 EPSG 代码相关的所有 WKT here

如果有人对原始问题有答案或解释,我很乐意阅读:)

您的问题很可能是 Tomcat(或用户 运行 它)没有对 java.io.tmpdir 的写入权限,这是 GeoTools 解压 H2 EPSG 数据库的位置它首先查找 EPSG 代码。

您可以更改临时目录的权限以允许 tomcat 在那里写入,或者您可以通过更改 catalina.sh 中的 CATALINA_TMPDIR 变量或 catalina.bat,或者只需将 Djava.io.tmpdir=c:\{yourDir} 添加到您的启动脚本。

这个 question 也有一些可能有帮助的答案。