Clara
Clara

Reputation: 25

Cannot create GeometryFactory : Caused by: java.lang.NoClassDefFoundError: org/geotools/geometry/jts/JTSFactoryFinder

I'm having some trouble trying to create a Point and Polygon with JTS and GeoTools. I don't know what is causing the problem and I've tried everything but nothing worked for me.

If I compile the project with maven everything works fine, but when I'm executing it with Eclipse, when I try to create the Geometry Factory:

GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

It appears the next error:

Caused by: java.lang.ClassNotFoundException: org.geotools.geometry.jts.JTSFactoryFinder from [Module "deployment.famaWebApp.war" from Service Module Loader]

It happens the same when using the constructor:

GeometryFactory geometryFactory = new GeometryFactory();;

It shows the following error:

Caused by: java.lang.ClassNotFoundException: org.locationtech.jts.geom.GeometryFactory from [Module "deployment.famaWebApp.war" from Service Module Loader]

These are the imports:

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;

And this is the code:

// Patrón de expresión regular para encontrar pares de números entre corchetes
Matcher matcher;
// GeometryFactory geometryFactory = new GeometryFactory();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
if ("Point".equals(geometryType))
{
    Pattern patternPoint = Pattern.compile("\\[([\\d.]+),\\s*([\\d.]+),\\s*([\\d.]+)\\]");
    matcher = patternPoint.matcher(coordinates);
    if (matcher.find())
    {
        double x = Double.parseDouble(matcher.group(1));
        double y = Double.parseDouble(matcher.group(2));

        Point point = geometryFactory.createPoint(new Coordinate(x, y));
        geometryFeature.put("coordinates", point);
    }
}
else
{
    Pattern patternPolyline = Pattern.compile("\\[\\s*([-+]?\\d+\\.\\d+),\\s*([-+]?\\d+\\.\\d+)\\s*\\]");
    matcher = patternPolyline.matcher(coordinates);

    // Buscar y crear objetos Point/Polygon
    int i = 0;
    Coordinate[] coords = new Coordinate[] {};
    while (matcher.find())
    {
        double x = Double.parseDouble(matcher.group(1));
        double y = Double.parseDouble(matcher.group(2));

        coords[i] = new Coordinate(x, y);
    }

    Polygon polygon = geometryFactory.createPolygon(coords);
    geometryFeature.put("coordinates", polygon);
}

The pom.xml is this:

<dependency>
    <groupId>org.locationtech.jts</groupId>
    <artifactId>jts-core</artifactId>
    <version>1.18.2</version>
</dependency>   
<dependency>
     <groupId>org.geotools</groupId>
     <artifactId>gt-main</artifactId>
     <version>28.5</version>
</dependency>
<dependency>
     <groupId>org.geotools</groupId>
     <artifactId>gt-metadata</artifactId>
     <version>28.5</version>
</dependency>

I don't know if I'm missing some dependencies or if I'm using the wrong version. In my project I'm using java 8. I've tried to add gt-main dependencies and is still not working. Do you think of something that I'm doing wrong?

Thank you very much.

Upvotes: 1

Views: 855

Answers (1)

Ankur Pandey
Ankur Pandey

Reputation: 51

I also faced a similar issue while migrating from org.datasyslab.GeoSpark to sedona. Here is the stack trace:

java.lang.NoClassDefFoundError: org/locationtech/jts/geom/Geometry
    at org.apache.spark.sql.sedona_sql.UDT.UdtRegistratorWrapper$.registerAll(UdtRegistratorWrapper.scala:27)
    at org.apache.sedona.sql.UDT.UdtRegistrator$.registerAll(UdtRegistrator.scala:26)
    at org.apache.sedona.sql.utils.SedonaSQLRegistrator$.registerAll(SedonaSQLRegistrator.scala:33)

Spark version: 3.2.0, Scala version: 2.12.10, Java version: 8

My issue was resolved after adding this dependency in pom.xml

<dependency>
    <groupId>org.locationtech.jts</groupId>
    <artifactId>jts-core</artifactId>
    <version>1.18.1</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-referencing</artifactId>
    <version>24.1</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-main</artifactId>
    <version>24.1</version>
</dependency>
<dependency>
    <groupId>org.wololo</groupId>
    <artifactId>jts2geojson</artifactId>
    <version>0.14.3</version>
</dependency>

Upvotes: 0

Related Questions