GeoTiffReader extends AbstractGridCoverage2DReader implements GridCoverage2DReader { public GridCoverage2D read(GeneralParameterValue[] params) throws IOException { GeneralEnvelope requestedEnvelope = null; Rectangle dim = null; Color inputTransparentColor=null; OverviewPolicy overviewPolicy=null; int[] suggestedTileSize=null; if (params != null) { if (params != null) { for (int i = 0; i < params.length; i++) { final ParameterValue param = (ParameterValue) params[i]; final ReferenceIdentifier name = param.getDescriptor().getName(); if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); requestedEnvelope = new GeneralEnvelope((Envelope) gg.getEnvelope2D()); dim = gg.getGridRange2D().getBounds(); continue; } if (name.equals(AbstractGridFormat.OVERVIEW_POLICY.getName())) { overviewPolicy=(OverviewPolicy) param.getValue(); continue; } if (name.equals(AbstractGridFormat.INPUT_TRANSPARENT_COLOR.getName())) { inputTransparentColor = (Color) param.getValue(); continue; } if (name.equals(AbstractGridFormat.SUGGESTED_TILE_SIZE.getName())) { String suggestedTileSize_= (String) param.getValue(); if(suggestedTileSize_!=null&&suggestedTileSize_.length()>0){ suggestedTileSize_=suggestedTileSize_.trim(); int commaPosition=suggestedTileSize_.indexOf(","); if(commaPosition<0){ int tileDim=Integer.parseInt(suggestedTileSize_); suggestedTileSize= new int[]{tileDim,tileDim}; } else { int tileW=Integer.parseInt(suggestedTileSize_.substring(0,commaPosition)); int tileH=Integer.parseInt(suggestedTileSize_.substring(commaPosition+1)); suggestedTileSize= new int[]{tileW,tileH}; } } continue; } } } } Integer imageChoice = new Integer(0); final ImageReadParam readP = new ImageReadParam(); try { imageChoice = setReadParams(overviewPolicy, readP,requestedEnvelope, dim); } catch (TransformException e) { new DataSourceException(e); } Hints newHints = null; if(suggestedTileSize!=null){ newHints= hints.clone(); final ImageLayout layout = new ImageLayout(); layout.setTileGridXOffset(0); layout.setTileGridYOffset(0); layout.setTileHeight(suggestedTileSize[1]); layout.setTileWidth(suggestedTileSize[0]); newHints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout)); } final ParameterBlock pbjRead = new ParameterBlock(); if (extOvrImgChoice >= 0 && imageChoice >= extOvrImgChoice) { pbjRead.add(ovrInStreamSPI.createInputStreamInstance(ovrSource, ImageIO.getUseCache(),ImageIO.getCacheDirectory())); pbjRead.add(imageChoice - extOvrImgChoice); } else { pbjRead.add(inStreamSPI != null ? inStreamSPI.createInputStreamInstance(source, ImageIO.getUseCache(), ImageIO.getCacheDirectory()) : ImageIO.createImageInputStream(source)); pbjRead.add(imageChoice); } pbjRead.add(Boolean.FALSE); pbjRead.add(Boolean.FALSE); pbjRead.add(Boolean.FALSE); pbjRead.add(null); pbjRead.add(null); pbjRead.add(readP); pbjRead.add(READER_SPI.createReaderInstance()); RenderedOp coverageRaster=JAI.create("ImageRead", pbjRead,newHints!=null?(RenderingHints) newHints:null); if(inputTransparentColor!=null){ coverageRaster= new ImageWorker(coverageRaster).setRenderingHints(newHints).makeColorTransparent(inputTransparentColor).getRenderedOperation(); } AffineTransform rasterToModel = getRescaledRasterToModel(coverageRaster); try { return createCoverage(coverageRaster, ProjectiveTransform.create(rasterToModel)); } catch (Exception e) { ImageUtilities.disposePlanarImageChain(coverageRaster); if (e instanceof DataSourceException) { throw (DataSourceException) e; } throw new DataSourceException(e); } } GeoTiffReader(Object input);  GeoTiffReader(Object input, Hints uHints); Format getFormat(); GridCoverage2D read(GeneralParameterValue[] params); GeoTiffIIOMetadataDecoder getMetadata(); @Override int getGridCoverageCount(); @Override GroundControlPoints getGroundControlPoints(); static final String OVERRIDE_CRS_SWITCH; }
@Test public void prjOverrideTesting1() throws IllegalArgumentException, IOException, FactoryException { final File noCrs = TestData.file(GeoTiffReaderTest.class, "override/sample.tif"); final AbstractGridFormat format = new GeoTiffFormat(); assertTrue(format.accepts(noCrs)); GeoTiffReader reader = (GeoTiffReader) format.getReader(noCrs); CoordinateReferenceSystem crs=reader.getCoordinateReferenceSystem(); final File prj= TestData.file(GeoTiffReaderTest.class, "override/sample.prj"); final CoordinateReferenceSystem crs_=new PrjFileReader(new FileInputStream(prj).getChannel()).getCoordinateReferenceSystem(); assertTrue(CRS.equalsIgnoreMetadata(crs, crs_)); GridCoverage2D coverage=reader.read(null); assertTrue(CRS.equalsIgnoreMetadata(coverage.getCoordinateReferenceSystem(), crs_)); coverage.dispose(true); }
@Test public void testReaderBadGeotiff() throws IllegalArgumentException, IOException, FactoryException { final File noCrs = TestData.file(GeoTiffReaderTest.class, "no_crs.tif"); final AbstractGridFormat format = new GeoTiffFormat(); assertTrue(format.accepts(noCrs)); GeoTiffReader reader = (GeoTiffReader) format.getReader(noCrs); CoordinateReferenceSystem crs=reader.getCoordinateReferenceSystem(); assertTrue(CRS.equalsIgnoreMetadata(crs, AbstractGridFormat.getDefaultCRS())); GridCoverage2D coverage=reader.read(null); assertTrue(CRS.equalsIgnoreMetadata(coverage.getCoordinateReferenceSystem(), AbstractGridFormat.getDefaultCRS())); crs = CRS.decode("EPSG:32632", true); final Hints hint = new Hints(); hint.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs); reader = new GeoTiffReader(noCrs, hint); GridCoverage2D coverage1 = reader.read(null); assertNotNull(coverage1); assertNotNull(coverage1.getCoordinateReferenceSystem()); assertEquals(CRS.lookupIdentifier(coverage1.getCoordinateReferenceSystem(), true), "EPSG:32632"); reader.dispose(); final File wldprjFile = TestData.file(GeoTiffReaderTest.class, "no_crs_no_envelope.tif"); assertTrue(format.accepts(wldprjFile)); reader = new GeoTiffReader(wldprjFile); GridCoverage2D coverage2 = reader.read(null); assertNotNull(coverage2); assertNotNull(coverage2.getCoordinateReferenceSystem()); assertEquals(CRS.lookupIdentifier(coverage2.getCoordinateReferenceSystem(), true), "EPSG:32632"); reader.dispose(); final File wldFile = TestData.file(GeoTiffReaderTest.class, "no_crs_no_envelope2.tif"); assertTrue(format.accepts(wldFile)); reader = new GeoTiffReader(wldFile, hint); GridCoverage2D coverage3 = reader.read(null); assertNotNull(coverage3); assertNotNull(coverage3.getCoordinateReferenceSystem()); assertEquals(CRS.lookupIdentifier(coverage3.getCoordinateReferenceSystem(), true), "EPSG:32632"); reader.dispose(); coverage1.dispose(true); coverage2.dispose(true); coverage3.dispose(true); }
@Test public void testUoMDefault() throws Exception { final File file = TestData.file(GeoTiffReaderTest.class,"no_uom.tif"); assertNotNull(file); final AbstractGridFormat format = new GeoTiffFormat(); GridCoverage2D coverage = format.getReader(file).read(null); CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem(); String crsDef = crs.toWKT(); assertTrue(crsDef.contains("UNIT[\"m\", 1.0]")); }
@Test public void testTransparencySettings() throws Exception { final AbstractGridFormat format = new GeoTiffFormat(); File file = TestData.file(GeoTiffReaderTest.class,"002025_0100_010722_l7_01_utm2.tiff"); if (format.accepts(file)) { GeoTiffReader reader = new GeoTiffReader(file, new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE)); if (reader != null) { GridCoverage2D coverage = reader.read(null); assertNotNull(coverage); assertTrue(coverage.getRenderedImage().getSampleModel().getNumBands() == 1); final ParameterValue<Color> colorPV = AbstractGridFormat.INPUT_TRANSPARENT_COLOR.createValue(); colorPV.setValue(Color.BLACK); coverage = reader.read(new GeneralParameterValue[] { colorPV }); assertNotNull(coverage); assertTrue(coverage.getRenderedImage().getSampleModel().getNumBands() == 2); if (TestData.isInteractiveTest()) coverage.show(); else PlanarImage.wrapRenderedImage(coverage.getRenderedImage()).getTiles(); } } else assertFalse(true); file = TestData.file(GeoTiffReaderTest.class,"gaarc_subset.tiff"); if (format.accepts(file)) { GeoTiffReader reader = new GeoTiffReader(file, new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE)); if (reader != null) { GridCoverage2D coverage = reader.read(null); assertNotNull(coverage); assertTrue(coverage.getRenderedImage().getSampleModel().getNumBands() == 3); final ParameterValue<Color> colorPV = AbstractGridFormat.INPUT_TRANSPARENT_COLOR.createValue(); colorPV.setValue(new Color(34,53,87)); coverage = reader.read(new GeneralParameterValue[] { colorPV }); assertNotNull(coverage); assertTrue(coverage.getRenderedImage().getSampleModel().getNumBands() == 4); if (TestData.isInteractiveTest()) coverage.show(); else PlanarImage.wrapRenderedImage(coverage.getRenderedImage()).getTiles(); } } else assertFalse(true); file = TestData.file(GeoTiffReaderTest.class,"wind.tiff"); if (format.accepts(file)) { GeoTiffReader reader = new GeoTiffReader(file, new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE)); if (reader != null) { GridCoverage2D coverage = reader.read(null); assertNotNull(coverage); assertTrue(coverage.getRenderedImage().getSampleModel().getNumBands() == 2); final ParameterValue<Color> colorPV = AbstractGridFormat.INPUT_TRANSPARENT_COLOR.createValue(); colorPV.setValue(new Color(34,53,87)); try{ coverage = reader.read(new GeneralParameterValue[] { colorPV }); assertFalse(true); } catch (Exception e) { } } } }
@Test public void testExternalOverviews() throws Exception { final File file = TestData.file(GeoTiffReaderTest.class, "ovr.tif"); assertNotNull(file); assertEquals(true, file.exists()); GeoTiffReader reader = new GeoTiffReader(file); final int nOvrs = reader.getNumOverviews(); LOGGER.info("Number of external overviews: " + nOvrs); assertEquals(4, nOvrs); double[][] availableResolutions = reader.getResolutionLevels(); assertEquals(availableResolutions.length, 5); final ParameterValue<GridGeometry2D> gg = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue(); final GeneralEnvelope envelope = reader.getOriginalEnvelope(); final Dimension dim = new Dimension(); dim.setSize(reader.getOriginalGridRange().getSpan(0)/64.0, reader.getOriginalGridRange().getSpan(1)/64.0); final Rectangle rasterArea=(( GridEnvelope2D)reader.getOriginalGridRange()); rasterArea.setSize(dim); final GridEnvelope2D range= new GridEnvelope2D(rasterArea); GridGeometry2D gridGeometry = new GridGeometry2D(range,envelope); gg.setValue(gridGeometry); GridCoverage2D coverage = reader.read(new GeneralParameterValue[] {gg}); assertEquals(coverage.getEnvelope(), reader.getOriginalEnvelope()); RenderedImage image = coverage.getRenderedImage(); assertEquals(image.getWidth(), 2); assertEquals(image.getHeight(), 2); final double delta = 0.00001; assertEquals(availableResolutions[0][0], 5, delta); assertEquals(availableResolutions[0][1], 5, delta); assertEquals(availableResolutions[1][0], 10, delta); assertEquals(availableResolutions[1][1], 10, delta); assertEquals(availableResolutions[2][0], 20, delta); assertEquals(availableResolutions[2][1], 20, delta); assertEquals(availableResolutions[3][0], 40, delta); assertEquals(availableResolutions[3][1], 40, delta); assertEquals(availableResolutions[4][0], 80, delta); assertEquals(availableResolutions[4][1], 80, delta); MathTransform transform = gridGeometry.getGridToCRS(); AffineTransform affine = (AffineTransform) transform; double resX = XAffineTransform.getScaleX0(affine); double resY = XAffineTransform.getScaleY0(affine); double[] resolutions = reader.getReadingResolutions(OverviewPolicy.QUALITY, new double[]{resX, resY}); assertEquals(resolutions[0], availableResolutions[nOvrs][0], delta); assertEquals(resolutions[1], availableResolutions[nOvrs][1], delta); resolutions = reader.getReadingResolutions(OverviewPolicy.QUALITY, new double[]{resX/8, resY/8}); assertEquals(resolutions[0], 40, delta); assertEquals(resolutions[1], 40, delta); resolutions = reader.getReadingResolutions(OverviewPolicy.QUALITY, availableResolutions[0]); assertEquals(resolutions[0], availableResolutions[0][0], delta); assertEquals(resolutions[1], availableResolutions[0][1], delta); }