@Test public void testWCS10GetCoverageDifferentCrs() throws Exception { // xMin,yMin 5988504.35,851278.90 : xMax,yMax 7585113.55,1950872.01 // xMin,yMin -95.1193,42.2802 : xMax,yMax -71.295,53.73 GetCoverageType gc = Wcs10Factory.eINSTANCE.createGetCoverageType(); net.opengis.wcs10.SpatialSubsetType spatialSubset = Wcs10Factory.eINSTANCE.createSpatialSubsetType(); CoordinateReferenceSystem crs = CRS.decode("EPSG:3348", false); CoordinateReferenceSystem logCrs = CRS.decode("EPSG:4326", false); GeneralEnvelope env = new GeneralEnvelope( new double[] {5988504.35, 851278.90}, new double[] {7585113.55, 1950872.01}); env.setCoordinateReferenceSystem(crs); BoundingBox bbox = new ReferencedEnvelope(42.2802, 53.73, -95.1193, -71.295, logCrs); spatialSubset.getEnvelope().clear(); spatialSubset.getEnvelope().add(env); net.opengis.wcs10.DomainSubsetType domainSubset = Wcs10Factory.eINSTANCE.createDomainSubsetType(); domainSubset.setSpatialSubset(spatialSubset); gc.setSourceCoverage("acme:foo"); gc.setDomainSubset(domainSubset); callback.operationDispatched(new Request(), op("GetCoverage", "WCS", "1.0.0", gc)); assertEquals("acme:foo", data.getResources().get(0)); BBoxAsserts.assertEqualsBbox(bbox, data.getBbox(), 0.1); }
/** * This method is responsible for computing the resolutions in for the provided grid geometry in * the provided crs. * * <p>It is worth to note that the returned resolution array is of length of 2 and it always is * lon, lat for the moment.<br> * It might be worth to remove the axes reordering code when we are confident enough with the code * to handle the north-up crs. * * <p>TODO use orthodromic distance? * * @param envelope the GeneralEnvelope * @param dim * @param crs * @throws DataSourceException */ protected static final double[] getResolution( GeneralEnvelope envelope, Rectangle2D dim, CoordinateReferenceSystem crs) throws DataSourceException { double[] requestedRes = null; try { if (dim != null && envelope != null && crs != null) { // do we need to transform the originalEnvelope? final CoordinateReferenceSystem envelopeCrs2D = CRS.getHorizontalCRS(envelope.getCoordinateReferenceSystem()); if (envelopeCrs2D != null && !CRS.equalsIgnoreMetadata(crs, envelopeCrs2D)) { CoordinateOperationFactory operationFactory = CRS.getCoordinateOperationFactory(true); CoordinateOperation op = operationFactory.createOperation(envelopeCrs2D, crs); envelope = CRS.transform(op, envelope); envelope.setCoordinateReferenceSystem(crs); } requestedRes = new double[2]; requestedRes[0] = envelope.getSpan(0) / dim.getWidth(); requestedRes[1] = envelope.getSpan(1) / dim.getHeight(); } return requestedRes; } catch (TransformException e) { throw new DataSourceException("Unable to get resolution", e); } catch (FactoryException e) { throw new DataSourceException("Unable to get resolution", e); } }
private GeneralEnvelope getResultEnvelope( final List<RasterQueryInfo> queryInfos, final GridEnvelope mosaicGeometry) { // use the same queryInfo used by setMosaicLocations final RasterQueryInfo baseQueryInfo = RasterUtils.findLowestResolution(queryInfos); GeneralEnvelope finalEnvelope = null; // if (queryInfos.size() == 1) { // finalEnvelope = queryInfos.get(0).getResultEnvelope(); // } else { int rasterIndex = baseQueryInfo.getRasterIndex(); int pyramidLevel = baseQueryInfo.getPyramidLevel(); MathTransform rasterToModel = rasterInfo.getRasterToModel(rasterIndex, pyramidLevel); CoordinateReferenceSystem coverageCrs = rasterInfo.getCoverageCrs(); GeneralEnvelope mosaicGeometryEnv = new GeneralEnvelope(coverageCrs); mosaicGeometryEnv.setEnvelope( mosaicGeometry.getLow(0), mosaicGeometry.getLow(1), 1 + mosaicGeometry.getHigh(0), 1 + mosaicGeometry.getHigh(1)); try { finalEnvelope = CRS.transform(rasterToModel, mosaicGeometryEnv); finalEnvelope.setCoordinateReferenceSystem(coverageCrs); } catch (TransformException e) { throw new RuntimeException(e); } // } // double[] resolution = baseQueryInfo.getResolution(); // finalEnvelope = toPixelCenter(resolution, finalEnvelope); return finalEnvelope; }
@Test public void testWCS10GetCoverage() throws Exception { GetCoverageType gc = Wcs10Factory.eINSTANCE.createGetCoverageType(); net.opengis.wcs10.SpatialSubsetType spatialSubset = Wcs10Factory.eINSTANCE.createSpatialSubsetType(); CoordinateReferenceSystem crs = CRS.decode("EPSG:4326"); GeneralEnvelope env = new GeneralEnvelope(new double[] {-123.4, 48.2}, new double[] {-120.9, 50.1}); env.setCoordinateReferenceSystem(crs); BoundingBox bbox = new ReferencedEnvelope(env); spatialSubset.getEnvelope().clear(); spatialSubset.getEnvelope().add(env); net.opengis.wcs10.DomainSubsetType domainSubset = Wcs10Factory.eINSTANCE.createDomainSubsetType(); domainSubset.setSpatialSubset(spatialSubset); gc.setSourceCoverage("acme:foo"); gc.setDomainSubset(domainSubset); callback.operationDispatched(new Request(), op("GetCoverage", "WCS", "1.0.0", gc)); assertEquals("acme:foo", data.getResources().get(0)); BBoxAsserts.assertEqualsBbox(bbox, data.getBbox(), 0.1); }
@Test public void testWCS11GetCoverage() throws Exception { net.opengis.wcs11.GetCoverageType gc = Wcs11Factory.eINSTANCE.createGetCoverageType(); CoordinateReferenceSystem crs = CRS.decode("EPSG:4326"); GeneralEnvelope env = new GeneralEnvelope(new double[] {48.2, -123.4}, new double[] {50.1, -120.9}); env.setCoordinateReferenceSystem(crs); BoundingBox bbox = new ReferencedEnvelope(env); net.opengis.ows11.BoundingBoxType wcsBbox = net.opengis.ows11.Ows11Factory.eINSTANCE.createBoundingBoxType(); wcsBbox.setLowerCorner(Arrays.asList(48.2d, -123.4d)); wcsBbox.setUpperCorner(Arrays.asList(50.1d, -120.9d)); // wcsBbox.setCrs("urn:ogc:def:crs:OGC:1.3:CRS84"); wcsBbox.setCrs("urn:ogc:def:crs:EPSG:4326"); net.opengis.wcs11.DomainSubsetType domainSubset = Wcs11Factory.eINSTANCE.createDomainSubsetType(); domainSubset.setBoundingBox(wcsBbox); gc.setDomainSubset(domainSubset); CodeType c = Ows11Factory.eINSTANCE.createCodeType(); c.setValue("acme:bar"); gc.setIdentifier(c); callback.operationDispatched(new Request(), op("GetCoverage", "WCS", "1.1.0", gc)); assertEquals("acme:bar", data.getResources().get(0)); BBoxAsserts.assertEqualsBbox(bbox, data.getBbox(), 0.1); }
/** Tests the transformations of an envelope. */ @Test public void testEnvelopeTransformation() throws FactoryException, TransformException { final CoordinateReferenceSystem mapCRS = CRS.parseWKT(WKT.UTM_10N); final CoordinateReferenceSystem WGS84 = DefaultGeographicCRS.WGS84; final MathTransform crsTransform = CRS.findMathTransform(WGS84, mapCRS, true); assertFalse(crsTransform.isIdentity()); final GeneralEnvelope firstEnvelope, transformedEnvelope, oldEnvelope; firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43}); firstEnvelope.setCoordinateReferenceSystem(WGS84); transformedEnvelope = CRS.transform(crsTransform, firstEnvelope); transformedEnvelope.setCoordinateReferenceSystem(mapCRS); oldEnvelope = CRS.transform(crsTransform.inverse(), transformedEnvelope); oldEnvelope.setCoordinateReferenceSystem(WGS84); assertTrue(oldEnvelope.contains(firstEnvelope, true)); assertTrue(oldEnvelope.equals(firstEnvelope, 0.02, true)); }
/** * Transforms the referenced envelope to the specified coordinate reference system using the * specified amount of points. * * <p>This method can handle the case where the envelope contains the North or South pole, or when * it cross the ±180� longitude. * * @param targetCRS The target coordinate reference system. * @param lenient {@code true} if datum shift should be applied even if there is insuffisient * information. Otherwise (if {@code false}), an exception is thrown in such case. * @param numPointsForTransformation The number of points to use for sampling the envelope. * @return The transformed envelope. * @throws FactoryException if the math transform can't be determined. * @throws TransformException if at least one coordinate can't be transformed. * @see CRS#transform(CoordinateOperation, org.opengis.geometry.Envelope) * @since 2.3 */ public ReferencedEnvelope transform( final CoordinateReferenceSystem targetCRS, final boolean lenient, final int numPointsForTransformation) throws TransformException, FactoryException { if (crs == null) { if (isEmpty()) { // We don't have a CRS yet because we are still empty, being empty is // something we can represent in the targetCRS return new ReferencedEnvelope(targetCRS); } else { // really this is a the code that created this ReferencedEnvelope throw new NullPointerException( "Unable to transform referenced envelope, crs has not yet been provided."); } } if (getDimension() != targetCRS.getCoordinateSystem().getDimension()) { if (lenient) { return JTS.transformTo3D(this, targetCRS, lenient, numPointsForTransformation); } else { throw new MismatchedDimensionException( Errors.format( ErrorKeys.MISMATCHED_DIMENSION_$3, crs.getName().getCode(), new Integer(getDimension()), new Integer(targetCRS.getCoordinateSystem().getDimension()))); } } /* * Gets a first estimation using an algorithm capable to take singularity in account * (North pole, South pole, 180� longitude). We will expand this initial box later. */ CoordinateOperationFactory coordinateOperationFactory = CRS.getCoordinateOperationFactory(lenient); final CoordinateOperation operation = coordinateOperationFactory.createOperation(crs, targetCRS); final GeneralEnvelope transformed = CRS.transform(operation, this); transformed.setCoordinateReferenceSystem(targetCRS); /* * Now expands the box using the usual utility methods. */ final ReferencedEnvelope target = new ReferencedEnvelope(transformed); final MathTransform transform = operation.getMathTransform(); JTS.transform(this, target, transform, numPointsForTransformation); return target; }
/** * <strong>Cropping</strong><br> * The crop operation is responsible for selecting geographic subareas of the source coverage. * * @param coverage Coverage * @param sourceEnvelope GeneralEnvelope * @param sourceCRS CoordinateReferenceSystem * @param destinationEnvelopeInSourceCRS GeneralEnvelope * @return GridCoverage2D * @throws WcsException */ public static GridCoverage2D crop( final Coverage coverage, final GeneralEnvelope sourceEnvelope, final CoordinateReferenceSystem sourceCRS, final GeneralEnvelope destinationEnvelopeInSourceCRS, final Boolean conserveEnvelope) throws WcsException { // /////////////////////////////////////////////////////////////////// // // CROP // // // /////////////////////////////////////////////////////////////////// final GridCoverage2D croppedGridCoverage; // intersect the envelopes final GeneralEnvelope intersectionEnvelope = new GeneralEnvelope(destinationEnvelopeInSourceCRS); intersectionEnvelope.setCoordinateReferenceSystem(sourceCRS); intersectionEnvelope.intersect((GeneralEnvelope) sourceEnvelope); // dow we have something to show? if (intersectionEnvelope.isEmpty()) { throw new WcsException("The Intersection is null. Check the requested BBOX!"); } if (!intersectionEnvelope.equals((GeneralEnvelope) sourceEnvelope)) { // get the cropped grid geometry // final GridGeometry2D cropGridGeometry = getCroppedGridGeometry( // intersectionEnvelope, gridCoverage); /* Operations.DEFAULT.crop(coverage, intersectionEnvelope) */ final ParameterValueGroup param = (ParameterValueGroup) cropParams.clone(); param.parameter("Source").setValue(coverage); param.parameter("Envelope").setValue(intersectionEnvelope); // param.parameter("ConserveEnvelope").setValue(conserveEnvelope); croppedGridCoverage = (GridCoverage2D) cropFactory.doOperation(param, hints); } else { croppedGridCoverage = (GridCoverage2D) coverage; } // prefetch to be faster afterwards. // This step is important since at this stage we might be loading tiles // from disk croppedGridCoverage.prefetch(intersectionEnvelope.toRectangle2D()); return croppedGridCoverage; }
private GridCoverage2D convertImageToGridCoverage( ReferencedEnvelope requestBBox, BufferedImage image) throws RenderException { Envelope env = requestBBox; GeneralEnvelope gtEnv = new GeneralEnvelope( new double[] {env.getMinX(), env.getMinY()}, new double[] {env.getMaxX(), env.getMaxY()}); try { gtEnv.setCoordinateReferenceSystem(requestBBox.getCoordinateReferenceSystem()); } catch (Exception e) { throw wrapException(e); } GridCoverageFactory factory = new GridCoverageFactory(); GridCoverage2D gc = (GridCoverage2D) factory.create("GridCoverage", image, gtEnv); // $NON-NLS-1$ return gc; }
/** * Tests the transformations of an envelope when the two CRS have identify transforms but * different datum names */ @Test public void testEnvelopeTransformation2() throws FactoryException, TransformException { final CoordinateReferenceSystem WGS84Altered = CRS.parseWKT(WKT.WGS84_ALTERED); final CoordinateReferenceSystem WGS84 = DefaultGeographicCRS.WGS84; final MathTransform crsTransform = CRS.findMathTransform(WGS84, WGS84Altered, true); assertTrue(crsTransform.isIdentity()); final GeneralEnvelope firstEnvelope; firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43}); firstEnvelope.setCoordinateReferenceSystem(WGS84); // this triggered a assertion error in GEOT-2934 Envelope transformed = CRS.transform(firstEnvelope, WGS84Altered); // check the envelope is what we expect assertEquals(transformed.getCoordinateReferenceSystem(), WGS84Altered); double EPS = 1e-9; assertEquals(transformed.getMinimum(0), firstEnvelope.getMinimum(0), EPS); assertEquals(transformed.getMinimum(1), firstEnvelope.getMinimum(1), EPS); assertEquals(transformed.getMaximum(0), firstEnvelope.getMaximum(0), EPS); assertEquals(transformed.getMaximum(1), firstEnvelope.getMaximum(1), EPS); }
/** * Applies a crop operation to a coverage. * * @see * org.geotools.coverage.processing.AbstractOperation#doOperation(org.opengis.parameter.ParameterValueGroup, * org.geotools.factory.Hints) */ @SuppressWarnings("unchecked") public Coverage doOperation(ParameterValueGroup parameters, Hints hints) { final Geometry cropRoi; // extracted from parameters GeneralEnvelope cropEnvelope = null; // extracted from parameters final GridCoverage2D source; // extracted from parameters final double roiTolerance = parameters.parameter(Crop.PARAMNAME_ROITOLERANCE).doubleValue(); final boolean forceMosaic = parameters.parameter(Crop.PARAMNAME_FORCEMOSAIC).booleanValue(); // ///////////////////////////////////////////////////////////////////// // // Assigning and checking input parameters // // /////////////////////////////////////////////////////////////////// // source coverage final ParameterValue sourceParameter = parameters.parameter("Source"); if (sourceParameter == null || !(sourceParameter.getValue() instanceof GridCoverage2D)) { throw new CannotCropException( Errors.format(ErrorKeys.NULL_PARAMETER_$2, "Source", GridCoverage2D.class.toString())); } source = (GridCoverage2D) sourceParameter.getValue(); // Check Envelope and ROI existence - we need at least one of them final ParameterValue envelopeParameter = parameters.parameter(PARAMNAME_ENVELOPE); final ParameterValue roiParameter = parameters.parameter(PARAMNAME_ROI); if ((envelopeParameter == null || envelopeParameter.getValue() == null) && (roiParameter == null || roiParameter.getValue() == null)) throw new CannotCropException( Errors.format( ErrorKeys.NULL_PARAMETER_$2, PARAMNAME_ENVELOPE, GeneralEnvelope.class.toString())); Object envelope = envelopeParameter.getValue(); if (envelope != null) { if (envelope instanceof GeneralEnvelope) { cropEnvelope = (GeneralEnvelope) envelope; } else if (envelope instanceof Envelope) { cropEnvelope = new GeneralEnvelope((Envelope) envelope); } } // may be null // Check crop ROI try { cropRoi = IntersectUtils.unrollGeometries( (Geometry) roiParameter.getValue()); // may throw if format not correct } catch (IllegalArgumentException ex) { throw new CannotCropException( Errors.format(ErrorKeys.ILLEGAL_ARGUMENT_$2, PARAMNAME_ROI, ex.getMessage()), ex); } // Setting a GeneralEnvelope from ROI if needed if (cropRoi != null && cropEnvelope == null) { Envelope e2d = JTS.getEnvelope2D(cropRoi.getEnvelopeInternal(), source.getCoordinateReferenceSystem()); cropEnvelope = new GeneralEnvelope(e2d); } // ///////////////////////////////////////////////////////////////////// // // Initialization // // We take the crop envelope and the source envelope then we check their // crs and we also check if they ever overlap. // // ///////////////////////////////////////////////////////////////////// // envelope of the source coverage final Envelope2D sourceEnvelope = source.getEnvelope2D(); // crop envelope Envelope2D destinationEnvelope = new Envelope2D(cropEnvelope); CoordinateReferenceSystem sourceCRS = sourceEnvelope.getCoordinateReferenceSystem(); CoordinateReferenceSystem destinationCRS = destinationEnvelope.getCoordinateReferenceSystem(); if (destinationCRS == null) { // Do not change the user provided object - clone it first. final Envelope2D ge = new Envelope2D(destinationEnvelope); destinationCRS = source.getCoordinateReferenceSystem2D(); ge.setCoordinateReferenceSystem(destinationCRS); destinationEnvelope = ge; } // // // // Source and destination crs must be equals // // // if (!CRS.equalsIgnoreMetadata(sourceCRS, destinationCRS)) { throw new CannotCropException( Errors.format( ErrorKeys.MISMATCHED_ENVELOPE_CRS_$2, sourceCRS.getName().getCode(), destinationCRS.getName().getCode())); } if (cropRoi != null) { // TODO: check ROI SRID } // // // // Check the intersection and, if needed, do the crop operation. // // // final GeneralEnvelope intersectionEnvelope = new GeneralEnvelope((Envelope) destinationEnvelope); intersectionEnvelope.setCoordinateReferenceSystem(source.getCoordinateReferenceSystem()); // intersect the envelopes intersectionEnvelope.intersect(sourceEnvelope); if (intersectionEnvelope.isEmpty()) throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP)); // intersect the ROI with the intersection envelope and throw an error if they do not intersect if (cropRoi != null) { final Geometry jis = JTS.toGeometry( (com.vividsolutions.jts.geom.Envelope) new ReferencedEnvelope(intersectionEnvelope)); if (!IntersectUtils.intersects(cropRoi, jis)) throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP)); } // // // // Get the grid-to-world transform by keeping into account translation // of grid geometry constructor for respecting OGC PIXEL-IS-CENTER // ImageDatum assumption. // // // final AffineTransform sourceCornerGridToWorld = (AffineTransform) ((GridGeometry2D) source.getGridGeometry()).getGridToCRS(PixelInCell.CELL_CORNER); // // // // I set the tolerance as half the scale factor of the grid-to-world // transform. This should more or less means in most cases "don't bother // to crop if the new envelope is as close to the old one that we go // deep under pixel size." // // // final double tolerance = XAffineTransform.getScale(sourceCornerGridToWorld); if (cropRoi != null || !intersectionEnvelope.equals(sourceEnvelope, tolerance / 2.0, false)) { cropEnvelope = intersectionEnvelope.clone(); return buildResult( cropEnvelope, cropRoi, roiTolerance, forceMosaic, (hints instanceof Hints) ? (Hints) hints : new Hints(hints), source, sourceCornerGridToWorld); } else { // // // // Note that in case we don't crop at all, WE DO NOT UPDATE the // envelope. If we did we might end up doing multiple successive // crop without actually cropping the image but, still, we would // shrink the envelope each time. Just think about having a loop // that crops recursively the same coverage specifying each time an // envelope whose URC is only a a scale quarter close to the LLC of // the old one. We would never crop the raster but we would modify // the grid-to-world transform each time. // // // return source; } }
private void computeCropBBOX() throws DataSourceException { // get the crs for the requested bbox if (requestCRS == null) requestCRS = CRS.getHorizontalCRS(requestedBBox.getCoordinateReferenceSystem()); try { // // The destination to source transform has been computed (and eventually erased) already // by inspectCoordinateSystem() // now transform the requested envelope to source crs if (destinationToSourceTransform != null && !destinationToSourceTransform.isIdentity()) { final GeneralEnvelope temp = new GeneralEnvelope(CRS.transform(requestedBBox, coverageProperties.crs2D)); temp.setCoordinateReferenceSystem(coverageProperties.crs2D); cropBBox = new ReferencedEnvelope(temp); needsReprojection = true; } else { // we do not need to do anything, but we do this in order to aboid problems with the // envelope checks cropBBox = new ReferencedEnvelope( requestedBBox.getMinX(), requestedBBox.getMaxX(), requestedBBox.getMinY(), requestedBBox.getMaxY(), coverageProperties.crs2D); } // intersect requested BBox in native CRS with coverage native bbox to get the crop bbox // intersect the requested area with the bounds of this layer in native crs if (!cropBBox.intersects((BoundingBox) coverageProperties.bbox)) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine( new StringBuilder("The computed CropBoundingBox ") .append(cropBBox) .append(" Doesn't intersect the coverage BoundingBox ") .append(coverageProperties.bbox) .append(" resulting in an empty request") .toString()); } cropBBox = null; empty = true; return; } // TODO XXX Optimize when referenced envelope has intersection method that actually retains // the CRS, this is the JTS one cropBBox = new ReferencedEnvelope( ((ReferencedEnvelope) cropBBox).intersection(coverageProperties.bbox), coverageProperties.crs2D); return; } catch (TransformException te) { // something bad happened while trying to transform this // envelope. let's try with wgs84 if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, te.getLocalizedMessage(), te); } try { // can we proceed? Do we have geo stuff to do all these operations? if (coverageProperties.geographicCRS2D != null && coverageProperties.geographicBBox != null) { // // If we can not reproject the requested envelope to the native CRS, // we go back to reproject in the geographic crs of the native // coverage since this usually happens for conversions between CRS // whose area of definition is different // // STEP 1 reproject the requested envelope to the coverage geographic bbox if (!CRS.equalsIgnoreMetadata(coverageProperties.geographicCRS2D, requestCRS)) { // try to convert the requested bbox to the coverage geocrs requestCRSToCoverageGeographicCRS2D = CRS.findMathTransform(requestCRS, coverageProperties.geographicCRS2D, true); if (!requestCRSToCoverageGeographicCRS2D.isIdentity()) { requestedBBOXInCoverageGeographicCRS = CRS.transform(requestedBBox, coverageProperties.geographicCRS2D); requestedBBOXInCoverageGeographicCRS.setCoordinateReferenceSystem( coverageProperties.geographicCRS2D); } } if (requestedBBOXInCoverageGeographicCRS == null) { requestedBBOXInCoverageGeographicCRS = new GeneralEnvelope(requestCRS); } // STEP 2 intersection with the geographic bbox for this coverage if (!requestedBBOXInCoverageGeographicCRS.intersects( coverageProperties.geographicBBox, true)) { cropBBox = null; empty = true; return; } // intersect with the coverage native geographic bbox // note that for the moment we got to use general envelope since there is no intersection // otherwise requestedBBOXInCoverageGeographicCRS.intersect(coverageProperties.geographicBBox); requestedBBOXInCoverageGeographicCRS.setCoordinateReferenceSystem( coverageProperties.geographicCRS2D); // now go back to the coverage native CRS in order to compute an approximate requested // resolution approximateRequestedBBoInNativeCRS = CRS.transform(requestedBBOXInCoverageGeographicCRS, coverageProperties.crs2D); approximateRequestedBBoInNativeCRS.setCoordinateReferenceSystem(coverageProperties.crs2D); cropBBox = new ReferencedEnvelope(approximateRequestedBBoInNativeCRS); return; } } catch (TransformException te) { // something bad happened while trying to transform this // envelope. let's try with wgs84 if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, te.getLocalizedMessage(), te); } catch (FactoryException fe) { // something bad happened while trying to transform this // envelope. let's try with wgs84 if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, fe.getLocalizedMessage(), fe); } LOGGER.log( Level.INFO, "We did not manage to crop the requested envelope, we fall back onto loading the whole coverage."); cropBBox = null; }
@Test public void NetCDFTestOnFilter() throws NoSuchAuthorityCodeException, FactoryException, IOException, ParseException { File mosaic = new File(TestData.file(this, "."), "NetCDFTestOnFilter"); if (mosaic.exists()) { FileUtils.deleteDirectory(mosaic); } assertTrue(mosaic.mkdirs()); File file = TestData.file(this, "O3-NO2.nc"); FileUtils.copyFileToDirectory(file, mosaic); file = new File(mosaic, "O3-NO2.nc"); final Hints hints = new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, CRS.decode("EPSG:4326", true)); // Get format final AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(file.toURI().toURL(), hints); final NetCDFReader reader = (NetCDFReader) format.getReader(file.toURI().toURL(), hints); assertNotNull(format); try { String[] names = reader.getGridCoverageNames(); names = new String[] {names[1]}; for (String coverageName : names) { final String[] metadataNames = reader.getMetadataNames(coverageName); assertNotNull(metadataNames); assertEquals(12, metadataNames.length); // Parsing metadata values assertEquals("true", reader.getMetadataValue(coverageName, "HAS_TIME_DOMAIN")); final String timeMetadata = reader.getMetadataValue(coverageName, "TIME_DOMAIN"); assertEquals( "2012-04-01T00:00:00.000Z/2012-04-01T00:00:00.000Z,2012-04-01T01:00:00.000Z/2012-04-01T01:00:00.000Z", timeMetadata); assertNotNull(timeMetadata); assertEquals( "2012-04-01T00:00:00.000Z", reader.getMetadataValue(coverageName, "TIME_DOMAIN_MINIMUM")); assertEquals( "2012-04-01T01:00:00.000Z", reader.getMetadataValue(coverageName, "TIME_DOMAIN_MAXIMUM")); assertEquals("true", reader.getMetadataValue(coverageName, "HAS_ELEVATION_DOMAIN")); final String elevationMetadata = reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN"); assertNotNull(elevationMetadata); assertEquals("10.0/10.0,450.0/450.0", elevationMetadata); assertEquals(2, elevationMetadata.split(",").length); assertEquals("10.0", reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN_MINIMUM")); assertEquals("450.0", reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN_MAXIMUM")); // subsetting the envelope final ParameterValue<GridGeometry2D> gg = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue(); final GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope(coverageName); final GeneralEnvelope reducedEnvelope = new GeneralEnvelope( new double[] { originalEnvelope.getLowerCorner().getOrdinate(0), originalEnvelope.getLowerCorner().getOrdinate(1) }, new double[] { originalEnvelope.getMedian().getOrdinate(0), originalEnvelope.getMedian().getOrdinate(1) }); reducedEnvelope.setCoordinateReferenceSystem( reader.getCoordinateReferenceSystem(coverageName)); // Selecting bigger gridRange for a zoomed result final Dimension dim = new Dimension(); GridEnvelope gridRange = reader.getOriginalGridRange(coverageName); dim.setSize(gridRange.getSpan(0) * 4.0, gridRange.getSpan(1) * 2.0); final Rectangle rasterArea = ((GridEnvelope2D) gridRange); rasterArea.setSize(dim); final GridEnvelope2D range = new GridEnvelope2D(rasterArea); gg.setValue(new GridGeometry2D(range, reducedEnvelope)); final ParameterValue<Filter> filterParam = NetCDFFormat.FILTER.createValue(); FilterFactory2 FF = FeatureUtilities.DEFAULT_FILTER_FACTORY; Filter filter = FF.equals(FF.property("z"), FF.literal(450.0)); filterParam.setValue(filter); GeneralParameterValue[] values = new GeneralParameterValue[] {filterParam}; GridCoverage2D coverage = reader.read(coverageName, values); assertNotNull(coverage); if (TestData.isInteractiveTest()) { coverage.show(); } else { PlanarImage.wrapRenderedImage(coverage.getRenderedImage()).getTiles(); } } } catch (Throwable t) { throw new RuntimeException(t); } finally { if (reader != null) { try { reader.dispose(); } catch (Throwable t) { // Does nothing } } } }
@Test public void NetCDFTestAscatL1() throws NoSuchAuthorityCodeException, FactoryException, IOException, ParseException { File mosaic = new File(TestData.file(this, "."), "NetCDFTestAscatL1"); if (mosaic.exists()) { FileUtils.deleteDirectory(mosaic); } assertTrue(mosaic.mkdirs()); File file = TestData.file(this, "ascatl1.nc"); FileUtils.copyFileToDirectory(file, mosaic); file = new File(mosaic, "ascatl1.nc"); final Hints hints = new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, CRS.decode("EPSG:4326", true)); hints.add(new Hints(Utils.EXCLUDE_MOSAIC, true)); // Get format final AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(file.toURI().toURL(), hints); final NetCDFReader reader = (NetCDFReader) format.getReader(file.toURI().toURL(), hints); assertNotNull(format); try { String[] names = reader.getGridCoverageNames(); names = new String[] {names[1]}; for (String coverageName : names) { final String[] metadataNames = reader.getMetadataNames(coverageName); assertNotNull(metadataNames); assertEquals(17, metadataNames.length); // Parsing metadata values assertEquals("false", reader.getMetadataValue(coverageName, "HAS_TIME_DOMAIN")); assertEquals("false", reader.getMetadataValue(coverageName, "HAS_ELEVATION_DOMAIN")); assertEquals("true", reader.getMetadataValue(coverageName, "HAS_NUMSIGMA_DOMAIN")); final String sigmaMetadata = reader.getMetadataValue(coverageName, "NUMSIGMA_DOMAIN"); assertNotNull(sigmaMetadata); assertEquals("0,1,2", sigmaMetadata); assertEquals(3, sigmaMetadata.split(",").length); // subsetting the envelope final ParameterValue<GridGeometry2D> gg = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue(); final GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope(coverageName); final GeneralEnvelope reducedEnvelope = new GeneralEnvelope( new double[] { originalEnvelope.getLowerCorner().getOrdinate(0), originalEnvelope.getLowerCorner().getOrdinate(1) }, new double[] { originalEnvelope.getMedian().getOrdinate(0), originalEnvelope.getMedian().getOrdinate(1) }); reducedEnvelope.setCoordinateReferenceSystem( reader.getCoordinateReferenceSystem(coverageName)); // Selecting bigger gridRange for a zoomed result final Dimension dim = new Dimension(); GridEnvelope gridRange = reader.getOriginalGridRange(coverageName); dim.setSize(gridRange.getSpan(0) * 4.0, gridRange.getSpan(1) * 2.0); final Rectangle rasterArea = ((GridEnvelope2D) gridRange); rasterArea.setSize(dim); final GridEnvelope2D range = new GridEnvelope2D(rasterArea); gg.setValue(new GridGeometry2D(range, reducedEnvelope)); ParameterValue<List<String>> sigmaValue = null; final String selectedSigma = "1"; Set<ParameterDescriptor<List>> params = reader.getDynamicParameters(coverageName); for (ParameterDescriptor param : params) { if (param.getName().getCode().equalsIgnoreCase("NUMSIGMA")) { sigmaValue = param.createValue(); sigmaValue.setValue( new ArrayList<String>() { { add(selectedSigma); } }); } } GeneralParameterValue[] values = new GeneralParameterValue[] {gg, sigmaValue}; GridCoverage2D coverage = reader.read(coverageName, values); assertNotNull(coverage); if (TestData.isInteractiveTest()) { coverage.show(); } else { PlanarImage.wrapRenderedImage(coverage.getRenderedImage()).getTiles(); } } } catch (Throwable t) { throw new RuntimeException(t); } finally { if (reader != null) { try { reader.dispose(); } catch (Throwable t) { // Does nothing } } } }
@Test public void NetCDFTestOn4DcoveragesWithImposedSchemas() throws NoSuchAuthorityCodeException, FactoryException, IOException, ParseException { File mosaic = new File(TestData.file(this, "."), "NetCDFTestOn4DcoveragesWithImposedSchemas"); if (mosaic.exists()) { FileUtils.deleteDirectory(mosaic); } assertTrue(mosaic.mkdirs()); File file = TestData.file(this, "O3NO2-noZ.nc"); File auxFile = TestData.file(this, "O3NO2-noZ.xml"); FileUtils.copyFileToDirectory(file, mosaic); FileUtils.copyFileToDirectory(auxFile, mosaic); file = new File(mosaic, "O3NO2-noZ.nc"); final Hints hints = new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, CRS.decode("EPSG:4326", true)); hints.put( Utils.AUXILIARY_FILES_PATH, new File(mosaic, "O3NO2-noZ.xml").getAbsolutePath()); // impose def // Get format final AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(file.toURI().toURL(), hints); final NetCDFReader reader = (NetCDFReader) format.getReader(file.toURI().toURL(), hints); assertNotNull(format); try { String[] names = reader.getGridCoverageNames(); for (String coverageName : names) { final String[] metadataNames = reader.getMetadataNames(coverageName); assertNotNull(metadataNames); assertEquals(metadataNames.length, 12); // Parsing metadata values assertEquals("true", reader.getMetadataValue(coverageName, "HAS_TIME_DOMAIN")); final String timeMetadata = reader.getMetadataValue(coverageName, "TIME_DOMAIN"); assertEquals( "2012-04-01T00:00:00.000Z/2012-04-01T00:00:00.000Z,2012-04-01T01:00:00.000Z/2012-04-01T01:00:00.000Z", timeMetadata); assertNotNull(timeMetadata); assertEquals( "2012-04-01T00:00:00.000Z", reader.getMetadataValue(coverageName, "TIME_DOMAIN_MINIMUM")); assertEquals( "2012-04-01T01:00:00.000Z", reader.getMetadataValue(coverageName, "TIME_DOMAIN_MAXIMUM")); if (coverageName.equalsIgnoreCase("O3")) { assertEquals("true", reader.getMetadataValue(coverageName, "HAS_ELEVATION_DOMAIN")); final String elevationMetadata = reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN"); assertNotNull(elevationMetadata); assertEquals("10.0/10.0,450.0/450.0", elevationMetadata); assertEquals(2, elevationMetadata.split(",").length); assertEquals("10.0", reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN_MINIMUM")); assertEquals("450.0", reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN_MAXIMUM")); } else { // Note that This sample doesn't have elevation for NO2 assertEquals("false", reader.getMetadataValue(coverageName, "HAS_ELEVATION_DOMAIN")); final String elevationMetadata = reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN"); assertNull(elevationMetadata); } // subsetting the envelope final ParameterValue<GridGeometry2D> gg = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue(); final GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope(coverageName); final GeneralEnvelope reducedEnvelope = new GeneralEnvelope( new double[] { originalEnvelope.getLowerCorner().getOrdinate(0), originalEnvelope.getLowerCorner().getOrdinate(1) }, new double[] { originalEnvelope.getMedian().getOrdinate(0), originalEnvelope.getMedian().getOrdinate(1) }); reducedEnvelope.setCoordinateReferenceSystem( reader.getCoordinateReferenceSystem(coverageName)); // Selecting bigger gridRange for a zoomed result final Dimension dim = new Dimension(); GridEnvelope gridRange = reader.getOriginalGridRange(coverageName); dim.setSize(gridRange.getSpan(0) * 4.0, gridRange.getSpan(1) * 2.0); final Rectangle rasterArea = ((GridEnvelope2D) gridRange); rasterArea.setSize(dim); final GridEnvelope2D range = new GridEnvelope2D(rasterArea); gg.setValue(new GridGeometry2D(range, reducedEnvelope)); final ParameterValue<List> time = ImageMosaicFormat.TIME.createValue(); final SimpleDateFormat formatD = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); formatD.setTimeZone(TimeZone.getTimeZone("GMT")); final Date timeD = formatD.parse("2012-04-01T00:00:00.000Z"); time.setValue( new ArrayList() { { add(timeD); } }); final ParameterValue<List> elevation = ImageMosaicFormat.ELEVATION.createValue(); elevation.setValue( new ArrayList() { { add(450d); // Elevation } }); GeneralParameterValue[] values = coverageName.equalsIgnoreCase("O3") ? new GeneralParameterValue[] {gg, time, elevation} : new GeneralParameterValue[] {gg, time}; GridCoverage2D coverage = reader.read(coverageName, values); assertNotNull(coverage); if (TestData.isInteractiveTest()) { coverage.show(); } else { PlanarImage.wrapRenderedImage(coverage.getRenderedImage()).getTiles(); } } } catch (Throwable t) { throw new RuntimeException(t); } finally { if (reader != null) { try { reader.dispose(); } catch (Throwable t) { // Does nothing } } } }
/** * Return a crop region from a specified envelope, leveraging on the grid to world transformation. * * @param refinedRequestedBBox the crop envelope * @return a {@code Rectangle} representing the crop region. * @throws TransformException in case a problem occurs when going back to raster space. * @throws DataSourceException */ private void computeCropRasterArea() throws DataSourceException { // we have nothing to crop if (cropBBox == null) { destinationRasterArea = null; return; } // // We need to invert the requested gridToWorld and then adjust the requested raster area are // accordingly // // invert the requested grid to world keeping into account the fact that it is related to cell // center // while the raster is related to cell corner MathTransform2D requestedWorldToGrid; try { requestedWorldToGrid = (MathTransform2D) PixelTranslation.translate( ProjectiveTransform.create(requestedGridToWorld), PixelInCell.CELL_CENTER, PixelInCell.CELL_CORNER) .inverse(); } catch (NoninvertibleTransformException e) { throw new DataSourceException(e); } if (destinationToSourceTransform == null || destinationToSourceTransform.isIdentity()) { // now get the requested bbox which have been already adjusted and project it back to raster // space try { destinationRasterArea = new GeneralGridEnvelope( CRS.transform(requestedWorldToGrid, new GeneralEnvelope(cropBBox)), PixelInCell.CELL_CORNER, false) .toRectangle(); } catch (IllegalStateException e) { throw new DataSourceException(e); } catch (TransformException e) { throw new DataSourceException(e); } } else { // // reproject the crop bbox back and then crop, notice that we are imposing // try { final GeneralEnvelope cropBBOXInRequestCRS = CRS.transform(cropBBox, requestedBBox.getCoordinateReferenceSystem()); cropBBOXInRequestCRS.setCoordinateReferenceSystem( requestedBBox.getCoordinateReferenceSystem()); // make sure it falls within the requested envelope cropBBOXInRequestCRS.intersect(requestedBBox); // now go back to raster space destinationRasterArea = new GeneralGridEnvelope( CRS.transform(requestedWorldToGrid, cropBBOXInRequestCRS), PixelInCell.CELL_CORNER, false) .toRectangle(); // intersect with the original requested raster space to be sure that we stay within the // requested raster area XRectangle2D.intersect(destinationRasterArea, requestedRasterArea, destinationRasterArea); } catch (NoninvertibleTransformException e) { throw new DataSourceException(e); } catch (TransformException e) { throw new DataSourceException(e); } } // is it empty?? if (destinationRasterArea.isEmpty()) { if (LOGGER.isLoggable(Level.FINE)) LOGGER.log( Level.FINE, "Requested envelope too small resulting in empty cropped raster region. cropBbox:" + cropBBox); // TODO: Future versions may define a 1x1 rectangle starting // from the lower coordinate empty = true; return; } }
/* * (non-Javadoc) * * @see org.geotools.gce.imagemosaic.jdbc.JDBCAccess#startTileDecoders(java.awt.Rectangle, * org.geotools.geometry.GeneralEnvelope, * org.geotools.gce.imagemosaic.jdbc.ImageLevelInfo, * java.util.concurrent.LinkedBlockingQueue) */ public void startTileDecoders( Rectangle pixelDimension, GeneralEnvelope requestEnvelope, ImageLevelInfo levelInfo, LinkedBlockingQueue<TileQueueElement> tileQueue, GridCoverageFactory coverageFactory) throws IOException { Date start = new Date(); Connection con = null; List<ImageDecoderThread> threads = new ArrayList<ImageDecoderThread>(); ExecutorService pool = getExecutorServivicePool(); String statementString = getGridSelectStatement(levelInfo); try { con = dataSource.getConnection(); PreparedStatement s = con.prepareStatement(statementString); setGridSelectParams(s, requestEnvelope, levelInfo); ResultSet r = s.executeQuery(); while (r.next()) { byte[] tileBytes = getTileBytes(r); Envelope env = getEnvelopeFromResultSet(r); String location = r.getString(config.getKeyAttributeNameInSpatialTable()); Rectangle2D tmp = new Rectangle2D.Double(env.getMinX(), env.getMinY(), env.getWidth(), env.getHeight()); GeneralEnvelope tileGeneralEnvelope = new GeneralEnvelope(tmp); tileGeneralEnvelope.setCoordinateReferenceSystem( requestEnvelope.getCoordinateReferenceSystem()); ImageDecoderThread thread = new ImageDecoderThread( tileBytes, location, tileGeneralEnvelope, pixelDimension, requestEnvelope, levelInfo, tileQueue, config); // thread.start(); threads.add(thread); pool.execute(thread); } ; r.close(); s.close(); // if (con.getAutoCommit() == false) { // con.commit(); // } con.close(); } catch (SQLException e) { try { // if (con.getAutoCommit() == false) { // con.rollback(); // } con.close(); } catch (SQLException e1) { } LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new IOException(e); } if (LOGGER.isLoggable(Level.INFO)) LOGGER.info( "Getting " + threads.size() + " Tiles needs " + ((new Date()).getTime() - start.getTime()) + " millisecs"); // wait for all threads dto finish and write end marker pool.shutdown(); try { pool.awaitTermination(3600, TimeUnit.SECONDS); // wait for one hour } catch (InterruptedException e) { throw new RuntimeException(e.getLocalizedMessage()); } // for (AbstractThread thread : threads) { // try { // thread.join(); // } catch (InterruptedException e) { // throw new RuntimeException(e.getLocalizedMessage()); // } // } tileQueue.add(TileQueueElement.ENDELEMENT); if (LOGGER.isLoggable(Level.INFO)) LOGGER.info( "Getting and decoding " + threads.size() + " Tiles needs " + ((new Date()).getTime() - start.getTime()) + " millisecs"); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test @Ignore public void IASI() throws Exception { final URL testURL = TestData.url(this, "IASI_C_EUMP_20121120062959_31590_eps_o_l2.nc"); final Hints hints = new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, CRS.decode("EPSG:4326", true)); // Get format final AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(testURL, hints); final NetCDFReader reader = (NetCDFReader) format.getReader(testURL, hints); assertNotNull(format); assertNotNull(reader); try { String[] names = reader.getGridCoverageNames(); assertNotNull(names); assertEquals(names.length, 20); // surface_emissivity final String coverageName = "surface_emissivity"; final String[] metadataNames = reader.getMetadataNames(coverageName); assertNotNull(metadataNames); assertEquals(14, metadataNames.length); // Parsing metadata values assertEquals("false", reader.getMetadataValue(coverageName, "HAS_TIME_DOMAIN")); assertEquals("false", reader.getMetadataValue(coverageName, "HAS_ELEVATION_DOMAIN")); assertEquals("true", reader.getMetadataValue(coverageName, "HAS_NEW_DOMAIN")); // additional domains final String newDomain = reader.getMetadataValue(coverageName, "NEW_DOMAIN"); assertNotNull(metadataNames); final String[] newDomainValues = newDomain.split(","); assertNotNull(newDomainValues); assertEquals(12, newDomainValues.length); assertEquals(13.063399669990758, Double.valueOf(newDomainValues[11]), 1E-6); assertEquals(3.6231999084702693, Double.valueOf(newDomainValues[0]), 1E-6); // subsetting the envelope final ParameterValue<GridGeometry2D> gg = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue(); final GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope(coverageName); final GeneralEnvelope reducedEnvelope = new GeneralEnvelope( new double[] { originalEnvelope.getLowerCorner().getOrdinate(0), originalEnvelope.getLowerCorner().getOrdinate(1) }, new double[] { originalEnvelope.getMedian().getOrdinate(0), originalEnvelope.getMedian().getOrdinate(1) }); reducedEnvelope.setCoordinateReferenceSystem( reader.getCoordinateReferenceSystem(coverageName)); // Selecting bigger gridRange for a zoomed result final Dimension dim = new Dimension(); GridEnvelope gridRange = reader.getOriginalGridRange(coverageName); dim.setSize(gridRange.getSpan(0) * 4.0, gridRange.getSpan(1) * 2.0); final Rectangle rasterArea = ((GridEnvelope2D) gridRange); rasterArea.setSize(dim); final GridEnvelope2D range = new GridEnvelope2D(rasterArea); gg.setValue(new GridGeometry2D(range, reducedEnvelope)); // specify additional Dimensions Set<ParameterDescriptor<List>> params = reader.getDynamicParameters(coverageName); ParameterValue<List> new_ = null; for (ParameterDescriptor param : params) { if (param.getName().getCode().equalsIgnoreCase("NEW")) { new_ = param.createValue(); new_.setValue( new ArrayList() { { add(Double.valueOf(newDomainValues[11])); } }); } } GeneralParameterValue[] values = new GeneralParameterValue[] {gg, new_}; GridCoverage2D coverage = reader.read(coverageName, values); assertNotNull(coverage); if (TestData.isInteractiveTest()) { coverage.show(); } else { PlanarImage.wrapRenderedImage(coverage.getRenderedImage()).getTiles(); } } finally { if (reader != null) { try { reader.dispose(); } catch (Throwable t) { // Does nothing } } } }