[英]Parsing GeoJSON Data to Polygon using Java
I have following Geo JSON file data which i want to parse in Polygon(using java)我有以下我想在 Polygon 中解析的 Geo JSON 文件数据(使用 java)
Geo JSON File地理 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
}
}
}
]
I have used geojson-jackson 1.0 for parsing using below Java code我已经使用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");
}
But i am getting an exception 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]
但我收到一个异常
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]
Can anyone tell me how can i use above JSON file data to parse in Polygon successfully?谁能告诉我如何使用上面的 JSON 文件数据在 Polygon 中成功解析?
geojson-jackson 1.0 is flagging your input GeoJSON as invalid. geojson-jackson 1.0将您的输入 GeoJSON 标记为无效。 The exception says that all valid GeoJSON inputs must have a type of either Feature, FeatureCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point or Polygon.
例外情况是所有有效的 GeoJSON 输入必须具有 Feature、FeatureCollection、LineString、MultiLineString、MultiPoint、MultiPolygon、Point 或 Polygon 的类型。 See https://github.com/opendatalab-de/geojson-jackson/blob/master/src/main/java/org/geojson/GeoJsonObject.java#L14-L16 for the actual source code.
有关实际源代码,请参阅https://github.com/opendatalab-de/geojson-jackson/blob/master/src/main/java/org/geojson/GeoJsonObject.java#L14-L16 。
The example input you provided has the type set to "relocationzone".您提供的示例输入的类型设置为“relocationzone”。
In order to get this to work you will need to extend geojson-jackson by creating new Java classes that more or less correspond to the structure of your GeoJSON inputs.为了让它工作,您需要通过创建或多或少对应于 GeoJSON 输入结构的新 Java 类来扩展 geojson-jackson。
As a starting point you will need to create a class that looks something like this:作为起点,您需要创建一个如下所示的类:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.