GeoJson types can be lowercase in Elasticsearch.

Original Pull Request #1657 
Closes #1655
This commit is contained in:
Peter-Josef Meisch 2021-01-19 20:25:38 +01:00 committed by GitHub
parent 89d6ae7f49
commit 159520d001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 25 deletions

View File

@ -172,19 +172,19 @@ public class GeoConverters {
String type = GeoConverters.getGeoJsonType(source); String type = GeoConverters.getGeoJsonType(source);
switch (type) { switch (type) {
case GeoJsonPoint.TYPE: case "point":
return MapToGeoJsonPointConverter.INSTANCE.convert(source); return MapToGeoJsonPointConverter.INSTANCE.convert(source);
case GeoJsonMultiPoint.TYPE: case "multipoint":
return MapToGeoJsonMultiPointConverter.INSTANCE.convert(source); return MapToGeoJsonMultiPointConverter.INSTANCE.convert(source);
case GeoJsonLineString.TYPE: case "linestring":
return MapToGeoJsonLineStringConverter.INSTANCE.convert(source); return MapToGeoJsonLineStringConverter.INSTANCE.convert(source);
case GeoJsonMultiLineString.TYPE: case "multilinestring":
return MapToGeoJsonMultiLineStringConverter.INSTANCE.convert(source); return MapToGeoJsonMultiLineStringConverter.INSTANCE.convert(source);
case GeoJsonPolygon.TYPE: case "polygon":
return MapToGeoJsonPolygonConverter.INSTANCE.convert(source); return MapToGeoJsonPolygonConverter.INSTANCE.convert(source);
case GeoJsonMultiPolygon.TYPE: case "multipolygon":
return MapToGeoJsonMultiPolygonConverter.INSTANCE.convert(source); return MapToGeoJsonMultiPolygonConverter.INSTANCE.convert(source);
case GeoJsonGeometryCollection.TYPE: case "geometrycollection":
return MapToGeoJsonGeometryCollectionConverter.INSTANCE.convert(source); return MapToGeoJsonGeometryCollectionConverter.INSTANCE.convert(source);
default: default:
throw new IllegalArgumentException("unknown GeoJson type " + type); throw new IllegalArgumentException("unknown GeoJson type " + type);
@ -217,7 +217,7 @@ public class GeoConverters {
public GeoJsonPoint convert(Map<String, Object> source) { public GeoJsonPoint convert(Map<String, Object> source) {
String type = GeoConverters.getGeoJsonType(source); String type = GeoConverters.getGeoJsonType(source);
Assert.isTrue(type.equals(GeoJsonPoint.TYPE), "does not contain a type 'Point'"); Assert.isTrue(type.equalsIgnoreCase(GeoJsonPoint.TYPE), "does not contain a type 'Point'");
Object coordinates = source.get("coordinates"); Object coordinates = source.get("coordinates");
Assert.notNull(coordinates, "Document to convert does not contain coordinates"); Assert.notNull(coordinates, "Document to convert does not contain coordinates");
@ -255,7 +255,7 @@ public class GeoConverters {
public GeoJsonMultiPoint convert(Map<String, Object> source) { public GeoJsonMultiPoint convert(Map<String, Object> source) {
String type = GeoConverters.getGeoJsonType(source); String type = GeoConverters.getGeoJsonType(source);
Assert.isTrue(type.equals(GeoJsonMultiPoint.TYPE), "does not contain a type 'MultiPoint'"); Assert.isTrue(type.equalsIgnoreCase(GeoJsonMultiPoint.TYPE), "does not contain a type 'MultiPoint'");
Object coordinates = source.get("coordinates"); Object coordinates = source.get("coordinates");
Assert.notNull(coordinates, "Document to convert does not contain coordinates"); Assert.notNull(coordinates, "Document to convert does not contain coordinates");
Assert.isTrue(coordinates instanceof List, "coordinates must be a List"); Assert.isTrue(coordinates instanceof List, "coordinates must be a List");
@ -290,7 +290,7 @@ public class GeoConverters {
public GeoJsonLineString convert(Map<String, Object> source) { public GeoJsonLineString convert(Map<String, Object> source) {
String type = GeoConverters.getGeoJsonType(source); String type = GeoConverters.getGeoJsonType(source);
Assert.isTrue(type.equals(GeoJsonLineString.TYPE), "does not contain a type 'LineString'"); Assert.isTrue(type.equalsIgnoreCase(GeoJsonLineString.TYPE), "does not contain a type 'LineString'");
Object coordinates = source.get("coordinates"); Object coordinates = source.get("coordinates");
Assert.notNull(coordinates, "Document to convert does not contain coordinates"); Assert.notNull(coordinates, "Document to convert does not contain coordinates");
Assert.isTrue(coordinates instanceof List, "coordinates must be a List"); Assert.isTrue(coordinates instanceof List, "coordinates must be a List");
@ -322,7 +322,7 @@ public class GeoConverters {
public GeoJsonMultiLineString convert(Map<String, Object> source) { public GeoJsonMultiLineString convert(Map<String, Object> source) {
String type = GeoConverters.getGeoJsonType(source); String type = GeoConverters.getGeoJsonType(source);
Assert.isTrue(type.equals(GeoJsonMultiLineString.TYPE), "does not contain a type 'MultiLineString'"); Assert.isTrue(type.equalsIgnoreCase(GeoJsonMultiLineString.TYPE), "does not contain a type 'MultiLineString'");
List<GeoJsonLineString> lines = geoJsonLineStringsFromMap(source); List<GeoJsonLineString> lines = geoJsonLineStringsFromMap(source);
return GeoJsonMultiLineString.of(lines); return GeoJsonMultiLineString.of(lines);
} }
@ -350,7 +350,7 @@ public class GeoConverters {
public GeoJsonPolygon convert(Map<String, Object> source) { public GeoJsonPolygon convert(Map<String, Object> source) {
String type = GeoConverters.getGeoJsonType(source); String type = GeoConverters.getGeoJsonType(source);
Assert.isTrue(type.equals(GeoJsonPolygon.TYPE), "does not contain a type 'Polygon'"); Assert.isTrue(type.equalsIgnoreCase(GeoJsonPolygon.TYPE), "does not contain a type 'Polygon'");
List<GeoJsonLineString> lines = geoJsonLineStringsFromMap(source); List<GeoJsonLineString> lines = geoJsonLineStringsFromMap(source);
Assert.isTrue(lines.size() > 0, "no linestrings defined in polygon"); Assert.isTrue(lines.size() > 0, "no linestrings defined in polygon");
GeoJsonPolygon geoJsonPolygon = GeoJsonPolygon.of(lines.get(0)); GeoJsonPolygon geoJsonPolygon = GeoJsonPolygon.of(lines.get(0));
@ -394,7 +394,7 @@ public class GeoConverters {
public GeoJsonMultiPolygon convert(Map<String, Object> source) { public GeoJsonMultiPolygon convert(Map<String, Object> source) {
String type = GeoConverters.getGeoJsonType(source); String type = GeoConverters.getGeoJsonType(source);
Assert.isTrue(type.equals(GeoJsonMultiPolygon.TYPE), "does not contain a type 'MultiPolygon'"); Assert.isTrue(type.equalsIgnoreCase(GeoJsonMultiPolygon.TYPE), "does not contain a type 'MultiPolygon'");
Object coordinates = source.get("coordinates"); Object coordinates = source.get("coordinates");
Assert.notNull(coordinates, "Document to convert does not contain coordinates"); Assert.notNull(coordinates, "Document to convert does not contain coordinates");
Assert.isTrue(coordinates instanceof List, "coordinates must be a List"); Assert.isTrue(coordinates instanceof List, "coordinates must be a List");
@ -441,7 +441,8 @@ public class GeoConverters {
public GeoJsonGeometryCollection convert(Map<String, Object> source) { public GeoJsonGeometryCollection convert(Map<String, Object> source) {
String type = GeoConverters.getGeoJsonType(source); String type = GeoConverters.getGeoJsonType(source);
Assert.isTrue(type.equals(GeoJsonGeometryCollection.TYPE), "does not contain a type 'GeometryCollection'"); Assert.isTrue(type.equalsIgnoreCase(GeoJsonGeometryCollection.TYPE),
"does not contain a type 'GeometryCollection'");
Object geometries = source.get("geometries"); Object geometries = source.get("geometries");
Assert.notNull(geometries, "Document to convert does not contain geometries"); Assert.notNull(geometries, "Document to convert does not contain geometries");
Assert.isTrue(geometries instanceof List, "geometries must be a List"); Assert.isTrue(geometries instanceof List, "geometries must be a List");
@ -461,7 +462,7 @@ public class GeoConverters {
Assert.notNull(type, "Document to convert does not contain a type"); Assert.notNull(type, "Document to convert does not contain a type");
Assert.isTrue(type instanceof String, "type must be a String"); Assert.isTrue(type instanceof String, "type must be a String");
return type.toString(); return type.toString().toLowerCase();
} }
private static List<Double> toCoordinates(Point point) { private static List<Double> toCoordinates(Point point) {

View File

@ -53,6 +53,9 @@ class GeoConvertersUnitTests {
@DisplayName("GeoJsonPoint") @DisplayName("GeoJsonPoint")
class GeoJsonPointUnitTests { class GeoJsonPointUnitTests {
// NOTE: the test converting from a map contains the type names in lowercase, that might be returned from
// Elasticsearch
@Test // DATAES-930 @Test // DATAES-930
@DisplayName("should be converted to a Map") @DisplayName("should be converted to a Map")
void shouldBeConvertedToAMap() throws JSONException { void shouldBeConvertedToAMap() throws JSONException {
@ -75,7 +78,7 @@ class GeoConvertersUnitTests {
// make sure we can read int values as well // make sure we can read int values as well
String json = "{\n" + // String json = "{\n" + //
" \"type\": \"Point\",\n" + // " \"type\": \"point\",\n" + //
" \"coordinates\": [12, 34.0]\n" + // " \"coordinates\": [12, 34.0]\n" + //
"}"; // "}"; //
@ -117,8 +120,14 @@ class GeoConvertersUnitTests {
void shouldBeConvertedFromAMap() { void shouldBeConvertedFromAMap() {
// make sure we can read int values as well // make sure we can read int values as well
String json = "{\n" + " \"type\": \"MultiPoint\",\n" + " \"coordinates\": [\n" + " [12.0, 34],\n" String json = "{\n" + //
+ " [56, 78.0]\n" + " ]\n" + "}\n"; " \"type\": \"multipoint\",\n" //
+ " \"coordinates\": [\n" + //
" [12.0, 34],\n" + //
" [56, 78.0]\n" + //
" ]\n" + //
"}\n"; //
Document document = Document.parse(json); Document document = Document.parse(json);
GeoJsonMultiPoint expected = GeoJsonMultiPoint.of(new Point(12, 34), new Point(56, 78)); GeoJsonMultiPoint expected = GeoJsonMultiPoint.of(new Point(12, 34), new Point(56, 78));
@ -158,7 +167,7 @@ class GeoConvertersUnitTests {
// make sure we can read int values as well // make sure we can read int values as well
String json = "{\n" + // String json = "{\n" + //
" \"type\": \"LineString\",\n" + // " \"type\": \"linestring\",\n" + //
" \"coordinates\": [\n" + // " \"coordinates\": [\n" + //
" [12.0, 34],\n" + // " [12.0, 34],\n" + //
" [56, 78.0]\n" // " [56, 78.0]\n" //
@ -205,7 +214,7 @@ class GeoConvertersUnitTests {
void shouldBeConvertedFromAMap() { void shouldBeConvertedFromAMap() {
// make sure we can read int values as well // make sure we can read int values as well
String json = "{\n" + // String json = "{\n" + //
" \"type\": \"MultiLineString\",\n" + // " \"type\": \"multilinestring\",\n" + //
" \"coordinates\": [\n" + // " \"coordinates\": [\n" + //
" [[12, 34.0], [56.0, 78]],\n" + // " [[12, 34.0], [56.0, 78]],\n" + //
" [[90.0, 12], [34, 56.0]]\n" + // " [[90.0, 12], [34, 56.0]]\n" + //
@ -256,7 +265,7 @@ class GeoConvertersUnitTests {
void shouldBeConvertedFromAMap() { void shouldBeConvertedFromAMap() {
String json = "{\n" + // String json = "{\n" + //
" \"type\": \"Polygon\",\n" + // " \"type\": \"polygon\",\n" + //
" \"coordinates\": [\n" + // " \"coordinates\": [\n" + //
" [[12, 34.0], [56.0, 78], [90, 12.0], [12, 34.0]],\n" + // " [[12, 34.0], [56.0, 78], [90, 12.0], [12, 34.0]],\n" + //
" [[56.0, 78], [90, 12.0], [34.0, 56], [56.0, 78]]\n" + // " [[56.0, 78], [90, 12.0], [34.0, 56], [56.0, 78]]\n" + //
@ -308,7 +317,7 @@ class GeoConvertersUnitTests {
void shouldBeConvertedFromAMap() { void shouldBeConvertedFromAMap() {
String json = "{\n" + // String json = "{\n" + //
" \"type\": \"MultiPolygon\",\n" + // " \"type\": \"multipolygon\",\n" + //
" \"coordinates\": [\n" + // " \"coordinates\": [\n" + //
" [[[12, 34.0], [56.0, 78], [90, 12.0], [12, 34.0]]],\n" + // " [[[12, 34.0], [56.0, 78], [90, 12.0], [12, 34.0]]],\n" + //
" [[[56, 78.0], [90, 12.0], [34.0, 56], [56, 78.0]]]\n" + // " [[[56, 78.0], [90, 12.0], [34.0, 56], [56, 78.0]]]\n" + //
@ -369,14 +378,14 @@ class GeoConvertersUnitTests {
void shouldBeConvertedFromAMap() { void shouldBeConvertedFromAMap() {
String json = "{\n" + // String json = "{\n" + //
" \"type\": \"GeometryCollection\",\n" + // " \"type\": \"geometrycollection\",\n" + //
" \"geometries\": [\n" + // " \"geometries\": [\n" + //
" {\n" + // " {\n" + //
" \"type\": \"Point\",\n" + // " \"type\": \"point\",\n" + //
" \"coordinates\": [12.0, 34.0]\n" + // " \"coordinates\": [12.0, 34.0]\n" + //
" },\n" + // " },\n" + //
" {\n" + // " {\n" + //
" \"type\": \"Polygon\",\n" + // " \"type\": \"polygon\",\n" + //
" \"coordinates\": [\n" + // " \"coordinates\": [\n" + //
" [[12.0, 34.0], [56.0, 78.0], [90.0, 12.0], [12.0, 34.0]]\n" + // " [[12.0, 34.0], [56.0, 78.0], [90.0, 12.0], [12.0, 34.0]]\n" + //
" ]\n" + // " ]\n" + //