Example usage for org.apache.lucene.spatial.prefix.tree GeohashPrefixTree getMaxLevelsPossible

List of usage examples for org.apache.lucene.spatial.prefix.tree GeohashPrefixTree getMaxLevelsPossible

Introduction

In this page you can find the example usage for org.apache.lucene.spatial.prefix.tree GeohashPrefixTree getMaxLevelsPossible.

Prototype

public static int getMaxLevelsPossible() 

Source Link

Document

Any more than this and there's no point (double lat and lon are the same).

Usage

From source file:com.stratio.cassandra.lucene.util.GeospatialUtils.java

License:Apache License

/**
 * Checks if the specified max levels is correct.
 *
 * @param maxLevels The maximum number of levels in the tree.
 * @return The validated max levels.//from   w ww  . ja v a 2 s .  c  om
 */
public static int validateGeohashMaxLevels(Integer maxLevels) {
    if (maxLevels == null) {
        return DEFAULT_GEOHASH_MAX_LEVELS;
    } else if (maxLevels < 1 || maxLevels > GeohashPrefixTree.getMaxLevelsPossible()) {
        throw new IndexException("max_levels must be in range [1, %s], but found %s",
                GeohashPrefixTree.getMaxLevelsPossible(), maxLevels);
    }
    return maxLevels;
}

From source file:org.codelibs.elasticsearch.common.geo.GeoUtils.java

License:Apache License

/**
 * Calculate the number of levels needed for a specific precision. GeoHash
 * cells will not exceed the specified size (diagonal) of the precision.
 * @param meters Maximum size of cells in meters (must greater or equal to zero)
 * @return levels need to achieve precision
 *///from   www .ja  v  a2  s .  c o m
public static int geoHashLevelsForPrecision(double meters) {
    assert meters >= 0;

    if (meters == 0) {
        return GeohashPrefixTree.getMaxLevelsPossible();
    } else {
        final double ratio = 1 + (EARTH_POLAR_DISTANCE / EARTH_EQUATOR); // cell ratio
        final double width = Math.sqrt((meters * meters) / (ratio * ratio)); // convert to cell width
        final double part = Math.ceil(EARTH_EQUATOR / width);
        if (part == 1) {
            return 1;
        }
        final int bits = (int) Math.round(Math.ceil(Math.log(part) / Math.log(2)));
        final int full = bits / 5; // number of 5 bit subdivisions
        final int left = bits - full * 5; // bit representing the last level
        final int even = full + (left > 0 ? 1 : 0); // number of even levels
        final int odd = full + (left > 3 ? 1 : 0); // number of odd levels
        return even + odd;
    }
}

From source file:org.elasticsearch.common.geo.GeoUtils.java

License:Apache License

/**
 * Calculate the number of levels needed for a specific precision. GeoHash
 * cells will not exceed the specified size (diagonal) of the precision.
 * @param meters Maximum size of cells in meters (must greater or equal to zero)
 * @return levels need to achieve precision  
 *//*  w w w .  j  a va 2 s  . c o  m*/
public static int geoHashLevelsForPrecision(double meters) {
    assert meters >= 0;

    if (meters == 0) {
        return GeohashPrefixTree.getMaxLevelsPossible();
    } else {
        final double ratio = 1 + (EARTH_POLAR_DISTANCE / EARTH_EQUATOR); // cell ratio
        final double width = Math.sqrt((meters * meters) / (ratio * ratio)); // convert to cell width
        final double part = Math.ceil(EARTH_EQUATOR / width);
        if (part == 1)
            return 1;
        final int bits = (int) Math.round(Math.ceil(Math.log(part) / Math.log(2)));
        final int full = bits / 5; // number of 5 bit subdivisions    
        final int left = bits - full * 5; // bit representing the last level
        final int even = full + (left > 0 ? 1 : 0); // number of even levels
        final int odd = full + (left > 3 ? 1 : 0); // number of odd levels
        return even + odd;
    }
}

From source file:org.elasticsearch.index.search.geo.GeoUtilsTests.java

License:Apache License

@Test
public void testPrefixTreeCellSizes() {
    assertThat(GeoUtils.EARTH_SEMI_MAJOR_AXIS, equalTo(DistanceUtils.EARTH_EQUATORIAL_RADIUS_KM * 1000));
    assertThat(GeoUtils.quadTreeCellWidth(0), lessThanOrEqualTo(GeoUtils.EARTH_EQUATOR));

    SpatialContext spatialContext = new SpatialContext(true);

    GeohashPrefixTree geohashPrefixTree = new GeohashPrefixTree(spatialContext,
            GeohashPrefixTree.getMaxLevelsPossible() / 2);
    Cell gNode = geohashPrefixTree.getWorldCell();

    for (int i = 0; i < geohashPrefixTree.getMaxLevels(); i++) {
        double width = GeoUtils.geoHashCellWidth(i);
        double height = GeoUtils.geoHashCellHeight(i);
        double size = GeoUtils.geoHashCellSize(i);
        double degrees = 360.0 * width / GeoUtils.EARTH_EQUATOR;
        int level = GeoUtils.quadTreeLevelsForPrecision(size);

        assertThat(GeoUtils.quadTreeCellWidth(level), lessThanOrEqualTo(width));
        assertThat(GeoUtils.quadTreeCellHeight(level), lessThanOrEqualTo(height));
        assertThat(GeoUtils.geoHashLevelsForPrecision(size),
                equalTo(geohashPrefixTree.getLevelForDistance(degrees)));

        assertThat("width at level " + i, gNode.getShape().getBoundingBox().getWidth(),
                equalTo(360.d * width / GeoUtils.EARTH_EQUATOR));
        assertThat("height at level " + i, gNode.getShape().getBoundingBox().getHeight(),
                equalTo(180.d * height / GeoUtils.EARTH_POLAR_DISTANCE));

        gNode = gNode.getSubCells(null).iterator().next();
    }/*from  ww w .  jav a2 s.c  o m*/

    QuadPrefixTree quadPrefixTree = new QuadPrefixTree(spatialContext);
    Cell qNode = quadPrefixTree.getWorldCell();
    for (int i = 0; i < QuadPrefixTree.DEFAULT_MAX_LEVELS; i++) {

        double degrees = 360.0 / (1L << i);
        double width = GeoUtils.quadTreeCellWidth(i);
        double height = GeoUtils.quadTreeCellHeight(i);
        double size = GeoUtils.quadTreeCellSize(i);
        int level = GeoUtils.quadTreeLevelsForPrecision(size);

        assertThat(GeoUtils.quadTreeCellWidth(level), lessThanOrEqualTo(width));
        assertThat(GeoUtils.quadTreeCellHeight(level), lessThanOrEqualTo(height));
        assertThat(GeoUtils.quadTreeLevelsForPrecision(size),
                equalTo(quadPrefixTree.getLevelForDistance(degrees)));

        assertThat("width at level " + i, qNode.getShape().getBoundingBox().getWidth(),
                equalTo(360.d * width / GeoUtils.EARTH_EQUATOR));
        assertThat("height at level " + i, qNode.getShape().getBoundingBox().getHeight(),
                equalTo(180.d * height / GeoUtils.EARTH_POLAR_DISTANCE));

        qNode = qNode.getSubCells(null).iterator().next();
    }
}