使用Java将GeoJSON数据解析为多边形

时间:2019-02-09 00:18:25

标签: java geometry geojson geojsonio

我有以下要在Polygon中解析的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-jackson 1.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文件数据成功解析Polygon吗?

1 个答案:

答案 0 :(得分:0)

geojson-jackson 1.0 将您的输入GeoJSON标记为无效。该例外规定所有有效的GeoJSON输入必须具有Feature,FeatureCollection,LineString,MultiLineString,MultiPoint,MultiPolygon,Point或Polygon的类型。有关实际源代码,请参见https://github.com/opendatalab-de/geojson-jackson/blob/master/src/main/java/org/geojson/GeoJsonObject.java#L14-L16

您提供的示例输入的类型设置为“ relocationzone”。

为了使其正常工作,您将需要通过创建新的Java类来扩展geojson-jackson,这些Java类或多或少与您的GeoJSON输入的结构相对应。

首先,您需要创建一个类似于以下内容的类:

View > Status Bar