This repository has been archived by the owner on Oct 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
GeoJsonDeserializer.java
109 lines (102 loc) · 4.63 KB
/
GeoJsonDeserializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* This file is part of the GeoLatte project. This code is licenced under
* the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and limitations under the
* License.
*
* Copyright (C) 2010 - 2010 and Ownership of code is shared by:
* Qmino bvba - Romeinsestraat 18 - 3001 Heverlee (http://www.Qmino.com)
* Geovise bvba - Generaal Eisenhowerlei 9 - 2140 Antwerpen (http://www.geovise.com)
*/
package org.geolatte.common.dataformats.json.jackson;
import java.io.IOException;
import java.util.Map;
/**
* Abstract deserialization class that implements the parsing of crs (srids) and is common to a number of deserializers
* that together implement the geojson spec, such as GeometryDeserizalir, FeatureDeserializer
* and FeaturecollectionDeserializer
* <p>
* <i>Creation-Date</i>: 1-sep-2010<br>
* <i>Creation-Time</i>: 18:13:55<br>
* </p>
*
* @author Yves Vandewoude
* @author <a href="http://www.qmino.com">Qmino bvba</a>
* @since SDK1.5
*/
public abstract class GeoJsonDeserializer<T> extends AbstractJsonDeserializer<T> {
private Class<T> deserializerClass;
public GeoJsonDeserializer(JsonMapper owner, Class<T> clazz) {
super(owner);
deserializerClass = clazz;
}
/**
* @return the class of the object that this deserializer can deserialize
*/
protected Class<T> getDeserializerClass()
{
return deserializerClass;
}
/**
* If an srid (crs) is specified in the json object, it is returned. If no srid is found in the current parameter-map
* null is returned. This is a simplified version from the actual GeoJSON specification, since the GeoJSON specification
* allows for relative links to either URLS or local files in which the crs should be defined. This implementation
* only supports named crs's: namely:
* <pre>
* "crs": {
* "type": "name",
* "properties": {
* "name": "...yourcrsname..."
* }
* }
* </pre>
* Besides the fact that only named crs is permitted for deserialization, the given name must either be of the form:
* <pre>
* urn:ogc:def:EPSG:x.y:4326
* </pre>
* (with x.y the version of the EPSG) or of the form:
* <pre>
* EPSG:4326
* </pre>
* @return the SRID value of the crs system in the json if it is present, null otherwise.
* @throws java.io.IOException If a crs object is present, but deserialization is not possible
*/
protected Integer getSrid() throws IOException {
Map<String, Object> crsContent = getTypedParam("crs", null, Map.class);
if (crsContent != null) {
if (crsContent.get("type") == null || !"name".equals(crsContent.get("type"))) {
throw new IOException("If the crs is specified the type must be specified. Currently, only named crses are supported.");
}
Object properties = crsContent.get("properties");
if (properties == null || !(properties instanceof Map) || (!((Map) properties).containsKey("name"))) {
throw new IOException("A crs specification requires a properties value containing a name value.");
}
String sridString = ((Map) properties).get("name").toString();
if (sridString.startsWith("EPSG:")) {
Integer srid = parseDefault(sridString.substring(5), (Integer) null);
if (srid == null) {
throw new IOException("Unable to derive SRID from crs name");
} else {
return srid;
}
} else if (sridString.startsWith("urn:ogc:def:crs:EPSG:")) {
String[] splits = sridString.split(":");
if (splits.length != 7) {
throw new IOException("Unable to derive SRID from crs name");
} else {
Integer srid = parseDefault(splits[6], (Integer) null);
if (srid == null) {
throw new IOException("Unable to derive SRID from crs name");
}
return srid;
}
} else {
throw new IOException("Unable to derive SRID from crs name");
}
}
return null;
}
}