2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.lucene.spatial.geohash;
20 import org.apache.lucene.util.LuceneTestCase;
21 import org.junit.Test;
24 * Tests for {@link GeoHashUtils}
26 public class TestGeoHashUtils extends LuceneTestCase {
29 * Pass condition: lat=42.6, lng=-5.6 should be encoded as "ezs42e44yx96",
30 * lat=57.64911 lng=10.40744 should be encoded as "u4pruydqqvj8"
33 public void testEncode() {
34 String hash = GeoHashUtils.encode(42.6, -5.6);
35 assertEquals("ezs42e44yx96", hash);
37 hash = GeoHashUtils.encode(57.64911, 10.40744);
38 assertEquals("u4pruydqqvj8", hash);
42 * Pass condition: lat=52.3738007, lng=4.8909347 should be encoded and then
43 * decoded within 0.00001 of the original value
46 public void testDecodePreciseLongitudeLatitude() {
47 String hash = GeoHashUtils.encode(52.3738007, 4.8909347);
49 double[] latitudeLongitude = GeoHashUtils.decode(hash);
51 assertEquals(52.3738007, latitudeLongitude[0], 0.00001D);
52 assertEquals(4.8909347, latitudeLongitude[1], 0.00001D);
56 * Pass condition: lat=84.6, lng=10.5 should be encoded and then decoded
57 * within 0.00001 of the original value
60 public void testDecodeImpreciseLongitudeLatitude() {
61 String hash = GeoHashUtils.encode(84.6, 10.5);
63 double[] latitudeLongitude = GeoHashUtils.decode(hash);
65 assertEquals(84.6, latitudeLongitude[0], 0.00001D);
66 assertEquals(10.5, latitudeLongitude[1], 0.00001D);
70 * see https://issues.apache.org/jira/browse/LUCENE-1815 for details
73 public void testDecodeEncode() {
74 String geoHash = "u173zq37x014";
75 assertEquals(geoHash, GeoHashUtils.encode(52.3738007, 4.8909347));
76 double[] decode = GeoHashUtils.decode(geoHash);
77 assertEquals(52.37380061d, decode[0], 0.000001d);
78 assertEquals(4.8909343d, decode[1], 0.000001d);
80 assertEquals(geoHash, GeoHashUtils.encode(decode[0], decode[1]));
83 decode = GeoHashUtils.decode("u173");
84 geoHash = GeoHashUtils.encode(decode[0], decode[1]);
85 assertEquals(decode[0], GeoHashUtils.decode(geoHash)[0], 0.000001d);
86 assertEquals(decode[1], GeoHashUtils.decode(geoHash)[1], 0.000001d);