DATAES-932 - GeoPoint - Point conversion is wrong.

Original PR: #521
This commit is contained in:
Peter-Josef Meisch
2020-09-20 21:28:25 +02:00
committed by GitHub
parent 5dc68600f4
commit 3edc5b0fb0
6 changed files with 59 additions and 42 deletions

View File

@@ -54,8 +54,8 @@ class GeoConverters {
public Map<String, Object> convert(Point source) {
Map<String, Object> target = new LinkedHashMap<>();
target.put("lat", source.getX());
target.put("lon", source.getY());
target.put("lat", source.getY());
target.put("lon", source.getX());
return target;
}
}
@@ -87,8 +87,8 @@ class GeoConverters {
@Override
public Point convert(Map<String, Object> source) {
Double x = NumberUtils.convertNumberToTargetClass((Number) source.get("lat"), Double.class);
Double y = NumberUtils.convertNumberToTargetClass((Number) source.get("lon"), Double.class);
Double x = NumberUtils.convertNumberToTargetClass((Number) source.get("lon"), Double.class);
Double y = NumberUtils.convertNumberToTargetClass((Number) source.get("lat"), Double.class);
return new Point(x, y);
}
@@ -104,10 +104,10 @@ class GeoConverters {
@Override
public GeoPoint convert(Map<String, Object> source) {
Double x = NumberUtils.convertNumberToTargetClass((Number) source.get("lat"), Double.class);
Double y = NumberUtils.convertNumberToTargetClass((Number) source.get("lon"), Double.class);
Double lat = NumberUtils.convertNumberToTargetClass((Number) source.get("lat"), Double.class);
Double lon = NumberUtils.convertNumberToTargetClass((Number) source.get("lon"), Double.class);
return new GeoPoint(x, y);
return new GeoPoint(lat, lon);
}
}
}

View File

@@ -15,10 +15,10 @@
*/
package org.springframework.data.elasticsearch.core.geo;
import org.springframework.data.geo.Point;
import java.util.Objects;
import org.springframework.data.geo.Point;
/**
* geo-location used for #{@link org.springframework.data.elasticsearch.core.query.Criteria}.
*
@@ -32,7 +32,7 @@ public class GeoPoint {
private double lon;
private GeoPoint() {
//required by mapper to instantiate object
// required by mapper to instantiate object
}
public GeoPoint(double latitude, double longitude) {
@@ -55,20 +55,21 @@ public class GeoPoint {
* @return a {@link org.springframework.data.elasticsearch.core.geo.GeoPoint}
*/
public static GeoPoint fromPoint(Point point) {
return new GeoPoint(point.getX(), point.getY());
return new GeoPoint(point.getY(), point.getX());
}
public static Point toPoint(GeoPoint point) {
return new Point(point.getLat(), point.getLon());
return new Point(point.getLon(), point.getLat());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
GeoPoint geoPoint = (GeoPoint) o;
return Double.compare(geoPoint.lat, lat) == 0 &&
Double.compare(geoPoint.lon, lon) == 0;
return Double.compare(geoPoint.lat, lat) == 0 && Double.compare(geoPoint.lon, lon) == 0;
}
@Override
@@ -78,11 +79,6 @@ public class GeoPoint {
@Override
public String toString() {
return "GeoPoint{" +
"lat=" + lat +
", lon=" + lon +
'}';
return "GeoPoint{" + "lat=" + lat + ", lon=" + lon + '}';
}
}

View File

