用Java实现GeoJSON数据到多边形的解析

laawzig2  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(255)

我有以下Geo JSON文件数据,我想在多边形中解析(使用Java)

地理位置JSON文件

[
  {
    "_id": "58a58bf685979b5415f3a39a",
    "updatedAt": "2017-03-27T14:04:34.470Z",
    "createdAt": "2017-02-16T11:24:38.375Z",
    "__v": 0,
    "name": "0",
    "cityId": "548876e13a9424d55af738b5",
    "legacyId": "18_92",
    "type": "relocationzone",
    "geoFeatures": [
      {
        "name": "opticalCenter",
        "geometry": {
          "type": "Point",
          "coordinates": [
            9.1371735,
            48.790337
          ]
        }
      },
      {
        "name": "center",
        "geometry": {
          "type": "Point",
          "coordinates": [
            9.137148666666667,
            48.79031233333333
          ]
        }
      }
    ],
    "options": {
      "active": true,
      "is_excluded": false,
      "area": 0.4
    },
    "timedOptions": [
      {
        "key": "min",
        "changesOverTime": [
          [
            0,
            0
          ]
        ]
      },
      {
        "key": "max",
        "changesOverTime": [
          [
            0,
            200
          ]
        ]
      },
      {
        "key": "idle_time",
        "changesOverTime": [
          [
            0,
            2000
          ]
        ]
      },
      {
        "key": "revenue",
        "changesOverTime": [
          [
            0,
            0
          ]
        ]
      },
      {
        "key": "walking_range1",
        "changesOverTime": [
          [
            0,
            0
          ]
        ]
      },
      {
        "key": "walking_range2",
        "changesOverTime": [
          [
            0,
            0
          ]
        ]
      }
    ],
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [
            9.137248,
            48.790411
          ],
          [
            9.137248,
            48.790263
          ],
          [
            9.13695,
            48.790263
          ],
          [
            9.137248,
            48.790411
          ]
        ]
      ]
    },
    "version": 1,
    "$computed": {
      "activeTimedOptions": {
        "min": 0,
        "max": 200,
        "idle_time": 2000,
        "revenue": 0,
        "walking_range1": 0,
        "walking_range2": 0
      }
    }
  }
]

我已经使用geojson-jackson1.0进行了解析,使用了以下Java代码

GeoJsonObject[] object = new ObjectMapper().readValue(new File(fileLocation), GeoJsonObject[].class);
        if (object[0] instanceof Polygon) {
            System.out.println("yes");
        }
        else {
            System.out.println("No");
        }

但是我得到了一个异常com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'relocationzone' as a subtype of [simple type, class org.geojson.GeoJsonObject]: known type ids = [Feature, FeatureCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon]
谁能告诉我如何使用上述JSON文件数据成功解析多边形?

yqkkidmi

yqkkidmi1#

geojson-jackson 1.0正在将您的输入GeoJSON标记为无效。异常表明所有有效的GeoJSON输入必须具有要素、FeatureCollection、线串、多线串、多点、多面、点或面的类型。https://github.com/opendatalab-de/geojson-jackson/blob/master/src/main/java/org/geojson/GeoJsonObject.java#L14-L16有关实际源代码,请访问www.example.com。

您提供的示例输入的类型设置为“relocationzone”。
为了使其工作,您需要通过创建新的Java类来扩展geojson-jackson,这些类或多或少地与GeoJSON输入的结构相对应。
作为起点,您需要创建一个类似于以下内容的类:

public class relocationzone extends GeoJsonObject {

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private Map<String, Object> properties = new HashMap<String, Object>();
    @JsonInclude(JsonInclude.Include.ALWAYS)

    // Expected JSON data
    private GeoJsonObject geometry;
    private String id;
    private String _id;
    private String updatedAt;;
    private String name;
    private String _v;
    private String createdAt;
    private String legacyId;

    public void setProperty(String key, Object value) {
        properties.put(key, value);
    }

    @SuppressWarnings("unchecked")
    public <T> T getProperty(String key) {
        return (T)properties.get(key);
    }

    public Map<String, Object> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, Object> properties) {
        this.properties = properties;
    }

    public GeoJsonObject getGeometry() {
        return geometry;
    }

    public void setGeometry(GeoJsonObject geometry) {
        this.geometry = geometry;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

相关问题