-
Notifications
You must be signed in to change notification settings - Fork 7
/
GeoJsonConverters.java
93 lines (75 loc) · 3.02 KB
/
GeoJsonConverters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package be.insaneprogramming.geojson;
import be.insaneprogramming.geojson.*;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.core.convert.converter.Converter;
import java.util.ArrayList;
import java.util.List;
public class GeoJsonConverters {
private GeoJsonConverters() {}
public static List<Converter<?, ?>> getConvertersToRegister() {
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(GeoJsonObjectToDBObjectConverter.INSTANCE);
converters.add(DBObjectToPointConverter.INSTANCE);
converters.add(DBObjectToPolygonConverter.INSTANCE);
converters.add(DBObjectToLineStringConverter.INSTANCE);
converters.add(DBObjectToMultiPointConverter.INSTANCE);
converters.add(DBObjectToMultiLineStringConverter.INSTANCE);
converters.add(DBObjectToMultiPolygonStringConverter.INSTANCE);
return converters;
}
public static enum GeoJsonObjectToDBObjectConverter implements Converter<GeoJsonObject, DBObject> {
INSTANCE;
@Override
public DBObject convert(GeoJsonObject source) {
DBObject dbObject = new BasicDBObject();
dbObject.put("type", source.getType());
dbObject.put("coordinates", source);
if(source.getBoundingBox() != null && source.getBoundingBox().length > 0)
dbObject.put("bbox", source.getBoundingBox());
return dbObject;
}
}
public static enum DBObjectToPointConverter implements Converter<DBObject, Point> {
INSTANCE;
@Override
public Point convert(DBObject source) {
return new Point(source.get("coordinates"));
}
}
public static enum DBObjectToLineStringConverter implements Converter<DBObject, LineString> {
INSTANCE;
@Override
public LineString convert(DBObject source) {
return new LineString(source.get("coordinates"));
}
}
public static enum DBObjectToPolygonConverter implements Converter<DBObject, Polygon> {
INSTANCE;
@Override
public Polygon convert(DBObject source) {
return new Polygon(source.get("coordinates"));
}
}
public static enum DBObjectToMultiPointConverter implements Converter<DBObject, MultiPoint> {
INSTANCE;
@Override
public MultiPoint convert(DBObject source) {
return new MultiPoint(source.get("coordinates"));
}
}
public static enum DBObjectToMultiLineStringConverter implements Converter<DBObject, MultiLineString> {
INSTANCE;
@Override
public MultiLineString convert(DBObject source) {
return new MultiLineString(source.get("coordinates"));
}
}
public static enum DBObjectToMultiPolygonStringConverter implements Converter<DBObject, MultiPolygon> {
INSTANCE;
@Override
public MultiPolygon convert(DBObject source) {
return new MultiPolygon(source.get("coordinates"));
}
}
}