Search in sources :

Example 11 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 12 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 13 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 14 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 15 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)76 Test (org.junit.Test)17 Point (org.locationtech.spatial4j.shape.Point)15 ArrayList (java.util.ArrayList)14 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)13 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)12 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)9 StoredField (org.apache.lucene.document.StoredField)7 LineStringBuilder (org.elasticsearch.common.geo.builders.LineStringBuilder)7 Rectangle (org.locationtech.spatial4j.shape.Rectangle)7 UnitNRShape (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape)6 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)6 StringField (org.apache.lucene.document.StringField)5 IndexableField (org.apache.lucene.index.IndexableField)5 TextField (org.apache.lucene.document.TextField)4 UnsupportedSpatialOperation (org.apache.lucene.spatial.query.UnsupportedSpatialOperation)4 LineString (com.vividsolutions.jts.geom.LineString)3