Search in sources :

Example 1 with BufferedLineString

use of org.locationtech.spatial4j.shape.impl.BufferedLineString 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

DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)1 SpatialContextFactory (org.locationtech.spatial4j.context.SpatialContextFactory)1 BinaryCodec (org.locationtech.spatial4j.io.BinaryCodec)1 GeoJSONReader (org.locationtech.spatial4j.io.GeoJSONReader)1 GeoJSONWriter (org.locationtech.spatial4j.io.GeoJSONWriter)1 WKTReader (org.locationtech.spatial4j.io.WKTReader)1 WKTWriter (org.locationtech.spatial4j.io.WKTWriter)1 Circle (org.locationtech.spatial4j.shape.Circle)1 Point (org.locationtech.spatial4j.shape.Point)1 Rectangle (org.locationtech.spatial4j.shape.Rectangle)1 Shape (org.locationtech.spatial4j.shape.Shape)1 ShapeCollection (org.locationtech.spatial4j.shape.ShapeCollection)1