|
48 | 48 |
|
49 | 49 | import java.io.IOException;
|
50 | 50 | import java.util.ArrayList;
|
| 51 | +import java.util.HashMap; |
51 | 52 | import java.util.List;
|
52 | 53 | import java.util.Locale;
|
| 54 | +import java.util.Map; |
53 | 55 |
|
54 | 56 | import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
|
55 | 57 | import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
|
@@ -206,6 +208,143 @@ private XContentBuilder coordinatesToXContent(Polygon polygon) throws IOExceptio
|
206 | 208 | return builder.endObject();
|
207 | 209 | }
|
208 | 210 |
|
| 211 | + /** |
| 212 | + * Produces that same GeoJSON as toXContent only in parsed map form |
| 213 | + */ |
| 214 | + public static Map<String, Object> toMap(Geometry geometry) { |
| 215 | + Map<String, Object> root = new HashMap<>(); |
| 216 | + root.put(FIELD_TYPE.getPreferredName(), getGeoJsonName(geometry)); |
| 217 | + |
| 218 | + geometry.visit(new GeometryVisitor<Void, RuntimeException>() { |
| 219 | + @Override |
| 220 | + public Void visit(Circle circle) { |
| 221 | + root.put(FIELD_RADIUS.getPreferredName(), DistanceUnit.METERS.toString(circle.getRadiusMeters())); |
| 222 | + root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), coordinatesToList(circle.getY(), circle.getX(), circle.getZ())); |
| 223 | + return null; |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public Void visit(GeometryCollection<?> collection) { |
| 228 | + List<Object> geometries = new ArrayList<>(collection.size()); |
| 229 | + |
| 230 | + for (Geometry g : collection) { |
| 231 | + geometries.add(toMap(g)); |
| 232 | + } |
| 233 | + root.put(FIELD_GEOMETRIES.getPreferredName(), geometries); |
| 234 | + return null; |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public Void visit(Line line) { |
| 239 | + root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), coordinatesToList(line)); |
| 240 | + return null; |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public Void visit(LinearRing ring) { |
| 245 | + throw new UnsupportedOperationException("linearRing cannot be serialized using GeoJson"); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public Void visit(MultiLine multiLine) { |
| 250 | + List<Object> lines = new ArrayList<>(multiLine.size()); |
| 251 | + for (int i = 0; i < multiLine.size(); i++) { |
| 252 | + lines.add(coordinatesToList(multiLine.get(i))); |
| 253 | + } |
| 254 | + root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), lines); |
| 255 | + return null; |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public Void visit(MultiPoint multiPoint) { |
| 260 | + List<Object> points = new ArrayList<>(multiPoint.size()); |
| 261 | + for (int i = 0; i < multiPoint.size(); i++) { |
| 262 | + Point p = multiPoint.get(i); |
| 263 | + List<Object> point = new ArrayList<>(); |
| 264 | + point.add(p.getX()); |
| 265 | + point.add(p.getY()); |
| 266 | + if (p.hasZ()) { |
| 267 | + point.add(p.getZ()); |
| 268 | + } |
| 269 | + points.add(point); |
| 270 | + } |
| 271 | + root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), points); |
| 272 | + return null; |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public Void visit(MultiPolygon multiPolygon) { |
| 277 | + List<Object> polygons = new ArrayList<>(); |
| 278 | + for (int i = 0; i < multiPolygon.size(); i++) { |
| 279 | + polygons.add(coordinatesToList(multiPolygon.get(i))); |
| 280 | + } |
| 281 | + root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), polygons); |
| 282 | + return null; |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public Void visit(Point point) { |
| 287 | + root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), coordinatesToList(point.getY(), point.getX(), point.getZ())); |
| 288 | + return null; |
| 289 | + } |
| 290 | + |
| 291 | + @Override |
| 292 | + public Void visit(Polygon polygon) { |
| 293 | + List<Object> coords = new ArrayList<>(polygon.getNumberOfHoles() + 1); |
| 294 | + coords.add(coordinatesToList(polygon.getPolygon())); |
| 295 | + for (int i = 0; i < polygon.getNumberOfHoles(); i++) { |
| 296 | + coords.add(coordinatesToList(polygon.getHole(i))); |
| 297 | + } |
| 298 | + root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), coords); |
| 299 | + return null; |
| 300 | + } |
| 301 | + |
| 302 | + @Override |
| 303 | + public Void visit(Rectangle rectangle) { |
| 304 | + List<Object> coords = new ArrayList<>(2); |
| 305 | + coords.add(coordinatesToList(rectangle.getMaxY(), rectangle.getMinX(), rectangle.getMinZ())); // top left |
| 306 | + coords.add(coordinatesToList(rectangle.getMinY(), rectangle.getMaxX(), rectangle.getMaxZ())); // bottom right |
| 307 | + root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), coords); |
| 308 | + return null; |
| 309 | + } |
| 310 | + |
| 311 | + private List<Object> coordinatesToList(double lat, double lon, double alt) { |
| 312 | + List<Object> coords = new ArrayList<>(3); |
| 313 | + coords.add(lon); |
| 314 | + coords.add(lat); |
| 315 | + if (Double.isNaN(alt) == false) { |
| 316 | + coords.add(alt); |
| 317 | + } |
| 318 | + return coords; |
| 319 | + } |
| 320 | + |
| 321 | + private List<Object> coordinatesToList(Line line) { |
| 322 | + List<Object> lines = new ArrayList<>(line.length()); |
| 323 | + for (int i = 0; i < line.length(); i++) { |
| 324 | + List<Object> coords = new ArrayList<>(3); |
| 325 | + coords.add(line.getX(i)); |
| 326 | + coords.add(line.getY(i)); |
| 327 | + if (line.hasZ()) { |
| 328 | + coords.add(line.getZ(i)); |
| 329 | + } |
| 330 | + lines.add(coords); |
| 331 | + } |
| 332 | + return lines; |
| 333 | + } |
| 334 | + |
| 335 | + private List<Object> coordinatesToList(Polygon polygon) { |
| 336 | + List<Object> coords = new ArrayList<>(polygon.getNumberOfHoles() + 1); |
| 337 | + coords.add(coordinatesToList(polygon.getPolygon())); |
| 338 | + for (int i = 0; i < polygon.getNumberOfHoles(); i++) { |
| 339 | + coords.add(coordinatesToList(polygon.getHole(i))); |
| 340 | + } |
| 341 | + return coords; |
| 342 | + } |
| 343 | + |
| 344 | + }); |
| 345 | + return root; |
| 346 | + } |
| 347 | + |
209 | 348 | private static final ConstructingObjectParser<Geometry, GeoJson> PARSER =
|
210 | 349 | new ConstructingObjectParser<>("geojson", true, (a, c) -> {
|
211 | 350 | String type = (String) a[0];
|
|
0 commit comments