/** * Writes the metadata file for this grid file. * * @param options * @return the file containing the metainfo * @throws IOException */ public File writeMetadataFile( RasterIOOptions options ) throws IOException { File metaInfo = null; if ( gridFile != null ) { metaInfo = GridMetaInfoFile.fileNameFromOptions( gridFile.getParent(), FileUtils.getFilename( gridFile ), options ); } else { throw new IOException( "No gridfile specified, could not write the info file" ); } GridMetaInfoFile.writeToFile( metaInfo, new GridMetaInfoFile( this.geoRef, this.rows, this.columns, this.tileRasterWidth, this.tileRasterHeight, this.dataInfo ), options ); return metaInfo; }
/** * @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() ); }
/** * @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; }
/** * @param rasterCoord * normally the y. * @return the row number of tile which holds the given raster coordinate. */ private int getRowNumber( float rasterCoord ) { int row = (int) Math.floor( rasterCoord / infoFile.getTileRasterHeight() ); if ( row < 0 ) { row = -1; } if ( row >= infoFile.rows() ) { row = infoFile.rows(); } return row; }
if ( cacheFile != null && cacheFile.exists() ) { String parent = cacheFile.getParent(); File metaInfo = GridMetaInfoFile.fileNameFromOptions( parent, FileUtils.getFilename( cacheFile ), null ); if ( !metaInfo.exists() ) { LOG.warn( try { reader = new BufferedReader( new FileReader( metaInfo ) ); GridMetaInfoFile gmif = GridMetaInfoFile.read( reader, null ); if ( gmif == null ) { throw new NullPointerException( "no info file could be read" ); boolean[][] tilesOnFile = new boolean[gmif.rows()][gmif.columns()]; int rows = gmif.rows(); for ( int row = 0; row < rows; ++row ) { String s = reader.readLine(); if ( s == null || s.length() != gmif.columns() ) { throw new NullPointerException( "the number of rows|columns read was not correct" );
/** * 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 ); }
/** * 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() ); }
/** * A gridded tile container which reads data from a deegree internal format. See d3_tools/RasterTreeGridifier on how * to create the format. This methods takes a single bob file instead of scanning a given directory * * @param blobFile * to read the tiles from. * @param metaInfoFile * @throws IOException */ public GriddedBlobTileContainer( File blobFile, GridMetaInfoFile metaInfoFile ) throws IOException { this( metaInfoFile ); if ( !blobFile.exists() ) { throw new IOException( "Given blobfile:" + blobFile + " does not exist." ); } long totalSize = blobFile.length(); LOG.debug( "Real blob size: " + totalSize ); // blobReaders = new GridFileReader[1]; blobReader = new GridFileReader( metaInfoFile, blobFile ); // tilesPerBlob = blobReader.getNumberOfTiles(); long expectedBlobSize = metaInfoFile.rows() * metaInfoFile.columns() * blobReader.getBytesPerTile(); if ( expectedBlobSize != totalSize ) { String msg = "Size of gridfile (=" + totalSize + ") does not match the expected size (=" + expectedBlobSize + ")."; throw new IllegalArgumentException( msg ); } }
/** * @param options * @throws IOException * @throws NumberFormatException */ private synchronized void instantiate( File gridFile, RasterIOOptions options ) throws NumberFormatException, IOException { if ( infoFile == null && gridFile != null ) { this.dataLocationId = options == null ? FileUtils.getFilename( gridFile ) : options.get( RasterIOOptions.ORIGIN_OF_RASTER ); File metaInfo = GridMetaInfoFile.fileNameFromOptions( gridFile.getParent(), FileUtils.getFilename( gridFile ), options ); this.instantiate( GridMetaInfoFile.readFromFile( metaInfo, options ), gridFile ); } }
/** * Calculates the id for a tile at a given position in the grid. * * @param columnId * column id, must be in the range [0 ... #columns - 1] * @param rowId * row id, must be in the range [0 ... #rows - 1] * @return the tile's id */ protected int getTileId( int columnId, int rowId ) { return rowId * infoFile.columns() + columnId; }
@Override public RasterGeoReference getGeoReference() { return infoFile.getGeoReference(); }
/** * Delete the file this cache reader is using. * * @return true if this cached reader no longer references an existing file. */ public boolean deleteCacheFile() { boolean result = true; synchronized ( LOCK ) { super.dispose(); File f = file(); if ( f != null ) { if ( f.exists() && f.isFile() ) { result = f.delete(); File metaInfo = GridMetaInfoFile.fileNameFromOptions( f.getParent(), FileUtils.getFilename( f ), null ); if ( metaInfo.exists() ) { boolean mR = metaInfo.delete(); if ( !mR ) { LOG.warn( "Could not delete meta info file for raster cache file: " + f.getAbsolutePath() ); } } } } } return result; }
/** * @return the width of a tile in raster coordinates. */ public int getTileRasterWidth() { return this.infoFile.getTileRasterWidth(); }
/** * @return the height of a tile in raster coordinates. */ public int getTileRasterHeight() { return this.infoFile.getTileRasterHeight(); }
/** * Read the grid info file which is basically a world file with supplement information. * * @param infoFile * to read. * @param options * containing information about the crs, origin location etc. * @return an new {@link GridMetaInfoFile} read from the given file. * @throws NumberFormatException * @throws IOException */ public static GridMetaInfoFile readFromFile( File infoFile, RasterIOOptions options ) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader( new FileReader( infoFile ) ); GridMetaInfoFile gmi = read( br, options ); br.close(); return gmi; }
/** * Reads the index file, 'gridded_raster.info' from the given directory reads the world file information as well as * the number of tiles. * * @param dir * to read the gridded_raster.info * @param options * which will hold information on the file. * @return a {@link GriddedBlobTileContainer} instantiated with the values read from the gridded_raster.info. * @throws IOException */ public static GriddedBlobTileContainer create( File dir, RasterIOOptions options ) throws IOException { File metaInfoFile = new File( dir, METAINFO_FILE_NAME ); if ( !metaInfoFile.exists() ) { throw new IOException( "Could not find the " + METAINFO_FILE_NAME + " in the given directory, " + dir ); } GridMetaInfoFile readFromFile = GridMetaInfoFile.readFromFile( metaInfoFile, options ); return new GriddedBlobTileContainer( readFromFile, dir ); }
return new GridMetaInfoFile( worldFile, rows, columns, tileSamplesX, tileSamplesY, bands, type );
@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 } ); } }
/** * Returns the min column, row and max column row of the given rect. The rectangle will be cut off to fit the data. * If the rect does not intersect the data, <code>null</code> will be returned. * * @param rect * @return {min column, min row, max column, max row} or <code>null</code> if the given rect does not intersect the * data. */ protected int[] getIntersectingTiles( RasterRect rect ) { RasterRect fRect = snapToGrid( rect ); if ( fRect != null ) { int minCol = getColNumber( fRect.x ); int minRow = getRowNumber( fRect.y ); int maxCol = getColNumber( fRect.x + fRect.width ); int maxRow = getRowNumber( fRect.y + fRect.height ); if ( ( maxCol != -1 ) && ( maxRow != -1 ) && ( minCol != infoFile.columns() ) && ( minRow != infoFile.rows() ) ) { return new int[] { minCol, minRow, maxCol, maxRow }; } } return null; }
/** * @return the number of grid columns */ public int getTileColumns() { return infoFile.columns(); }