Java Code Examples for org.opengis.geometry.Envelope#getMinimum()

The following examples show how to use org.opengis.geometry.Envelope#getMinimum() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MosaicFormModel.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
public Product getBoundaryProduct() throws FactoryException, TransformException {
    final CoordinateReferenceSystem mapCRS = getTargetCRS();
    if (mapCRS != null) {
        final ReferencedEnvelope envelope = getTargetEnvelope();
        final Envelope mapEnvelope = envelope.transform(mapCRS, true);

        final double pixelSizeX = (Double) getPropertyValue(PROPERTY_PIXEL_SIZE_X);
        final double pixelSizeY = (Double) getPropertyValue(PROPERTY_PIXEL_SIZE_Y);
        final int w = MathUtils.floorInt(mapEnvelope.getSpan(0) / pixelSizeX);
        final int h = MathUtils.floorInt(mapEnvelope.getSpan(1) / pixelSizeY);

        final Product product = new Product("mosaic", "MosaicBounds", w, h);
        final GeoCoding geoCoding = new CrsGeoCoding(mapCRS,
                                                     w, h,
                                                     mapEnvelope.getMinimum(0),
                                                     mapEnvelope.getMaximum(1),
                                                     pixelSizeX, pixelSizeY);
        product.setSceneGeoCoding(geoCoding);

        return product;
    }
    return null;
}
 
Example 2
Source File: RasterUtils.java    From geowave with Apache License 2.0 6 votes vote down vote up
public static ReferencedEnvelope getReferenceEnvelope(
    final GridCoverage gridCoverage,
    final CoordinateReferenceSystem targetCrs) {
  final CoordinateReferenceSystem sourceCrs = gridCoverage.getCoordinateReferenceSystem();
  final Envelope sampleEnvelope = gridCoverage.getEnvelope();

  final ReferencedEnvelope sampleReferencedEnvelope =
      new ReferencedEnvelope(
          new org.locationtech.jts.geom.Envelope(
              sampleEnvelope.getMinimum(0),
              sampleEnvelope.getMaximum(0),
              sampleEnvelope.getMinimum(1),
              sampleEnvelope.getMaximum(1)),
          gridCoverage.getCoordinateReferenceSystem());

  ReferencedEnvelope projectedReferenceEnvelope = sampleReferencedEnvelope;
  if ((targetCrs != null) && !targetCrs.equals(sourceCrs)) {
    try {
      projectedReferenceEnvelope = sampleReferencedEnvelope.transform(targetCrs, true);
    } catch (TransformException | FactoryException e) {
      LOGGER.warn("Unable to transform envelope of grid coverage to " + targetCrs.getName(), e);
    }
  }
  return projectedReferenceEnvelope;
}
 
