Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for geohash neighbors when geohash length is even. #8529

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/main/java/org/elasticsearch/common/geo/GeoHashUtils.java
Expand Up @@ -159,15 +159,13 @@ private final static String neighbor(String geohash, int level, int dx, int dy)
final int nx = ((level % 2) == 1) ? (x + dx) : (x + dy);
final int ny = ((level % 2) == 1) ? (y + dy) : (y + dx);

// define grid limits for current level
final int xLimit = ((level % 2) == 0) ? 7 : 3;
final int yLimit = ((level % 2) == 0) ? 3 : 7;

// if the defined neighbor has the same parent a the current cell
// encode the cell directly. Otherwise find the cell next to this
// cell recursively. Since encoding wraps around within a cell
// it can be encoded here.
if (nx >= 0 && nx <= xLimit && ny >= 0 && ny <= yLimit) {
// xLimit and YLimit must always be respectively 7 and 3
// since x and y semantics are swapping on each level.
if (nx >= 0 && nx <= 7 && ny >= 0 && ny <= 3) {
return geohash.substring(0, level - 1) + encode(nx, ny);
} else {
String neighbor = neighbor(geohash, level - 1, dx, dy);
Expand Down
Expand Up @@ -104,5 +104,35 @@ public void testNeighbours() {
Collection<? super String> neighbors = new ArrayList<>();
GeoHashUtils.addNeighbors(geohash, neighbors );
assertEquals(expectedNeighbors, neighbors);

// Border odd geohash
geohash = "u09x";
expectedNeighbors = new ArrayList<>();
expectedNeighbors.add("u0c2");
expectedNeighbors.add("u0c8");
expectedNeighbors.add("u0cb");
expectedNeighbors.add("u09r");
expectedNeighbors.add("u09z");
expectedNeighbors.add("u09q");
expectedNeighbors.add("u09w");
expectedNeighbors.add("u09y");
neighbors = new ArrayList<>();
GeoHashUtils.addNeighbors(geohash, neighbors );
assertEquals(expectedNeighbors, neighbors);

// Border even geohash
geohash = "u09tv";
expectedNeighbors = new ArrayList<>();
expectedNeighbors.add("u09wh");
expectedNeighbors.add("u09wj");
expectedNeighbors.add("u09wn");
expectedNeighbors.add("u09tu");
expectedNeighbors.add("u09ty");
expectedNeighbors.add("u09ts");
expectedNeighbors.add("u09tt");
expectedNeighbors.add("u09tw");
neighbors = new ArrayList<>();
GeoHashUtils.addNeighbors(geohash, neighbors );
assertEquals(expectedNeighbors, neighbors);
}
}