org.locationtech.spatial4j.shape.SpatialRelation Java Examples

The following examples show how to use org.locationtech.spatial4j.shape.SpatialRelation. 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 File: NumberRangePrefixTree.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public SpatialRelation relate(SpanUnitsNRShape ext) {
  //This logic somewhat mirrors RectangleImpl.relate_range()
  int extMin_intMax = comparePrefix(ext.getMinUnit(), getMaxUnit());
  if (extMin_intMax > 0)
    return SpatialRelation.DISJOINT;
  int extMax_intMin = comparePrefix(ext.getMaxUnit(), getMinUnit());
  if (extMax_intMin < 0)
    return SpatialRelation.DISJOINT;
  int extMin_intMin = comparePrefix(ext.getMinUnit(), getMinUnit());
  int extMax_intMax = comparePrefix(ext.getMaxUnit(), getMaxUnit());
  if ((extMin_intMin > 0 || extMin_intMin == 0 && ext.getMinUnit().getLevel() >= getMinUnit().getLevel())
      && (extMax_intMax < 0 || extMax_intMax == 0 && ext.getMaxUnit().getLevel() >= getMaxUnit().getLevel()))
    return SpatialRelation.CONTAINS;
  if ((extMin_intMin < 0 || extMin_intMin == 0 && ext.getMinUnit().getLevel() <= getMinUnit().getLevel())
      && (extMax_intMax > 0 || extMax_intMax == 0 && ext.getMaxUnit().getLevel() <= getMaxUnit().getLevel()))
    return SpatialRelation.WITHIN;
  return SpatialRelation.INTERSECTS;
}
 
Example #2
Source File: RandomSpatialOpFuzzyPrefixTreeTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private SpatialRelation relateApprox(Shape other) {
  if (biasContainsThenWithin) {
    if (shape1.relate(other) == CONTAINS || shape1.equals(other)
        || shape2.relate(other) == CONTAINS || shape2.equals(other)) return CONTAINS;

    if (shape1.relate(other) == WITHIN && shape2.relate(other) == WITHIN) return WITHIN;

  } else {
    if ((shape1.relate(other) == WITHIN || shape1.equals(other))
        && (shape2.relate(other) == WITHIN || shape2.equals(other))) return WITHIN;

    if (shape1.relate(other) == CONTAINS || shape2.relate(other) == CONTAINS) return CONTAINS;
  }

  if (shape1.relate(other).intersects() || shape2.relate(other).intersects())
    return INTERSECTS;//might actually be 'CONTAINS' if the pair are adjacent but we handle that later
  return DISJOINT;
}
 
Example #3
Source File: FilterCellIterator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasNext() {
  thisCell = null;
  if (nextCell != null)//calling hasNext twice in a row
    return true;
  while (baseIter.hasNext()) {
    nextCell = baseIter.next();
    if (shapeFilter == null) {
      return true;
    } else {
      SpatialRelation rel = nextCell.getShape().relate(shapeFilter);
      if (rel.intersects()) {
        nextCell.setShapeRel(rel);
        if (rel == SpatialRelation.WITHIN)
          nextCell.setLeaf();
        return true;
      }
    }
  }
  return false;
}
 
Example #4
Source File: Geo3dShape.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SpatialRelation relate(Shape other) {
  int relationship;
  if (other instanceof Geo3dShape<?>) {
    relationship = relate((Geo3dShape<?>) other);
  } else if (other instanceof Rectangle) {
    relationship = relate((Rectangle) other);
  } else if (other instanceof Point) {
    relationship = relate((Point) other);
  } else {
    throw new RuntimeException("Unimplemented shape relationship determination: " + other.getClass());
  }

  switch (relationship) {
    case GeoArea.DISJOINT:
      return SpatialRelation.DISJOINT;
    case GeoArea.OVERLAPS:
      return (other instanceof Point ? SpatialRelation.CONTAINS : SpatialRelation.INTERSECTS);
    case GeoArea.CONTAINS:
      return (other instanceof Point ? SpatialRelation.CONTAINS : SpatialRelation.WITHIN);
    case GeoArea.WITHIN:
      return SpatialRelation.CONTAINS;
  }

  throw new RuntimeException("Undetermined shape relationship: " + relationship);
}
 
