Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 16th, 2010  |  syntax: None  |  size: 3.48 KB  |  hits: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.         // get a reader
  2.         final MrSIDReader reader = new MrSIDReader(file);
  3.        
  4.         //
  5.         // read once
  6.         //
  7.         GridCoverage2D gc = (GridCoverage2D) reader.read(null);
  8.  
  9.         //
  10.         // read again with subsampling and crop
  11.         //
  12.         final int originalW = gc.getRenderedImage().getWidth();
  13.         final int originalH = gc.getRenderedImage().getHeight();
  14.         final Rectangle range = ((GridEnvelope2D)reader.getOriginalGridRange());
  15.         final GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope();
  16.         final GeneralEnvelope reducedEnvelope = new GeneralEnvelope(new double[] {
  17.                 originalEnvelope.getLowerCorner().getOrdinate(0),
  18.                 originalEnvelope.getLowerCorner().getOrdinate(1)},
  19.                 new double[] { originalEnvelope.getMedian().getOrdinate(0),
  20.                         originalEnvelope.getMedian().getOrdinate(1)});
  21.         reducedEnvelope.setCoordinateReferenceSystem(reader.getCrs());
  22.  
  23.         final ParameterValue gg = (ParameterValue) ((AbstractGridFormat) reader
  24.                 .getFormat()).READ_GRIDGEOMETRY2D.createValue();
  25.         gg.setValue(new GridGeometry2D(new GridEnvelope2D(new Rectangle(0, 0,
  26.                 (int) (range.width / 2.0),
  27.                 (int) (range.height / 2.0))), reducedEnvelope));
  28.         gc = (GridCoverage2D) reader.read(new GeneralParameterValue[] { gg });
  29.         Assert.assertNotNull(gc);
  30.         // NOTE: in some cases might be too restrictive
  31.         Assert.assertTrue(reducedEnvelope.equals(gc.getEnvelope(), XAffineTransform
  32.                 .getScale(((AffineTransform) ((GridGeometry2D) gc
  33.                         .getGridGeometry()).getGridToCRS2D())) / 2, true));
  34.         // this should be fine since we give 1 pixel tolerance
  35.         Assert.assertEquals(originalW / 2.0 , gc.getRenderedImage()
  36.                 .getWidth(), 1);
  37.         Assert.assertEquals(originalH / 2.0 , gc.getRenderedImage()
  38.                 .getHeight(), 1);
  39.  
  40.         forceDataLoading(gc);
  41.  
  42.         // /////////////////////////////////////////////////////////////////////
  43.         //
  44.         // Read ignoring overviews with subsampling and crop, using Jai,
  45.         // multithreading and customized tilesize
  46.         //
  47.         // /////////////////////////////////////////////////////////////////////
  48.         final ParameterValue policy = (ParameterValue) ((AbstractGridFormat) reader
  49.                 .getFormat()).OVERVIEW_POLICY.createValue();
  50.         policy.setValue(OverviewPolicy.IGNORE);
  51.        
  52.         // //
  53.         //
  54.         // Enable multithreading read
  55.         //
  56.         // //
  57.         final ParameterValue mt = (ParameterValue) ((BaseGDALGridFormat) reader
  58.                 .getFormat()).USE_MULTITHREADING.createValue();
  59.         mt.setValue(true);
  60.        
  61.         // //
  62.         //
  63.         // Customizing Tile Size
  64.         //
  65.         // //
  66.         final ParameterValue tilesize = (ParameterValue) ((BaseGDALGridFormat) reader
  67.                 .getFormat()).SUGGESTED_TILE_SIZE.createValue();
  68.         tilesize.setValue("512,512");
  69.        
  70.         // //
  71.         //
  72.         // Setting read type: use JAI ImageRead
  73.         //
  74.         // //
  75.         final ParameterValue useJaiRead = (ParameterValue) ((BaseGDALGridFormat) reader
  76.                 .getFormat()).USE_JAI_IMAGEREAD.createValue();
  77.         useJaiRead.setValue(true);
  78.         gc = (GridCoverage2D) reader.read(new GeneralParameterValue[] { gg,
  79.                 policy, mt, tilesize, useJaiRead });