Search in sources :

Example 31 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 32 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)

Example 33 with Shape

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

the class DateRangeField method parseSpatialArgs.

@Override
protected SpatialArgs parseSpatialArgs(QParser parser, String externalVal) {
    //We avoid SpatialArgsParser entirely because it isn't very Solr-friendly
    final Shape shape = parseShape(externalVal);
    final SolrParams localParams = parser.getLocalParams();
    SpatialOperation op = SpatialOperation.Intersects;
    if (localParams != null) {
        String opStr = localParams.get(OP_PARAM);
        if (opStr != null)
            op = SpatialOperation.get(opStr);
    }
    return new SpatialArgs(op, shape);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) NRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.NRShape) Shape(org.locationtech.spatial4j.shape.Shape) UnitNRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape) SolrParams(org.apache.solr.common.params.SolrParams) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation)

Example 34 with Shape

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

the class SpatialRPTFieldTypeTest method testShapeToFromStringGeoJSON.

public void testShapeToFromStringGeoJSON() throws Exception {
    setupRPTField("miles", "true", "GeoJSON", random().nextBoolean() ? new SpatialRecursivePrefixTreeFieldType() : new RptWithGeometrySpatialField());
    AbstractSpatialFieldType ftype = (AbstractSpatialFieldType) h.getCore().getLatestSchema().getField("geo").getType();
    String json = "{\"type\":\"Point\",\"coordinates\":[1,2]}";
    Shape shape = ftype.parseShape(json);
    String out = ftype.shapeToString(shape);
    assertEquals(json, out);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape)

Example 35 with Shape

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

the class TestGeoJSONResponseWriter method testTransformToAllFormats.

@Test
public void testTransformToAllFormats() throws Exception {
    String wkt = "POINT( 1 2 )";
    SupportedFormats fmts = SpatialContext.GEO.getFormats();
    Shape shape = fmts.read(wkt);
    String[] check = new String[] { "srpt_geohash", "srpt_geohash", "srpt_quad", "srpt_packedquad", "srptgeom" };
    String[] checkFormats = new String[] { "GeoJSON", "WKT", "POLY" };
    for (String field : check) {
        // Add a document with the given field
        assertU(adoc("id", "test", field, wkt));
        assertU(commit());
        for (String fmt : checkFormats) {
            String json = h.query(req("q", "id:test", "wt", "json", "indent", "true", "fl", "xxx:[geo f=" + field + " w=" + fmt + "]"));
            Map<String, Object> doc = readFirstDoc(json);
            Object v = doc.get("xxx");
            String expect = fmts.getWriter(fmt).toString(shape);
            if (!(v instanceof String)) {
                v = normalizeMapToJSON(v.toString());
                expect = normalizeMapToJSON(expect);
            }
            assertEquals("Bad result: " + field + "/" + fmt, expect, v.toString());
        }
    }
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) SupportedFormats(org.locationtech.spatial4j.io.SupportedFormats) Test(org.junit.Test)

Aggregations

Shape (org.locationtech.spatial4j.shape.Shape)81 Test (org.junit.Test)18 Point (org.locationtech.spatial4j.shape.Point)16 ArrayList (java.util.ArrayList)14 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)13 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)13 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)8 Rectangle (org.locationtech.spatial4j.shape.Rectangle)8 StoredField (org.apache.lucene.document.StoredField)7 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)7 LineStringBuilder (org.elasticsearch.common.geo.builders.LineStringBuilder)7 UnitNRShape (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape)6 StringField (org.apache.lucene.document.StringField)5 TextField (org.apache.lucene.document.TextField)4 IndexableField (org.apache.lucene.index.IndexableField)4 UnsupportedSpatialOperation (org.apache.lucene.spatial.query.UnsupportedSpatialOperation)4 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)4