Java Code Examples for org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree

The following examples show how to use org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: SpatialDocMaker.java    License: Apache License 2.0 6 votes vote down vote up
protected RecursivePrefixTreeStrategy makeRPTStrategy(String spatialField, Config config,
                                                      Map<String, String> configMap, SpatialContext ctx) {
  //A factory for the prefix tree grid
  SpatialPrefixTree grid = SpatialPrefixTreeFactory.makeSPT(configMap, null, ctx);

  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, spatialField);
  strategy.setPointsOnly(config.get("spatial.docPointsOnly", false));
  final boolean pruneLeafyBranches = config.get("spatial.pruneLeafyBranches", true);
  if (grid instanceof PackedQuadPrefixTree) {
    ((PackedQuadPrefixTree) grid).setPruneLeafyBranches(pruneLeafyBranches);
    strategy.setPruneLeafyBranches(false);//always leave it to packed grid, even though it isn't the same
  } else {
    strategy.setPruneLeafyBranches(pruneLeafyBranches);
  }

  int prefixGridScanLevel = config.get("query.spatial.prefixGridScanLevel", -4);
  if (prefixGridScanLevel < 0)
    prefixGridScanLevel = grid.getMaxLevels() + prefixGridScanLevel;
  strategy.setPrefixGridScanLevel(prefixGridScanLevel);

  double distErrPct = config.get("spatial.distErrPct", .025);//doc & query; a default
  strategy.setDistErrPct(distErrPct);
  return strategy;
}
 
Example 2
Source Project: lucene-solr   Source File: QueryEqualsHashCodeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEqualsHashCode() {

  switch (random().nextInt(4)) {//0-3
    case 0: predicate = SpatialOperation.Contains; break;
    case 1: predicate = SpatialOperation.IsWithin; break;

    default: predicate = SpatialOperation.Intersects; break;
  }
  final SpatialPrefixTree gridQuad = new QuadPrefixTree(ctx,10);
  final SpatialPrefixTree gridGeohash = new GeohashPrefixTree(ctx,10);

  Collection<SpatialStrategy> strategies = new ArrayList<>();
  RecursivePrefixTreeStrategy recursive_geohash = new RecursivePrefixTreeStrategy(gridGeohash, "recursive_geohash");
  strategies.add(recursive_geohash);
  strategies.add(new TermQueryPrefixTreeStrategy(gridQuad, "termquery_quad"));
  strategies.add(PointVectorStrategy.newInstance(ctx, "pointvector"));
  strategies.add(BBoxStrategy.newInstance(ctx, "bbox"));
  final SerializedDVStrategy serialized = new SerializedDVStrategy(ctx, "serialized");
  strategies.add(serialized);
  strategies.add(new CompositeSpatialStrategy("composite", recursive_geohash, serialized));
  for (SpatialStrategy strategy : strategies) {
    testEqualsHashcode(strategy);
  }
}
 
Example 3
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test28() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  ShapeReadWriter<SpatialContext> shapeReadWriter = new ShapeReadWriter<SpatialContext>(ctx);
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_KM));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  String writeSpatialArgs = SpatialArgsParser.writeSpatialArgs(args, shapeReadWriter);

  // This has to be done because of rounding.
  SpatialArgs spatialArgs = SpatialArgsParser.parse(writeSpatialArgs, shapeReadWriter);
  Query q1 = sq(strategy.makeQuery(spatialArgs));
  Query q = parseSq("a.id_gis:\"" + writeSpatialArgs + "\"");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example 4
Source Project: rdf4j   Source File: LuceneIndex.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Function<String, ? extends SpatialStrategy> createSpatialStrategyMapper(Map<String, String> parameters) {
	ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
	SpatialContext geoContext = SpatialContextFactory.makeSpatialContext(parameters, classLoader);
	final SpatialPrefixTree spt = SpatialPrefixTreeFactory.makeSPT(parameters, classLoader, geoContext);
	return new Function<String, SpatialStrategy>() {

		@Override
		public SpatialStrategy apply(String field) {
			return new RecursivePrefixTreeStrategy(spt, GEO_FIELD_PREFIX + field);
		}

	};
}
 
