'How to configure Spring Boot entity for GeoJSON?

I want to save a GeoJSON to database by using Spring Boot and JPA

Example JSON object:

    {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "type": "polyline"
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            21.011127,
            52.230914
          ],
          [
            21.013166,
            52.229978
          ],
          [
            21.011331,
            52.22911
          ],
          [
            21.00845,
            52.228953
          ],
          [
            21.009062,
            52.230011
          ],
          [
            21.009513,
            52.230287
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "type": "polyline"
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            21.022489,
            52.226406
          ],
          [
            21.023004,
            52.228995
          ],
          [
            21.018069,
            52.230007
          ],
          [
            21.018755,
            52.225618
          ],
          [
            21.016202,
            52.22454
          ],
          [
            21.013906,
            52.226735
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "type": "polyline"
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            21.013627,
            52.232005
          ],
          [
            21.018165,
            52.231933
          ],
          [
            21.019228,
            52.232537
          ],
          [
            21.019989,
            52.232925
          ],
          [
            21.019109,
            52.233201
          ]
        ]
      }
    }
  ]
}

I tried to configure the entity by using @ElementCollection and @Embedded.

GeoJson entity:

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
public class GeoJson {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String type;
    @ElementCollection(targetClass = Feature.class)
    private List<Feature> features;
}

Feature:

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
public class Feature {
    private String type;
    @Embedded
    private Properties properties;
    @Embedded
    private Geometry geometry;
}

Properties:

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
public class Properties {
    private String type;
}

Geometry:

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
public class Geometry {
    private String type;
    @ElementCollection(targetClass = Coordinate.class)
    private List<Coordinate> coordinates;
}

Coordinate:

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
public class Coordinate {
    private BigDecimal lat;
    private BigDecimal lng;
}

This configuration gives error:

Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: geo_json_features, for columns: [org.hibernate.mapping.Column(coordinates)] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]

I tried other mapping methods like @ManytoOne or @ManytoMany, but none of them works. How can I configure GeoJson entity to persist? Maybe i should add @CollectionTable annotation in both ElementCollection but i don't know how to do it correctly



Solution 1:[1]

You can work with the geotools library it handles geometry type you need to add the needed dependencies in your pom.xml Here are the dependencies :

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.n52.jackson</groupId>
        <artifactId>jackson-datatype-jts</artifactId>
        <version>1.2.10</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-geojsondatastore</artifactId>
        <version>26.4</version>
    </dependency>

and try this piece of code you can get the geometries from your geojson file :

       File inFile = new File("yourpath/file.geojson");
        var geojsonStore = new GeoJSONDataStore(inFile);
        SimpleFeatureSource source = geojsonStore.getFeatureSource();
        SimpleFeatureCollection features = source.getFeatures();
        var iterator = features.features();
        while (iterator.hasNext()) {
            // copy the contents of each feature and transform the geometry
            SimpleFeature feature = iterator.next();
            System.out.println(feature.getAttributes());

        }  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ilyasse Hanafi