/** * @return the width of a tile in raster coordinates. */ public int getTileRasterWidth() { return this.infoFile.getTileRasterWidth(); }
/** * @param rasterCoord * in raster coordinates normally x. * @return the column number of tile which holds the given raster coordinate. */ private int getColNumber( float rasterCoord ) { int column = (int) Math.floor( rasterCoord / infoFile.getTileRasterWidth() ); if ( column < 0 ) { column = -1; } if ( column >= infoFile.columns() ) { column = infoFile.columns(); } return column; }
/** * Allocate a buffer which can hold a tile. * * @param direct * if the buffer should be direct * @param forCache * if the buffer is used for caching mechanisms. * * @return a ByteBuffer which can hold a tile. */ protected ByteBuffer allocateTileBuffer( boolean direct, boolean forCache ) { return ByteBufferPool.allocate( sampleSize * infoFile.getTileRasterHeight() * infoFile.getTileRasterWidth(), direct, forCache ); }
@Override protected void read( int columnId, int rowId, ByteBuffer buffer ) throws IOException { long begin = System.currentTimeMillis(); int tileId = getTileId( columnId, rowId ); buffer.rewind(); // transfer the data from the blob try { int blobNo = tileId / getTilesPerBlob(); int tileInBlob = tileId % getTilesPerBlob(); LOG.debug( "Tile id: {} -> pos in blob: {}", tileId, +tileInBlob ); FileChannel channel = blobChannels[blobNo]; channel.position( tileInBlob * getBytesPerTile() ); channel.read( buffer ); buffer.rewind(); } catch ( IOException e ) { LOG.error( "Error reading tile data from blob: " + e.getMessage(), e ); } if ( LOG.isDebugEnabled() ) { long elapsed = System.currentTimeMillis() - begin; LOG.debug( "Loading of tile ({}x{}) in {} ms.", new Object[] { infoFile.getTileRasterWidth(), infoFile.getTileRasterHeight(), elapsed } ); } }
@Override protected void read( int columnId, int rowId, ByteBuffer buffer ) throws IOException { int tileId = getTileId( columnId, rowId ); long begin = System.currentTimeMillis(); int tileInBlob = tileId % getTilesPerBlob(); // transfer the data from the blob try { synchronized ( LOCK ) { FileChannel channel = getFileChannel(); // MappedByteBuffer map = channel.map( MapMode.READ_ONLY, tileInBlob * getBytesPerTile(), // buffer.remaining() ); // buffer.put( map ); // map. // LOG.debug( "Tile id: {} -> pos in blob: {}", tileId, +tileInBlob ); channel.position( tileInBlob * getBytesPerTile() ); channel.read( buffer ); closeReadStream(); } // rewinding is not an option, buffer.rewind(); } catch ( IOException e ) { LOG.error( "Error reading tile data from blob: " + e.getMessage(), e ); } long elapsed = System.currentTimeMillis() - begin; if ( LOG.isDebugEnabled() ) { LOG.debug( "Loading of tile ({}x{}) in {} ms.", new Object[] { infoFile.getTileRasterWidth(), infoFile.getTileRasterHeight(), elapsed } ); } }
/** * Calculates the envelope for a tile at a given position in the grid. * * @param column * column id, must be in the range [0 ... #columns - 1] * @param row * row id, must be in the range [0 ... #rows - 1] * @return the tile's envelope */ protected Envelope getTileEnvelope( int column, int row ) { int xOffset = column * infoFile.getTileRasterWidth(); int yOffset = row * infoFile.getTileRasterHeight(); RasterRect rect = new RasterRect( xOffset, yOffset, infoFile.getTileRasterWidth(), infoFile.getTileRasterHeight() ); return infoFile.getGeoReference().getEnvelope( rect, null ); // double xOffset = columnId * tileWidth; // double yOffset = ( infoFile.rows() - rowId - 1 ) * tileHeight; // // double minX = envelope.getMin().get0() + xOffset; // double minY = envelope.getMin().get1() + yOffset; // double maxX = minX + tileWidth; // double maxY = minY + tileHeight; // // return geomFac.createEnvelope( minX, minY, maxX, maxY, envelope.getCoordinateSystem() ); }
ByteBuffer resultBuffer ) throws IOException { RasterRect tileRect = new RasterRect( infoFile.getTileRasterWidth() * tileColumn, infoFile.getTileRasterHeight() * tileRow, infoFile.getTileRasterWidth(), infoFile.getTileRasterHeight() ); RasterRect inter = RasterRect.intersection( tileRect, fRect );
/** * Read a raster from the grid file at location (row,column). * * @param columnId * * @param rowId * * @return the read raster or null if it could not be read. * @throws IOException */ public AbstractRaster getTile( int columnId, int rowId ) throws IOException { Envelope tileEnvelope = getTileEnvelope( columnId, rowId ); RasterGeoReference tileRasterReference = RasterGeoReference.create( OriginLocation.OUTER, tileEnvelope, infoFile.getTileRasterWidth(), infoFile.getTileRasterHeight() ); RasterRect tileRect = getGeoReference().createRelocatedReference( OriginLocation.OUTER ).convertEnvelopeToRasterCRS( tileEnvelope ); TileOffsetReader tReader = new TileOffsetReader( this, tileRect ); ByteBufferRasterData tileData = RasterDataFactory.createRasterData( infoFile.getTileRasterWidth(), infoFile.getTileRasterHeight(), getRasterDataInfo(), tReader, false ); SimpleRaster tile = new SimpleRaster( tileData, tileEnvelope, tileRasterReference, null ); return tile; }
/** * Instantiates this grid reader with the given information. * * @param infoFile */ protected synchronized void instantiate( GridMetaInfoFile infoFile ) { this.infoFile = infoFile; this.envelope = infoFile.getEnvelope( OriginLocation.OUTER ); this.rasterRect = new RasterRect( 0, 0, infoFile.columns() * infoFile.getTileRasterWidth(), infoFile.rows() * infoFile.getTileRasterHeight() ); this.rasterDataInfo = infoFile.getDataInfo(); // this.envelopeWidth = envelope.getMax().get0() - envelope.getMin().get0(); // this.envelopeHeight = envelope.getMax().get1() - envelope.getMin().get1(); // this.envelopeWidth = envelope.getSpan0(); // this.envelopeHeight = envelope.getSpan1(); // this.rows = infoFile.rows(); // this.columns = infoFile.columns(); // this.tileSamplesX = infoFile.getTileSamplesX(); // this.tileSamplesY = infoFile.getTileSamplesY(); // this.tileWidth = envelope.getSpan0() / infoFile.columns(); // this.tileHeight = envelope.getSpan1() / infoFile.rows(); this.tilesPerBlob = infoFile.columns() * infoFile.rows(); this.sampleSize = ( rasterDataInfo.getDataType().getSize() * rasterDataInfo.bands() ); this.bytesPerTile = infoFile.getTileRasterWidth() * infoFile.getTileRasterHeight() * sampleSize; }
/** * @param gMif * @param width * @param height */ private CacheInfoFile( GridMetaInfoFile gMif, int width, int height, boolean[][] tilesOnFile, long modificationTime ) { super( gMif.getGeoReference(), gMif.rows(), gMif.columns(), gMif.getTileRasterWidth(), gMif.getTileRasterHeight(), gMif.getDataInfo() ); this.rWidth = width; this.rHeight = height; this.tilesOnFile = tilesOnFile; this.modificationTime = modificationTime; this.envelope = gMif.getGeoReference().getEnvelope( OriginLocation.OUTER, rWidth, rHeight, envelope.getCoordinateSystem() ); }
/** * Creates a new {@link GriddedTileContainer} instances. * * @param infoFile * containing the relevant information. * */ protected GriddedTileContainer( GridMetaInfoFile infoFile ) { this.rdi = infoFile.getDataInfo(); this.envelope = infoFile.getEnvelope( OriginLocation.OUTER ); this.envelopeWidth = envelope.getMax().get0() - envelope.getMin().get0(); this.envelopeHeight = envelope.getMax().get1() - envelope.getMin().get1(); // this.envelopeWidth = envelope.getSpan0(); // this.envelopeHeight = envelope.getSpan1(); this.rows = infoFile.rows(); this.columns = infoFile.columns(); this.tileSamplesX = infoFile.getTileRasterWidth(); this.tileSamplesY = infoFile.getTileRasterHeight(); this.tileWidth = envelopeWidth / columns; this.tileHeight = envelopeHeight / rows; this.rasterReference = infoFile.getGeoReference(); LOG.debug( "envelope: " + envelope ); LOG.debug( "raster reference: " + rasterReference ); SampleResolution sr = new SampleResolution( new double[] { rasterReference.getResolutionX(), rasterReference.getResolutionY() } ); this.resolutionInfo = new ResolutionInfo( sr ); }