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.
This commit is contained in:
committed by
Mark Paluch
parent
a5394074c5
commit
ba8ece334a
@@ -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.
|
||||
@@ -599,7 +599,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());
|
||||
}
|
||||
}
|
||||
@@ -832,7 +832,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()));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
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;
|
||||
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;
|
||||
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 {
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user