private SpatialRelation getSpatialRelation(Geoshape other) { Preconditions.checkNotNull(other); return shape.relate(other.shape); }
@Override public boolean equals(Object other) { if (this==other) return true; else if (other==null) return false; else if (!getClass().isInstance(other)) return false; Geoshape oth = (Geoshape)other; return shape.equals(oth.shape); }
/** * Returns the singleton point of this shape. Only applicable for point and circle shapes. * * @return */ public Point getPoint() { Preconditions.checkArgument(getType()==Type.POINT || getType()==Type.CIRCLE,"Shape does not have a single point"); return new Point(shape.getCenter().getY(), shape.getCenter().getX()); }
case MULTILINESTRING: case MULTIPOLYGON: Coordinate coordinate = ((JtsGeometry) shape).getGeom().getCoordinates()[position]; return new Geoshape.Point(coordinate.y, coordinate.x); case POINT: 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 GEOMETRYCOLLECTION: return ((ShapeCollection<Shape>) shape).getShapes().stream() .flatMap(internShape -> IntStream.range(0, size(internShape)) .mapToObj(i -> new AbstractMap.SimpleImmutableEntry<>(internShape, i)))
remove(store, "doc1", ImmutableMultimap.of(TIME, 1001), false); add(store, "doc1", ImmutableMultimap.of(TIME, 1005, WEIGHT, 11.1, LOCATION, Geoshape.point(-48.0, 0.0), BOUNDARY, Geoshape.circle(-48.0, 0.0, 1.0)), false); final Geoshape multiPoint = Geoshape.geoshape(Geoshape.getShapeFactory().multiPoint().pointXY(60.0, 60.0).pointXY(120.0, 60.0).build()); add(store, "doc5", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiPoint, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400)), true); final Geoshape multiLine = Geoshape.geoshape(Geoshape.getShapeFactory().multiLineString().add(Geoshape.getShapeFactory().lineString().pointXY(59.0, 60.0).pointXY(61.0, 60.0)) .add(Geoshape.getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0)).build()); add(store, "doc6", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiLine, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400)), true); final Geoshape multiPolygon = Geoshape.geoshape(Geoshape.getShapeFactory().multiPolygon() .add(Geoshape.getShapeFactory().polygon().pointXY(59.0, 59.0).pointXY(61.0, 59.0).pointXY(61.0, 61.0).pointXY(59.0, 61.0).pointXY(59.0, 59.0)) .add(Geoshape.getShapeFactory().polygon().pointXY(119.0, 59.0).pointXY(121.0, 59.0).pointXY(121.0, 61.0).pointXY(119.0, 61.0).pointXY(119.0, 59.0)).build()); add(store, "doc7", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiPolygon, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400)), true); final Geoshape geometryCollection = Geoshape.geoshape(Geoshape.getGeometryCollectionBuilder().add(Geoshape.getShapeFactory().pointXY(60.0, 60.0)) .add(Geoshape.getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0).build()).build()); add(store, "doc8", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), geometryCollection, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400)), true);
/** * Constructs a line from list of coordinates * @param coordinates Coordinate (lon,lat) pairs * @return */ public static Geoshape line(List<double[]> coordinates) { Preconditions.checkArgument(coordinates.size() >= 2, "Too few coordinate pairs provided"); final LineStringBuilder builder = getShapeFactory().lineString(); for (double[] coordinate : coordinates) { Preconditions.checkArgument(isValidCoordinate(coordinate[1], coordinate[0]), "Invalid coordinate provided"); builder.pointXY(coordinate[0], coordinate[1]); } return new Geoshape(builder.build()); }
@Override public boolean evaluate(Shape indexedShape, Shape queryShape) { return ! indexedShape.relate(queryShape).intersects(); } };
@Override public boolean evaluate(Shape indexedShape, Shape queryShape) { return indexedShape.getBoundingBox().relate(queryShape).intersects(); } };
public int size(Shape shape) { switch(getType(shape)) { case LINE: case POLYGON: case MULTIPOINT: case MULTILINESTRING: case MULTIPOLYGON: return ((JtsGeometry) shape).getGeom().getCoordinates().length; case POINT: return 1; case CIRCLE: return 1; case BOX: return 2; case GEOMETRYCOLLECTION: return ((ShapeCollection<?>) shape).getShapes().stream().map(s -> (Shape) s).mapToInt(s -> size(s)).sum(); default: throw new IllegalStateException("size() not supported for type: " + getType(shape)); } }
public Geoshape polygon(List<double[]> coordinates) { Preconditions.checkArgument(coordinates.size() >= 4, "Too few coordinate pairs provided"); Preconditions.checkArgument(Arrays.equals(coordinates.get(0), coordinates.get(coordinates.size()-1)), "Polygon is not closed"); final PolygonBuilder builder = this.getContext().getShapeFactory().polygon(); for (double[] coordinate : coordinates) { Preconditions.checkArgument(coordinate.length==2 && Geoshape.isValidCoordinate(coordinate[1], coordinate[0]), "Invalid coordinate provided"); builder.pointXY(coordinate[0], coordinate[1]); } return new Geoshape(builder.build()); }
private org.locationtech.spatial4j.shape.Point getSpatial4jPoint() { return getShapeFactory().pointXY(longitude, latitude); }
/** * Constructs a circle from a given center point and a radius in kilometer * @param latitude * @param longitude * @param radiusInKM * @return */ public static Geoshape circle(final double latitude, final double longitude, final double radiusInKM) { Preconditions.checkArgument(isValidCoordinate(latitude, longitude), "Invalid coordinate provided"); Preconditions.checkArgument(radiusInKM > 0, "Invalid radius provided [%s]", radiusInKM); return new Geoshape(getShapeFactory().circle(longitude, latitude, DistanceUtils.dist2Degrees(radiusInKM, DistanceUtils.EARTH_MEAN_RADIUS_KM))); }
/** * Returns the radius in kilometers of this circle. Only applicable to circle shapes. * @return */ public double getRadius() { Preconditions.checkArgument(getType()==Type.CIRCLE,"This shape is not a circle"); double radiusInDeg = ((Circle) shape).getRadius(); return DistanceUtils.degrees2Dist(radiusInDeg, DistanceUtils.EARTH_MEAN_RADIUS_KM); }
/** * Constructs a new box shape which is identified by its south-west and north-east corner points * @param southWestLatitude * @param southWestLongitude * @param northEastLatitude * @param northEastLongitude * @return */ public static Geoshape box(final double southWestLatitude, final double southWestLongitude, final double northEastLatitude, final double northEastLongitude) { Preconditions.checkArgument(isValidCoordinate(southWestLatitude, southWestLongitude), "Invalid south-west coordinate provided"); Preconditions.checkArgument(isValidCoordinate(northEastLatitude, northEastLongitude), "Invalid north-east coordinate provided"); return new Geoshape(getShapeFactory().rect(southWestLongitude, northEastLongitude, southWestLatitude, northEastLatitude)); }
public static MultiShapeBuilder<Shape> getGeometryCollectionBuilder() { return getShapeFactory().multiShape(Shape.class); }
@Override public boolean evaluate(Shape indexedShape, Shape queryShape) { return indexedShape.relate(queryShape).intersects(); } };
@SuppressWarnings("unchecked") public Geoshape.Type getType(Shape shape) { final Geoshape.Type type; if (JtsGeometry.class.isAssignableFrom(shape.getClass()) && "LineString".equals(((JtsGeometry) shape).getGeom().getGeometryType())) { type = Geoshape.Type.LINE; } else if (JtsGeometry.class.isAssignableFrom(shape.getClass())) { try { type = Geoshape.Type.fromGson((((JtsGeometry) shape).getGeom().getGeometryType())); } catch (IllegalArgumentException e) { throw new IllegalStateException("Unrecognized shape type"); type = Geoshape.Type.LINE; } else if (ShapeCollection.class.isAssignableFrom(shape.getClass())) { final Set<Geoshape.Type> types = ((ShapeCollection<? extends Shape>) shape).getShapes().stream() .map(this::getType) .collect(Collectors.toSet());
/** * Constructs a point from its latitude and longitude information * @param latitude * @param longitude * @return */ public static Geoshape point(final double latitude, final double longitude) { Preconditions.checkArgument(isValidCoordinate(latitude, longitude), "Invalid coordinate provided"); return new Geoshape(getShapeFactory().pointXY(longitude, latitude)); }