【问题标题】:NoSuchMethodException while calling GeometryJSON().read()调用 GeometryJSON().read() 时出现 NoSuchMethodException
【发布时间】:2015-01-30 04:40:40
【问题描述】:

我正在使用 JTS(来自 VividSolutions)和 GeoTools。我有以下代码:

public Geometry jsonToGeom(String json) throws IOException {
    Geometry obj = new GeometryJSON().read(json);
    return obj;
}

但是,这会返回以下 RunTimeException:

java.lang.RuntimeException: java.lang.NoSuchMethodException: org.geotools.geojson.feature.FeatureHandler.<init>(com.vividsolutions.jts.geom.GeometryFactory)
at org.geotools.geojson.DelegatingHandler.createDelegate(DelegatingHandler.java:130)
at org.geotools.geojson.geom.GeometryHandler.primitive(GeometryHandler.java:68)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.geotools.geojson.GeoJSONUtil.parse(GeoJSONUtil.java:236)
at org.geotools.geojson.geom.GeometryJSON.parse(GeometryJSON.java:655)
at org.geotools.geojson.geom.GeometryJSON.read(GeometryJSON.java:196)
at am.abhi.experiments.geotoolstest.GeoJson.jsonToGeom(GeoJson.java:13)
at am.abhi.experiments.geotoolstest.SomeTest.testSomething(SomeTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.NoSuchMethodException: org.geotools.geojson.feature.FeatureHandler.<init>(com.vividsolutions.jts.geom.GeometryFactory)
at java.lang.Class.getConstructor0(Class.java:2849)
at java.lang.Class.getConstructor(Class.java:1718)
at org.geotools.geojson.DelegatingHandler.createDelegate(DelegatingHandler.java:123)
... 33 more

在单步执行代码时,我在 org.geotools.geojson.DelegatingHandler 中找到了导致错误的方法:

protected IContentHandler createDelegate(Class clazz, Object[] args) {
    try {
        if (args != null && args.length > 0) {
            Class[] types = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                types[i] = args[i].getClass();
            }

            return (IContentHandler) clazz.getConstructor(types).newInstance(args);
        }
        else {
            return (IContentHandler) clazz.newInstance();
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

在线return (IContentHandler) clazz.getConstructor(types).newInstance(args).

它在调用 FeatureHandler 并尝试将 GeometryFactory 作为参数传递时失败。我正在使用 JTS 1.8 和 GeoTools 13-SNAPSHOT。

任何帮助或解决方法将不胜感激。

【问题讨论】:

    标签: java geotools jts


    【解决方案1】:

    我遇到了同样的问题 - 但结果证明它与类路径或 JTS 版本无关。事实证明,人们使用术语“GeoJSON”来表示不止一件事。

    我下载了一些GeoJSON files to describe timezones,它包含这样的json:

    {
      "type": "FeatureCollection", 
      "features": [
        {
          "geometry": {
            "type": "MultiPolygon", 
            "coordinates": [
              [
    ...
    

    如果您使用此问题中的代码来读取该文件:

    Geometry obj = new GeometryJSON().read(json); 
    

    但是您的 json 文件实际上是 FeatureJSON,就像我的一样,您会收到报告的(无用的)错误消息。相反,尝试这样的事情:

    FeatureJSON fjson = new FeatureJSON();
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("test.geojson");
    FeatureCollection features = fjson.readFeatureCollection(is);
    

    【讨论】:

    • 嘿,你们有没有机会,@nont @Abhishek-Dey-Das,以不同的方式解决了这个问题? Official geojson specification 表明 FeatureFeatureCollection 类型作为 GeoJson 类型完全有效。我的问题是我读了一个包含 geojson 的 json。我不知道它会持有哪种类型,我也不想使用 try-catch 机制来“猜测”它
    • 原始代码使用new GeometryJSON.read() - 我希望GeoJSON.read() 能够很好地应对,但是当给定的对象本身不是有效的几何图形时,几何阅读器失败是公平的。
    【解决方案2】:

    你有错误的 JTS 版本——我怀疑这意味着你没有使用 maven。您需要 1.13 版才能使用 GeoTools 13-SNAPSHOT。

    $  mvn dependency:tree
    
    [INFO] Scanning for projects...
    [INFO]
    [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building GeoJSON Support 13-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ gt-geojson ---
    [INFO] org.geotools:gt-geojson:jar:13-SNAPSHOT
    [INFO] +- org.geotools:gt-main:jar:13-SNAPSHOT:compile
    [INFO] |  +- org.geotools:gt-api:jar:13-SNAPSHOT:compile
    [INFO] |  +- com.vividsolutions:jts:jar:1.13:compile
    [INFO] |  \- org.jdom:jdom:jar:1.1.3:compile
    [INFO] +- com.googlecode.json-simple:json-simple:jar:1.1:compile
    [INFO] +- org.geotools:gt-epsg-hsql:jar:13-SNAPSHOT:test
    [INFO] |  +- org.geotools:gt-referencing:jar:13-SNAPSHOT:compile
    [INFO] |  |  +- java3d:vecmath:jar:1.3.2:compile
    [INFO] |  |  +- commons-pool:commons-pool:jar:1.5.4:compile
    [INFO] |  |  +- org.geotools:gt-metadata:jar:13-SNAPSHOT:compile
    [INFO] |  |  |  \- org.geotools:gt-opengis:jar:13-SNAPSHOT:compile
    [INFO] |  |  |     \- net.java.dev.jsr-275:jsr-275:jar:1.0-beta-2:compile
    [INFO] |  |  \- jgridshift:jgridshift:jar:1.0:compile
    [INFO] |  \- org.hsqldb:hsqldb:jar:2.2.8:test
    [INFO] +- javax.media:jai_core:jar:1.1.3:compile
    [INFO] \- junit:junit:jar:4.11:test
    [INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
    

    【讨论】:

    • 我认为还有更多内容 - 我正在使用带有 geotools 13.0 的 maven,它给了我同样的错误消息。我认为还有其他一些类路径冲突
    • @nont 你最好提出一个包含你的代码的新问题,这样我就可以尝试构建和运行它
    • 实际上,它与JTS版本或类路径无关。
    【解决方案3】:

    解决这个问题的原因是我有一个项目使用 JTS 1.8,而 Geotools 运行 1.13。在 Spring-Boot 中,显然只有 1.8 最终出现在 jar 中,从而导致冲突和方法未找到。我将我的依赖项目更改为与 geotools 相同的版本,并且已修复。

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2013-07-16
      • 2012-09-17
      • 2016-07-23
      • 1970-01-01
      相关资源
      最近更新 更多