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);
}
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);
}
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);
}
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);
}
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());
}
}
}
Aggregations