Search in sources :

Example 21 with Shape

use of org.locationtech.spatial4j.shape.Shape in project lucene-solr by apache.

the class BBoxStrategy method makeQuery.

//---------------------------------
// Query Building
//---------------------------------
//  Utility on SpatialStrategy?
//  public Query makeQueryWithValueSource(SpatialArgs args, ValueSource valueSource) {
//    return new CustomScoreQuery(makeQuery(args), new FunctionQuery(valueSource));
//or...
//  return new BooleanQuery.Builder()
//      .add(new FunctionQuery(valueSource), BooleanClause.Occur.MUST)//matches everything and provides score
//      .add(filterQuery, BooleanClause.Occur.FILTER)//filters (score isn't used)
//  .build();
//  }
@Override
public Query makeQuery(SpatialArgs args) {
    Shape shape = args.getShape();
    if (!(shape instanceof Rectangle))
        throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);
    Rectangle bbox = (Rectangle) shape;
    Query spatial;
    // Useful for understanding Relations:
    // http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm
    SpatialOperation op = args.getOperation();
    if (op == SpatialOperation.BBoxIntersects)
        spatial = makeIntersects(bbox);
    else if (op == SpatialOperation.BBoxWithin)
        spatial = makeWithin(bbox);
    else if (op == SpatialOperation.Contains)
        spatial = makeContains(bbox);
    else if (op == SpatialOperation.Intersects)
        spatial = makeIntersects(bbox);
    else if (op == SpatialOperation.IsEqualTo)
        spatial = makeEquals(bbox);
    else if (op == SpatialOperation.IsDisjointTo)
        spatial = makeDisjoint(bbox);
    else if (op == SpatialOperation.IsWithin)
        spatial = makeWithin(bbox);
    else {
        //no Overlaps support yet
        throw new UnsupportedSpatialOperation(op);
    }
    return new ConstantScoreQuery(spatial);
}
Also used : UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Rectangle(org.locationtech.spatial4j.shape.Rectangle) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation)

Example 22 with Shape

use of org.locationtech.spatial4j.shape.Shape in project lucene-solr by apache.

the class GeoJSONWriter method writeGeo.

protected void writeGeo(Object geo) throws IOException {
    Shape shape = null;
    String str = null;
    if (geo instanceof Shape) {
        shape = (Shape) geo;
    } else if (geo instanceof IndexableField) {
        str = ((IndexableField) geo).stringValue();
    } else if (geo instanceof WriteableGeoJSON) {
        shape = ((WriteableGeoJSON) geo).shape;
    } else {
        str = geo.toString();
    }
    if (str != null) {
        // Assume it is well formed JSON
        if (str.startsWith("{") && str.endsWith("}")) {
            writer.write(str);
            return;
        }
        if (formats == null) {
            // *stored* values for the field look like JSON until we actually try to read them
            throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON unable to write field: '&" + GeoJSONResponseWriter.FIELD + "=" + geofield + "' (" + str + ")");
        }
        shape = formats.read(str);
    }
    if (geowriter == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON unable to write field: '&" + GeoJSONResponseWriter.FIELD + "=" + geofield + "'");
    }
    if (shape != null) {
        geowriter.write(writer, shape);
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) WriteableGeoJSON(org.apache.solr.response.transform.WriteableGeoJSON) Shape(org.locationtech.spatial4j.shape.Shape) SolrException(org.apache.solr.common.SolrException)

Example 23 with Shape

use of org.locationtech.spatial4j.shape.Shape in project lucene-solr by apache.

the class RecursivePrefixTreeStrategy method makeQuery.

@Override
public Query makeQuery(SpatialArgs args) {
    final SpatialOperation op = args.getOperation();
    Shape shape = args.getShape();
    int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct));
    if (op == SpatialOperation.Intersects) {
        if (isGridAlignedShape(args.getShape())) {
            return makeGridShapeIntersectsQuery(args.getShape());
        }
        return new IntersectsPrefixTreeQuery(shape, getFieldName(), grid, detailLevel, prefixGridScanLevel);
    } else if (op == SpatialOperation.IsWithin) {
        return new WithinPrefixTreeQuery(shape, getFieldName(), grid, detailLevel, prefixGridScanLevel, //-1 flag is slower but ensures correct results
        -1);
    } else if (op == SpatialOperation.Contains) {
        return new ContainsPrefixTreeQuery(shape, getFieldName(), grid, detailLevel, multiOverlappingIndexedShapes);
    }
    throw new UnsupportedSpatialOperation(op);
}
Also used : UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) Shape(org.locationtech.spatial4j.shape.Shape) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation)

