Browse Source

DATAMONGO-1399 - Allow adding hole to GeoJSON Polygon.

We now allow creation of GeoJsonPolygon having an outer and multiple inner rings.

Original pull request: #352.
pull/359/merge
Christoph Strobl 10 years ago committed by Mark Paluch
parent
commit
7d70a8677e
  1. 51
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonPolygon.java
  2. 41
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoJsonConverterUnitTests.java

51
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,10 +18,12 @@ package org.springframework.data.mongodb.core.geo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import org.springframework.data.geo.Point; import org.springframework.data.geo.Point;
import org.springframework.data.geo.Polygon; 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 * {@link GeoJson} representation of {@link Polygon}. Unlike {@link Polygon} the {@link GeoJsonPolygon} requires a
@ -62,6 +64,53 @@ public class GeoJsonPolygon extends Polygon implements GeoJson<List<GeoJsonLineS
this.coordinates.add(new GeoJsonLineString(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) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.geo.GeoJson#getType() * @see org.springframework.data.mongodb.core.geo.GeoJson#getType()

41
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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_2 = new Point(100, 100);
static final Point POINT_3 = new Point(0, 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 GeoJsonMultiPoint MULTI_POINT = new GeoJsonMultiPoint(POINT_0, POINT_2, POINT_3);
static final GeoJsonLineString LINE_STRING = new GeoJsonLineString(POINT_0, POINT_1, POINT_2); static final GeoJsonLineString LINE_STRING = new GeoJsonLineString(POINT_0, POINT_1, POINT_2);
@SuppressWarnings("unchecked") static final GeoJsonMultiLineString MULTI_LINE_STRING = new GeoJsonMultiLineString( @SuppressWarnings("unchecked") static final GeoJsonMultiLineString MULTI_LINE_STRING = new GeoJsonMultiLineString(
Arrays.asList(POINT_0, POINT_1, POINT_2), Arrays.asList(POINT_3, POINT_0)); 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 = 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 GeoJsonMultiPolygon MULTI_POLYGON = new GeoJsonMultiPolygon(Arrays.asList(POLYGON));
static final GeoJsonGeometryCollection GEOMETRY_COLLECTION = new GeoJsonGeometryCollection( static final GeoJsonGeometryCollection GEOMETRY_COLLECTION = new GeoJsonGeometryCollection(
Arrays.<GeoJson<?>> asList(SINGLE_POINT, POLYGON)); Arrays.<GeoJson<?>> 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_3.getX()).add(POINT_3.getY()).get()) //
.add(new BasicDbListBuilder().add(POINT_0.getX()).add(POINT_0.getY()).get()) // .add(new BasicDbListBuilder().add(POINT_0.getX()).add(POINT_0.getY()).get()) //
.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 BasicDBList POLYGON_CORDS = new BasicDbListBuilder().add(POLYGON_OUTER_CORDS).get();
static final DBObject POLYGON_DBO = new BasicDBObjectBuilder() // static final DBObject POLYGON_DBO = new BasicDBObjectBuilder() //
.add("type", "Polygon") // .add("type", "Polygon") //
.add("coordinates", POLYGON_CORDS) // .add("coordinates", POLYGON_CORDS) //
.get(); .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 // LineString
static final BasicDBList LINE_STRING_CORDS_0 = new BasicDbListBuilder() // static final BasicDBList LINE_STRING_CORDS_0 = new BasicDbListBuilder() //
.add(new BasicDbListBuilder().add(POINT_0.getX()).add(POINT_0.getY()).get()) // .add(new BasicDbListBuilder().add(POINT_0.getX()).add(POINT_0.getY()).get()) //
@ -190,6 +213,14 @@ public class GeoJsonConverterUnitTests {
converter.convert(new BasicDBObject("type", "YouDontKonwMe")); 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 class GeoJsonConverterUnitTests {
public void shouldConvertGeometryCollectionCorrectly() { public void shouldConvertGeometryCollectionCorrectly() {
assertThat(converter.convert(GEOMETRY_COLLECTION), equalTo(GEOMETRY_COLLECTION_DBO)); 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));
}
} }
} }

Loading…
Cancel
Save