/** * Computes the distance between this and another <code>Envelope</code>. The distance between * overlapping Envelopes is 0. Otherwise, the distance is the Euclidean distance between the * closest points. */ @Override public double distance(final Envelope env) { if (intersects(env)) { return 0; } double dx = 0.0; if (getMaxX() < env.getMinX()) { dx = env.getMinX() - getMaxX(); } else if (getMinX() > env.getMaxX()) { dx = getMinX() - env.getMaxX(); } double dy = 0.0; if (getMaxY() < env.getMinY()) { dy = env.getMinY() - getMaxY(); } else if (getMinY() > env.getMaxY()) { dy = getMinY() - env.getMaxY(); } double dz = 0.0; double otherMinZ = getMinZOf(env); double otherMaxZ = getMaxZOf(env); if (maxz < otherMinZ) { dz = otherMinZ - maxz; } else if (minz > otherMaxZ) { dz = minz - otherMaxZ; } // if either is zero, the envelopes overlap either vertically or // horizontally if (dx == 0.0 && dz == 0.0) { return dy; } if (dy == 0.0 && dz == 0.0) { return dx; } if (dx == 0.0 && dy == 0.0) { return dz; } return Math.sqrt(dx * dx + dy * dy + dz * dz); }
/** * Creates a {@link Geometry} with the same extent as the given envelope. The Geometry returned is * guaranteed to be valid. To provide this behaviour, the following cases occur: * * <p>If the <code>Envelope</code> is: * * <ul> * <li>null : returns an empty {@link Point} * <li>a point : returns a non-empty {@link Point} * <li>a line : returns a two-point {@link LineString} * <li>a rectangle : returns a {@link Polygon}> whose points are (minx, miny), (minx, maxy), * (maxx, maxy), (maxx, miny), (minx, miny). * </ul> * * @param envelope the <code>Envelope</code> to convert * @return an empty <code>Point</code> (for null <code>Envelope</code>s), a <code>Point</code> * (when min x = max x and min y = max y) or a <code>Polygon</code> (in all other cases) */ public Geometry toGeometry(Envelope envelope) { // null envelope - return empty point geometry if (envelope.isNull()) { return createPoint((CoordinateSequence) null); } // point? if (envelope.getMinX() == envelope.getMaxX() && envelope.getMinY() == envelope.getMaxY()) { return createPoint(new Coordinate(envelope.getMinX(), envelope.getMinY())); } // vertical or horizontal line? if (envelope.getMinX() == envelope.getMaxX() || envelope.getMinY() == envelope.getMaxY()) { return createLineString( new Coordinate[] { new Coordinate(envelope.getMinX(), envelope.getMinY()), new Coordinate(envelope.getMaxX(), envelope.getMaxY()) }); } // create a CW ring for the polygon return createPolygon( createLinearRing( new Coordinate[] { new Coordinate(envelope.getMinX(), envelope.getMinY()), new Coordinate(envelope.getMinX(), envelope.getMaxY()), new Coordinate(envelope.getMaxX(), envelope.getMaxY()), new Coordinate(envelope.getMaxX(), envelope.getMinY()), new Coordinate(envelope.getMinX(), envelope.getMinY()) }), null); }
/** * Computes the intersection of two {@link Envelope}s. * * @param env the envelope to intersect with * @return a new Envelope representing the intersection of the envelopes (this will be the null * envelope if either argument is null, or they do not intersect */ @Override public Envelope3D intersection(final Envelope env) { if (isNull() || env.isNull() || !intersects(env)) { return new Envelope3D(); } Envelope xyInt = super.intersection(env); double otherMinZ = getMinZOf(env); double intMinZ = minz > otherMinZ ? minz : otherMinZ; double otherMaxZ = getMaxZOf(env); double intMaxZ = maxz < otherMaxZ ? maxz : otherMaxZ; return new Envelope3D( xyInt.getMinX(), xyInt.getMaxX(), xyInt.getMinY(), xyInt.getMaxY(), intMinZ, intMaxZ); }
/** * Computes the list of envelopes (from 2 to 4, possibily 0 if env covers this) resulting from the * extrusion of env from this. Only in 2D for the moment. Does not return null envelopes. */ public List<Envelope> extrusion(final Envelope env) { List<Envelope> list = new ArrayList(); double x1 = getMinX(); double x2 = getMaxX(); double y1 = getMinY(); double y2 = getMaxY(); double xx1 = env.getMinX(); double xx2 = env.getMaxX(); double yy1 = env.getMinY(); double yy2 = env.getMaxY(); if (x2 >= x1 && yy1 >= y1) { list.add(new Envelope(x1, x2, y1, yy1)); } if (xx1 >= x1 && y2 >= yy1) { list.add(new Envelope(x1, xx1, yy1, y2)); } if (x2 >= xx1 && y2 >= yy2) { list.add(new Envelope(xx1, x2, yy2, y2)); } if (x2 >= xx2 && yy2 >= yy1) { list.add(new Envelope(xx2, x2, yy1, yy2)); } return list; }