Example #5
Source File: HeatmapFacetCounterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void validateHeatmapResult(Rectangle inputRange, int facetLevel, HeatmapFacetCounter.Heatmap heatmap)
    throws IOException {
  final Rectangle heatRect = heatmap.region;
  assertTrue(heatRect.relate(inputRange) == SpatialRelation.CONTAINS || heatRect.equals(inputRange));
  final double cellWidth = heatRect.getWidth() / heatmap.columns;
  final double cellHeight = heatRect.getHeight() / heatmap.rows;
  for (int c = 0; c < heatmap.columns; c++) {
    for (int r = 0; r < heatmap.rows; r++) {
      final int facetCount = heatmap.getCount(c, r);
      double x = DistanceUtils.normLonDEG(heatRect.getMinX() + c * cellWidth + cellWidth / 2);
      double y = DistanceUtils.normLatDEG(heatRect.getMinY() + r * cellHeight + cellHeight / 2);
      Point pt =  shapeFactory.pointXY(x, y);
      assertEquals(countMatchingDocsAtLevel(pt, facetLevel), facetCount);
    }
  }
}
 
Example #6
Source File: DateRangePrefixTreeTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testShapeRelations() throws ParseException {
  //note: left range is 264000 at the thousand year level whereas right value is exact year
  assertEquals(SpatialRelation.WITHIN,
      tree.parseShape("[-264000 TO -264000-11-20]").relate(tree.parseShape("-264000")));

  Shape shapeA = tree.parseShape("[3122-01-23 TO 3122-11-27]");
  Shape shapeB = tree.parseShape("[3122-08 TO 3122-11]");
  assertEquals(SpatialRelation.INTERSECTS, shapeA.relate(shapeB));

  shapeA = tree.parseShape("3122");
  shapeB = tree.parseShape("[* TO 3122-10-31]");
  assertEquals(SpatialRelation.INTERSECTS, shapeA.relate(shapeB));

  shapeA = tree.parseShape("[3122-05-28 TO 3122-06-29]");
  shapeB = tree.parseShape("[3122 TO 3122-04]");
  assertEquals(SpatialRelation.DISJOINT, shapeA.relate(shapeB));
}
 
Example #7
Source File: PackedQuadPrefixTree.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void checkBattenbergNotRobustly(byte quad, double cx, double cy, int level, List<Cell> matches,
                               long term, Shape shape, int maxLevel) {
  // short-circuit if we find a match for the point (no need to continue recursion)
  if (shape instanceof Point && !matches.isEmpty())
    return;
  double w = levelW[level] / 2;
  double h = levelH[level] / 2;

  SpatialRelation v = shape.relate(ctx.getShapeFactory().rect(cx - w, cx + w, cy - h, cy + h));

  if (SpatialRelation.DISJOINT == v) {
    return;
  }

  // set bits for next level
  term |= (((long)(quad))<<(64-(++level<<1)));
  // increment level
  term = ((term>>>1)+1)<<1;

  if (SpatialRelation.CONTAINS == v || (level >= maxLevel)) {
    matches.add(new PackedQuadCell(term, v.transpose()));
  } else {// SpatialRelation.WITHIN, SpatialRelation.INTERSECTS
    buildNotRobustly(cx, cy, level, matches, term, shape, maxLevel);
  }
}
 
Example #8
Source File: NumberRangePrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasNext() {
  thisCell = null;
  if (nextCell != null)//calling hasNext twice in a row
    return true;

  if (cellNumber >= iterLastCellNumber)
    return false;

  resetCellWithCellNum(cellNumber < iterFirstCellNumber ? iterFirstCellNumber : cellNumber + 1);

  boolean hasChildren =
      (cellNumber == iterFirstCellNumber && iterFirstIsIntersects)
          || (cellNumber == iterLastCellNumber && iterLastIsIntersects);

  if (!hasChildren) {
    setLeaf();
    setShapeRel(SpatialRelation.WITHIN);
  } else if (iterFirstCellNumber == iterLastCellNumber) {
    setShapeRel(SpatialRelation.CONTAINS);
  } else {
    setShapeRel(SpatialRelation.INTERSECTS);
  }

  nextCell = this;
  return true;
}
 