Example 24 with Shape

use of org.locationtech.spatial4j.shape.Shape in project lucene-solr by apache.

the class BBoxStrategy method makeQuery.

//---------------------------------
// Query Building
//---------------------------------
//  Utility on SpatialStrategy?
//  public Query makeQueryWithValueSource(SpatialArgs args, ValueSource valueSource) {
//    return new CustomScoreQuery(makeQuery(args), new FunctionQuery(valueSource));
//or...
//  return new BooleanQuery.Builder()
//      .add(new FunctionQuery(valueSource), BooleanClause.Occur.MUST)//matches everything and provides score
//      .add(filterQuery, BooleanClause.Occur.FILTER)//filters (score isn't used)
//  .build();
//  }
@Override
public Query makeQuery(SpatialArgs args) {
    Shape shape = args.getShape();
    if (!(shape instanceof Rectangle))
        throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);
    Rectangle bbox = (Rectangle) shape;
    Query spatial;
    // Useful for understanding Relations:
    // http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm
    SpatialOperation op = args.getOperation();
    if (op == SpatialOperation.BBoxIntersects)
        spatial = makeIntersects(bbox);
    else if (op == SpatialOperation.BBoxWithin)
        spatial = makeWithin(bbox);
    else if (op == SpatialOperation.Contains)
        spatial = makeContains(bbox);
    else if (op == SpatialOperation.Intersects)
        spatial = makeIntersects(bbox);
    else if (op == SpatialOperation.IsEqualTo)
        spatial = makeEquals(bbox);
    else if (op == SpatialOperation.IsDisjointTo)
        spatial = makeDisjoint(bbox);
    else if (op == SpatialOperation.IsWithin)
        spatial = makeWithin(bbox);
    else {
        //no Overlaps support yet
        throw new UnsupportedSpatialOperation(op);
    }
    return new ConstantScoreQuery(spatial);
}
Also used : UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Rectangle(org.locationtech.spatial4j.shape.Rectangle) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation)

Example 25 with Shape

use of org.locationtech.spatial4j.shape.Shape in project lucene-solr by apache.

the class AbstractSpatialFieldType method createSpatialQuery.

//--------------------------------------------------------------
// Query Support
//--------------------------------------------------------------
/**
   * Implemented for compatibility with geofilt & bbox query parsers:
   * {@link SpatialQueryable}.
   */
@Override
public Query createSpatialQuery(QParser parser, SpatialOptions options) {
    Point pt = SpatialUtils.parsePointSolrException(options.pointStr, ctx);
    double distDeg = DistanceUtils.dist2Degrees(options.distance, options.radius);
    Shape shape = ctx.makeCircle(pt, distDeg);
    if (options.bbox)
        shape = shape.getBoundingBox();
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, shape);
    return getQueryFromSpatialArgs(parser, options.field, spatialArgs);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Shape(org.locationtech.spatial4j.shape.Shape) Point(org.locationtech.spatial4j.shape.Point)

Aggregations

Shape (org.locationtech.spatial4j.shape.Shape)76 Test (org.junit.Test)17 Point (org.locationtech.spatial4j.shape.Point)15 ArrayList (java.util.ArrayList)14 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)13 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)12 PolygonBuilder (org.elasticsearch.common.geo.builders.PolygonBuilder)11 Document (org.apache.lucene.document.Document)10 Field (org.apache.lucene.document.Field)9 Query (org.apache.lucene.search.Query)9 StoredField (org.apache.lucene.document.StoredField)7 LineStringBuilder (org.elasticsearch.common.geo.builders.LineStringBuilder)7 Rectangle (org.locationtech.spatial4j.shape.Rectangle)7 UnitNRShape (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape)6 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)6 StringField (org.apache.lucene.document.StringField)5 IndexableField (org.apache.lucene.index.IndexableField)5 TextField (org.apache.lucene.document.TextField)4 UnsupportedSpatialOperation (org.apache.lucene.spatial.query.UnsupportedSpatialOperation)4 LineString (com.vividsolutions.jts.geom.LineString)3