Skip to content

Commit a8870ef

Browse files
authored
Refactor GeoHashUtils (#40869)
This commit refactors GeoHashUtils class into a new Geohash utility class located in the ES geo library. The intent is to not only better control what geo methods are whitelisted for painless scripting but to clean up the geo utility API in general.
1 parent bc95794 commit a8870ef

File tree

25 files changed

+385
-339
lines changed

25 files changed

+385
-339
lines changed

libs/geo/src/main/java/org/elasticsearch/geo/geometry/Rectangle.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ public boolean crossesDateline() {
163163
return maxLon < minLon;
164164
}
165165

166+
/** returns true if rectangle (defined by minLat, maxLat, minLon, maxLon) contains the lat lon point */
167+
public boolean containsPoint(final double lat, final double lon) {
168+
if (lat >= minLat && lat <= maxLat) {
169+
return crossesDateline() ? lon >= minLon || lon <= maxLon : lon >= minLon && lon <= maxLon;
170+
}
171+
return false;
172+
}
173+
166174
@Override
167175
public boolean equals(Object o) {
168176
if (this == o) return true;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.geo.utils;
20+
21+
/**
22+
* Utilities for common Bit twiddling methods. Borrowed heavily from Lucene (org.apache.lucene.util.BitUtil).
23+
*/
24+
public class BitUtil { // magic numbers for bit interleaving
25+
private static final long MAGIC[] = {
26+
0x5555555555555555L, 0x3333333333333333L,
27+
0x0F0F0F0F0F0F0F0FL, 0x00FF00FF00FF00FFL,
28+
0x0000FFFF0000FFFFL, 0x00000000FFFFFFFFL,
29+
0xAAAAAAAAAAAAAAAAL
30+
};
31+
// shift values for bit interleaving
32+
private static final short SHIFT[] = {1, 2, 4, 8, 16};
33+
34+
/**
35+
* Interleaves the first 32 bits of each long value
36+
*
37+
* Adapted from: http://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN
38+
*/
39+
public static long interleave(int even, int odd) {
40+
long v1 = 0x00000000FFFFFFFFL & even;
41+
long v2 = 0x00000000FFFFFFFFL & odd;
42+
v1 = (v1 | (v1 << SHIFT[4])) & MAGIC[4];
43+
v1 = (v1 | (v1 << SHIFT[3])) & MAGIC[3];
44+
v1 = (v1 | (v1 << SHIFT[2])) & MAGIC[2];
45+
v1 = (v1 | (v1 << SHIFT[1])) & MAGIC[1];
46+
v1 = (v1 | (v1 << SHIFT[0])) & MAGIC[0];
47+
v2 = (v2 | (v2 << SHIFT[4])) & MAGIC[4];
48+
v2 = (v2 | (v2 << SHIFT[3])) & MAGIC[3];
49+
v2 = (v2 | (v2 << SHIFT[2])) & MAGIC[2];
50+
v2 = (v2 | (v2 << SHIFT[1])) & MAGIC[1];
51+
v2 = (v2 | (v2 << SHIFT[0])) & MAGIC[0];
52+
53+
return (v2<<1) | v1;
54+
}
55+
56+
/**
57+
* Extract just the even-bits value as a long from the bit-interleaved value
58+
*/
59+
public static long deinterleave(long b) {
60+
b &= MAGIC[0];
61+
b = (b ^ (b >>> SHIFT[0])) & MAGIC[1];
62+
b = (b ^ (b >>> SHIFT[1])) & MAGIC[2];
63+
b = (b ^ (b >>> SHIFT[2])) & MAGIC[3];
64+
b = (b ^ (b >>> SHIFT[3])) & MAGIC[4];
65+
b = (b ^ (b >>> SHIFT[4])) & MAGIC[5];
66+
return b;
67+
}
68+
69+
/**
70+
* flip flops odd with even bits
71+
*/
72+
public static final long flipFlop(final long b) {
73+
return ((b & MAGIC[6]) >>> 1) | ((b & MAGIC[0]) << 1 );
74+
}
75+
}

0 commit comments

Comments
 (0)