Search in sources :

Example 36 with Shape

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;
        }
    };
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Random(java.util.Random) Point(org.locationtech.spatial4j.shape.Point)

Example 37 with 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;
}
Also used : Field(org.apache.lucene.document.Field) Shape(org.locationtech.spatial4j.shape.Shape) Document(org.apache.lucene.document.Document)

Example 38 with Shape

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()]);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Config(org.apache.lucene.benchmark.byTask.utils.Config) ArrayList(java.util.ArrayList) Properties(java.util.Properties)

Example 39 with Shape

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;
}
Also used : Field(org.apache.lucene.document.Field) Shape(org.locationtech.spatial4j.shape.Shape) Document(org.apache.lucene.document.Document)

Example 40 with Shape

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);
                }
            }
        }
    }
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) ResultImpl(ddf.catalog.data.impl.ResultImpl) ParseException(java.text.ParseException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result)

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