1 | /* |
---|
2 | * Copyright (C) 2009 Camptocamp |
---|
3 | * |
---|
4 | * This file is part of MapFish Server |
---|
5 | * |
---|
6 | * MapFish Server is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU Lesser General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * MapFish Server is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU Lesser General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU Lesser General Public License |
---|
17 | * along with MapFish Server. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | package org.mapfish.geo; |
---|
21 | |
---|
22 | import com.vividsolutions.jts.geom.Coordinate; |
---|
23 | import com.vividsolutions.jts.geom.Geometry; |
---|
24 | import com.vividsolutions.jts.geom.GeometryFactory; |
---|
25 | import com.vividsolutions.jts.geom.LineString; |
---|
26 | import com.vividsolutions.jts.geom.LinearRing; |
---|
27 | import com.vividsolutions.jts.geom.Point; |
---|
28 | import com.vividsolutions.jts.geom.Polygon; |
---|
29 | import org.json.JSONArray; |
---|
30 | import org.json.JSONException; |
---|
31 | import org.json.JSONObject; |
---|
32 | |
---|
33 | import java.io.InputStream; |
---|
34 | import java.util.ArrayList; |
---|
35 | import java.util.Collection; |
---|
36 | |
---|
37 | public class MfGeoJSONReader { |
---|
38 | private final MfGeoFactory mfFactory; |
---|
39 | private final GeometryFactory jtsFactory; |
---|
40 | |
---|
41 | public MfGeoJSONReader(MfGeoFactory mfFactory) { |
---|
42 | this(mfFactory, new GeometryFactory()); |
---|
43 | } |
---|
44 | |
---|
45 | public MfGeoJSONReader(MfGeoFactory mfFactory, GeometryFactory jtsFactory) { |
---|
46 | this.mfFactory = mfFactory; |
---|
47 | this.jtsFactory = jtsFactory; |
---|
48 | } |
---|
49 | |
---|
50 | public MfGeo decode(InputStream input) throws JSONException { |
---|
51 | JSONObject json = new JSONObject(input); |
---|
52 | return decode(json); |
---|
53 | } |
---|
54 | |
---|
55 | public MfGeo decode(String input) throws JSONException { |
---|
56 | JSONObject json = new JSONObject(input); |
---|
57 | return decode(json); |
---|
58 | } |
---|
59 | |
---|
60 | public MfGeo decode(JSONObject json) throws JSONException { |
---|
61 | final String type = json.getString("type"); |
---|
62 | if (type.equals("FeatureCollection")) { |
---|
63 | return decodeFeatureCollection(json); |
---|
64 | } else if (type.equals("Feature")) { |
---|
65 | return decodeFeature(json); |
---|
66 | } else { |
---|
67 | return decodeGeometry(json); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | private MfFeatureCollection decodeFeatureCollection(JSONObject json) throws JSONException { |
---|
72 | JSONArray features = json.getJSONArray("features"); |
---|
73 | Collection<MfFeature> collection = new ArrayList<MfFeature>(features.length()); |
---|
74 | for (int cpt = 0; cpt < features.length(); ++cpt) { |
---|
75 | collection.add(decodeFeature(features.getJSONObject(cpt))); |
---|
76 | } |
---|
77 | return mfFactory.createFeatureCollection(collection); |
---|
78 | } |
---|
79 | |
---|
80 | private MfFeature decodeFeature(JSONObject json) throws JSONException { |
---|
81 | JSONObject geometry = json.optJSONObject("geometry"); |
---|
82 | String id = json.optString("id", null); |
---|
83 | JSONObject properties = json.getJSONObject("properties"); |
---|
84 | return mfFactory.createFeature(id, decodeGeometry(geometry), properties); |
---|
85 | } |
---|
86 | |
---|
87 | private MfGeometry decodeGeometry(JSONObject json) throws JSONException { |
---|
88 | return mfFactory.createGeometry(decodeJtsGeometry(json)); |
---|
89 | } |
---|
90 | |
---|
91 | private Geometry decodeJtsGeometry(JSONObject json) throws JSONException { |
---|
92 | String type = json.getString("type"); |
---|
93 | final Geometry geometry; |
---|
94 | |
---|
95 | |
---|
96 | if (type.equals("GeometryCollection")) { |
---|
97 | JSONArray geoCoords = json.getJSONArray("geometries"); |
---|
98 | Geometry[] geometries = new Geometry[geoCoords.length()]; |
---|
99 | for (int i = 0; i < geometries.length; ++i) { |
---|
100 | geometries[i] = decodeJtsGeometry(geoCoords.getJSONObject(i)); |
---|
101 | } |
---|
102 | geometry = jtsFactory.createGeometryCollection(geometries); |
---|
103 | |
---|
104 | } else { |
---|
105 | JSONArray coordinates = json.getJSONArray("coordinates"); |
---|
106 | |
---|
107 | if (type.equals("Point")) { |
---|
108 | geometry = jtsFactory.createPoint(decodeCoordinate(coordinates)); |
---|
109 | |
---|
110 | } else if (type.equals("LineString")) { |
---|
111 | geometry = jtsFactory.createLineString(decodeCoordinates(coordinates)); |
---|
112 | |
---|
113 | } else if (type.equals("Polygon")) { |
---|
114 | geometry = decodePolygon(coordinates); |
---|
115 | |
---|
116 | } else if (type.equals("MultiPoint")) { |
---|
117 | Point[] points = new Point[coordinates.length()]; |
---|
118 | for (int i = 0; i < points.length; ++i) { |
---|
119 | points[i] = jtsFactory.createPoint(decodeCoordinate(coordinates.getJSONArray(i))); |
---|
120 | |
---|
121 | } |
---|
122 | geometry = jtsFactory.createMultiPoint(points); |
---|
123 | |
---|
124 | } else if (type.equals("MultiLineString")) { |
---|
125 | LineString[] lineStrings = new LineString[coordinates.length()]; |
---|
126 | for (int i = 0; i < lineStrings.length; ++i) { |
---|
127 | lineStrings[i] = jtsFactory.createLineString(decodeCoordinates(coordinates.getJSONArray(i))); |
---|
128 | } |
---|
129 | geometry = jtsFactory.createMultiLineString(lineStrings); |
---|
130 | |
---|
131 | } else if (type.equals("MultiPolygon")) { |
---|
132 | Polygon[] polygons = new Polygon[coordinates.length()]; |
---|
133 | for (int i = 0; i < polygons.length; ++i) { |
---|
134 | polygons[i] = decodePolygon(coordinates.getJSONArray(i)); |
---|
135 | |
---|
136 | } |
---|
137 | geometry = jtsFactory.createMultiPolygon(polygons); |
---|
138 | |
---|
139 | } else { |
---|
140 | return null; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | return geometry; |
---|
145 | } |
---|
146 | |
---|
147 | private Polygon decodePolygon(JSONArray coordinates) throws JSONException { |
---|
148 | LinearRing outer = jtsFactory.createLinearRing(decodeCoordinates(coordinates.getJSONArray(0))); |
---|
149 | LinearRing[] holes = new LinearRing[coordinates.length() - 1]; |
---|
150 | for (int i = 1; i < coordinates.length(); ++i) { |
---|
151 | holes[i - 1] = jtsFactory.createLinearRing(decodeCoordinates(coordinates.getJSONArray(i))); |
---|
152 | } |
---|
153 | return jtsFactory.createPolygon(outer, holes); |
---|
154 | } |
---|
155 | |
---|
156 | private Coordinate[] decodeCoordinates(JSONArray coordinates) throws JSONException { |
---|
157 | Coordinate[] result = new Coordinate[coordinates.length()]; |
---|
158 | for (int i = 0; i < result.length; ++i) { |
---|
159 | result[i] = decodeCoordinate(coordinates.getJSONArray(i)); |
---|
160 | } |
---|
161 | return result; |
---|
162 | } |
---|
163 | |
---|
164 | private Coordinate decodeCoordinate(JSONArray coord) throws JSONException { |
---|
165 | if (coord.length() > 2) { |
---|
166 | return new Coordinate(coord.getDouble(0), coord.getDouble(1), coord.getDouble(2)); |
---|
167 | } else { |
---|
168 | return new Coordinate(coord.getDouble(0), coord.getDouble(1)); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | } |
---|