I am using JTS (from VividSolutions) and GeoTools. I have the following code:
public Geometry jsonToGeom(String json) throws IOException {
Geometry obj = new GeometryJSON().read(json);
return obj;
}
However, this returns the following 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
On stepping through the code, I found this method in org.geotools.geojson.DelegatingHandler which causes the error:
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);
}
}
on line return (IContentHandler) clazz.getConstructor(types).newInstance(args)
.
It fails when it calls FeatureHandler and tries to pass a GeometryFactory as an argument. I am on JTS 1.8 and GeoTools 13-SNAPSHOT.
Any help or workaround would be appreciated.
I had this same problem - but it turned out to have nothing to do with the classpath or the version of JTS. It turns out that people use the term "GeoJSON" to mean more than one thing.
I downloaded some GeoJSON files to describe timezones and it contained json like this:
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
...
If you use the code in this question to read that file:
Geometry obj = new GeometryJSON().read(json);
but your json file is actually FeatureJSON like mine, you'll get the (unhelpful) error messages reported. Instead, try something like this:
FeatureJSON fjson = new FeatureJSON();
InputStream is = this.getClass().getClassLoader().getResourceAsStream("test.geojson");
FeatureCollection features = fjson.readFeatureCollection(is);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With