Skip to content

Commit 1c43a3d

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1135 - Add support for GeoJson.
We’ve added special types representing GeoJson structures. This allows to use those within both queries and domain types. GeoJson types should only be used in combination with a 2dsphere index as 2d index is not able to handle the structure. Though legacy coordinate pairs and GeoJson types can be mixed inside MongoDB, we currently do not support conversion of legacy coordinates to GeoJson types.
1 parent 60ca1b3 commit 1c43a3d

16 files changed

+2210
-13
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java

Lines changed: 347 additions & 13 deletions
Large diffs are not rendered by default.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.geo;
17+
18+
/**
19+
* Interface definition for structures defined in GeoJSON ({@link http://geojson.org/}) format.
20+
*
21+
* @author Christoph Strobl
22+
* @since 1.7
23+
*/
24+
public interface GeoJson<T extends Iterable<?>> {
25+
26+
/**
27+
* String value representing the type of the {@link GeoJson} object.
28+
*
29+
* @return never {@literal null}.
30+
* @see http://geojson.org/geojson-spec.html#geojson-objects
31+
*/
32+
String getType();
33+
34+
/**
35+
* The value of the coordinates member is always an {@link Iterable}. The structure for the elements within is
36+
* determined by {@link #getType()} of geometry.
37+
*
38+
* @return never {@literal null}.
39+
* @see http://geojson.org/geojson-spec.html#geometry-objects
40+
*/
41+
T getCoordinates();
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.geo;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import org.springframework.util.Assert;
23+
import org.springframework.util.ObjectUtils;
24+
25+
/**
26+
* Defines a {@link GeoJsonGeometryCollection} that consists of a {@link List} of {@link GeoJson} objects.
27+
*
28+
* @author Christoph Strobl
29+
* @since 1.7
30+
* @see http://geojson.org/geojson-spec.html#geometry-collection
31+
*/
32+
public class GeoJsonGeometryCollection implements GeoJson<Iterable<GeoJson<?>>> {
33+
34+
private static final String TYPE = "GeometryCollection";
35+
private final List<GeoJson<?>> geometries = new ArrayList<GeoJson<?>>();
36+
37+
/**
38+
* @param geometries
39+
*/
40+
public GeoJsonGeometryCollection(List<GeoJson<?>> geometries) {
41+
42+
Assert.notNull(geometries);
43+
this.geometries.addAll(geometries);
44+
}
45+
46+
/*
47+
* (non-Javadoc)
48+
* @see org.springframework.data.mongodb.core.geo.GeoJson#getType()
49+
*/
50+
@Override
51+
public String getType() {
52+
return TYPE;
53+
}
54+
55+
/*
56+
* (non-Javadoc)
57+
* @see org.springframework.data.mongodb.core.geo.GeoJson#getCoordinates()
58+
*/
59+
@Override
60+
public Iterable<GeoJson<?>> getCoordinates() {
61+
return Collections.unmodifiableList(this.geometries);
62+
}
63+
64+
/*
65+
* (non-Javadoc)
66+
* @see java.lang.Object#hashCode()
67+
*/
68+
@Override
69+
public int hashCode() {
70+
return ObjectUtils.nullSafeHashCode(this.geometries);
71+
}
72+
73+
/*
74+
* (non-Javadoc)
75+
* @see java.lang.Object#equals(java.lang.Object)
76+
*/
77+
@Override
78+
public boolean equals(Object obj) {
79+
if (this == obj) {
80+
return true;
81+
}
82+
if (obj == null) {
83+
return false;
84+
}
85+
if (!(obj instanceof GeoJsonGeometryCollection)) {
86+
return false;
87+
}
88+
GeoJsonGeometryCollection other = (GeoJsonGeometryCollection) obj;
89+
return ObjectUtils.nullSafeEquals(this.geometries, other.geometries);
90+
}
91+
92+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.geo;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.geo.Point;
21+
22+
/**
23+
* {@link GeoJsonLineString} is defined as list of at least 2 {@link Point}s.
24+
*
25+
* @author Christoph Strobl
26+
* @since 1.7
27+
* @see http://geojson.org/geojson-spec.html#linestring
28+
*/
29+
public class GeoJsonLineString extends GeoJsonMultiPoint {
30+
31+
private static final String TYPE = "LineString";
32+
33+
/**
34+
* @param points must not be {@literal null} and have at least 2 entries.
35+
*/
36+
public GeoJsonLineString(List<Point> points) {
37+
super(points);
38+
}
39+
40+
/**
41+
* @param p0 must not be {@literal null}
42+
* @param p1 must not be {@literal null}
43+
* @param others can be {@literal null}
44+
*/
45+
public GeoJsonLineString(Point p0, Point p1, Point... others) {
46+
super(p0, p1, others);
47+
}
48+
49+
/*
50+
* (non-Javadoc)
51+
* @see org.springframework.data.mongodb.core.geo.GeoJsonMultiPoint#getType()
52+
*/
53+
@Override
54+
public String getType() {
55+
return TYPE;
56+
}
57+
58+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.geo;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import org.springframework.data.geo.Point;
23+
import org.springframework.util.Assert;
24+
import org.springframework.util.ObjectUtils;
25+
26+
/**
27+
* {@link GeoJsonMultiLineString} is defined as list of {@link GeoJsonLineString}s.
28+
*
29+
* @author Christoph Strobl
30+
* @since 1.7
31+
* @see http://geojson.org/geojson-spec.html#multilinestring
32+
*/
33+
public class GeoJsonMultiLineString implements GeoJson<Iterable<GeoJsonLineString>> {
34+
35+
private static final String TYPE = "MultiLineString";
36+
private List<GeoJsonLineString> coordinates = new ArrayList<GeoJsonLineString>();
37+
38+
/**
39+
* Creates new {@link GeoJsonMultiLineString}.
40+
*
41+
* @param lines must not be {@literal null}.
42+
*/
43+
@SuppressWarnings("unchecked")
44+
public GeoJsonMultiLineString(List<Point>... lines) {
45+
46+
Assert.notEmpty(lines, "Lines for MultiLineString must not be null!");
47+
for (List<Point> line : lines) {
48+
this.coordinates.add(new GeoJsonLineString(line));
49+
}
50+
}
51+
52+
/**
53+
* @param lines must not be {@literal null}.
54+
*/
55+
public GeoJsonMultiLineString(List<GeoJsonLineString> lines) {
56+
57+
Assert.notNull(lines, "Lines for MultiLineString must not be null!");
58+
this.coordinates.addAll(lines);
59+
}
60+
61+
/*
62+
* (non-Javadoc)
63+
* @see org.springframework.data.mongodb.core.geo.GeoJson#getType()
64+
*/
65+
@Override
66+
public String getType() {
67+
return TYPE;
68+
}
69+
70+
/*
71+
* (non-Javadoc)
72+
* @see org.springframework.data.mongodb.core.geo.GeoJson#getCoordinates()
73+
*/
74+
@Override
75+
public Iterable<GeoJsonLineString> getCoordinates() {
76+
return Collections.unmodifiableList(this.coordinates);
77+
}
78+
79+
/*
80+
* (non-Javadoc)
81+
* @see java.lang.Object#hashCode()
82+
*/
83+
@Override
84+
public int hashCode() {
85+
return ObjectUtils.nullSafeHashCode(this.coordinates);
86+
}
87+
88+
/*
89+
* (non-Javadoc)
90+
* @see java.lang.Object#equals(java.lang.Object)
91+
*/
92+
@Override
93+
public boolean equals(Object obj) {
94+
if (this == obj) {
95+
return true;
96+
}
97+
if (obj == null) {
98+
return false;
99+
}
100+
if (!(obj instanceof GeoJsonMultiLineString)) {
101+
return false;
102+
}
103+
return ObjectUtils.nullSafeEquals(this.coordinates, ((GeoJsonMultiLineString) obj).coordinates);
104+
}
105+
106+
}

0 commit comments

Comments
 (0)