5

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 を使用しています。

任意のヘルプまたは回避策をいただければ幸いです。

4

3 に答える 3

3

私はこれと同じ問題を抱えていましたが、クラスパスや JTS のバージョンとは何の関係もないことが判明しました。「GeoJSON」という用語は、複数の意味で使用されていることがわかりました。

タイムゾーンを記述するためにいくつかの GeoJSON ファイルをダウンロードしたところ、次のような 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);
于 2015-08-11T14:42:47.697 に答える
0

JTS のバージョンが間違っています。これは、maven を使用していないことを意味していると思われます。GeoTools 13-SNAPSHOT を使用するには、バージョン 1.13 が必要です。

$  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
于 2014-12-03T08:33:23.500 に答える