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()
private SpatialRelation getSpatialRelation(Geoshape other) { Preconditions.checkNotNull(other); return shape.relate(other.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()); }
@Override public SpatialRelation relate(Shape other) { // ** NOTE ** the overall order of logic is kept consistent here with simple.PointImpl. if (isEmpty() || other.isEmpty()) return SpatialRelation.DISJOINT; if (other instanceof org.locationtech.spatial4j.shape.Point) return this.equals(other) ? SpatialRelation.INTERSECTS : SpatialRelation.DISJOINT; return other.relate(this).transpose(); }
@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 a ValueSource with values ranging from 1 to 0, depending inversely * on the distance from {@link #makeDistanceValueSource(org.locationtech.spatial4j.shape.Point,double)}. * The formula is {@code c/(d + c)} where 'd' is the distance and 'c' is * one tenth the distance to the farthest edge from the center. Thus the * scores will be 1 for indexed points at the center of the query shape and as * low as ~0.1 at its furthest edges. */ public final DoubleValuesSource makeRecipDistanceValueSource(Shape queryShape) { Rectangle bbox = queryShape.getBoundingBox(); double diagonalDist = ctx.getDistCalc().distance( ctx.makePoint(bbox.getMinX(), bbox.getMinY()), bbox.getMaxX(), bbox.getMaxY()); double distToEdge = diagonalDist * 0.5; float c = (float)distToEdge * 0.1f;//one tenth DoubleValuesSource distance = makeDistanceValueSource(queryShape.getCenter(), 1.0); return new ReciprocalDoubleValuesSource(c, distance); }
@Override public OSpatialQueryContext build(Map<String, Object> query) throws Exception { Shape shape = parseShape(query); SpatialStrategy strategy = manager.strategy(); Number distance = (Number) query.get("distance"); if (distance != null) { shape = shape.getBuffered(distance.doubleValue(), factory.context()); } if (isOnlyBB(strategy)) { shape = shape.getBoundingBox(); } SpatialArgs args1 = new SpatialArgs(SpatialOperation.Intersects, shape); Query filterQuery = strategy.makeQuery(args1); BooleanQuery q = new BooleanQuery.Builder().add(filterQuery, BooleanClause.Occur.MUST) .add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD).build(); return new OSpatialQueryContext(null, manager.searcher(), q); }
@Override public ShapeCollection getBuffered(double distance, SpatialContext ctx) { List<Shape> bufColl = new ArrayList<Shape>(size()); for (Shape shape : shapes) { bufColl.add(shape.getBuffered(distance, ctx)); } return ctx.makeCollection(bufColl); }
/** * Returns a buffered representation (TRIP_BUFFER_METERS) of a GTFS trip shape from shapes.txt for the given tripId, * or null if a shape doesn't exist for the given tripId. * <p> * * @param tripId the GTFS trip_id to retrieve a buffered trip shape for * @return a buffered representation (TRIP_BUFFER_METERS) of a GTFS trip shape from shapes.txt for the given tripId, * or null if a shape doesn't exist for the given trip. */ public Shape getBufferedTripShape(String tripId) { if (mTripShapes == null) { // No shapes at all return null; } Shape s = mTripShapes.get(tripId); if (s == null) { // No shape for this trip_id return null; } // Create the buffered version of the trip shape if it doesn't yet exist return mTripShapesBuffered.computeIfAbsent(tripId, k -> s.getBuffered(TRIP_BUFFER_DEGREES, s.getContext())); }
int totalArea = 0; for (Shape shape : shapes) { totalArea += shape.getArea(); }
Shape s = new Rectangle(); // Works fine Rectangle r = (Rectangle)s; // Works fine Rectangle r2 = s; // Needs typecasting, Compile fail Rectangle[] rar = new Rectangle[1]; // Fine, array of length 1 s.setS1(3.0); // Shape doesn't know about setS1(), compile fail s.computeArea(); // Works fine, though abstract method, known that an solid class has to implement rar[0].computeArea(); // Run time NullPointerException, rar[0] not initialized r.s1 = 4.5; // s1 is private, compile error r.setS1(5.0); // works fine r.setS2(3.0); // works fine s.getArea(); // works fine System.out.println(r.computeArea()); // Can't print void method, compile error r = null; // Works fine rar[1] = new Rectangle(); // Runtime ArrayIndexOutOfBoundsException, your array size is 1 System.out.println(Rectangle.name); // Works fine
Shape shape = new Circle(scan.nextDouble()); // Use variable of type shape here .. // this code is the same for all shapes double boundaryLength = shape.getBoundaryLength(length); double area = shape.getArea(length); System.out.println("Boundary Length = " + Math.round(boundaryLength)); System.out.println("Area = " + Math.round(area));
@Override public boolean isEmpty() { return s.isEmpty(); }
@Override public void serialize(Shape value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { StringWriter str = new StringWriter(); value.getContext().getFormats().getWktWriter().write(str, value); gen.writeString(str.toString()); } }
@Override public boolean evaluate(Shape indexedShape, Shape queryShape) { return indexedShape.equals(queryShape); } };
/** * Parses the BUFFER operation applied to a parsed shape. * * <pre> * '(' shape ',' number ')' * </pre> * * Whereas 'number' is the distance to buffer the shape by. */ protected Shape parseBufferShape(State state) throws ParseException { state.nextExpect('('); Shape shape = shape(state); state.nextExpect(','); double distance = shapeFactory.normDist(state.nextDouble()); state.nextExpect(')'); return shape.getBuffered(distance, ctx); }
public static void main(final String[] args){ for(final Shape shape : Shape.values()){ System.out.printf("Shape: %s, area: %1.2f\n", shape, shape.getArea()); } }