From 125b33d3dc9dfe261807ad2015697b47744d045a Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Wed, 13 Mar 2013 09:32:43 +0100 Subject: [PATCH] GeoJSONShapeParser parses JSON correctly and extracts coordinates even if 'crs' field is included. Fixes #2763 --- .../common/geo/GeoJSONShapeParser.java | 7 +++++-- .../common/geo/GeoJSONShapeParserTests.java | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/geo/GeoJSONShapeParser.java b/src/main/java/org/elasticsearch/common/geo/GeoJSONShapeParser.java index c6863037dba..d457c821625 100644 --- a/src/main/java/org/elasticsearch/common/geo/GeoJSONShapeParser.java +++ b/src/main/java/org/elasticsearch/common/geo/GeoJSONShapeParser.java @@ -80,14 +80,17 @@ public class GeoJSONShapeParser { String fieldName = parser.currentName(); if ("type".equals(fieldName)) { - token = parser.nextToken(); + parser.nextToken(); shapeType = parser.text().toLowerCase(Locale.ENGLISH); if (shapeType == null) { throw new ElasticSearchParseException("Unknown Shape type [" + parser.text() + "]"); } } else if ("coordinates".equals(fieldName)) { - token = parser.nextToken(); + parser.nextToken(); node = parseCoordinates(parser); + } else { + parser.nextToken(); + parser.skipChildren(); } } } diff --git a/src/test/java/org/elasticsearch/test/unit/common/geo/GeoJSONShapeParserTests.java b/src/test/java/org/elasticsearch/test/unit/common/geo/GeoJSONShapeParserTests.java index a4205e7a466..99d1e05b197 100644 --- a/src/test/java/org/elasticsearch/test/unit/common/geo/GeoJSONShapeParserTests.java +++ b/src/test/java/org/elasticsearch/test/unit/common/geo/GeoJSONShapeParserTests.java @@ -210,6 +210,27 @@ public class GeoJSONShapeParserTests { assertGeometryEquals(new JtsGeometry(expected, GeoShapeConstants.SPATIAL_CONTEXT, false), multiPolygonGeoJson); } + @Test + public void testThatParserExtractsCorrectTypeAndCoordinatesFromArbitraryJson() throws IOException { + String pointGeoJson = XContentFactory.jsonBuilder().startObject() + .startObject("crs") + .field("type", "name") + .startObject("properties") + .field("name", "urn:ogc:def:crs:OGC:1.3:CRS84") + .endObject() + .endObject() + .field("bbox", "foobar") + .field("type", "point") + .field("bubu", "foobar") + .startArray("coordinates").value(100.0).value(0.0).endArray() + .startObject("nested").startArray("coordinates").value(200.0).value(0.0).endArray().endObject() + .startObject("lala").field("type", "NotAPoint").endObject() + .endObject().string(); + + Point expected = GEOMETRY_FACTORY.createPoint(new Coordinate(100.0, 0.0)); + assertGeometryEquals(new JtsPoint(expected, GeoShapeConstants.SPATIAL_CONTEXT), pointGeoJson); + } + private void assertGeometryEquals(Shape expected, String geoJson) throws IOException { XContentParser parser = JsonXContent.jsonXContent.createParser(geoJson); parser.nextToken();