Example #9
Source File: NumberRangePrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SpatialRelation relate(SpanUnitsNRShape spanShape) {
  assertDecoded();
  int startCmp = comparePrefix(spanShape.getMinUnit(), this);
  if (startCmp > 0) {//start comes after this cell
    return SpatialRelation.DISJOINT;
  }
  int endCmp = comparePrefix(spanShape.getMaxUnit(), this);
  if (endCmp < 0) {//end comes before this cell
    return SpatialRelation.DISJOINT;
  }
  int nrMinLevel = spanShape.getMinUnit().getLevel();
  int nrMaxLevel = spanShape.getMaxUnit().getLevel();
  if ((startCmp < 0 || startCmp == 0 && nrMinLevel <= getLevel())
      && (endCmp > 0 || endCmp == 0 && nrMaxLevel <= getLevel()))
    return SpatialRelation.WITHIN;//or equals
  //At this point it's Contains or Within.
  if (startCmp != 0 || endCmp != 0)
    return SpatialRelation.INTERSECTS;
  //if min or max Level is less, it might be on the equivalent edge.
  for (;nrMinLevel < getLevel(); nrMinLevel++) {
    if (getValAtLevel(nrMinLevel + 1) != 0)
      return SpatialRelation.INTERSECTS;
  }
  for (;nrMaxLevel < getLevel(); nrMaxLevel++) {
    if (getValAtLevel(nrMaxLevel + 1) != getNumSubCells(getShapeAtLevel(nrMaxLevel)) - 1)
      return SpatialRelation.INTERSECTS;
  }
  return SpatialRelation.CONTAINS;
}
 
Example #10
Source File: NumberRangePrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SpatialRelation relate(Shape shape) {
  assertDecoded();
  if (shape == iterFilter && cellShapeRel != null)
    return cellShapeRel;
  if (shape instanceof UnitNRShape)
    return relate((UnitNRShape)shape);
  if (shape instanceof SpanUnitsNRShape)
    return relate((SpanUnitsNRShape)shape);
  return shape.relate(this).transpose();
}
 
Example #11
Source File: NumberRangePrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SpatialRelation relate(UnitNRShape lv) {
  assertDecoded();
  int cmp = comparePrefix(this, lv);
  if (cmp != 0)
    return SpatialRelation.DISJOINT;
  if (getLevel() > lv.getLevel())
    return SpatialRelation.WITHIN;
  return SpatialRelation.CONTAINS;//or equals
  //no INTERSECTS; that won't happen.
}
 
Example #12
Source File: IntersectsRPTVerifyQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean visitPrefix(Cell cell) throws IOException {
  if (cell.getShapeRel() == SpatialRelation.WITHIN) {
    exactIsEmpty = false;
    collectDocs(exactBuilder);//note: we'll add exact to approx on finish()
    return false;
  } else if (cell.getLevel() == detailLevel) {
    approxIsEmpty = false;
    collectDocs(approxBuilder);
    return false;
  }
  return true;
}
 
Example #13
Source File: IntersectsRPTVerifyQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void visitLeaf(Cell cell) throws IOException {
  if (cell.getShapeRel() == SpatialRelation.WITHIN) {
    exactIsEmpty = false;
    collectDocs(exactBuilder);//note: we'll add exact to approx on finish()
  } else {
    approxIsEmpty = false;
    collectDocs(approxBuilder);
  }
}
 
Example #14
Source File: RandomSpatialOpFuzzyPrefixTreeTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testShapePair() {
  ctx = SpatialContext.GEO;
  setupCtx2D(ctx);

  Shape leftShape = new ShapePair(ctx.makeRectangle(-74, -56, -8, 1), ctx.makeRectangle(-180, 134, -90, 90), true);
  Shape queryShape = ctx.makeRectangle(-180, 180, -90, 90);
  assertEquals(SpatialRelation.WITHIN, leftShape.relate(queryShape));
}
 
