Skip to content

Instantly share code, notes, and snippets.

@lievendoclo
Created December 5, 2014 13:04
Show Gist options
  • Save lievendoclo/1b2283f10b360e967348 to your computer and use it in GitHub Desktop.
Save lievendoclo/1b2283f10b360e967348 to your computer and use it in GitHub Desktop.
GeoJsonConverters for Spring Data MongoDB
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.geo.Point;
import org.springframework.data.geo.Polygon;
import java.util.ArrayList;
import java.util.List;
/**
* Wrapper class to contain useful geo structure converters adhering to the GeoJSON format for the usage with Mongo.
*
* @author Lieven Doclo
*/
public class GeoJsonConverters
{
private GeoJsonConverters() {}
/**
* Returns the geo converters to be registered.
*
* @return
*/
static List<Converter<?, ?>> getConvertersToRegister() {
List<Converter<?, ?>> list = new ArrayList<Converter<?, ?>>();
list.add(DBObjectToGeoPointConverter.INSTANCE);
list.add(DBObjectToGeoPolygonConverter.INSTANCE);
list.add(GeoPointToDBObjectConverter.INSTANCE);
list.add(GeoPolygonToDBObjectConverter.INSTANCE);
return list;
}
/**
* Converts a {@link org.springframework.data.geo.Point} into a {@link com.mongodb.DBObject} adhering to the GeoJSON format.
*
* @author Lieven Doclo
*/
@WritingConverter
public static enum GeoPointToDBObjectConverter implements Converter<Point, DBObject> {
INSTANCE;
@Override
public DBObject convert(Point source) {
DBObject object = new BasicDBObject();
object.put("type", "Point");
object.put("coordinates", new double[] { source.getX(), source.getY() } );
return object;
}
}
/**
* Converts a {@link com.mongodb.DBObject} adhering to the GeoJSON format into a {@link org.springframework.data.geo.Point}.
*
* @author Lieven Doclo
*/
@ReadingConverter
public static enum DBObjectToGeoPointConverter implements Converter<DBObject, Point> {
INSTANCE;
@Override
public Point convert(DBObject source)
{
double[] coordinates = (double[]) source.get("coordinates");
return new Point(coordinates[0], coordinates[1]);
}
}
/**
* Converts a {@link org.springframework.data.geo.Polygon} into a {@link com.mongodb.DBObject} adhering to the GeoJSON format.
*
* @author Lieven Doclo
*/
@WritingConverter
public static enum GeoPolygonToDBObjectConverter implements Converter<Polygon, DBObject> {
INSTANCE;
@Override
public DBObject convert(Polygon source) {
BasicDBList list = new BasicDBList();
for (Point point : source.getPoints())
{
list.add(new double[] { point.getX(), point.getY() });
}
DBObject object = new BasicDBObject();
object.put("type", "Polygon");
object.put("coordinates", list );
return object;
}
}
/**
* Converts a {@link com.mongodb.DBObject} adhering to the GeoJSON format into a {@link org.springframework.data.geo.Polygon}.
*
* @author Lieven Doclo
*/
@ReadingConverter
public static enum DBObjectToGeoPolygonConverter implements Converter<DBObject, Polygon> {
INSTANCE;
@Override
public Polygon convert(DBObject source) {
double[][] coordinates = (double[][]) source.get("coordinates");
final List<Point> points = new ArrayList<Point>(coordinates.length);
for (double[] coordinate : coordinates) {
points.add(new Point(coordinate[0], coordinate[1]));
}
return new Polygon(points);
}
}
}
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.geo.Point;
import org.springframework.data.geo.Polygon;
import static org.hamcrest.CoreMatchers.equalTo;
public class GeoJsonConvertersTest {
@Test
public void testPointToDBObject() {
Converter<Point, DBObject> converter = GeoJsonConverters.GeoPointToDBObjectConverter.INSTANCE;
DBObject expected = new BasicDBObject();
expected.put("type", "Point");
expected.put("coordinates", new double[]{100, 100});
Point point = new Point(100, 100);
DBObject result = converter.convert(point);
Assert.assertThat(result, equalTo(expected));
}
@Test
public void testPolygonToDBObject() {
Converter<Polygon, DBObject> converter = GeoJsonConverters.GeoPolygonToDBObjectConverter.INSTANCE;
DBObject expected = new BasicDBObject();
expected.put("type", "Polygon");
expected.put("coordinates", new double[][] {{100,100}, {120,120}, {50,50}} );
Polygon polygon = new Polygon(new Point(100, 100),new Point(120, 120),new Point(50, 50));
DBObject result = converter.convert(polygon);
Assert.assertThat(result, equalTo(expected));
}
@Test
public void testDBObjectToPoint() {
Converter<DBObject, Point> converter = GeoJsonConverters.DBObjectToGeoPointConverter.INSTANCE;
DBObject dbObject = new BasicDBObject();
dbObject.put("type", "Point");
dbObject.put("coordinates", new double[]{100, 100});
Point expected = new Point(100, 100);
Point result = converter.convert(dbObject);
Assert.assertThat(result, equalTo(expected));
}
@Test
public void testDBObjectToPolygon() {
Converter<DBObject, Polygon> converter = GeoJsonConverters.DBObjectToGeoPolygonConverter.INSTANCE;
DBObject dbObject = new BasicDBObject();
dbObject.put("type", "Polygon");
dbObject.put("coordinates", new double[][] {{100,100}, {120,120}, {50,50}} );
Polygon expected = new Polygon(new Point(100, 100),new Point(120, 120),new Point(50, 50));
Polygon result = converter.convert(dbObject);
Assert.assertThat(result, equalTo(expected));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment