Skip to content

DATAMONGO-1399 - Allow adding hole to GeoJSON Polygon. #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1399-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1399-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1399-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1399-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1399-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1399-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -62,6 +64,53 @@ public GeoJsonPolygon(List<Point> 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<Point> 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<GeoJsonLineString> 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.<GeoJson<?>> asList(SINGLE_POINT, POLYGON));
Expand Down Expand Up @@ -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()) //
Expand Down Expand Up @@ -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));
}

}

/**
Expand Down Expand Up @@ -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));
}
}
}