Example 5
Source Project: lucene-solr   Source File: WithinPrefixTreeQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * See {@link AbstractVisitingPrefixTreeQuery#AbstractVisitingPrefixTreeQuery(org.locationtech.spatial4j.shape.Shape, String, org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree, int, int)}.
 * {@code queryBuffer} is the (minimum) distance beyond the query shape edge
 * where non-matching documents are looked for so they can be excluded. If
 * -1 is used then the whole world is examined (a good default for correctness).
 */
public WithinPrefixTreeQuery(Shape queryShape, String fieldName, SpatialPrefixTree grid,
                             int detailLevel, int prefixGridScanLevel,
                             double queryBuffer) {
  super(queryShape, fieldName, grid, detailLevel, prefixGridScanLevel);
  this.bufferedQueryShape = queryBuffer == -1 ? null : bufferShape(queryShape, queryBuffer);
}
 
Example 6
Source Project: lucene-solr   Source File: JtsPolygonTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * A PrefixTree pruning optimization gone bad.
 * See <a href="https://issues.apache.org/jira/browse/LUCENE-4770">LUCENE-4770</a>.
 */
@Test
public void testBadPrefixTreePrune() throws Exception {

  Shape area = ctx.readShapeFromWkt("POLYGON((-122.83 48.57, -122.77 48.56, -122.79 48.53, -122.83 48.57))");

  SpatialPrefixTree trie = new QuadPrefixTree(ctx, 12);
  TermQueryPrefixTreeStrategy strategy = new TermQueryPrefixTreeStrategy(trie, "geo");
  Document doc = new Document();
  doc.add(new TextField("id", "1", Store.YES));

  Field[] fields = strategy.createIndexableFields(area, 0.025);
  for (Field field : fields) {
    doc.add(field);
  }
  addDocument(doc);

  Point upperleft = ctx.getShapeFactory().pointXY(-122.88, 48.54);
  Point lowerright = ctx.getShapeFactory().pointXY(-122.82, 48.62);

  Query query = strategy.makeQuery(new SpatialArgs(SpatialOperation.Intersects, ctx.getShapeFactory().rect(upperleft, lowerright)));
  commit();

  TopDocs search = indexSearcher.search(query, 10);
  ScoreDoc[] scoreDocs = search.scoreDocs;
  for (ScoreDoc scoreDoc : scoreDocs) {
    System.out.println(indexSearcher.doc(scoreDoc.doc));
  }

  assertEquals(1, search.totalHits.value);
}
 
Example 7
Source Project: lucene-solr   Source File: SpatialExample.java    License: Apache License 2.0 5 votes vote down vote up
protected void init() {
  //Typical geospatial context
  //  These can also be constructed from SpatialContextFactory
  this.ctx = SpatialContext.GEO;

  int maxLevels = 11;//results in sub-meter precision for geohash
  //TODO demo lookup by detail distance
  //  This can also be constructed from SpatialPrefixTreeFactory
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);

  this.strategy = new RecursivePrefixTreeStrategy(grid, "myGeoField");

  this.directory = new ByteBuffersDirectory();
}
 
Example 8
public OLuceneSpatialIndexManager(OShapeFactory factory) {
  super();
  this.ctx = SpatialContext.GEO;
  this.factory = factory;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, 11);
  this.strategy = new RecursivePrefixTreeStrategy(grid, "location");
}
 
Example 9
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test29() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_KM));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  Query q1 = sq(strategy.makeQuery(args));
  Query q = parseSq("a.id_gis:\"Intersects(Circle(33.000000,-80.000000 d=10.0km))\"");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example 10
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test30() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_MI));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  Query q1 = sq(strategy.makeQuery(args));
  Query q = parseSq("a.id_gis:\"Intersects(Circle(33.000000,-80.000000 d=10.0m))\"");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example 11
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test45() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_MI));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  Query q1 = sq(bq(bc_m(strategy.makeQuery(args))));
  Query q = parseSq("<+a.id_gis:\"Intersects(Circle(33.000000,-80.000000 d=10.0m))\">");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example 12
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test46() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_MI));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  Query q1 = sq(bq(bc_m(strategy.makeQuery(args)), bc(tq("rowid", "12345"))));
  Query q = parseSq("<+a.id_gis:\"Intersects(Circle(33.000000,-80.000000 d=10.0m))\" rowid:12345>");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example 13
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test47() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_MI));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  Query q1 = sq(strategy.makeQuery(args));
  Query q = parseSq("<a.id_gis:\"Intersects(Circle(33.000000,-80.000000 d=10.0m))\">");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example 14
public AbstractVisitingPrefixTreeQuery(Shape queryShape, String fieldName, SpatialPrefixTree grid,
                                       int detailLevel, int prefixGridScanLevel) {
  super(queryShape, fieldName, grid, detailLevel);
  this.prefixGridScanLevel = Math.max(0, Math.min(prefixGridScanLevel, grid.getMaxLevels() - 1));
  assert detailLevel <= grid.getMaxLevels();
}
 
Example 15
Source Project: lucene-solr   Source File: RecursivePrefixTreeStrategy.java    License: Apache License 2.0 4 votes vote down vote up
public RecursivePrefixTreeStrategy(SpatialPrefixTree grid, String fieldName) {
  super(grid, fieldName);
  prefixGridScanLevel = grid.getMaxLevels() - 4;//TODO this default constant is dependent on the prefix grid size
}
 
