Skip to content

Commit ba8ece3

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1453 - Fix GeoJson conversion when coordinates are Integers.
We now use Number instead of Double for reading "coordinates" from GeoJSON representations. Original pull request: #369.
1 parent a539407 commit ba8ece3

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -599,7 +599,7 @@ public GeoJsonPoint convert(DBObject source) {
599599
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "Point"),
600600
String.format("Cannot convert type '%s' to Point.", source.get("type")));
601601

602-
List<Double> dbl = (List<Double>) source.get("coordinates");
602+
List<Number> dbl = (List<Number>) source.get("coordinates");
603603
return new GeoJsonPoint(dbl.get(0).doubleValue(), dbl.get(1).doubleValue());
604604
}
605605
}
@@ -832,7 +832,7 @@ static List<Point> toListOfPoint(BasicDBList listOfCoordinatePairs) {
832832

833833
Assert.isInstanceOf(List.class, point);
834834

835-
List<Double> coordinatesList = (List<Double>) point;
835+
List<Number> coordinatesList = (List<Number>) point;
836836

837837
points.add(new GeoJsonPoint(coordinatesList.get(0).doubleValue(), coordinatesList.get(1).doubleValue()));
838838
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,25 +29,31 @@
2929
import org.junit.runner.RunWith;
3030
import org.springframework.beans.factory.annotation.Autowired;
3131
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.dao.DataAccessException;
3233
import org.springframework.data.annotation.Id;
3334
import org.springframework.data.annotation.PersistenceConstructor;
3435
import org.springframework.data.geo.GeoResults;
3536
import org.springframework.data.geo.Metric;
3637
import org.springframework.data.geo.Metrics;
3738
import org.springframework.data.geo.Point;
3839
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
40+
import org.springframework.data.mongodb.core.CollectionCallback;
3941
import org.springframework.data.mongodb.core.MongoTemplate;
4042
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
4143
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
4244
import org.springframework.data.mongodb.core.index.GeospatialIndex;
4345
import org.springframework.data.mongodb.core.mapping.Document;
4446
import org.springframework.data.mongodb.core.query.NearQuery;
4547
import org.springframework.data.mongodb.core.query.Query;
48+
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
4649
import org.springframework.test.context.ContextConfiguration;
4750
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4851

52+
import com.mongodb.BasicDBObject;
53+
import com.mongodb.DBCollection;
4954
import com.mongodb.Mongo;
5055
import com.mongodb.MongoClient;
56+
import com.mongodb.MongoException;
5157
import com.mongodb.WriteConcern;
5258

5359
/**
@@ -317,6 +323,66 @@ public void nearWithMinAndMaxDistance() {
317323
assertThat(venues.size(), is(2));
318324
}
319325

326+
/**
327+
* @see DATAMONGO-1453
328+
*/
329+
@Test
330+
public void shouldConvertPointRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
331+
332+
this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class),
333+
new CollectionCallback<Object>() {
334+
335+
@Override
336+
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
337+
338+
BasicDBObject pointRepresentation = new BasicDBObject();
339+
pointRepresentation.put("type", "Point");
340+
pointRepresentation.put("coordinates", new BasicDbListBuilder().add(0).add(0).get());
341+
342+
BasicDBObject document = new BasicDBObject();
343+
document.append("_id", "datamongo-1453");
344+
document.append("geoJsonPoint", pointRepresentation);
345+
346+
return collection.save(document);
347+
}
348+
});
349+
350+
assertThat(template.findOne(query(where("id").is("datamongo-1453")),
351+
DocumentWithPropertyUsingGeoJsonType.class).geoJsonPoint, is(equalTo(new GeoJsonPoint(0D, 0D))));
352+
}
353+
354+
/**
355+
* @see DATAMONGO-1453
356+
*/
357+
@Test
358+
public void shouldConvertLineStringRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
359+
360+
this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class),
361+
new CollectionCallback<Object>() {
362+
363+
@Override
364+
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
365+
366+
BasicDBObject lineStringRepresentation = new BasicDBObject();
367+
lineStringRepresentation.put("type", "LineString");
368+
lineStringRepresentation.put("coordinates",
369+
new BasicDbListBuilder().add(new BasicDbListBuilder().add(0).add(0).get())
370+
.add(new BasicDbListBuilder().add(1).add(1).get()).get());
371+
372+
BasicDBObject document = new BasicDBObject();
373+
document.append("_id", "datamongo-1453");
374+
document.append("geoJsonLineString", lineStringRepresentation);
375+
376+
return collection.save(document);
377+
}
378+
});
379+
380+
assertThat(
381+
template.findOne(query(where("id").is("datamongo-1453")),
382+
DocumentWithPropertyUsingGeoJsonType.class).geoJsonLineString,
383+
is(equalTo(new GeoJsonLineString(new Point(0D, 0D), new Point(1, 1)))));
384+
}
385+
320386
private void addVenues() {
321387

322388
template.insert(new Venue2DSphere("Penn Station", -73.99408, 40.75057));

0 commit comments

Comments
 (0)