Java Code Examples for org.locationtech.spatial4j.shape.Rectangle#getMinY()

The following examples show how to use org.locationtech.spatial4j.shape.Rectangle#getMinY() . 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: QuadPrefixTree.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public QuadPrefixTree(
    SpatialContext ctx, Rectangle bounds, int maxLevels) {
  super(ctx, maxLevels);
  this.xmin = bounds.getMinX();
  this.xmax = bounds.getMaxX();
  this.ymin = bounds.getMinY();
  this.ymax = bounds.getMaxY();

  levelW = new double[maxLevels + 1];
  levelH = new double[maxLevels + 1];

  gridW = xmax - xmin;
  gridH = ymax - ymin;
  this.xmid = xmin + gridW/2.0;
  this.ymid = ymin + gridH/2.0;
  levelW[0] = gridW/2.0;
  levelH[0] = gridH/2.0;

  for (int i = 1; i < levelW.length; i++) {
    levelW[i] = levelW[i - 1] / 2.0;
    levelH[i] = levelH[i - 1] / 2.0;
  }
}
 
Example 2
Source File: SpatialArgs.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Computes the distance given a shape and the {@code distErrPct}.  The
 * algorithm is the fraction of the distance from the center of the query
 * shape to its closest bounding box corner.
 *
 * @param shape Mandatory.
 * @param distErrPct 0 to 0.5
 * @param ctx Mandatory
 * @return A distance (in degrees).
 */
public static double calcDistanceFromErrPct(Shape shape, double distErrPct, SpatialContext ctx) {
  if (distErrPct < 0 || distErrPct > 0.5) {
    throw new IllegalArgumentException("distErrPct " + distErrPct + " must be between [0 to 0.5]");
  }
  if (distErrPct == 0 || shape instanceof Point) {
    return 0;
  }
  Rectangle bbox = shape.getBoundingBox();
  //Compute the distance from the center to a corner.  Because the distance
  // to a bottom corner vs a top corner can vary in a geospatial scenario,
  // take the closest one (greater precision).
  Point ctr = bbox.getCenter();
  double y = (ctr.getY() >= 0 ? bbox.getMaxY() : bbox.getMinY());
  double diagonalDist = ctx.getDistCalc().distance(ctr, bbox.getMaxX(), y);
  return diagonalDist * distErrPct;
}
 
Example 3
Source File: Geo3dShapeFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void verifyY(double y) {
  Rectangle bounds = this.context.getWorldBounds();
  if (y < bounds.getMinY() || y > bounds.getMaxY()) {
    throw new InvalidShapeException("Bad Y value " + y + " is not in boundary " + bounds);
  }
}
 
