From f51d18748f9de227114831586d5fcbf5215f747d Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 24 Mar 2016 08:03:48 +0100 Subject: [PATCH 1/2] DATAMONGO-1399 - Allow adding hole to GeoJSON Polygon. Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb-log4j/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 1d9b760700..3856c2b4b0 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1399-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index fd36debedd..ccd7987566 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1399-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1399-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 28c91bc332..9e2520b8da 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1399-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index dfe146ff96..8581c7d1a9 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1399-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 0fcdb2f39f..68419ac6a5 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATAMONGO-1399-SNAPSHOT ../pom.xml From 165455731f2b109f3b8441d276dac8647da5f3f1 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 24 Mar 2016 08:57:32 +0100 Subject: [PATCH 2/2] DATAMONGO-1399 - Allow adding hole to GeoJSON Polygon. We now allow creation of GeoJsonPolygon having an outer and multiple inner rings. --- .../data/mongodb/core/geo/GeoJsonPolygon.java | 51 ++++++++++++++++++- .../convert/GeoJsonConverterUnitTests.java | 41 ++++++++++++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonPolygon.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonPolygon.java index 0963e18084..61ab5d910e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonPolygon.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonPolygon.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; import java.util.List; import org.springframework.data.geo.Point; import org.springframework.data.geo.Polygon; +import org.springframework.util.Assert; /** * {@link GeoJson} representation of {@link Polygon}. Unlike {@link Polygon} the {@link GeoJsonPolygon} requires a @@ -62,6 +64,53 @@ public GeoJsonPolygon(List points) { this.coordinates.add(new GeoJsonLineString(points)); } + /** + * Creates a new {@link GeoJsonPolygon} with an inner ring defined be the given {@link Point}s. + * + * @param first must not be {@literal null}. + * @param second must not be {@literal null}. + * @param third must not be {@literal null}. + * @param fourth must not be {@literal null}. + * @param others can be {@literal null}. + * @return new {@link GeoJsonPolygon}. + * @since 1.9 + */ + public GeoJsonPolygon withInnerRing(Point first, Point second, Point third, Point fourth, final Point... others) { + return withInnerRing(asList(first, second, third, fourth, others)); + } + + /** + * Creates a new {@link GeoJsonPolygon} with an inner ring defined be the given {@link List} of {@link Point}s. + * + * @param points must not be {@literal null}. + * @return new {@link GeoJsonPolygon}. + */ + public GeoJsonPolygon withInnerRing(List points) { + return withInnerRing(new GeoJsonLineString(points)); + } + + /** + * Creates a new {@link GeoJsonPolygon} with an inner ring defined be the given {@link GeoJsonLineString}. + * + * @param lineString must not be {@literal null}. + * @return new {@link GeoJsonPolygon}. + * @since 1.9 + */ + public GeoJsonPolygon withInnerRing(GeoJsonLineString lineString) { + + Assert.notNull(lineString, "LineString must not be null!"); + + Iterator it = this.coordinates.iterator(); + GeoJsonPolygon polygon = new GeoJsonPolygon(it.next().getCoordinates()); + + while (it.hasNext()) { + polygon.coordinates.add(it.next()); + } + + polygon.coordinates.add(lineString); + return polygon; + } + /* * (non-Javadoc) * @see org.springframework.data.mongodb.core.geo.GeoJson#getType() diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoJsonConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoJsonConverterUnitTests.java index 33fb1bf2c6..935d135e83 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoJsonConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoJsonConverterUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,11 +73,18 @@ public class GeoJsonConverterUnitTests { static final Point POINT_2 = new Point(100, 100); static final Point POINT_3 = new Point(0, 100); + static final Point INNER_POINT_0 = new Point(10, 10); + static final Point INNER_POINT_1 = new Point(90, 10); + static final Point INNER_POINT_2 = new Point(90, 90); + static final Point INNER_POINT_3 = new Point(10, 90); + static final GeoJsonMultiPoint MULTI_POINT = new GeoJsonMultiPoint(POINT_0, POINT_2, POINT_3); static final GeoJsonLineString LINE_STRING = new GeoJsonLineString(POINT_0, POINT_1, POINT_2); @SuppressWarnings("unchecked") static final GeoJsonMultiLineString MULTI_LINE_STRING = new GeoJsonMultiLineString( Arrays.asList(POINT_0, POINT_1, POINT_2), Arrays.asList(POINT_3, POINT_0)); static final GeoJsonPolygon POLYGON = new GeoJsonPolygon(POINT_0, POINT_1, POINT_2, POINT_3, POINT_0); + static final GeoJsonPolygon POLYGON_WITH_2_RINGS = POLYGON.withInnerRing(INNER_POINT_0, INNER_POINT_1, INNER_POINT_2, + INNER_POINT_3, INNER_POINT_0); static final GeoJsonMultiPolygon MULTI_POLYGON = new GeoJsonMultiPolygon(Arrays.asList(POLYGON)); static final GeoJsonGeometryCollection GEOMETRY_COLLECTION = new GeoJsonGeometryCollection( Arrays.> asList(SINGLE_POINT, POLYGON)); @@ -114,12 +121,28 @@ public class GeoJsonConverterUnitTests { .add(new BasicDbListBuilder().add(POINT_3.getX()).add(POINT_3.getY()).get()) // .add(new BasicDbListBuilder().add(POINT_0.getX()).add(POINT_0.getY()).get()) // .get(); + + static final BasicDBList POLYGON_INNER_CORDS = new BasicDbListBuilder() // + .add(new BasicDbListBuilder().add(INNER_POINT_0.getX()).add(INNER_POINT_0.getY()).get()) // + .add(new BasicDbListBuilder().add(INNER_POINT_1.getX()).add(INNER_POINT_1.getY()).get()) // + .add(new BasicDbListBuilder().add(INNER_POINT_2.getX()).add(INNER_POINT_2.getY()).get()) // + .add(new BasicDbListBuilder().add(INNER_POINT_3.getX()).add(INNER_POINT_3.getY()).get()) // + .add(new BasicDbListBuilder().add(INNER_POINT_0.getX()).add(INNER_POINT_0.getY()).get()) // + .get(); + static final BasicDBList POLYGON_CORDS = new BasicDbListBuilder().add(POLYGON_OUTER_CORDS).get(); static final DBObject POLYGON_DBO = new BasicDBObjectBuilder() // .add("type", "Polygon") // .add("coordinates", POLYGON_CORDS) // .get(); + static final BasicDBList POLYGON_WITH_2_RINGS_CORDS = new BasicDbListBuilder().add(POLYGON_OUTER_CORDS) + .add(POLYGON_INNER_CORDS).get(); + static final DBObject POLYGON_WITH_2_RINGS_DBO = new BasicDBObjectBuilder() // + .add("type", "Polygon") // + .add("coordinates", POLYGON_WITH_2_RINGS_CORDS) // + .get(); + // LineString static final BasicDBList LINE_STRING_CORDS_0 = new BasicDbListBuilder() // .add(new BasicDbListBuilder().add(POINT_0.getX()).add(POINT_0.getY()).get()) // @@ -190,6 +213,14 @@ public void shouldThrowExceptionWhenTypeDoesNotMatchPolygon() { converter.convert(new BasicDBObject("type", "YouDontKonwMe")); } + /** + * @see DATAMONGO-1399 + */ + @Test + public void shouldConvertDboWithMultipleRingsCorrectly() { + assertThat(converter.convert(POLYGON_WITH_2_RINGS_DBO), equalTo(POLYGON_WITH_2_RINGS)); + } + } /** @@ -446,5 +477,13 @@ public void shouldConvertGeoJsonMultiPolygonCorrectly() { public void shouldConvertGeometryCollectionCorrectly() { assertThat(converter.convert(GEOMETRY_COLLECTION), equalTo(GEOMETRY_COLLECTION_DBO)); } + + /** + * @see DATAMONGO-1399 + */ + @Test + public void shouldConvertGeoJsonPolygonWithMultipleRingsCorrectly() { + assertThat(converter.convert(POLYGON_WITH_2_RINGS), equalTo(POLYGON_WITH_2_RINGS_DBO)); + } } }