public static void main(String[] args) throws Exception {
    URL imageURL = RasterTutorial.class.getResource("image.png");

    BufferedImage image = ImageIO.read(imageURL);

    // coordinates of our raster image, in lat/lon
    double minx = -92.36918018580701;
    double miny = -49.043520894708884;

    double maxx = -42.25153935511384;
    double maxy = 2.1002762835725868;

    CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
    ReferencedEnvelope envelope = new ReferencedEnvelope(minx, maxx, miny, maxy, crs);

    String name = "GridCoverage";

    GridCoverageFactory factory = new GridCoverageFactory();
    GridCoverage2D gridCoverage = (GridCoverage2D) factory.create(name, image, envelope);

    CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:24882");

    RenderingHints hints = new RenderingHints(Hints.LENIENT_DATUM_SHIFT, Boolean.TRUE);
    CoverageProcessor processor = new CoverageProcessor(hints);

    ParameterValueGroup param = processor.getOperation("Resample").getParameters();
    param.parameter("Source").setValue(gridCoverage);
    param.parameter("CoordinateReferenceSystem").setValue(targetCRS);
    param.parameter("InterpolationType").setValue("NearestNeighbor");

    GridCoverage2D reprojected = (GridCoverage2D) processor.doOperation(param);

    ViewerOld.show(gridCoverage, "Normal Grid Coverage");
    ViewerOld.show(reprojected, "Reprojected Grid Coverage");
  }
Exemple #2
0
  @Test
  public void NetCDFNoDataOperation()
      throws NoSuchAuthorityCodeException, FactoryException, IOException, ParseException {
    File mosaic = new File(TestData.file(this, "."), "NetCDFGOME2");
    if (mosaic.exists()) {
      FileUtils.deleteDirectory(mosaic);
    }
    assertTrue(mosaic.mkdirs());
    File file = TestData.file(this, "DUMMY.GOME2.NO2.PGL.nc");
    FileUtils.copyFileToDirectory(file, mosaic);
    file = new File(mosaic, "DUMMY.GOME2.NO2.PGL.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);
    GridCoverage2D gc = null;
    try {
      String[] names = reader.getGridCoverageNames();
      names = new String[] {names[0]};
      gc = reader.read(null);
    } catch (Throwable t) {
      throw new RuntimeException(t);
    } finally {
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }
    }
    // Checking NoData
    Object noData = CoverageUtilities.getNoDataProperty(gc);
    assertNotNull(noData);
    assertTrue(noData instanceof NoDataContainer);
    Double d = ((NoDataContainer) noData).getAsSingleValue();
    assertEquals(d, -999d, DELTA);
    // Try to execute an operation on the NoData and check if the result contains also NoData
    CoverageProcessor instance = CoverageProcessor.getInstance();
    Operation scale = instance.getOperation("Scale");
    ParameterValueGroup params = scale.getParameters();
    params.parameter("Source0").setValue(gc);
    params.parameter("backgroundValues").setValue(new double[] {0});
    GridCoverage2D result = (GridCoverage2D) instance.doOperation(params);
    noData = CoverageUtilities.getNoDataProperty(result);
    assertNotNull(noData);
    assertTrue(noData instanceof NoDataContainer);
    d = ((NoDataContainer) noData).getAsSingleValue();
    assertEquals(d, 0d, DELTA);
  }