簡體   English   中英

如何為 GeoJSON 配置 Spring 引導實體?

[英]How to configure Spring Boot entity for GeoJSON?

我想使用 Spring Boot 和 JPA 將 GeoJSON 保存到數據庫

示例 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
          ]
        ]
      }
    }
  ]
}

我嘗試使用@ElementCollection 和@Embedded 來配置實體。

GeoJson 實體:

@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;
}

特征:

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

特性:

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

幾何學:

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

協調:

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

此配置給出錯誤:

init 方法調用失敗; 嵌套異常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 嵌套異常是 org.hibernate.MappingException:無法確定類型:java.util.List,表:geo_json_features,列:org.springframework.beans.factory 的 [org.hibernate.mapping.Column(coordinates)]。支持.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)~[spring-beans-5.3.15.jar:5.3.15]

我嘗試了其他映射方法,如@ManytoOne 或@ManytoMany,但它們都不起作用。 如何配置 GeoJson 實體以持久保存? 也許我應該在兩個 ElementCollection 中添加 @CollectionTable 注釋,但我不知道如何正確地做

您可以使用它處理幾何類型的 geotools 庫,您需要在 pom.xml 中添加所需的依賴項以下是依賴項:

    <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>

並嘗試這段代碼,您可以從 geojson 文件中獲取幾何圖形:

       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());

        }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM