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

The following examples show how to use org.opengis.geometry.Envelope#getCoordinateReferenceSystem() . 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: ArrayEnvelope.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new envelope with the same data than the specified envelope.
 *
 * @param envelope  the envelope to copy.
 */
public ArrayEnvelope(final Envelope envelope) {
    ensureNonNull("envelope", envelope);
    /*
     * Do not optimize with `if (envelope instanceof ArrayEnvelope)` because subclasses may change the semantic.
     * In particular the SubEnvelope subclass uses only a subrange of this array. If we still want to optimize,
     * we would have to test for specific classes. It is probably not worth at this stage.
     */
    crs = envelope.getCoordinateReferenceSystem();
    final int dimension = envelope.getDimension();
    coordinates = new double[dimension * 2];
    final DirectPosition lowerCorner = envelope.getLowerCorner();
    final DirectPosition upperCorner = envelope.getUpperCorner();
    for (int i=0; i<dimension; i++) {
        coordinates[i]           = lowerCorner.getOrdinate(i);
        coordinates[i+dimension] = upperCorner.getOrdinate(i);
    }
    verifyRanges(crs, coordinates);
}
 
Example 2
Source File: ServicesForMetadata.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Sets a geographic bounding box from the specified envelope.
 * If the envelope has no CRS, then (<var>longitude</var>, <var>latitude</var>) axis order is assumed.
 * If the envelope CRS is not geographic, then the envelope will be transformed to a geographic CRS.
 * If {@code findOpCaller} is {@code true}, then some failures will cause this method to return {@code null}
 * instead than throwing an exception, and warning may be logged with assumption that caller is the named
 * method from {@link Envelopes} — typically {@link Envelopes#findOperation(Envelope, Envelope)}.
 *
 * @param  envelope      the source envelope.
 * @param  target        the target bounding box, or {@code null} for creating it automatically.
 * @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.
 */
@Override
public DefaultGeographicBoundingBox setBounds(final Envelope envelope, final DefaultGeographicBoundingBox target,
        final String findOpCaller) throws TransformException
{
    final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
    GeographicCRS normalizedCRS = ReferencingUtilities.toNormalizedGeographicCRS(crs, false, false);
    if (normalizedCRS == null) {
        if (crs != null) {
            normalizedCRS = CommonCRS.defaultGeographic();
        } else if (envelope.getDimension() != 2) {
            if (findOpCaller != null) return null;
            throw new TransformException(dimensionNotFound(Resources.Keys.MissingHorizontalDimension_1, crs));
        }
    }
    return setGeographicExtent(envelope, target, crs, normalizedCRS, findOpCaller);
}
 
Example 3
Source File: Envelopes.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Finds a mathematical operation from the CRS of the given source envelope to the CRS of the given target envelope.
 * For non-null georeferenced envelopes, this method is equivalent to the following code with {@code areaOfInterest}
 * computed as the union of the two envelopes:
 *
 * <blockquote><code>return {@linkplain CRS#findOperation(CoordinateReferenceSystem, CoordinateReferenceSystem,
 * GeographicBoundingBox)} CRS.findOperation(source.getCoordinateReferenceSystem(), target.getCoordinateReferenceSystem(),
 * <var>areaOfInterest</var>)</code></blockquote>
 *
 * If at least one envelope is null or has no CRS, then this method returns {@code null}.
 *
 * @param  source  the source envelope, or {@code null}.
 * @param  target  the target envelope, or {@code null}.
 * @return the mathematical operation from {@code source} CRS to {@code target} CRS,
 *         or {@code null} if at least one argument is null or has no CRS.
 * @throws OperationNotFoundException if no operation was found between the given pair of CRS.
 * @throws FactoryException if the operation can not be created for another reason.
 *
 * @see CRS#findOperation(CoordinateReferenceSystem, CoordinateReferenceSystem, GeographicBoundingBox)
 *
 * @since 1.0
 */
public static CoordinateOperation findOperation(final Envelope source, final Envelope target) throws FactoryException {
    if (source != null && target != null) {
        final CoordinateReferenceSystem sourceCRS, targetCRS;
        if ((sourceCRS = source.getCoordinateReferenceSystem()) != null &&
            (targetCRS = target.getCoordinateReferenceSystem()) != null)
        {
            /*
             * Computing the area of interest (AOI) unconditionally would be harmless, but it is useless if the CRS
             * are the same since the result will be identity anyway. Conversely we could skip AOI computation more
             * often for example with the following condition instead of !=:
             *
             *     !Utilities.deepEquals(sourceCRS, targetCRS, ComparisonMode.ALLOW_VARIANT)
             *
             * but it would not be very advantageous if testing the condition is almost as long as computing the AOI.
             * For now we keep != as a very cheap test which will work quite often.
             */
            DefaultGeographicBoundingBox areaOfInterest = null;
            if (sourceCRS != targetCRS) try {
                final DefaultGeographicBoundingBox targetAOI;
                final ReferencingServices converter = ReferencingServices.getInstance();
                areaOfInterest = converter.setBounds(source, null, "findOperation");                // Should be first.
                targetAOI      = converter.setBounds(target, null, "findOperation");
                if (areaOfInterest == null) {
                    areaOfInterest = targetAOI;
                } else if (targetAOI != null) {
                    areaOfInterest.add(targetAOI);
                }
            } catch (TransformException e) {
                /*
                 * Note: we may succeed to transform 'source' and fail to transform 'target' to geographic bounding box,
                 * but the opposite is unlikely because 'source' should not have less dimensions than 'target'.
                 */
                Logging.recoverableException(Logging.getLogger(Loggers.GEOMETRY), Envelopes.class, "findOperation", e);
            }
            return CRS.findOperation(sourceCRS, targetCRS, areaOfInterest);
        }
    }
    return null;
}
 
