Example usage for org.apache.lucene.spatial.query SpatialOperation Intersects

List of usage examples for org.apache.lucene.spatial.query SpatialOperation Intersects

Introduction

In this page you can find the example usage for org.apache.lucene.spatial.query SpatialOperation Intersects.

Prototype

SpatialOperation Intersects

To view the source code for org.apache.lucene.spatial.query SpatialOperation Intersects.

Click Source Link

Document

Meets the "Intersects" OGC definition.

Usage

From source file:com.berico.clavin.resolver.impl.lucene.LuceneCoordinateIndex.java

License:Apache License

/**
 * Search for locations around the supplied coordinate.
 * @param coordinate Coordinate to search for nearby locations.
 * @param distanceInKm Kilometer radius to search around the
 * target coordinate for named locations.
 * @param limit Max number of results to return from the index.
 * @return ResolvedCoordinate instance.//from ww  w  .  ja v  a 2 s . co  m
 */
List<ResolvedCoordinate> performSearch(CoordinateOccurrence<?> coordinate, int distanceInKm, int limit)
        throws Exception {

    // Acquire a searcher.
    IndexSearcher searcher = this.lucene.getSearcherManager().acquire();

    // Convert the KM distance to degrees.
    double distanceInDegrees = DistanceUtils.dist2Degrees(distanceInKm, DistanceUtils.EARTH_MEAN_RADIUS_KM);

    // Convert the coordinate to it's lat/lon representation.
    LatLon center = coordinate.convertToLatLon();

    // Create a circular bounding box using the coordinate as the center
    // and the distance as the circle's radius.
    Circle queryBoundary = this.lucene.getSpatialContext().makeCircle(center.getLongitude(),
            center.getLatitude(), distanceInDegrees);

    // Create a spatial search configuration
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, queryBoundary);

    // Get a Lucene filter from the spatial config.
    Filter filter = this.lucene.getSpatialStrategy().makeFilter(spatialArgs);

    // Search the index using the circle as a bounding box (er...circle).
    TopDocs results = searcher.search(new MatchAllDocsQuery(), filter, DEFAULT_LIMIT);

    // Convert the results to a ResolvedCoordinate
    return LuceneUtils.convertToCoordinate(coordinate, searcher, results, lucene);
}

From source file:com.boundlessgeo.elasticsearch.geoheatmap.GeoHeatmapAggregatorFactory.java

License:Apache License

private Integer resolveGridLevel(Optional<Double> distErrOp, Optional<Double> distErrPctOp) {
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, this.inputShape);
    if (distErrOp.isPresent()) {
        spatialArgs.setDistErr(distErrOp.get() * DistanceUnit.DEFAULT.getDistancePerDegree());
    }/*from  ww w .java 2s.co  m*/
    spatialArgs.setDistErrPct(distErrPctOp.orElse(DEFAULT_DIST_ERR_PCT));
    double distErr = spatialArgs.resolveDistErr(strategy.getSpatialContext(), DEFAULT_DIST_ERR_PCT);
    if (distErr <= 0) {
        throw new AggregationInitializationException(String.format(Locale.ROOT,
                "%s or %s should be > 0 or instead provide %s=%s for absolute maximum detail",
                GeoHeatmapAggregationBuilder.DIST_ERR_PCT_FIELD, GeoHeatmapAggregationBuilder.DIST_ERR_FIELD,
                GeoHeatmapAggregationBuilder.GRID_LEVEL_FIELD, strategy.getGrid().getMaxLevels()));
    }
    return strategy.getGrid().getLevelForDistance(distErr);
}

From source file:com.orientechnologies.lucene.manager.OLuceneSpatialIndexManager.java

License:Apache License

@Override
public Object get(Object key) {
    try {/*  w  ww .  j a va2  s  .  co m*/
        if (key instanceof OSpatialCompositeKey) {
            final OSpatialCompositeKey newKey = (OSpatialCompositeKey) key;

            final SpatialOperation strategy = newKey.getOperation() != null ? newKey.getOperation()
                    : SpatialOperation.Intersects;

            if (SpatialOperation.Intersects.equals(strategy))
                return searchIntersect(newKey, newKey.getMaxDistance(), newKey.getContext());
            else if (SpatialOperation.IsWithin.equals(strategy))
                return searchWithin(newKey, newKey.getContext());

        } else if (key instanceof OCompositeKey)
            return searchIntersect((OCompositeKey) key, 0, null);

    } catch (IOException e) {
        OLogManager.instance().error(this, "Error on getting entry against Lucene index", e);
    }

    return null;
}

From source file:com.orientechnologies.lucene.manager.OLuceneSpatialIndexManager.java

License:Apache License

public Object searchIntersect(OCompositeKey key, double distance, OCommandContext context) throws IOException {

    double lat = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(0), Double.class)).doubleValue();
    double lng = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(1), Double.class)).doubleValue();
    SpatialOperation operation = SpatialOperation.Intersects;

    Point p = ctx.makePoint(lng, lat);
    SpatialArgs args = new SpatialArgs(operation,
            ctx.makeCircle(lng, lat, DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
    Filter filter = strategy.makeFilter(args);
    IndexSearcher searcher = getSearcher();
    ValueSource valueSource = strategy.makeDistanceValueSource(p);
    Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher);

    return new LuceneResultSet(this,
            new SpatialQueryContext(context, searcher, new MatchAllDocsQuery(), filter, distSort)
                    .setSpatialArgs(args));
}

From source file:com.orientechnologies.spatial.engine.OLuceneLegacySpatialIndexEngine.java

License:Apache License

