Search in sources :

Example 76 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class GeoNamesQueryLuceneDirectoryIndex method getCountryCode.

@Override
public Optional<String> getCountryCode(String wktLocation, int radius) throws GeoEntryQueryException, ParseException {
    final Directory directory = openDirectoryAndCheckForIndex();
    Shape shape = getShape(wktLocation);
    String countryCode = doGetCountryCode(shape, radius, directory);
    return Optional.ofNullable(countryCode);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 77 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class GeoNamesWebService method createPointFromWkt.

Point createPointFromWkt(String wkt) {
    try {
        JtsSpatialContextFactory contextFactory = new JtsSpatialContextFactory();
        contextFactory.allowMultiOverlap = true;
        SpatialContext spatialContext = contextFactory.newSpatialContext();
        Shape shape = (Shape) spatialContext.readShapeFromWkt(wkt);
        Point center = shape.getCenter();
        return center;
    } catch (java.text.ParseException parseException) {
        LOGGER.debug(parseException.getMessage(), parseException);
    }
    return null;
}
Also used : SpatialContext(org.locationtech.spatial4j.context.SpatialContext) Shape(org.locationtech.spatial4j.shape.Shape) Point(org.locationtech.spatial4j.shape.Point) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory)

Example 78 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class TestGeoNamesQueryLuceneIndex method testDoGetCountryCodeNullShape.

@Test(expected = IllegalArgumentException.class)
public void testDoGetCountryCodeNullShape() throws GeoEntryQueryException {
    Shape shape = null;
    directoryIndex.doGetCountryCode(shape, 10, null);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Test(org.junit.Test)

Example 79 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class TestGeoNamesQueryLuceneIndex method testDoGetCountryCodeNullLuceneDirectory.

@Test(expected = IllegalArgumentException.class)
public void testDoGetCountryCodeNullLuceneDirectory() throws GeoEntryQueryException {
    Shape shape = mock(Shape.class);
    Directory directory = null;
    directoryIndex.doGetCountryCode(shape, 10, directory);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 80 with Shape

use of org.locationtech.spatial4j.shape.Shape in project janusgraph by JanusGraph.

the class GeoshapeHelper method getPoint.

@SuppressWarnings("unchecked")
public Geoshape.Point getPoint(Geoshape geoshape, int position) {
    final Shape shape = geoshape.getShape();
    final Point p;
    if (position < 0 || position >= size(shape))
        throw new ArrayIndexOutOfBoundsException("Invalid position: " + position);
    switch(getType(shape)) {
        case POINT:
        case CIRCLE:
            return geoshape.getPoint();
        case BOX:
            if (position == 0)
                return new Geoshape.Point(shape.getBoundingBox().getMinY(), shape.getBoundingBox().getMinX());
            else
                return new Geoshape.Point(shape.getBoundingBox().getMaxY(), shape.getBoundingBox().getMaxX());
        case LINE:
            p = ((BufferedLineString) shape).getPoints().get(position);
            break;
        case MULTIPOINT:
            p = ((ShapeCollection<Point>) shape).getShapes().get(position);
            break;
        case MULTILINESTRING:
            p = ((ShapeCollection<BufferedLineString>) shape).getShapes().stream().flatMap(line -> line.getPoints().stream()).skip(position).findFirst().orElse(null);
            break;
        case GEOMETRYCOLLECTION:
            return ((ShapeCollection<Shape>) shape).getShapes().stream().flatMap(internShape -> IntStream.range(0, size(internShape)).mapToObj(i -> new AbstractMap.SimpleImmutableEntry<>(internShape, i))).skip(position).findFirst().map(entry -> getPoint(new Geoshape(entry.getKey()), entry.getValue())).orElse(null);
        default:
            throw new IllegalStateException("getPoint(int) not supported for type: " + getType(shape));
    }
    if (p == null)
        throw new ArrayIndexOutOfBoundsException("Invalid position: " + position);
    return new Geoshape.Point(p.getY(), p.getX());
}
Also used : WKTWriter(org.locationtech.spatial4j.io.WKTWriter) IntStream(java.util.stream.IntStream) DataInputStream(java.io.DataInputStream) Circle(org.locationtech.spatial4j.shape.Circle) GeoJSONReader(org.locationtech.spatial4j.io.GeoJSONReader) Set(java.util.Set) IOException(java.io.IOException) BinaryCodec(org.locationtech.spatial4j.io.BinaryCodec) Shape(org.locationtech.spatial4j.shape.Shape) Collectors(java.util.stream.Collectors) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) Point(org.locationtech.spatial4j.shape.Point) AbstractMap(java.util.AbstractMap) List(java.util.List) Rectangle(org.locationtech.spatial4j.shape.Rectangle) DataOutputStream(java.io.DataOutputStream) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) WKTReader(org.locationtech.spatial4j.io.WKTReader) GeoJSONWriter(org.locationtech.spatial4j.io.GeoJSONWriter) ShapeCollection(org.locationtech.spatial4j.shape.ShapeCollection) BufferedLineString(org.locationtech.spatial4j.shape.impl.BufferedLineString) ShapeCollection(org.locationtech.spatial4j.shape.ShapeCollection) Shape(org.locationtech.spatial4j.shape.Shape) Point(org.locationtech.spatial4j.shape.Point) BufferedLineString(org.locationtech.spatial4j.shape.impl.BufferedLineString)

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