/** {@inheritDoc} */ @Override public Envelope getEnvelope() throws FactoryException { if (domainSubset == null || domainSubset.getBoundingBox() == null) { return null; } final BoundingBoxType boundingBox = domainSubset.getBoundingBox().getValue(); final List<Double> lowerCorner = boundingBox.getLowerCorner(); final List<Double> upperCorner = boundingBox.getUpperCorner(); final CoordinateReferenceSystem crs = getCRS(); final GeneralEnvelope objEnv = new GeneralEnvelope(crs); objEnv.setRange(0, lowerCorner.get(0), upperCorner.get(0)); objEnv.setRange(1, lowerCorner.get(1), upperCorner.get(1)); // If the CRS has a vertical part, then the envelope to return should be a 3D one. if (CRS.getVerticalComponent(crs, true) != null) { objEnv.setRange(2, lowerCorner.get(2), upperCorner.get(2)); } return objEnv; }
/** * Converts a string representing the bbox coordinates into a {@link GeneralEnvelope}. * * @param bbox Coordinates of the bounding box, seperated by comas. * @param crs The {@linkplain CoordinateReferenceSystem coordinate reference system} in which the * envelope is expressed. Must not be {@code null}. * @return The enveloppe for the bounding box specified, or an {@linkplain * GeneralEnvelope#setToInfinite infinite envelope} if the bbox is {@code null}. */ public static Envelope toEnvelope( final String bbox, final CoordinateReferenceSystem crs, final String strElevation, final String strTime) throws IllegalArgumentException, ParseException, FactoryException { final CoordinateReferenceSystem horizontalCRS = CRS.getHorizontalComponent(crs); final VerticalCRS verticalCRS; final TemporalCRS temporalCRS; final double[] dimX = new double[] {Double.NaN, Double.NaN}; final double[] dimY = new double[] {Double.NaN, Double.NaN}; final double[] dimZ = new double[] {Double.NaN, Double.NaN}; final double[] dimT = new double[] {Double.NaN, Double.NaN}; // parse bbox ----------------------------------------------------------- if (bbox == null) { // set to infinity dimX[0] = dimY[0] = Double.NEGATIVE_INFINITY; dimX[1] = dimY[1] = Double.POSITIVE_INFINITY; } else { final StringTokenizer tokens = new StringTokenizer(bbox, ",;"); final double[] values = new double[4]; int index = 0; while (tokens.hasMoreTokens()) { values[index] = toDouble(tokens.nextToken()); if (index >= 4) { throw new IllegalArgumentException(Errors.format(Errors.Keys.IndexOutOfBounds_1, index)); } index++; } if (index != 5) { throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgument_1, index)); } dimX[0] = values[0]; dimX[1] = values[2]; dimY[0] = values[1]; dimY[1] = values[3]; } // parse elevation ------------------------------------------------------ if (strElevation != null) { final double elevation = toDouble(strElevation); dimZ[0] = dimZ[1] = elevation; final VerticalCRS zCRS = CRS.getVerticalComponent(crs, true); verticalCRS = (zCRS != null) ? zCRS : CommonCRS.Vertical.MEAN_SEA_LEVEL.crs(); } else { verticalCRS = null; } // parse temporal ------------------------------------------------------- if (strTime != null) { final Date date = TemporalUtilities.parseDateSafe(strTime, true); final TemporalCRS tCRS = CRS.getTemporalComponent(crs); temporalCRS = (tCRS != null) ? tCRS : CommonCRS.Temporal.MODIFIED_JULIAN.crs(); dimT[0] = dimT[1] = ((DefaultTemporalCRS) temporalCRS).toValue(date); } else { temporalCRS = null; } // create the 2/3/4 D BBox ---------------------------------------------- if (verticalCRS != null && temporalCRS != null) { final CoordinateReferenceSystem finalCRS = new GeodeticObjectBuilder() .addName("rendering bbox") .createCompoundCRS(horizontalCRS, verticalCRS, temporalCRS); final GeneralEnvelope envelope = new GeneralEnvelope(finalCRS); envelope.setRange(0, dimX[0], dimX[1]); envelope.setRange(1, dimY[0], dimY[1]); envelope.setRange(2, dimZ[0], dimZ[1]); envelope.setRange(3, dimT[0], dimT[1]); return envelope; } else if (verticalCRS != null) { final CoordinateReferenceSystem finalCRS = new GeodeticObjectBuilder() .addName("rendering bbox") .createCompoundCRS(horizontalCRS, verticalCRS); final GeneralEnvelope envelope = new GeneralEnvelope(finalCRS); envelope.setRange(0, dimX[0], dimX[1]); envelope.setRange(1, dimY[0], dimY[1]); envelope.setRange(2, dimZ[0], dimZ[1]); return envelope; } else if (temporalCRS != null) { final CoordinateReferenceSystem finalCRS = new GeodeticObjectBuilder() .addName("rendering bbox") .createCompoundCRS(horizontalCRS, temporalCRS); final GeneralEnvelope envelope = new GeneralEnvelope(finalCRS); envelope.setRange(0, dimX[0], dimX[1]); envelope.setRange(1, dimY[0], dimY[1]); envelope.setRange(2, dimT[0], dimT[1]); return envelope; } else { final GeneralEnvelope envelope = new GeneralEnvelope(horizontalCRS); envelope.setRange(0, dimX[0], dimX[1]); envelope.setRange(1, dimY[0], dimY[1]); return envelope; } }
/** * Converts a string representing the bbox coordinates into a {@link GeneralEnvelope}. * * @param bbox Coordinates of the bounding box, seperated by comas. * @param crs The {@linkplain CoordinateReferenceSystem coordinate reference system} in which the * envelope is expressed. Must not be {@code null}. * @return The enveloppe for the bounding box specified, or an {@linkplain * GeneralEnvelope#setToInfinite infinite envelope} if the bbox is {@code null}. * @throws IllegalArgumentException if the given CRS is {@code null}, or if the bbox string * contains too much parameters to fill the CRS ranges. */ public static Envelope toEnvelope(final String bbox, final CoordinateReferenceSystem crs) throws IllegalArgumentException { if (crs == null) { throw new IllegalArgumentException("The CRS must not be null"); } if (bbox == null) { final GeneralEnvelope infinite = new GeneralEnvelope(crs); infinite.setToInfinite(); return infinite; } final GeneralEnvelope envelope = new GeneralEnvelope(crs); final int dimension = envelope.getDimension(); final StringTokenizer tokens = new StringTokenizer(bbox, ",;"); final double[] coordinates = new double[dimension * 2]; int index = 0; while (tokens.hasMoreTokens()) { final double value = toDouble(tokens.nextToken()); if (index >= coordinates.length) { throw new IllegalArgumentException( Errors.format(Errors.Keys.IllegalCsDimension_1, coordinates.length)); } coordinates[index++] = value; } if ((index & 1) != 0) { throw new IllegalArgumentException(Errors.format(Errors.Keys.OddArrayLength_1, index)); } // Fallthrough in every cases. switch (index) { default: { while (index >= 6) { final double maximum = coordinates[--index]; final double minimum = coordinates[--index]; envelope.setRange(index >> 1, minimum, maximum); } } case 4: envelope.setRange(1, coordinates[1], coordinates[3]); case 3: case 2: envelope.setRange(0, coordinates[0], coordinates[2]); case 1: case 0: break; } /* * Checks the envelope validity. Given that the parameter order in the bounding box * is a little-bit counter-intuitive, it is worth to perform this check in order to * avoid a NonInvertibleTransformException at some later stage. */ for (index = 0; index < dimension; index++) { final double minimum = envelope.getMinimum(index); final double maximum = envelope.getMaximum(index); if (!(minimum < maximum)) { throw new IllegalArgumentException( Errors.format(Errors.Keys.IllegalRange_2, minimum, maximum)); } } return envelope; }