mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-06 04:58:50 +00:00
Geo: fixes computation of geohash neighbours
The geohash grid it 8 cells wide and 4 cells tall. GeoHashUtils.neighbor(String,int,int.int) set the limit of the number of cells in y to < 3 rather than <= 3 resulting in it either not finding all neighbours or incorrectly searching for a neighbour in a different parent cell. Closes #7226
This commit is contained in:
parent
2906d3e6dc
commit
cd4aea841a
@ -185,7 +185,7 @@ public class GeoHashUtils {
|
|||||||
// encode the cell directly. Otherwise find the cell next to this
|
// encode the cell directly. Otherwise find the cell next to this
|
||||||
// cell recursively. Since encoding wraps around within a cell
|
// cell recursively. Since encoding wraps around within a cell
|
||||||
// it can be encoded here.
|
// it can be encoded here.
|
||||||
if (nx >= 0 && nx <= xLimit && ny >= 0 && ny < yLimit) {
|
if (nx >= 0 && nx <= xLimit && ny >= 0 && ny <= yLimit) {
|
||||||
return geohash.substring(0, level - 1) + encode(nx, ny);
|
return geohash.substring(0, level - 1) + encode(nx, ny);
|
||||||
} else {
|
} else {
|
||||||
String neighbor = neighbor(geohash, level - 1, dx, dy);
|
String neighbor = neighbor(geohash, level - 1, dx, dy);
|
||||||
@ -511,5 +511,5 @@ public class GeoHashUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return interval;
|
return interval;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -24,7 +24,9 @@ import org.elasticsearch.common.geo.GeoPoint;
|
|||||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -86,4 +88,21 @@ public class GeoHashUtilsTests extends ElasticsearchTestCase {
|
|||||||
|
|
||||||
assertEquals(geoHash, GeoHashUtils.encode(decode.lat(), decode.lon()));
|
assertEquals(geoHash, GeoHashUtils.encode(decode.lat(), decode.lon()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNeighbours() {
|
||||||
|
String geohash = "gcpv";
|
||||||
|
List<String> expectedNeighbors = new ArrayList<>();
|
||||||
|
expectedNeighbors.add("gcpw");
|
||||||
|
expectedNeighbors.add("gcpy");
|
||||||
|
expectedNeighbors.add("u10n");
|
||||||
|
expectedNeighbors.add("gcpt");
|
||||||
|
expectedNeighbors.add("u10j");
|
||||||
|
expectedNeighbors.add("gcps");
|
||||||
|
expectedNeighbors.add("gcpu");
|
||||||
|
expectedNeighbors.add("u10h");
|
||||||
|
Collection<? super String> neighbors = new ArrayList<>();
|
||||||
|
GeoHashUtils.addNeighbors(geohash, neighbors );
|
||||||
|
assertEquals(expectedNeighbors, neighbors);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user