java - 调用 GeometryJSON().read() 时出现 NoSuchMethodException

标签 java geotools jts

我正在使用 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);
    }
}

在线 返回 (IContentHandler) clazz.getConstructor(types).newInstance(args)

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

如有任何帮助或解决方法,我们将不胜感激。

最佳答案

我遇到了同样的问题 - 但事实证明它与类路径或 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);

关于java - 调用 GeometryJSON().read() 时出现 NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27240869/

相关文章:

java - android studio 中未使用的导入语句和导入为灰色

java - GeoTiffReader 阻止 deleteOnExit

java - Geotools 从多边形和维度创建 CRS

geotools - Repast Simphony 上的最短距离?

java - 如何使用 GeoTools/ProJ.4(或其他 api)将坐标从 HK80 GRID 转换为纬度/经度?

mysql - com.vividsolutions.jts.geom.Point 的 AttributeConverter 被忽略

java - IntelliJ IDEA 中的 JTS 拓扑套件

java - 如何使用 setLayout(null) 使 JPanel 保持在顶部(在 z 轴上)

javax.persistence.NoResultException : No entity found for query

Java 需要一个分号,而显然有一个分号