Example #15
Source File: RandomSpatialOpFuzzyPrefixTreeTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SpatialRelation relate(Shape other) {
  SpatialRelation r = relateApprox(other);
  if (r == DISJOINT)
    return r;
  if (r == CONTAINS)
    return r;
  if (r == WITHIN && !biasContainsThenWithin)
    return r;

  //See if the correct answer is actually Contains, when the indexed shapes are adjacent,
  // creating a larger shape that contains the input shape.
  boolean pairTouches = shape1.relate(shape2).intersects();
  if (!pairTouches)
    return r;
  //test all 4 corners
  // Note: awkwardly, we use a non-geo context for this because in geo, -180 & +180 are the same place, which means
  //  that "other" might wrap the world horizontally and yet all its corners could be in shape1 (or shape2) even
  //  though shape1 is only adjacent to the dateline. I couldn't think of a better way to handle this.
  Rectangle oRect = (Rectangle)other;
  if (cornerContainsNonGeo(oRect.getMinX(), oRect.getMinY())
      && cornerContainsNonGeo(oRect.getMinX(), oRect.getMaxY())
      && cornerContainsNonGeo(oRect.getMaxX(), oRect.getMinY())
      && cornerContainsNonGeo(oRect.getMaxX(), oRect.getMaxY()) )
    return CONTAINS;
  return r;
}
 
Example #16
Source File: Geo3dShapeSphereModelRectRelationTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void pointBearingTest(){
  double radius = 136;
  double distance = 135.97;
  double bearing = 188;
  Point p = ctx.getShapeFactory().pointXY(35, 85);
  Circle circle = ctx.getShapeFactory().circle(p, radius);
  Point bPoint = ctx.getDistCalc().pointOnBearing(p, distance, bearing, ctx, (Point) null);

  double d = ctx.getDistCalc().distance(p, bPoint);
  assertEquals(d, distance, 10-8);

  assertEquals(circle.relate(bPoint), SpatialRelation.CONTAINS);
}
 
Example #17
Source File: Geo3dShapeWGS84ModelRectRelationTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void pointBearingTest(){
  double radius = 136;
  double distance = 135.97;
  double bearing = 188;
  Point p = ctx.getShapeFactory().pointXY(35, 85);
  Circle circle = ctx.getShapeFactory().circle(p, radius);
  Point bPoint = ctx.getDistCalc().pointOnBearing(p, distance, bearing, ctx, (Point) null);

  double d = ctx.getDistCalc().distance(p, bPoint);
  assertEquals(d, distance, 10-8);

  assertEquals(circle.relate(bPoint), SpatialRelation.CONTAINS);
}
 
Example #18
Source File: FilterToElasticHelper.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
private void visitGeoShapeBinarySpatialOperator(BinarySpatialOperator filter, Expression e1, Expression e2,
                                                boolean swapped, Object extraData) {

    SpatialRelation shapeRelation;
    if (filter instanceof Disjoint) {
        shapeRelation = SpatialRelation.DISJOINT;
    } else if ((!swapped && filter instanceof Within) || (swapped && filter instanceof Contains)) {
        shapeRelation = SpatialRelation.WITHIN;
    } else if (filter instanceof Intersects || filter instanceof BBOX) {
        shapeRelation = SpatialRelation.INTERSECTS;
    } else {
        FilterToElastic.LOGGER.fine(filter.getClass().getSimpleName() 
                + " is unsupported for geo_shape types");
        shapeRelation = null;
        delegate.fullySupported = false;
    }

    if (shapeRelation != null) {
        e1.accept(delegate, extraData);
        key = (String) delegate.field;
        e2.accept(delegate, extraData);
        shapeBuilder = delegate.currentShapeBuilder;
    }

    if (shapeRelation != null && shapeBuilder != null) {
        delegate.queryBuilder = ImmutableMap.of("bool", ImmutableMap.of("must", MATCH_ALL, "filter",
                ImmutableMap.of("geo_shape", ImmutableMap.of(key,
                        ImmutableMap.of("shape", shapeBuilder, "relation", shapeRelation)))));
    } else {
        delegate.queryBuilder = MATCH_ALL;
    }
}
 
Example #19
Source File: WithinFunction.java    From crate with Apache License 2.0 5 votes vote down vote up
public Boolean evaluate(Input leftInput, Input rightInput) {
    Object left = leftInput.value();
    if (left == null) {
        return null;
    }
    Object right = rightInput.value();
    if (right == null) {
        return null;
    }
    return parseLeftShape(left).relate(parseRightShape(right)) == SpatialRelation.WITHIN;
}
 