Example 3
Source File: Envelope2D.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@code true} if {@code this} envelope bounds is equal to {@code that} envelope
 * bounds in two specified dimensions. The coordinate reference system is not compared, since
 * it doesn't need to have the same number of dimensions.
 *
 * @param that  the envelope to compare to.
 * @param xDim  the dimension of {@code that} envelope to compare to the <var>x</var> dimension of {@code this} envelope.
 * @param yDim  the dimension of {@code that} envelope to compare to the <var>y</var> dimension of {@code this} envelope.
 * @param eps   a small tolerance number for floating point number comparisons. This value will be scaled
 *              according this envelope {@linkplain #width width} and {@linkplain #height height}.
 * @return {@code true} if the envelope bounds are the same (up to the specified tolerance
 *         level) in the specified dimensions, or {@code false} otherwise.
 */
public boolean boundsEquals(final Envelope that, final int xDim, final int yDim, double eps) {
    eps *= 0.5*(width + height);
    for (int i=0; i<4; i++) {
        final int dim2D = (i & 1);
        final int dimND = (dim2D == 0) ? xDim : yDim;
        final double value2D, valueND;
        if ((i & 2) == 0) {
            value2D = this.getMinimum(dim2D);
            valueND = that.getMinimum(dimND);
        } else {
            value2D = this.getMaximum(dim2D);
            valueND = that.getMaximum(dimND);
        }
        // Use '!' for catching NaN values.
        if (!(Math.abs(value2D - valueND) <= eps)) {
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: Envelopes.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Formats the given envelope as a {@code POLYGON} element in the <cite>Well Known Text</cite>
 * (WKT) format. {@code POLYGON} can be used as an alternative to {@code BOX} when the element
 * needs to be considered as a standard WKT geometry.
 *
 * <p>The string returned by this method can be {@linkplain GeneralEnvelope#GeneralEnvelope(CharSequence)
 * parsed} by the {@code GeneralEnvelope} constructor.</p>
 *
 * @param  envelope  the envelope to format.
 * @return the envelope as a {@code POLYGON} in WKT format.
 * @throws IllegalArgumentException if the given envelope can not be formatted.
 *
 * @see org.apache.sis.io.wkt
 */
public static String toPolygonWKT(final Envelope envelope) throws IllegalArgumentException {
    /*
     * Get the dimension, ignoring the trailing ones which have infinite values.
     */
    int dimension = envelope.getDimension();
    while (dimension != 0) {
        final double length = envelope.getSpan(dimension - 1);
        if (Double.isFinite(length)) {
            break;
        }
        dimension--;
    }
    if (dimension < 2) {
        throw new IllegalArgumentException(Errors.format(Errors.Keys.EmptyEnvelope2D));
    }
    final StringBuilder buffer = new StringBuilder("POLYGON(");
    String separator = "(";
    for (int corner = 0; corner < CORNERS.length; corner += 2) {
        for (int i=0; i<dimension; i++) {
            final double value;
            switch (i) {
                case  0: // Fall through
                case  1: value = CORNERS[corner+i] ? envelope.getMaximum(i) : envelope.getMinimum(i); break;
                default: value = envelope.getMedian(i); break;
            }
            trimFractionalPart(buffer.append(separator).append(value));
            separator = " ";
        }
        separator = ", ";
    }
    return buffer.append("))").toString();
}
 
Example 5
Source File: ReferencingAssert.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if the given {@code e1} envelope is disjoint with the given {@code e2} envelope.
 * This method will also verify class consistency by invoking the {@code contains} method,
 * and by interchanging the arguments.
 *
 * @param e1  the first envelope to test.
 * @param e2  the second envelope to test.
 */
public static void assertDisjoint(final AbstractEnvelope e1, final Envelope e2) {
    assertFalse("e1.intersects(e2)", e1.intersects(e2, false));
    assertFalse("e1.intersects(e2)", e1.intersects(e2, true));
    assertFalse("e1.contains(e2)",   e1.contains  (e2, false));
    assertFalse("e1.contains(e2)",   e1.contains  (e2, true));
    if (e2 instanceof AbstractEnvelope) {
        final AbstractEnvelope ae = (AbstractEnvelope) e2;
        assertFalse("e2.intersects(e1)", ae.intersects(e1, false));
        assertFalse("e2.intersects(e1)", ae.intersects(e1, true));
        assertFalse("e2.contains(e1)",   ae.contains  (e1, false));
        assertFalse("e2.contains(e1)",   ae.contains  (e1, true));
    }
    final int dimension = e1.getDimension();
    final int numCases = toIntExact(round(pow(3, dimension)));
    final GeneralDirectPosition pos = new GeneralDirectPosition(dimension);
    for (int index=0; index<numCases; index++) {
        int n = index;
        for (int i=0; i<dimension; i++) {
            final double coordinate;
            switch (n % 3) {
                case 0: coordinate = e2.getMinimum(i); break;
                case 1: coordinate = e2.getMedian (i); break;
                case 2: coordinate = e2.getMaximum(i); break;
                default: throw new AssertionError(i);
            }
            pos.setOrdinate(i, coordinate);
            n /= 3;
        }
        assertEquals(0, n); // Opportunist check of this assert method.
        assertFalse("e1.contains(" + pos + ')', e1.contains(pos));
    }
}
 
Example 6
Source File: RasterDataAdapter.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public HadoopWritableSerializer<GridCoverage, GridCoverageWritable> createWritableSerializer() {
  return new HadoopWritableSerializer<GridCoverage, GridCoverageWritable>() {

    @Override
    public GridCoverageWritable toWritable(final GridCoverage entry) {
      final Envelope env = entry.getEnvelope();
      final DataBuffer dataBuffer =
          entry.getRenderedImage().copyData(
              new InternalWritableRaster(
                  sampleModel.createCompatibleSampleModel(tileSize, tileSize),
                  new Point())).getDataBuffer();
      Persistable metadata = null;
      if (entry instanceof GridCoverage2D) {
        final Object metadataObj =
            ((GridCoverage2D) entry).getProperty(TILE_METADATA_PROPERTY_KEY);
        if ((metadataObj != null) && (metadataObj instanceof Persistable)) {
          metadata = (Persistable) metadataObj;
        }
      }
      return new GridCoverageWritable(
          new RasterTile(dataBuffer, metadata),
          env.getMinimum(0),
          env.getMaximum(0),
          env.getMinimum(1),
          env.getMaximum(1),
          env.getCoordinateReferenceSystem());
    }

    @Override
    public GridCoverage fromWritable(final GridCoverageWritable writable) {
      final ReferencedEnvelope mapExtent =
          new ReferencedEnvelope(
              writable.getMinX(),
              writable.getMaxX(),
              writable.getMinY(),
              writable.getMaxY(),
              writable.getCrs());
      try {
        return prepareCoverage(writable.getRasterTile(), tileSize, mapExtent);
      } catch (final IOException e) {
        LOGGER.error("Unable to read raster data", e);
      }
      return null;
    }
  };
}
 
Example 7
Source File: ServicesForMetadata.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Implementation of the public {@code setBounds(…, DefaultGeographicBoundingBox, …)} methods for
 * the horizontal extent. If the {@code crs} argument is null, then it is caller's responsibility
 * to ensure that the given envelope is two-dimensional.
 *
 * <p>If {@code findOpCaller} is non-null, then this method will be executed in optional mode:
 * some failures will cause this method to return {@code null} instead than throwing an exception.
 * Note that {@link TransformException} may still be thrown but not directly by this method.
 * Warning may be logged, but in such case this method presumes that public caller is the named
 * method from {@link Envelopes} — typically {@link Envelopes#findOperation(Envelope, Envelope)}.</p>
 *
 * @param  envelope       the source envelope.
 * @param  target         the target bounding box, or {@code null} for creating it automatically.
 * @param  crs            the envelope CRS, or {@code null} if unknown.
 * @param  normalizedCRS  the horizontal component of the given CRS, or null if the {@code crs} argument is null.
 * @param  findOpCaller   non-null for replacing some (not all) exceptions by {@code null} return value.
 * @return the bounding box or {@code null} on failure. Never {@code null} if {@code findOpCaller} argument is {@code null}.
 * @throws TransformException if the given envelope can not be transformed.
 */
private static DefaultGeographicBoundingBox setGeographicExtent(Envelope envelope, DefaultGeographicBoundingBox target,
        final CoordinateReferenceSystem crs, final GeographicCRS normalizedCRS, final String findOpCaller) throws TransformException
{
    if (normalizedCRS != null) {
        // No need to check for dimension, since GeodeticCRS can not have less than 2.
        final CoordinateSystem cs1 = crs.getCoordinateSystem();
        final CoordinateSystem cs2 = normalizedCRS.getCoordinateSystem();
        if (!Utilities.equalsIgnoreMetadata(cs2.getAxis(0), cs1.getAxis(0)) ||
            !Utilities.equalsIgnoreMetadata(cs2.getAxis(1), cs1.getAxis(1)))
        {
            final CoordinateOperation operation;
            final CoordinateOperationFactory factory = CoordinateOperations.factory();
            try {
                operation = factory.createOperation(crs, normalizedCRS);
            } catch (FactoryException e) {
                if (findOpCaller != null) {
                    // See javadoc for the assumption that optional mode is used by Envelopes.findOperation(…).
                    Logging.recoverableException(Logging.getLogger(Modules.REFERENCING), Envelopes.class, findOpCaller, e);
                    return null;
                }
                throw new TransformException(Resources.format(Resources.Keys.CanNotTransformEnvelopeToGeodetic), e);
            }
            envelope = Envelopes.transform(operation, envelope);
        }
    }
    /*
     * At this point, the envelope should use (longitude, latitude) coordinates in degrees.
     * The envelope may cross the anti-meridian if the envelope implementation is an Apache SIS one.
     * For other implementations, the longitude range may be conservatively expanded to [-180 … 180]°.
     */
    double westBoundLongitude, eastBoundLongitude;
    double southBoundLatitude, northBoundLatitude;
    if (envelope instanceof AbstractEnvelope) {
        final AbstractEnvelope ae = (AbstractEnvelope) envelope;
        westBoundLongitude = ae.getLower(0);
        eastBoundLongitude = ae.getUpper(0);            // Cross anti-meridian if eastBoundLongitude < westBoundLongitude.
        southBoundLatitude = ae.getLower(1);
        northBoundLatitude = ae.getUpper(1);
    } else {
        westBoundLongitude = envelope.getMinimum(0);
        eastBoundLongitude = envelope.getMaximum(0);    // Expanded to [-180 … 180]° if it was crossing the anti-meridian.
        southBoundLatitude = envelope.getMinimum(1);
        northBoundLatitude = envelope.getMaximum(1);
    }
    /*
     * The envelope transformation at the beginning of this method intentionally avoided to apply datum shift.
     * This implies that the prime meridian has not been changed and may be something else than Greenwich.
     * We need to take it in account manually.
     *
     * Note that there is no need to normalize the longitudes back to the [-180 … +180]° range after the rotation, or
     * to verify if the longitude span is 360°. Those verifications will be done automatically by target.setBounds(…).
     */
    if (normalizedCRS != null) {
        final double rotation = CRS.getGreenwichLongitude(normalizedCRS);
        westBoundLongitude += rotation;
        eastBoundLongitude += rotation;
    }
    /*
     * In the particular case where this method is invoked (indirectly) for Envelopes.findOperation(…) purposes,
     * replace NaN values by the whole world.  We do that only for Envelopes.findOperation(…) since we know that
     * the geographic bounding box will be used for choosing a CRS, and a conservative approach is to select the
     * CRS valid in the widest area. If this method is invoked for other usages, then we keep NaN values because
     * we don't know the context (union, intersection, something else?).
     */
    if (findOpCaller != null) {
        if (Double.isNaN(southBoundLatitude)) southBoundLatitude = Latitude.MIN_VALUE;
        if (Double.isNaN(northBoundLatitude)) northBoundLatitude = Latitude.MAX_VALUE;
        if (Double.isNaN(eastBoundLongitude) || Double.isNaN(westBoundLongitude)) {
            // Conservatively set the two bounds because may be spanning the anti-meridian.
            eastBoundLongitude = Longitude.MIN_VALUE;
            westBoundLongitude = Longitude.MAX_VALUE;
        }
    }
    if (target == null) {
        target = new DefaultGeographicBoundingBox();
    }
    target.setBounds(westBoundLongitude, eastBoundLongitude, southBoundLatitude, northBoundLatitude);
    target.setInclusion(Boolean.TRUE);
    return target;
}
 
Example 8
Source File: IntervalRectangle.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs a rectangle initialized to the two first dimensions of the given envelope.
 * If the given envelope crosses the anti-meridian, then the new rectangle will span the
 * full longitude range (i.e. this constructor does not preserve the convention of using
 * negative width for envelopes crossing anti-meridian).
 *
 * <div class="note"><b>Note:</b> this constructor expands envelopes that cross the anti-meridian
 * because the methods defined in this class are not designed for handling such envelopes.
 * If a rectangle with negative width is nevertheless desired for envelope spanning the anti-meridian,
 * one can use the following constructor:
 *
 * {@preformat java
 *     new IntervalRectangle(envelope.getLowerCorner(), envelope.getUpperCorner());
 * }
 * </div>
 *
 * @param envelope  the envelope from which to copy the values.
 */
public IntervalRectangle(final Envelope envelope) {
    xmin = envelope.getMinimum(0);
    xmax = envelope.getMaximum(0);
    ymin = envelope.getMinimum(1);
    ymax = envelope.getMaximum(1);
}