Example usage for org.apache.lucene.spatial.prefix.tree Cell getShape

List of usage examples for org.apache.lucene.spatial.prefix.tree Cell getShape

Introduction

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

Prototype

Shape getShape();

Source Link

Document

Gets the shape for this cell; typically a Rectangle.

Usage

From source file:index.NumberRangePrefixTreeStrategy.java

License:Apache License

/**
 * Calculates facets (aggregated counts) given a range shape (start-end span) and a level, which specifies the detail.
 * To get the level of an existing shape, say a Calendar, call
 * {@link org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree#toUnitShape(Object)} then call
 * {@link org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape#getLevel()}.
 * Facet computation is implemented by navigating the underlying indexed terms efficiently.
 *//*w w  w . j  a  va2 s. c  o  m*/
public Facets calcFacets(IndexReaderContext context, Filter filter, Shape facetRange, final int level)
        throws IOException {
    final Facets facets = new Facets(level);
    PrefixTreeFacetCounter.compute(this, context, filter, facetRange, level,
            new PrefixTreeFacetCounter.FacetVisitor() {
                Facets.FacetParentVal parentFacet;
                UnitNRShape parentShape;

                @Override
                public void visit(Cell cell, int count) {
                    if (cell.getLevel() < level - 1) {//some ancestor of parent facet level, direct or distant
                        parentFacet = null;//reset
                        parentShape = null;//reset
                        facets.topLeaves += count;
                    } else if (cell.getLevel() == level - 1) {//parent
                        //set up FacetParentVal
                        setupParent((UnitNRShape) cell.getShape());
                        parentFacet.parentLeaves += count;
                    } else {//at facet level
                        UnitNRShape unitShape = (UnitNRShape) cell.getShape();
                        UnitNRShape unitShapeParent = unitShape.getShapeAtLevel(unitShape.getLevel() - 1);
                        if (parentFacet == null || !parentShape.equals(unitShapeParent)) {
                            setupParent(unitShapeParent);
                        }
                        //lazy init childCounts
                        if (parentFacet.childCounts == null) {
                            parentFacet.childCounts = new int[parentFacet.childCountsLen];
                        }
                        parentFacet.childCounts[unitShape.getValAtLevel(cell.getLevel())] += count;
                    }
                }

                private void setupParent(UnitNRShape unitShape) {
                    parentShape = unitShape.clone();
                    //Look for existing parentFacet (from previous segment), or create anew if needed
                    parentFacet = facets.parents.get(parentShape);
                    if (parentFacet == null) {//didn't find one; make a new one
                        parentFacet = new Facets.FacetParentVal();
                        parentFacet.childCountsLen = getGrid().getNumSubCells(parentShape);
                        facets.parents.put(parentShape, parentFacet);
                    }
                }
            });
    return facets;
}

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();
    }/*ww w .jav  a 2 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();
    }
}