Utilities { public static Rectangle getCropRegion(GeneralEnvelope envelope, final MathTransform gridToWorldTransform) throws TransformException { if (envelope == null || gridToWorldTransform == null) { boolean isEnvelope = envelope == null; boolean isG2W = gridToWorldTransform == null; boolean twoErrors = isEnvelope && isG2W; final StringBuilder errorMessage = new StringBuilder(); errorMessage.append("Specified ").append(isEnvelope ? "envelope" : "") .append(twoErrors ? ", " : "") .append(isG2W ? "grid to world transformation " : "").append("is null"); throw new IllegalArgumentException(errorMessage.toString()); } final MathTransform worldToGridTransform = gridToWorldTransform.inverse(); final GeneralEnvelope rasterArea = CRS.transform(worldToGridTransform, envelope); final Rectangle2D ordinates = rasterArea.toRectangle2D(); return ordinates.getBounds(); } private  Utilities(); static ReferenceIdentifier[] getIdentifiers(final String nameIdentifier); static DefaultGeodeticDatum getDefaultGeodeticDatum(final String name,
            final double equatorialRadius, final double inverseFlattening, Unit unit); static File getFileFromCustomInput(Object input); @SuppressWarnings("deprecation") static CoordinateReferenceSystem getMercator2SPProjectedCRS(
            final double standardParallel, final double centralMeridian, final double natOriginLat,
            GeographicCRS sourceCRS, Hints hints); static GeographicCRS getBaseCRS(final double equatorialRadius,
            final double inverseFlattening); static String getAttributeValue(NamedNodeMap attributes, String attributeName); static Unit<? extends Quantity> parseUnit(final String uom); static Envelope getEnvelopeAsWGS84(final Envelope envelope, boolean get2D); static GeneralEnvelope getRequestedEnvelope2D(GeneralEnvelope requestedEnvelope); static Rectangle getCropRegion(GeneralEnvelope envelope,
            final MathTransform gridToWorldTransform); static GeneralEnvelope getIntersection(final Envelope2D baseEnvelope2D,
            final CoordinateReferenceSystem spatialReferenceSystem2D,
            GeneralEnvelope requestedEnvelope2D, Rectangle requestedDim,
            MathTransform2D readGridToWorld, final Envelope2D wgs84BaseEnvelope2D); static MathTransform getOriginalGridToWorld(MathTransform raster2Model,
            final PixelInCell pixInCell); static GeneralEnvelope evaluateRequestedParams(GridEnvelope originalGridRange,
            Envelope2D baseEnvelope2D, CoordinateReferenceSystem spatialReferenceSystem2D,
            MathTransform originalGridToWorld, GeneralEnvelope requestedEnvelope,
            Rectangle sourceRegion, Rectangle requestedDim, MathTransform2D readGridToWorld,
            Envelope2D wgs84BaseEnvelope2D); static GridCoverage createCoverageFromImage(final GridCoverageFactory coverageFactory,
            final String coverageName, int imageIndex, PlanarImage image,
            MathTransform raster2Model, final CoordinateReferenceSystem spatialReferenceSystem2D,
            GeneralEnvelope coverageEnvelope2D, final GridSampleDimension[] sampleDimensions,
            final boolean getGeophysics); static void setDecimationParameters(ImageReadParam readP, GridEnvelope baseGridRange,
            double[] requestedRes, double[] highestRes); static NameImpl buildCoverageName(URL input); static void setReadParameters(OverviewPolicy overviewPolicy, ImageReadParam readParam,
            GeneralEnvelope requestedEnvelope, Rectangle requestedDim, double[] highestRes,
            GridEnvelope gridRange, PixelInCell pixelInCell); static GridCoverage createCoverage(ImageReaderSpi spi, Object input,
            final int imageIndex, ImageReadParam imageReadParam, final boolean useJAI,
            final boolean useMultithreading, final boolean newTransform,
            final GridSampleDimension[] sampleDimensions, final String coverageName,
            GridCoverageFactory coverageFactory, MathTransform raster2Model,
            CoordinateReferenceSystem coordinateReferenceSystem, GeneralEnvelope coverageEnvelope2D); static PlanarImage readImage(final ImageReaderSpi spi, final Object input,
            final int imageIndex, final boolean useJAI, final ImageReadParam imageReadParam,
            final boolean useMultithreading); static GridCoverage compute(Object input, final int imageIndex,
            final boolean needTransformation, final boolean isEmptyRequest, final boolean useJAI,
            ImageReadParam imageReadParam, final boolean useMultithreading,
            final GridSampleDimension[] sampleDimensions, final ImageReaderSpi imageReaderSpi,
            final String coverageName, final GridCoverageFactory coverageFactory,
            final MathTransform raster2Model,
            final CoordinateReferenceSystem coordinateReferenceSystem,
            final GeneralEnvelope envelope2D); final static boolean ensureValidString(final String... strings); static String buildIso8601Time(String date, String time); static boolean checkFileReadable(final File file); static String getFileInfo(final File file); static Properties loadPropertiesFromURL(URL propsURL);  }
@Test public void testCropRegion() throws TransformException{ GeneralEnvelope envelope = new GeneralEnvelope(DefaultGeographicCRS.WGS84); envelope.setEnvelope(0, 0, 10, 10); Rectangle rect = new Rectangle(0, 0, 20, 20); GridEnvelope2D env = new GridEnvelope2D(rect); GridGeometry2D gg2D = new GridGeometry2D(env, envelope); Rectangle cropRegion = Utilities.getCropRegion(envelope, gg2D.getGridToCRS2D(PixelOrientation.UPPER_LEFT)); assertTrue(cropRegion.contains(rect)); boolean notThrown = false; try { Utilities.getCropRegion(envelope, null); notThrown = true; } catch (IllegalArgumentException e) { } assertFalse(notThrown); notThrown = false; try { Utilities.getCropRegion(null, gg2D.getGridToCRS2D(PixelOrientation.UPPER_LEFT)); notThrown = true; } catch (IllegalArgumentException e) { } assertFalse(notThrown); }