|
| 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.common.geo; |
| 20 | + |
| 21 | +import org.apache.lucene.geo.Rectangle; |
| 22 | +import org.apache.lucene.util.BitUtil; |
| 23 | + |
| 24 | +import static org.elasticsearch.common.geo.GeoUtils.normalizeLat; |
| 25 | +import static org.elasticsearch.common.geo.GeoUtils.normalizeLon; |
| 26 | + |
| 27 | +/** |
| 28 | + * Implements quad key hashing, same as used by map tiles. |
| 29 | + * The string key is formatted as "zoom/x/y" |
| 30 | + * The hash value (long) contains all three of those values. |
| 31 | + */ |
| 32 | +public class QuadKeyHash { |
| 33 | + |
| 34 | + /** |
| 35 | + * Largest number of tiles (precision) to use. |
| 36 | + * This value cannot be more than (64-5)/2 = 29, because 5 bits are used for zoom level itself |
| 37 | + * If zoom is not stored inside hash, it would be possible to use up to 32 |
| 38 | + */ |
| 39 | + private static final int MAX_ZOOM = 29; |
| 40 | + |
| 41 | + /** Maximum number of bits used by quadkey in a hash */ |
| 42 | + private static final int BITS_IN_HASH = MAX_ZOOM * 2; |
| 43 | + |
| 44 | + /** Maximum number of bits used by quadkey in a hash */ |
| 45 | + private static final long QUADKEY_MASK = (1L << BITS_IN_HASH) - 1; |
| 46 | + |
| 47 | + /** |
| 48 | + * Convert [longitude, latitude] to a hash tha combines zoom, x, and y of the tile. |
| 49 | + */ |
| 50 | + public static long geoToHash(final double longitude, final double latitude, final int zoom) { |
| 51 | + // Adapted from https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Java |
| 52 | + |
| 53 | + // How many tiles in X and in Y |
| 54 | + final int tiles = 1 << validatePrecision(zoom); |
| 55 | + final double lon = normalizeLon(longitude); |
| 56 | + final double lat = normalizeLat(latitude); |
| 57 | + |
| 58 | + int xtile = (int) Math.floor((lon + 180) / 360 * tiles); |
| 59 | + int ytile = (int) Math.floor( |
| 60 | + (1 - Math.log( |
| 61 | + Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat)) |
| 62 | + ) / Math.PI) / 2 * tiles); |
| 63 | + if (xtile < 0) |
| 64 | + xtile = 0; |
| 65 | + if (xtile >= tiles) |
| 66 | + xtile = (tiles - 1); |
| 67 | + if (ytile < 0) |
| 68 | + ytile = 0; |
| 69 | + if (ytile >= tiles) |
| 70 | + ytile = (tiles - 1); |
| 71 | + |
| 72 | + // Zoom value is placed in front of all the bits used for the quadkey |
| 73 | + // e.g. if max zoom is 26, the largest index would use 52 bits (51st..0th), |
| 74 | + // leaving 12 bits unused. Zoom cannot be >26, so it can fit into 5 bits (56th..52nd) |
| 75 | + return BitUtil.interleave(xtile, ytile) | ((long) zoom << BITS_IN_HASH); |
| 76 | + } |
| 77 | + |
| 78 | + private static int[] parseHash(final long hash) { |
| 79 | + final int zoom = validatePrecision((int) (hash >>> BITS_IN_HASH)); |
| 80 | + final int tiles = 1 << zoom; |
| 81 | + |
| 82 | + // decode the quadkey bits as interleaved xtile and ytile |
| 83 | + long val = hash & QUADKEY_MASK; |
| 84 | + int xtile = (int) BitUtil.deinterleave(val); |
| 85 | + int ytile = (int) BitUtil.deinterleave(val >>> 1); |
| 86 | + if (xtile < 0 || ytile < 0 || xtile >= tiles || ytile >= tiles) { |
| 87 | + throw new IllegalArgumentException("hash-tile"); |
| 88 | + } |
| 89 | + |
| 90 | + return new int[]{zoom, xtile, ytile}; |
| 91 | + } |
| 92 | + |
| 93 | + public static String hashToKey(final long geohashAsLong) { |
| 94 | + int[] res = parseHash(geohashAsLong); |
| 95 | + return "" + res[0] + "/" + res[1] + "/" + res[2]; |
| 96 | + } |
| 97 | + |
| 98 | + private static double tile2lon(final int x, final double tiles) { |
| 99 | + return x / tiles * 360.0 - 180; |
| 100 | + } |
| 101 | + |
| 102 | + private static double tile2lat(final int y, final double tiles) { |
| 103 | + double n = Math.PI - (2.0 * Math.PI * y) / tiles; |
| 104 | + return Math.toDegrees(Math.atan(Math.sinh(n))); |
| 105 | + } |
| 106 | + |
| 107 | + public static Rectangle bboxFromTileIndex(long geohashAsLong) { |
| 108 | + int[] res = parseHash(geohashAsLong); |
| 109 | + int zoom = res[0], x = res[1], y = res[2]; |
| 110 | + double tiles = Math.pow(2.0, zoom); // optimization |
| 111 | + |
| 112 | + return new Rectangle( |
| 113 | + tile2lat(y, tiles), tile2lat(y + 1, tiles), |
| 114 | + tile2lon(x, tiles), tile2lon(x + 1, tiles)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Validate precision parameter |
| 119 | + * @param precision as submitted by the user |
| 120 | + */ |
| 121 | + private static int validatePrecision(int precision) { |
| 122 | + if (precision < 0 || precision > MAX_ZOOM) { |
| 123 | + throw new IllegalArgumentException("Invalid geohash quadkey aggregation precision of " + |
| 124 | + precision + ". Must be between 0 and " + MAX_ZOOM + "."); |
| 125 | + } |
| 126 | + return precision; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Attempt to parse precision string into an integer value |
| 131 | + */ |
| 132 | + public static int parsePrecisionString(String precision) { |
| 133 | + try { |
| 134 | + // we want to treat simple integer strings as precision levels, not distances |
| 135 | + return validatePrecision(Integer.parseInt(precision)); |
| 136 | + // Do not catch IllegalArgumentException here |
| 137 | + } catch (NumberFormatException e) { |
| 138 | + // try to parse as a distance value |
| 139 | + final int parsedPrecision = GeoUtils.quadTreeLevelsForPrecision(precision); |
| 140 | + try { |
| 141 | + return validatePrecision(parsedPrecision); |
| 142 | + } catch (IllegalArgumentException e2) { |
| 143 | + // this happens when distance too small, so precision > . |
| 144 | + // We'd like to see the original string |
| 145 | + throw new IllegalArgumentException("precision too high [" + precision + "]", e2); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private QuadKeyHash() { |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments