Skip to content

Commit

Permalink
Geo: fixes computation of geohash neighbours
Browse files Browse the repository at this point in the history
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
  • Loading branch information
colings86 authored and areek committed Sep 8, 2014
1 parent 6b1335d commit 5517dda
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/elasticsearch/common/geo/GeoHashUtils.java
Expand Up @@ -185,7 +185,7 @@ private final static String neighbor(String geohash, int level, int dx, int dy)
// 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) {
if (nx >= 0 && nx <= xLimit && ny >= 0 && ny <= yLimit) {
return geohash.substring(0, level - 1) + encode(nx, ny);
} else {
String neighbor = neighbor(geohash, level - 1, dx, dy);
Expand Down Expand Up @@ -511,5 +511,5 @@ private static double[] decodeCell(long geohash) {
}
}
return interval;
}
}
}
Expand Up @@ -24,7 +24,9 @@
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
*
Expand Down Expand Up @@ -86,4 +88,21 @@ public void testDecodeEncode() {

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);
}
}

0 comments on commit 5517dda

Please sign in to comment.