private Object legacySearch(Object key) throws IOException {
    if (key instanceof OSpatialCompositeKey) {
        final OSpatialCompositeKey newKey = (OSpatialCompositeKey) key;

        final SpatialOperation strategy = newKey.getOperation() != null ? newKey.getOperation()
                : SpatialOperation.Intersects;

        if (SpatialOperation.Intersects.equals(strategy))
            return searchIntersect(newKey, newKey.getMaxDistance(), newKey.getContext());
        else if (SpatialOperation.IsWithin.equals(strategy))
            return searchWithin(newKey, newKey.getContext());

    } else if (key instanceof OCompositeKey) {
        return searchIntersect((OCompositeKey) key, 0, null);
    }/*from w ww  .  j a v a2  s. c  o m*/
    throw new OIndexEngineException("Unknown key" + key, null);

}

From source file:com.orientechnologies.spatial.engine.OLuceneLegacySpatialIndexEngine.java

License:Apache License

public Object searchIntersect(OCompositeKey key, double distance, OCommandContext context) throws IOException {

    double lat = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(0), Double.class)).doubleValue();
    double lng = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(1), Double.class)).doubleValue();
    SpatialOperation operation = SpatialOperation.Intersects;

    Point p = ctx.makePoint(lng, lat);
    SpatialArgs args = new SpatialArgs(operation,
            ctx.makeCircle(lng, lat, DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
    Filter filter = strategy.makeFilter(args);
    IndexSearcher searcher = searcher();
    ValueSource valueSource = strategy.makeDistanceValueSource(p);
    Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher);

    return new LuceneResultSet(this,
            new SpatialQueryContext(context, searcher, new MatchAllDocsQuery(), filter, distSort)
                    .setSpatialArgs(args));
}

From source file:com.orientechnologies.spatial.sandbox.LuceneGeoTest.java

License:Apache License

@Test
public void geoIntersectTest() throws IOException, ParseException {

    RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(
            new GeohashPrefixTree(JtsSpatialContext.GEO, 11), "location");

    strategy.setDistErrPct(0);//from w  ww.  ja  v a2 s .  c  o m

    IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
    final RAMDirectory directory = new RAMDirectory();
    final IndexWriter writer = new IndexWriter(directory, conf);

    Shape point = JtsSpatialContext.GEO.getWktShapeParser().parse("POINT (9.4714708 47.6819432)");

    Shape polygon = JtsSpatialContext.GEO.getWktShapeParser().parse(
            "POLYGON((9.481201171875 47.64885294675266,9.471416473388672 47.65128140482982,9.462661743164062 47.64781214443791,9.449443817138672 47.656947367880335,9.445838928222656 47.66110972448931,9.455795288085938 47.667352637215,9.469013214111328 47.67255449415724,9.477081298828125 47.679142768657066,9.490299224853516 47.678680460743834,9.506263732910156 47.679258344995326,9.51364517211914 47.68191653011071,9.518795013427734 47.677177931734406,9.526691436767578 47.679489496903706,9.53390121459961 47.67139857075435,9.50918197631836 47.66180341832901,9.50815200805664 47.6529003141482,9.51192855834961 47.64654002455372,9.504375457763672 47.64237650648966,9.49270248413086 47.649662445325035,9.48617935180664 47.65151268066222,9.481201171875 47.64885294675266))");

    Document doc = new Document();

    Assert.assertNotEquals(point.relate(polygon), SpatialRelation.INTERSECTS);
    for (IndexableField f : strategy.createIndexableFields(point)) {
        doc.add(f);
    }

    writer.addDocument(doc);
    writer.commit();

    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, polygon.getBoundingBox());
    Filter filter = strategy.makeFilter(args);
    IndexReader reader = DirectoryReader.open(directory);

    IndexSearcher searcher = new IndexSearcher(reader);

    TopDocs search = searcher.search(new MatchAllDocsQuery(), filter, 1000);
    Assert.assertEquals(search.totalHits, 0);

    reader.close();
    writer.close();
}

From source file:com.orientechnologies.spatial.strategy.SpatialQueryBuilderContains.java

License:Apache License

@Override
public SpatialQueryContext build(Map<String, Object> query) throws Exception {
    Shape shape = parseShape(query);
    SpatialStrategy strategy = manager.strategy();

    if (isOnlyBB(strategy)) {
        shape = shape.getBoundingBox();//w ww  .j a  v a  2s  . c  om
    }
    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, shape);
    Filter filter = strategy.makeFilter(args);
    return new SpatialQueryContext(null, manager.searcher(), new MatchAllDocsQuery(), filter);
}

From source file:com.orientechnologies.spatial.strategy.SpatialQueryBuilderNear.java

License:Apache License

@Override
public SpatialQueryContext build(Map<String, Object> query) throws Exception {
    Shape shape = parseShape(query);

    double distance = 0;

    Number n = (Number) query.get(MAX_DISTANCE);
    if (n != null) {
        distance = n.doubleValue();/*from   w w w  .j a v a  2  s  .c o  m*/
    }

    Point p = (Point) shape;

    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, factory.context().makeCircle(p.getX(),
            p.getY(), DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
    Filter filter = manager.strategy().makeFilter(args);
    ValueSource valueSource = manager.strategy().makeDistanceValueSource(p);
    IndexSearcher searcher = manager.searcher();
    Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher);
    return new SpatialQueryContext(null, searcher, new MatchAllDocsQuery(), filter, distSort)
            .setSpatialArgs(args);
}

From source file:com.orientechnologies.spatial.strategy.SpatialQueryBuilderOverlap.java

License:Apache License

@Override
public SpatialQueryContext build(Map<String, Object> query) throws Exception {
    Shape shape = parseShape(query);
    SpatialStrategy strategy = manager.strategy();
    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, shape.getBoundingBox());
    Filter filter = strategy.makeFilter(args);
    return new SpatialQueryContext(null, manager.searcher(), new MatchAllDocsQuery(), filter);
}