@@ -143,13 +143,13 @@ public class MappingElasticsearchConverterUnitTests {
observatoryRoad = new Address();
observatoryRoad.city = "Los Angeles";
observatoryRoad.street = "2800 East Observatory Road";
observatoryRoad.location = new Point(34.118347D, -118.3026284D);
observatoryRoad.location = new Point(-118.3026284D, 34.118347D);
bigBunsCafe = new Place();
bigBunsCafe.name = "Big Buns Cafe";
bigBunsCafe.city = "Los Angeles";
bigBunsCafe.street = "15 South Fremont Avenue";
bigBunsCafe.location = new Point(34.0945637D, -118.1545845D);
bigBunsCafe.location = new Point(-118.1545845D, 34.0945637D);
sarahAsMap = Document.create();
sarahAsMap.put("id", "sarah");
@@ -272,22 +272,44 @@ public class MappingElasticsearchConverterUnitTests {
}
@Test // DATAES-530
public void shouldMapGeoPointElasticsearchNames() {
public void shouldMapGeoPointElasticsearchNames() throws JSONException {
// given
Point point = new Point(10, 20);
String pointAsString = point.getX() + "," + point.getY();
double[] pointAsArray = { point.getX(), point.getY() };
double lon = 5;
double lat = 48;
Point point = new Point(lon, lat);
// ES has Strings in "lat,lon", but has arrays as [lon,lat]!!
String pointAsString = lat + "," + lon;
double[] pointAsArray = { lon, lat };
String expected = "{\n" + //
" \"pointA\": {\n" + //
" \"lon\": 5.0,\n" + //
" \"lat\": 48.0\n" + //
" },\n" + //
" \"pointB\": {\n" + //
" \"lon\": 5.0,\n" + //
" \"lat\": 48.0\n" + //
" },\n" + //
" \"pointC\": \"48.0,5.0\",\n" + //
" \"pointD\": [\n" + //
" 5.0,\n" + //
" 48.0\n" + //
" ]\n" + //
"}\n"; //
GeoEntity geoEntity = GeoEntity.builder().pointA(point).pointB(GeoPoint.fromPoint(point)).pointC(pointAsString)
.pointD(pointAsArray).build();
// when
String jsonResult = mappingElasticsearchConverter.mapObject(geoEntity).toJson();
// then
assertThat(jsonResult).contains(pointTemplate("pointA", point));
assertThat(jsonResult).contains(pointTemplate("pointB", point));
assertThat(jsonResult).contains(String.format(Locale.ENGLISH, "\"%s\":\"%s\"", "pointC", pointAsString));
assertThat(jsonResult)
.contains(String.format(Locale.ENGLISH, "\"%s\":[%.1f,%.1f]", "pointD", pointAsArray[0], pointAsArray[1]));
assertEquals(expected, jsonResult, false);
// assertThat(jsonResult).contains(pointTemplate("pointA", point));
// assertThat(jsonResult).contains(pointTemplate("pointB", point));
// assertThat(jsonResult).contains(String.format(Locale.ENGLISH, "\"%s\":\"%s\"", "pointC", pointAsString));
// assertThat(jsonResult)
// .contains(String.format(Locale.ENGLISH, "\"%s\":[%.1f,%.1f]", "pointD", pointAsArray[0], pointAsArray[1]));
}
@Test // DATAES-530
@@ -868,7 +890,7 @@ public class MappingElasticsearchConverterUnitTests {
}
private String pointTemplate(String name, Point point) {
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getY(), point.getX());
}
private Map<String, Object> writeToMap(Object source) {

View File

@@ -302,7 +302,7 @@ public class ElasticsearchTemplateGeoTests {
// given
loadClassBaseEntities();
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
new Criteria("location").boundedBy(new Point(53.5171d, 0), new Point(49.5171d, 0.2062d)));
new Criteria("location").boundedBy(new Point(0, 53.5171d), new Point(0.2062d, 49.5171d)));
// when
SearchHits<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = operations.search(geoLocationCriteriaQuery3,

View File

@@ -641,7 +641,6 @@ public class MappingBuilderTests extends MappingContextBaseTests {
String mapping = getMappingBuilder().buildPropertyMapping(RankFeatureEntity.class);
System.out.println(mapping);
assertEquals(expected, mapping, false);
}

View File

@@ -798,7 +798,7 @@ public abstract class CustomMethodRepositoryBaseTests {
repository.save(sampleEntity);
// when
Page<SampleEntity> page = repository.findByLocationWithin(new Point(45.7806d, 3.0875d),
Page<SampleEntity> page = repository.findByLocationWithin(new Point(3.0875d, 45.7806d),
new Distance(2, Metrics.KILOMETERS), PageRequest.of(0, 10));
// then
@@ -838,7 +838,7 @@ public abstract class CustomMethodRepositoryBaseTests {
assertThat(pageAll.getTotalElements()).isEqualTo(2L);
// when
Page<SampleEntity> page = repository.findByLocationNear(new Box(new Point(46d, 3d), new Point(45d, 4d)),
Page<SampleEntity> page = repository.findByLocationNear(new Box(new Point(3d, 46d), new Point(4d, 45d)),
PageRequest.of(0, 10));
// then
@@ -861,7 +861,7 @@ public abstract class CustomMethodRepositoryBaseTests {
repository.save(sampleEntity);
// when
Page<SampleEntity> page = repository.findByLocationNear(new Point(45.7806d, 3.0875d),
Page<SampleEntity> page = repository.findByLocationNear(new Point(3.0875d, 45.7806d),
new Distance(2, Metrics.KILOMETERS), PageRequest.of(0, 10));
// then
@@ -1334,7 +1334,7 @@ public abstract class CustomMethodRepositoryBaseTests {
repository.save(sampleEntity2);
// when
long count = repository.countByLocationWithin(new Point(45.7806d, 3.0875d), new Distance(2, Metrics.KILOMETERS));
long count = repository.countByLocationWithin(new Point(3.0875d, 45.7806d), new Distance(2, Metrics.KILOMETERS));
// then
assertThat(count).isEqualTo(1L);
@@ -1365,7 +1365,7 @@ public abstract class CustomMethodRepositoryBaseTests {
repository.save(sampleEntity2);
// when
long count = repository.countByLocationNear(new Box(new Point(46d, 3d), new Point(45d, 4d)));
long count = repository.countByLocationNear(new Box(new Point(3d, 46d), new Point(4d, 45d)));
// then
assertThat(count).isEqualTo(1L);
@@ -1396,7 +1396,7 @@ public abstract class CustomMethodRepositoryBaseTests {
repository.save(sampleEntity2);
// when
long count = repository.countByLocationNear(new Point(45.7806d, 3.0875d), new Distance(2, Metrics.KILOMETERS));
long count = repository.countByLocationNear(new Point(3.0875d, 45.7806d), new Distance(2, Metrics.KILOMETERS));
// then
assertThat(count).isEqualTo(1L);