Browse Source

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.
pull/663/head
Christoph Strobl 10 years ago committed by Mark Paluch
parent
commit
d4dba035dc
  1. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java
  2. 68
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -595,7 +595,7 @@ abstract class GeoConverters { @@ -595,7 +595,7 @@ abstract class GeoConverters {
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "Point"),
String.format("Cannot convert type '%s' to Point.", source.get("type")));
List<Double> dbl = (List<Double>) source.get("coordinates");
List<Number> dbl = (List<Number>) source.get("coordinates");
return new GeoJsonPoint(dbl.get(0).doubleValue(), dbl.get(1).doubleValue());
}
}
@ -828,7 +828,7 @@ abstract class GeoConverters { @@ -828,7 +828,7 @@ abstract class GeoConverters {
Assert.isInstanceOf(List.class, point);
List<Double> coordinatesList = (List<Double>) point;
List<Number> coordinatesList = (List<Number>) point;
points.add(new GeoJsonPoint(coordinatesList.get(0).doubleValue(), coordinatesList.get(1).doubleValue()));
}

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import org.junit.Test; @@ -29,6 +29,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.geo.GeoResults;
@ -36,6 +37,7 @@ import org.springframework.data.geo.Metric; @@ -36,6 +37,7 @@ import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.CollectionCallback;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
@ -43,11 +45,15 @@ import org.springframework.data.mongodb.core.index.GeospatialIndex; @@ -43,11 +45,15 @@ import org.springframework.data.mongodb.core.index.GeospatialIndex;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
/**
@ -317,6 +323,66 @@ public class GeoJsonTests { @@ -317,6 +323,66 @@ public class GeoJsonTests {
assertThat(venues.size(), is(2));
}
/**
* @see DATAMONGO-1453
*/
@Test
public void shouldConvertPointRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class),
new CollectionCallback<Object>() {
@Override
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
BasicDBObject pointRepresentation = new BasicDBObject();
pointRepresentation.put("type", "Point");
pointRepresentation.put("coordinates", new BasicDbListBuilder().add(0).add(0).get());
BasicDBObject document = new BasicDBObject();
document.append("_id", "datamongo-1453");
document.append("geoJsonPoint", pointRepresentation);
return collection.save(document);
}
});
assertThat(template.findOne(query(where("id").is("datamongo-1453")),
DocumentWithPropertyUsingGeoJsonType.class).geoJsonPoint, is(equalTo(new GeoJsonPoint(0D, 0D))));
}
/**
* @see DATAMONGO-1453
*/
@Test
public void shouldConvertLineStringRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class),
new CollectionCallback<Object>() {
@Override
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
BasicDBObject lineStringRepresentation = new BasicDBObject();
lineStringRepresentation.put("type", "LineString");
lineStringRepresentation.put("coordinates",
new BasicDbListBuilder().add(new BasicDbListBuilder().add(0).add(0).get())
.add(new BasicDbListBuilder().add(1).add(1).get()).get());
BasicDBObject document = new BasicDBObject();
document.append("_id", "datamongo-1453");
document.append("geoJsonLineString", lineStringRepresentation);
return collection.save(document);
}
});
assertThat(
template.findOne(query(where("id").is("datamongo-1453")),
DocumentWithPropertyUsingGeoJsonType.class).geoJsonLineString,
is(equalTo(new GeoJsonLineString(new Point(0D, 0D), new Point(1, 1)))));
}
private void addVenues() {
template.insert(new Venue2DSphere("Penn Station", -73.99408, 40.75057));

Loading…
Cancel
Save