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:
Colin Goodheart-Smithe 2014-08-12 21:12:38 +01:00
parent 2906d3e6dc
commit cd4aea841a
2 changed files with 22 additions and 3 deletions

View File

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

View File

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