Example 4
Source File: ServicesForMetadata.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Sets a vertical extent with the value inferred from the given envelope.
 * Only the vertical coordinates are extracted; all other coordinates are ignored.
 *
 * @param  envelope  the source envelope.
 * @param  target    the target vertical extent where to store envelope information.
 * @throws TransformException if no vertical component can be extracted from the given envelope.
 */
@Override
public void setBounds(final Envelope envelope, final DefaultVerticalExtent target) throws TransformException {
    final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
    final VerticalCRS verticalCRS = CRS.getVerticalComponent(crs, true);
    if (verticalCRS == null && envelope.getDimension() != 1) {
        throw new TransformException(dimensionNotFound(Resources.Keys.MissingVerticalDimension_1, crs));
    }
    setVerticalExtent(envelope, target, crs, verticalCRS);
}
 
Example 5
Source File: ServicesForMetadata.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Sets a temporal extent with the value inferred from the given envelope.
 * Only the vertical coordinates are extracted; all other coordinates are ignored.
 *
 * @param  envelope  the source envelope.
 * @param  target    the target temporal extent where to store envelope information.
 * @throws TransformException if no temporal component can be extracted from the given envelope.
 */
@Override
public void setBounds(final Envelope envelope, final DefaultTemporalExtent target) throws TransformException {
    final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
    final TemporalCRS temporalCRS = CRS.getTemporalComponent(crs);
    if (temporalCRS == null) {                          // Mandatory for the conversion from numbers to dates.
        throw new TransformException(dimensionNotFound(Resources.Keys.MissingTemporalDimension_1, crs));
    }
    setTemporalExtent(envelope, target, crs, temporalCRS);
}
 
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
/**
 * Sets the geographic, vertical and temporal extents with the values inferred from the given envelope.
 * If the given {@code target} has more geographic or vertical extents than needed (0 or 1), then the
 * extraneous extents are removed.
 *
 * @param  envelope  the source envelope.
 * @param  target    the target spatiotemporal extent where to store envelope information.
 * @throws TransformException if no temporal component can be extracted from the given envelope.
 */
@Override
public void setBounds(final Envelope envelope, final DefaultSpatialTemporalExtent target) throws TransformException {
    final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
    final SingleCRS horizontalCRS = CRS.getHorizontalComponent(crs);
    final VerticalCRS verticalCRS = CRS.getVerticalComponent(crs, true);
    final TemporalCRS temporalCRS = CRS.getTemporalComponent(crs);
    if (horizontalCRS == null && verticalCRS == null && temporalCRS == null) {
        throw new TransformException(dimensionNotFound(Resources.Keys.MissingSpatioTemporalDimension_1, crs));
    }
    /*
     * Try to set the geographic bounding box first, because this operation may fail with a
     * TransformException while the other operations (vertical and temporal) should not fail.
     * So doing the geographic part first help us to get a "all or nothing" behavior.
     */
    DefaultGeographicBoundingBox box = null;
    boolean useExistingBox = (horizontalCRS != null);
    final Collection<GeographicExtent> spatialExtents = target.getSpatialExtent();
    final Iterator<GeographicExtent> it = spatialExtents.iterator();
    while (it.hasNext()) {
        final GeographicExtent extent = it.next();
        if (extent instanceof GeographicBoundingBox) {
            if (useExistingBox && (extent instanceof DefaultGeographicBoundingBox)) {
                box = (DefaultGeographicBoundingBox) extent;
                useExistingBox = false;
            } else {
                it.remove();
            }
        }
    }
    if (horizontalCRS != null) {
        if (box == null) {
            box = new DefaultGeographicBoundingBox();
            spatialExtents.add(box);
        }
        GeographicCRS normalizedCRS = ReferencingUtilities.toNormalizedGeographicCRS(crs, false, false);
        if (normalizedCRS == null) {
            normalizedCRS = CommonCRS.defaultGeographic();
        }
        setGeographicExtent(envelope, box, crs, normalizedCRS, null);
    }
    /*
     * Other dimensions (vertical and temporal).
     */
    if (verticalCRS != null) {
        VerticalExtent e = target.getVerticalExtent();
        if (!(e instanceof DefaultVerticalExtent)) {
            e = new DefaultVerticalExtent();
            target.setVerticalExtent(e);
        }
        setVerticalExtent(envelope, (DefaultVerticalExtent) e, crs, verticalCRS);
    } else {
        target.setVerticalExtent(null);
    }
    if (temporalCRS != null) {
        setTemporalExtent(envelope, target, crs, temporalCRS);
    } else {
        target.setExtent(null);
    }
}
 
Example 8
Source File: Envelope2D.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a two-dimensional envelope defined by an other {@link Envelope}.
 *
 * @param  envelope  the envelope to copy (can not be {@code null}).
 * @throws MismatchedDimensionException if the given envelope is not two-dimensional.
 */
public Envelope2D(final Envelope envelope) throws MismatchedDimensionException {
    this(envelope.getCoordinateReferenceSystem(), envelope.getLowerCorner(), envelope.getUpperCorner());
}
 
Example 9
Source File: GridGeometry.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the coordinate reference system of the given envelope if defined, or {@code null} if none.
 * Contrarily to {@link #getCoordinateReferenceSystem()}, this method does not throw exception.
 */
private static CoordinateReferenceSystem getCoordinateReferenceSystem(final Envelope envelope) {
    return (envelope != null) ? envelope.getCoordinateReferenceSystem() : null;
}