Example 4
Source File: LatLonType.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Query createSpatialQuery(QParser parser, SpatialOptions options) {
  Point point = SpatialUtils.parsePointSolrException(options.pointStr, SpatialContext.GEO);

  // lat & lon in degrees
  double latCenter = point.getY();
  double lonCenter = point.getX();
  
  double distDeg = DistanceUtils.dist2Degrees(options.distance, options.radius);
  Rectangle bbox = DistanceUtils.calcBoxByDistFromPtDEG(latCenter, lonCenter, distDeg, SpatialContext.GEO, null);
  double latMin = bbox.getMinY();
  double latMax = bbox.getMaxY();
  double lonMin, lonMax, lon2Min, lon2Max;
  if (bbox.getCrossesDateLine()) {
     lonMin = -180;
     lonMax = bbox.getMaxX();
     lon2Min = bbox.getMinX();
     lon2Max = 180;
  } else {
     lonMin = bbox.getMinX();
     lonMax = bbox.getMaxX();
     lon2Min = -180;
     lon2Max = 180;
  }
  
  IndexSchema schema = parser.getReq().getSchema();
  
  // Now that we've figured out the ranges, build them!
  SchemaField latSF = subField(options.field, LAT, schema);
  SchemaField lonSF = subField(options.field, LON, schema);

  SpatialDistanceQuery spatial = new SpatialDistanceQuery();


  if (options.bbox) {
    BooleanQuery.Builder result = new BooleanQuery.Builder();

    Query latRange = latSF.getType().getRangeQuery(parser, latSF,
              String.valueOf(latMin),
              String.valueOf(latMax),
              true, true);
    result.add(latRange, BooleanClause.Occur.MUST);

    if (lonMin != -180 || lonMax != 180) {
      Query lonRange = lonSF.getType().getRangeQuery(parser, lonSF,
              String.valueOf(lonMin),
              String.valueOf(lonMax),
              true, true);
      if (lon2Min != -180 || lon2Max != 180) {
        // another valid longitude range
        BooleanQuery.Builder bothLons = new BooleanQuery.Builder();
        bothLons.add(lonRange, BooleanClause.Occur.SHOULD);

        lonRange = lonSF.getType().getRangeQuery(parser, lonSF,
              String.valueOf(lon2Min),
              String.valueOf(lon2Max),
              true, true);
        bothLons.add(lonRange, BooleanClause.Occur.SHOULD);

        lonRange = bothLons.build();
      }

      result.add(lonRange, BooleanClause.Occur.MUST);
    }

    spatial.bboxQuery = result.build();
  }


  spatial.origField = options.field.getName();
  spatial.latSource = latSF.getType().getValueSource(latSF, parser);
  spatial.lonSource = lonSF.getType().getValueSource(lonSF, parser);
  spatial.latMin = latMin;
  spatial.latMax = latMax;
  spatial.lonMin = lonMin;
  spatial.lonMax = lonMax;
  spatial.lon2Min = lon2Min;
  spatial.lon2Max = lon2Max;
  spatial.lon2 = lon2Min != -180 || lon2Max != 180;

  spatial.latCenter = latCenter;
  spatial.lonCenter = lonCenter;
  spatial.dist = options.distance;
  spatial.planetRadius = options.radius;

  spatial.calcDist = !options.bbox;

  return spatial;
}
 
Example 5
Source File: TestSolr4Spatial.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void checkHits(String fieldName, boolean exact, String ptStr, double distKM, double sphereRadius, int count, int ... docIds) throws ParseException {
  if (exact && isBBoxField(fieldName)) {
    return; // bbox field only supports rectangular query
  }
  String [] tests = new String[docIds != null && docIds.length > 0 ? docIds.length + 1 : 1];
  //test for presence of required ids first
  int i = 0;
  if (docIds != null && docIds.length > 0) {
    for (int docId : docIds) {
      tests[i++] = "//result/doc/*[@name='id'][.='" + docId + "']";
    }
  }
  //check total length last; maybe response includes ids it shouldn't.  Nicer to check this last instead of first so
  // that there may be a more specific detailed id to investigate.
  tests[i++] = "*[count(//doc)=" + count + "]";

  //Test using the Lucene spatial syntax
  {
    //never actually need the score but lets test
    String score = randomScoreMode();

    double distDEG = DistanceUtils.dist2Degrees(distKM, DistanceUtils.EARTH_MEAN_RADIUS_KM);
    Point point = SpatialUtils.parsePoint(ptStr, SpatialContext.GEO);
    String circleStr = "BUFFER(POINT(" + point.getX()+" "+point.getY()+")," + distDEG + ")";
    String shapeStr;
    if (exact) {
      shapeStr = circleStr;
    } else {//bbox
      //the GEO is an assumption
      SpatialContext ctx = SpatialContext.GEO;
      Rectangle bbox = ctx.readShapeFromWkt(circleStr).getBoundingBox();
      shapeStr = "ENVELOPE(" + bbox.getMinX() + ", " + bbox.getMaxX() +
          ", " + bbox.getMaxY() + ", " + bbox.getMinY() + ")";
    }

    //FYI default distErrPct=0.025 works with the tests in this file
    assertQ(req(
          "fl", "id", "q","*:*", "rows", "1000",
          "fq", "{!field f=" + fieldName + (score==null?"":" score="+score)
            + "}Intersects(" + shapeStr + ")"),
        tests);
  }
  //Test using geofilt
  {
    assertQ(req(
        "fl", "id", "q", "*:*", "rows", "1000",
        "fq", "{!" + (exact ? "geofilt" : "bbox") + " sfield=" + fieldName + " pt='" + ptStr + "' d=" + distKM + " sphere_radius=" + sphereRadius + "}"),
        tests);
  }

}