Example 16
Source Project: lucene-solr   Source File: AbstractPrefixTreeQuery.java    License: Apache License 2.0 4 votes vote down vote up
public AbstractPrefixTreeQuery(Shape queryShape, String fieldName, SpatialPrefixTree grid, int detailLevel) {
  this.queryShape = queryShape;
  this.fieldName = fieldName;
  this.grid = grid;
  this.detailLevel = detailLevel;
}
 
Example 17
Source Project: lucene-solr   Source File: ContainsPrefixTreeQuery.java    License: Apache License 2.0 4 votes vote down vote up
public ContainsPrefixTreeQuery(Shape queryShape, String fieldName, SpatialPrefixTree grid, int detailLevel, boolean multiOverlappingIndexedShapes) {
  super(queryShape, fieldName, grid, detailLevel);
  this.multiOverlappingIndexedShapes = multiOverlappingIndexedShapes;
}
 
Example 18
public PointPrefixTreeFieldCacheProvider(SpatialPrefixTree grid, String shapeField, int defaultSize) {
  super( shapeField, defaultSize );
  this.grid = grid;
}
 
Example 19
Source Project: lucene-solr   Source File: IntersectsPrefixTreeQuery.java    License: Apache License 2.0 4 votes vote down vote up
public IntersectsPrefixTreeQuery(Shape queryShape, String fieldName,
                                 SpatialPrefixTree grid, int detailLevel,
                                 int prefixGridScanLevel) {
  super(queryShape, fieldName, grid, detailLevel, prefixGridScanLevel);
}
 
Example 20
Source Project: lucene-solr   Source File: TermQueryPrefixTreeStrategy.java    License: Apache License 2.0 4 votes vote down vote up
public TermQueryPrefixTreeStrategy(SpatialPrefixTree grid, String fieldName) {
  super(grid, fieldName);
}
 
Example 21
Source Project: lucene-solr   Source File: PrefixTreeStrategy.java    License: Apache License 2.0 4 votes vote down vote up
public PrefixTreeStrategy(SpatialPrefixTree grid, String fieldName) {
  super(grid.getSpatialContext(), fieldName);
  this.grid = grid;
}
 
Example 22
Source Project: lucene-solr   Source File: PrefixTreeStrategy.java    License: Apache License 2.0 4 votes vote down vote up
public SpatialPrefixTree getGrid() {
  return grid;
}
 
Example 23
Source Project: lucene-solr   Source File: IntersectsRPTVerifyQuery.java    License: Apache License 2.0 4 votes vote down vote up
public IntersectsRPTVerifyQuery(Shape queryShape, String fieldName, SpatialPrefixTree grid, int detailLevel,
                                int prefixGridScanLevel, ShapeValuesPredicate predicateValueSource) {
  this.predicateValueSource = predicateValueSource;
  this.intersectsDiffQuery = new IntersectsDifferentiatingQuery(queryShape, fieldName, grid, detailLevel,
      prefixGridScanLevel);
}
 
Example 24
Source Project: lucene-solr   Source File: IntersectsRPTVerifyQuery.java    License: Apache License 2.0 4 votes vote down vote up
public IntersectsDifferentiatingQuery(Shape queryShape, String fieldName, SpatialPrefixTree grid,
                                      int detailLevel, int prefixGridScanLevel) {
  super(queryShape, fieldName, grid, detailLevel, prefixGridScanLevel);
}
 
Example 25
public RecursivePrefixTreeStrategy(SpatialPrefixTree grid, String fieldName, boolean docValue) {
  super(grid, fieldName, true, docValue);// simplify indexed cells
  prefixGridScanLevel = grid.getMaxLevels() - 4;// TODO this default constant
                                                // is dependent on the prefix
                                                // grid size
}
 
Example 26
public TermQueryPrefixTreeStrategy(SpatialPrefixTree grid, String fieldName, boolean docValue) {
  super(grid, fieldName, false, docValue);// do not simplify indexed cells
}
 
Example 27
Source Project: incubator-retired-blur   Source File: PrefixTreeStrategy.java    License: Apache License 2.0 4 votes vote down vote up
public PrefixTreeStrategy(SpatialPrefixTree grid, String fieldName, boolean simplifyIndexedCells, boolean docValue) {
  super(grid.getSpatialContext(), fieldName);
  this.grid = grid;
  this.simplifyIndexedCells = simplifyIndexedCells;
  this.docValue = docValue;
}
 
Example 28
Source Project: incubator-retired-blur   Source File: PrefixTreeStrategy.java    License: Apache License 2.0 4 votes vote down vote up
public SpatialPrefixTree getGrid() {
  return grid;
}