Java Code Examples for org.locationtech.spatial4j.shape.SpatialRelation#WITHIN

The following examples show how to use org.locationtech.spatial4j.shape.SpatialRelation#WITHIN . 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: 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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
Source File: SpatialOperation.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public boolean evaluate(Shape indexedShape, Shape queryShape) {
  Rectangle bbox = indexedShape.getBoundingBox();
  return bbox.relate(queryShape) == SpatialRelation.WITHIN || bbox.equals(queryShape);
}
 
Example 13
Source File: SpatialOperation.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public boolean evaluate(Shape indexedShape, Shape queryShape) {
  return indexedShape.relate(queryShape) == SpatialRelation.WITHIN || indexedShape.equals(queryShape);
}