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 jena by apache.
the class SpatialIndexLucene method doc.
private Document doc(String entityURI, Shape... shapes) {
Document doc = new Document();
Field entField = new Field(docDef.getEntityField(), entityURI, ftIRI);
doc.add(entField);
for (Shape shape : shapes) {
for (IndexableField f : strategy.createIndexableFields(shape)) {
doc.add(f);
}
}
return doc;
}
use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.
the class QueryRunnable method normalizeDistances.
protected void normalizeDistances(Filter query, Map<String, Result> results) {
String wkt = extractQueryWkt(query);
if (StringUtils.isNotBlank(wkt)) {
Shape queryShape;
try {
queryShape = getShape(wkt);
} catch (ParseException e) {
LOGGER.debug("Unable to parse query WKT to calculate distance", e);
return;
}
for (Map.Entry<String, Result> entry : results.entrySet()) {
Result result = entry.getValue();
if (result.getMetacard() != null && StringUtils.isNotBlank(result.getMetacard().getLocation())) {
try {
Shape locationShape = getShape(result.getMetacard().getLocation());
double distance = DistanceUtils.degrees2Dist(SPATIAL_CONTEXT.calcDistance(locationShape.getCenter(), queryShape.getCenter()), DistanceUtils.EARTH_MEAN_RADIUS_KM) * METERS_IN_KILOMETERS;
ResultImpl updatedResult = new ResultImpl(new MetacardImpl(result.getMetacard()));
updatedResult.setDistanceInMeters(distance);
results.put(entry.getKey(), updatedResult);
} catch (ParseException e) {
LOGGER.debug("Unable to parse metacard WKT to calculate distance", e);
}
}
}
}
}
Aggregations