List of usage examples for org.apache.lucene.spatial.query SpatialArgs SpatialArgs
public SpatialArgs(SpatialOperation operation, Shape shape)
From source file:bzh.terrevirtuelle.navisu.gazetteer.impl.lucene.GeoNameResolver.java
License:Apache License
/** * Returns a list of location near a certain coordinate. * * @param latitude, @param longitude - Center of search area * @param distanceInMiles - Search Radius in miles * @param indexerPath - Path to Lucene index * @param count - Upper bound to number of results * @return - List of locations sorted by population * @throws IOException/*from w ww.j a v a 2 s.co m*/ */ public List<Location> searchNearby(Double latitude, Double longitude, Double distanceInMiles, String indexerPath, int count) throws IOException { double distanceInDeg = DistanceUtils.dist2Degrees(distanceInMiles, DistanceUtils.EARTH_EQUATORIAL_RADIUS_MI); SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.IsWithin, ctx.makeCircle(longitude, latitude, distanceInDeg)); String key = latitude + "-" + longitude; Filter filter = strategy.makeFilter(spatialArgs); IndexSearcher searcher = new IndexSearcher(createIndexReader(indexerPath)); Sort sort = new Sort(populationSort); TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), filter, count, sort); ScoreDoc[] scoreDocs = topDocs.scoreDocs; HashMap<String, List<Location>> allCandidates = new HashMap<String, List<Location>>(); getMatchingCandidates(searcher, allCandidates, key, scoreDocs); List<Location> results = allCandidates.get(key); return results; }
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 www . j av 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 www . jav a2s. com*/ 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
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.lucene.manager.OLuceneSpatialIndexManager.java
License:Apache License
public Object searchWithin(OSpatialCompositeKey key, OCommandContext context) throws IOException { Set<OIdentifiable> result = new HashSet<OIdentifiable>(); Shape shape = factory.makeShape(key, ctx); if (shape == null) return null; SpatialArgs args = new SpatialArgs(SpatialOperation.IsWithin, shape); IndexSearcher searcher = getSearcher(); Filter filter = strategy.makeFilter(args); return new LuceneResultSet(this, new SpatialQueryContext(context, searcher, new MatchAllDocsQuery(), filter)); }
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.engine.OLuceneLegacySpatialIndexEngine.java
License:Apache License
public Object searchWithin(OSpatialCompositeKey key, OCommandContext context) throws IOException { Set<OIdentifiable> result = new HashSet<OIdentifiable>(); Shape shape = legacyBuilder.makeShape(key, ctx); if (shape == null) return null; SpatialArgs args = new SpatialArgs(SpatialOperation.IsWithin, shape); IndexSearcher searcher = searcher(); Filter filter = strategy.makeFilter(args); return new LuceneResultSet(this, new SpatialQueryContext(context, searcher, new MatchAllDocsQuery(), filter)); }
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 ww w . j ava 2s . co 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();//from w w w . j a v a2s . c o m } 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.SpatialQueryBuilderDWithin.java
License:Apache License
@Override public SpatialQueryContext build(Map<String, Object> query) throws Exception { Shape shape = parseShape(query); System.out.println("qui:: " + shape); SpatialStrategy strategy = manager.strategy(); if (isOnlyBB(strategy)) { shape = shape.getBoundingBox();/* w ww .j a v a2 s . c om*/ } SpatialArgs args1 = new SpatialArgs(SpatialOperation.IsWithin, shape); // SpatialArgs args2 = new SpatialArgs(SpatialOperation., shape); Geometry geo = OShapeFactory.INSTANCE.toGeometry(shape); Filter filter = strategy.makeFilter(args1); return new SpatialQueryContext(null, manager.searcher(), new MatchAllDocsQuery(), filter); }