'Parsing GeoJSON Data to Polygon using Java
I have following Geo JSON file data which i want to parse in Polygon(using java)
Geo JSON File
[
{
"_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
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]
Can anyone tell me how can i use above JSON file data to parse in Polygon successfully?
Solution 1:[1]
geojson-jackson 1.0 is flagging your input GeoJSON as invalid. The exception says that all valid GeoJSON inputs must have a type of either Feature, FeatureCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point or 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.
The example input you provided has the type set to "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.
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;
}
}
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 | Victor |