Example #20
Source File: NumberRangePrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
    public SpatialRelation relate(Shape shape) {
//      if (shape instanceof UnitNRShape)
//        return relate((UnitNRShape)shape);
      if (shape instanceof SpanUnitsNRShape)
        return relate((SpanUnitsNRShape) shape);
      return shape.relate(this).transpose();//probably a UnitNRShape
    }
 
Example #21
Source File: AbstractVisitingPrefixTreeQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * The cell is either indexed as a leaf or is the last level of detail. It
 * might not even intersect the query shape, so be sure to check for that.
 * The default implementation will check that and if passes then call
 * {@link #visitLeaf(org.apache.lucene.spatial.prefix.tree.Cell)} or
 * {@link #visitPrefix(org.apache.lucene.spatial.prefix.tree.Cell)}.
 */
protected void visitScanned(Cell cell) throws IOException {
  final SpatialRelation relate = cell.getShape().relate(queryShape);
  if (relate.intersects()) {
    cell.setShapeRel(relate);//just being pedantic
    if (cell.isLeaf()) {
      visitLeaf(cell);
    } else {
      visitPrefix(cell);
    }
  }
}
 
Example #22
Source File: PackedQuadPrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasNext() {
  if (nextCell != null) {
    return true;
  }
  SpatialRelation rel;
  // loop until we're at the end of the quad tree or we hit a relation
  while (thisCell != null) {
    rel = thisCell.getShape().relate(shape);
    if (rel == SpatialRelation.DISJOINT) {
      thisCell = thisCell.nextCell(false);
    } else { // within || intersects || contains
      thisCell.setShapeRel(rel);
      nextCell = thisCell;
      if (rel == SpatialRelation.WITHIN) {
        thisCell.setLeaf();
        thisCell = thisCell.nextCell(false);
      } else {  // intersects || contains
        level = (short) (thisCell.getLevel());
        if (level == detailLevel || pruned(rel)) {
          thisCell.setLeaf();
          if (shape instanceof Point) {
            thisCell.setShapeRel(SpatialRelation.WITHIN);
            thisCell = null;
          } else {
            thisCell = thisCell.nextCell(false);
          }
          break;
        }
        thisCell = thisCell.nextCell(true);
      }
      break;
    }
  }
  return nextCell != null;
}
 
Example #23
Source File: PackedQuadPrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean pruned(SpatialRelation rel) {
  int leaves;
  if (rel == SpatialRelation.INTERSECTS && leafyPrune && level == detailLevel - 1) {
    for (leaves=0, pruneIter=thisCell.getNextLevelCells(shape); pruneIter.hasNext(); pruneIter.next(), ++leaves);
    return leaves == 4;
  }
  return false;
}
 
Example #24
Source File: Geo3dRectangleShape.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public SpatialRelation relateYRange(double minY, double maxY) {
  Rectangle r = spatialcontext.getShapeFactory().rect(-180, 180, minY, maxY);
  return relate(r);
}
 
Example #25
Source File: DefaultSpatialAlgebra.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean sfDisjoint(Shape s1, Shape s2) {
	return SpatialRelation.DISJOINT == s1.relate(s2);
}
 
Example #26
Source File: DefaultSpatialAlgebra.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean sfIntersects(Shape s1, Shape s2) {
	return SpatialRelation.INTERSECTS == s1.relate(s2);
}
 
Example #27
Source File: DefaultSpatialAlgebra.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean ehDisjoint(Shape s1, Shape s2) {
	return SpatialRelation.DISJOINT == s1.relate(s2);
}
 
Example #28
Source File: DefaultSpatialAlgebra.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean ehCovers(Shape s1, Shape s2) {
	return SpatialRelation.CONTAINS == s1.relate(s2);
}
 
Example #29
Source File: DefaultSpatialAlgebra.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean ehCoveredBy(Shape s1, Shape s2) {
	return SpatialRelation.WITHIN == s1.relate(s2);
}
 
Example #30
Source File: LegacyCell.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public SpatialRelation getShapeRel() {
  return shapeRel;
}