use of org.locationtech.spatial4j.shape.Shape in project lucene-solr by apache.
the class SpatialDocMaker method makeShapeConverter.
/**
* Optionally converts points to circles, and optionally bbox'es result.
*/
public static ShapeConverter makeShapeConverter(final SpatialStrategy spatialStrategy, Config config, String configKeyPrefix) {
//by default does no conversion
final double radiusDegrees = config.get(configKeyPrefix + "radiusDegrees", 0.0);
final double plusMinus = config.get(configKeyPrefix + "radiusDegreesRandPlusMinus", 0.0);
final boolean bbox = config.get(configKeyPrefix + "bbox", false);
return new ShapeConverter() {
@Override
public Shape convert(Shape shape) {
if (shape instanceof Point && (radiusDegrees != 0.0 || plusMinus != 0.0)) {
Point point = (Point) shape;
double radius = radiusDegrees;
if (plusMinus > 0.0) {
//use hashCode so it's reproducibly random
Random random = new Random(point.hashCode());
radius += random.nextDouble() * 2 * plusMinus - plusMinus;
//can happen if configured plusMinus > radiusDegrees
radius = Math.abs(radius);
}
shape = spatialStrategy.getSpatialContext().makeCircle(point, radius);
}
if (bbox)
shape = shape.getBoundingBox();
return shape;
}
};
}
use of org.locationtech.spatial4j.shape.Shape in project lucene-solr by apache.
the class SpatialDocMaker method makeDocument.
@Override
public Document makeDocument() throws Exception {
DocState docState = getDocState();
Document doc = super.makeDocument();
// Set SPATIAL_FIELD from body
DocData docData = docState.docData;
// makeDocument() resets docState.getBody() so we can't look there; look in Document
String shapeStr = doc.getField(DocMaker.BODY_FIELD).stringValue();
Shape shape = makeShapeFromString(strategy, docData.getName(), shapeStr);
if (shape != null) {
shape = shapeConverter.convert(shape);
//index
for (Field f : strategy.createIndexableFields(shape)) {
doc.add(f);
}
}
return doc;
}
use of org.locationtech.spatial4j.shape.Shape in project lucene-solr by apache.
the class SpatialFileQueryMaker method prepareQueries.
@Override
protected Query[] prepareQueries() throws Exception {
final int maxQueries = config.get("query.file.maxQueries", 1000);
Config srcConfig = new Config(new Properties());
srcConfig.set("docs.file", config.get("query.file", null));
srcConfig.set("line.parser", config.get("query.file.line.parser", null));
srcConfig.set("content.source.forever", "false");
List<Query> queries = new ArrayList<>();
LineDocSource src = new LineDocSource();
try {
src.setConfig(srcConfig);
src.resetInputs();
DocData docData = new DocData();
for (int i = 0; i < maxQueries; i++) {
docData = src.getNextDocData(docData);
Shape shape = SpatialDocMaker.makeShapeFromString(strategy, docData.getName(), docData.getBody());
if (shape != null) {
shape = shapeConverter.convert(shape);
queries.add(makeQueryFromShape(shape));
} else {
//skip
i--;
}
}
} catch (NoMoreDataException e) {
//all-done
} finally {
src.close();
}
return queries.toArray(new Query[queries.size()]);
}
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