/** * Returns the CrsId of the spatial extent * * @return the CrsId of the spatial extent */ public CoordinateReferenceSystem<?> getCoordinateReferenceSystem() { return extent.getCoordinateReferenceSystem(); }
private Envelope<P> mkEmpty() { return new Envelope<P>(Double.NaN, Double.NaN, Double.NaN, Double.NaN, getCoordinateReferenceSystem()); }
public String toString() { StringBuilder builder = new StringBuilder(getCoordinateReferenceSystem().getCrsId().toString()); builder.append("LL: ") .append(this.lowerLeft.toString()) .append(" - UR: ") .append(this.upperRight.toString()); return builder.toString(); }
@Override public Point<P> generate() { return new Point(positionWithin(bbox, rnd), bbox.getCoordinateReferenceSystem()); }
/** * Returns the upper-left point of this <code>Envelope</code>. * * @return the upper-left point */ public P upperLeft() { return Positions.mkPosition(getCoordinateReferenceSystem(), lowerLeft.getCoordinate(0), upperRight.getCoordinate(1)); }
/** * Returns the lower-right point of this <code>Envelope</code>. * * @return the lower-right point */ public P lowerRight() { return Positions.mkPosition(getCoordinateReferenceSystem(), upperRight.getCoordinate(0), lowerLeft.getCoordinate(1)); }
/** * Creates an <code>Envelope</code> that is the set-theoretic union of this {@code Envelope} with the specified {@code Envelope} * * @param other other Envelope * @return an <code>Envelope</code> that encompasses both operands. * @throws IllegalArgumentException when the operand <code>Envelope</code>s don't have the same coordinate reference system. */ public Envelope<P> union(Envelope<P> other) { if (other == null || other.isEmpty()) return this; if (!this.getCoordinateReferenceSystem().equals(other.getCoordinateReferenceSystem())) throw new IllegalArgumentException("Envelopes have different CRS."); double minC0 = Math.min(getMinC0(), other.getMinC0()); double minC1 = Math.min(getMinC1(), other.getMinC1()); double maxC0 = Math.max(getMaxC0(), other.getMaxC0()); double maxC1 = Math.max(getMaxC1(), other.getMaxC1()); return new Envelope<P>(minC0, minC1, maxC0, maxC1, this.getCoordinateReferenceSystem()); }
/** * Intersects the specified <code>Envelope</code> with this <code>Envelope</code> and returns the result. * * @param other the Envelope to intersect with this instance * @return the set-theoretic intersection of this Envelope and the specified Envelope. * @throws IllegalArgumentException when the specified <code>Envelope</code> doesn't have the same coordinate reference system as this instance. */ public Envelope<P> intersect(Envelope<P> other) { if (this.isEmpty() || other.isEmpty()) return mkEmpty(); if (!this.getCoordinateReferenceSystem().equals(other.getCoordinateReferenceSystem())) throw new IllegalArgumentException("Envelopes have different CRS."); double minC0 = Math.max(getMinC0(), other.getMinC0()); double minC1 = Math.max(getMinC1(), other.getMinC1()); double maxC0 = Math.min(getMaxC0(), other.getMaxC0()); double maxC1 = Math.min(getMaxC1(), other.getMaxC1()); if (minC0 > maxC0 || minC1 > maxC1) return mkEmpty(); return new Envelope<P>(minC0, minC1, maxC0, maxC1, this.getCoordinateReferenceSystem()); }
public static <P extends Position> P positionWithin(Envelope<P> bbox, Random rnd) { P ll = bbox.lowerLeft(); P ur = bbox.upperRight(); CoordinateReferenceSystem<P> crs = bbox.getCoordinateReferenceSystem(); double[] coords = new double[crs.getCoordinateDimension()]; for (int i = 0; i < coords.length; i++) { coords[i] = ll.getCoordinate(i) + (ur.getCoordinate(i) - ll.getCoordinate(i)) * rnd.nextDouble(); } return Positions.mkPosition(crs, coords); } }
/** * Checks whether this <code>Envelope</code> is contained within the specified <code>Envelope</code> * * @param other the other <code>Envelope</code> * @return true iff this instance is contained within the specified <code>Envelope</code> * @throws IllegalArgumentException when the specified <code>Envelope</code> doesn't have the same coordinate reference system as this instance. */ public boolean within(Envelope<P> other) { if (isEmpty()) return true; if (other.isEmpty()) return false; if (!this.getCoordinateReferenceSystem().equals(other.getCoordinateReferenceSystem())) throw new IllegalArgumentException("Envelopes have different CRS."); return other.getMinC0() <= this.getMinC0() && other.getMaxC0() >= this.getMaxC0() && other.getMinC1() <= this.getMinC1() && other.getMaxC1() >= this.getMaxC1(); }
/** * Checks whether this <code>Envelope</code> contains the specifies <code>Envelope</code>. * * @param p the <code>Point</code> * @return true iff this instance contains the specified <code>Point</code> * @throws IllegalArgumentException when the specified <code>Point</code> doesn't have the same coordinate reference system as this instance. */ public boolean contains(P p) { if (!p.getClass().equals(this.getCoordinateReferenceSystem().getPositionClass())) throw new IllegalArgumentException("Position and envelope of different types"); if (isEmpty()) return false; return getMinC0() <= p.getCoordinate(0) && getMaxC0() >= p.getCoordinate(0) && getMinC1() <= p.getCoordinate(1) && getMaxC1() >= p.getCoordinate(1); }
private Envelope<P> envelopeOf(String mortoncode, int index, Envelope<P> extent) { assert (extent != null); if (index >= mortoncode.length()) { return extent; } char c = mortoncode.charAt(index); double minX = extent.lowerLeft().getCoordinate(0); double minY = extent.lowerLeft().getCoordinate(1); double w = (extent.extentAlongDimension(0)) / 2.0; double h = (extent.extentAlongDimension(1)) / 2.0; CoordinateReferenceSystem<P> crs = extent.getCoordinateReferenceSystem(); switch (c) { case '0': return envelopeOf(mortoncode, ++index, new Envelope<P>(minX, minY, minX + w, minY + h, crs)); case '1': return envelopeOf(mortoncode, ++index, new Envelope<P>(minX, minY + h, minX + w, minY + 2 * h, crs)); case '2': return envelopeOf(mortoncode, ++index, new Envelope<P>(minX + w, minY, minX + 2 * w, minY + h, crs)); case '3': return envelopeOf(mortoncode, ++index, new Envelope<P>(minX + w, minY + h, minX + 2 * w, minY + 2 * h, crs)); default: //this can only be a programming error! throw new IllegalStateException("Received a mortoncode element that is not 0, 1, 2 or 3."); } }