/** * Create an intersection of the given RasterRects, if the given rasterects do not intersect, null will be returned. * * @param first * @param second * @return the intersection or <code>null</code> if the given rectangles do not intersect. */ public static final RasterRect intersection( RasterRect first, RasterRect second ) { int fmaxX = first.x + first.width; int smaxX = second.x + second.width; int fmaxY = first.y + first.height; int smaxY = second.y + second.height; if ( second.x >= fmaxX || smaxX <= first.x || second.y >= fmaxY || smaxY <= first.y ) { /* right outside || left outside || bottom outside || top outside */ return null; } int x = Math.max( first.x, second.x ); int y = Math.max( first.y, second.y ); int width = Math.min( smaxX, fmaxX ) - x; int height = Math.min( smaxY, fmaxY ) - y; // y values return new RasterRect( x, y, width, height ); } }
@Override public boolean equals( Object other ) { if ( other != null && other instanceof DataView ) { final DataView that = (DataView) other; return super.equals( that ) && this.dataInfo.equals( that.dataInfo ); } return false; }
this.maxDataWidth = maxDataWidth; this.maxDataHeight = maxDataHeight; RasterRect origData = new RasterRect( 0, 0, maxDataWidth, maxDataHeight ); maxViewData = RasterRect.intersection( origData, view ); if ( maxViewData == null ) { maxViewData = new RasterRect( 0, 0, 0, 0 );
RasterRect tileRect = new RasterRect( entry.getRasterRect() ); try { BufferResult read = cachedReader.read( tileRect, result ); if ( !rect.equals( tileRect ) ) {
/** * Get intersection of the requested rectangle with the rectangle of the grid file. * * @param original * @return the intersection with the grids raster rectangle and the given. */ protected RasterRect snapToGrid( RasterRect original ) { return RasterRect.intersection( rasterRect, original ); }
/** * @param floatBuffer * @param noData * @param bufferDomain * @param view */ public RawDataBufferFloat( ByteBuffer floatBuffer, float noData, RasterRect bufferDomain, DataView view ) { super( DataBuffer.TYPE_FLOAT, floatBuffer.capacity() / SIZE ); this.floatBuffer = floatBuffer; this.noData = Float.floatToIntBits( noData ); this.maxViewData = RasterRect.intersection( bufferDomain, view ); toNullPoint = ( ( bufferDomain.width * maxViewData.y ) + maxViewData.x ); lineStride = bufferDomain.width; }
/** * Creates a RasterData object object with given size, number of bands, data type and interleaving. The view will be * 0, 0, width, height, the default raster cache location will be used. * * @param width * width of the raster * @param height * height of the raster * @param dataInfo * defining the bands, datatype and interleave type. * @param reader * to get the data from, maybe <code>null</code> * @param addToCache * if the reader should be added to the cache (almost always should be true). * @return new RasterData */ public static ByteBufferRasterData createRasterData( int width, int height, RasterDataInfo dataInfo, RasterReader reader, boolean addToCache ) { return createRasterData( new RasterRect( 0, 0, width, height ), dataInfo, reader, addToCache, null ); }
@Override public void setFloats( int x, int y, int width, int height, int band, float[] values ) { RasterRect requestedRect = new RasterRect( x, y, width, height ); int[] interSectingTiles = getIntersectingTiles( requestedRect ); if ( interSectingTiles != null ) { final RasterRect tileRect = new RasterRect( 0, 0, tileWidth, tileHeight ); for ( int row = interSectingTiles[1]; row < interSectingTiles[3]; ++row ) { for ( int col = interSectingTiles[0]; col < interSectingTiles[2]; ++col ) { tileRect.y = row * tileHeight; final RasterRect intersectionWithTile = RasterRect.intersection( tileRect, requestedRect ); if ( intersectionWithTile != null ) { final int heightOffset = ( intersectionWithTile.y / tileHeight ); final RasterRect tReq = new RasterRect( ( intersectionWithTile.x - widthOffset ), ( intersectionWithTile.y - heightOffset ), intersectionWithTile.width,
ByteBuffer destBuffer, int sampleSize ) throws IOException { RasterRect inter = RasterRect.intersection( srcRect, destRect ); if ( inter != null ) {
/** * Returns an Envelope for a raster with given size. * * The calculation considers the origin and resolution of the raster. * * @param targetLocation * of the origin, specifies if the the newly created envelope should consider the origin located at the * OUTER or CENTER of a pixel. * @param width * in raster coordinates * @param height * in raster coordinates * @param crs * the coordinate system for the envelope * * @return the calculated envelope */ public Envelope getEnvelope( OriginLocation targetLocation, int width, int height, ICRS crs ) { return getEnvelope( targetLocation, new RasterRect( 0, 0, width, height ), crs ); }
@Override public void setShorts( int x, int y, int width, int height, int band, short[] values ) { RasterRect requestedRect = new RasterRect( x, y, width, height ); int[] interSectingTiles = getIntersectingTiles( requestedRect ); if ( interSectingTiles != null ) { RasterRect tileRect = new RasterRect( 0, 0, tileWidth, tileHeight ); for ( int row = interSectingTiles[1]; row < interSectingTiles[3]; ++row ) { for ( int col = interSectingTiles[0]; col < interSectingTiles[2]; ++col ) { tileRect.y = row * tileHeight; final RasterRect intersectionWithTile = RasterRect.intersection( tileRect, requestedRect ); if ( intersectionWithTile != null ) { final int heightOffset = ( intersectionWithTile.y / tileHeight ); final RasterRect tReq = new RasterRect( ( intersectionWithTile.x - widthOffset ), ( intersectionWithTile.y - heightOffset ), intersectionWithTile.width,
/** * 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. */ private final int[] getIntersectingTiles( RasterRect rect ) { RasterRect fRect = RasterRect.intersection( sampleDomain, rect ); if ( fRect != null ) { final int minCol = getColNumber( fRect.x ); final int minRow = getRowNumber( fRect.y ); final int maxCol = getColNumber( fRect.x + fRect.width ); final int maxRow = getRowNumber( fRect.y + fRect.height ); if ( ( maxCol != -1 ) && ( maxRow != -1 ) && ( minCol != columns ) && ( minRow != rows ) ) { return new int[] { minCol, minRow, maxCol, maxRow }; } } return null; }
/** * Calculates the envelope for a tile at a given position in the grid. * * @param column * column , must be in the range [0 ... #columns - 1] * @param row * row , must be in the range [0 ... #rows - 1] * @return the tile's envelope */ protected Envelope getTileEnvelope( int column, int row ) { int xOffset = column * tileRasterWidth; int yOffset = row * tileRasterHeight; RasterRect rect = new RasterRect( xOffset, yOffset, tileRasterWidth, tileRasterHeight ); return this.geoRef.getEnvelope( rect, null ); // double xOffset = column * tileWidth; // double yOffset = ( rows - row - 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() ); }
@Override public void setInts( int x, int y, int width, int height, int band, int[] values ) { RasterRect requestedRect = new RasterRect( x, y, width, height ); int[] interSectingTiles = getIntersectingTiles( requestedRect ); if ( interSectingTiles != null ) { RasterRect tileRect = new RasterRect( 0, 0, tileWidth, tileHeight ); for ( int row = interSectingTiles[1]; row < interSectingTiles[3]; ++row ) { for ( int col = interSectingTiles[0]; col < interSectingTiles[2]; ++col ) { tileRect.y = row * tileHeight; final RasterRect intersectionWithTile = RasterRect.intersection( tileRect, requestedRect ); if ( intersectionWithTile != null ) { final int heightOffset = ( intersectionWithTile.y / tileHeight ); final RasterRect tReq = new RasterRect( ( intersectionWithTile.x - widthOffset ), ( intersectionWithTile.y - heightOffset ), intersectionWithTile.width,
/** * Creates a RasterData object object with given size, number of bands, data type and interleaving filled with the * given ByteArray. A cachereader will be created. * * @param width * width of the raster * @param height * height of the raster * @param rdi * containing the number of bands, the data type and the interleave type. * @param geoRef * needed for the reader. * @param byteBuffer * on which the memory reader will operate upon. * @param createCacheFile * true if a cache file should back the data * @param cacheId * the name of the cache file to use in the default {@link RasterCache}. * @param options * which can be used to get the cache data from. * @return new RasterData */ public static ByteBufferRasterData createRasterData( int width, int height, RasterDataInfo rdi, RasterGeoReference geoRef, ByteBuffer byteBuffer, boolean createCacheFile, String cacheId, RasterIOOptions options ) { return createRasterData( new RasterRect( 0, 0, width, height ), rdi, geoRef, byteBuffer, createCacheFile, cacheId, options ); }
@Override public void setBytes( int x, int y, int width, int height, int band, byte[] values ) { RasterRect requestedRect = new RasterRect( x, y, width, height ); int[] interSectingTiles = getIntersectingTiles( requestedRect ); if ( interSectingTiles != null ) { final RasterRect tileRect = new RasterRect( 0, 0, tileWidth, tileHeight ); for ( int row = interSectingTiles[1]; row < interSectingTiles[3]; ++row ) { for ( int col = interSectingTiles[0]; col < interSectingTiles[2]; ++col ) { tileRect.y = row * tileHeight; final RasterRect intersectionWithTile = RasterRect.intersection( tileRect, requestedRect ); if ( intersectionWithTile != null ) { final int heightOffset = ( intersectionWithTile.y / tileHeight ); final RasterRect tReq = new RasterRect( ( intersectionWithTile.x - widthOffset ), ( intersectionWithTile.y - heightOffset ), intersectionWithTile.width,
@Override public BufferResult read( RasterRect rect, ByteBuffer buffer ) throws IOException { RasterRect tmpRect = new RasterRect( rect.x + tileRectInGrid.x, rect.y + tileRectInGrid.y, rect.width, rect.height ); BufferResult bufferResult = originalReader.read( tmpRect, buffer ); bufferResult.getResult().clear(); // PixelInterleavedRasterData rd = new PixelInterleavedRasterData( bufferResult.getRect(), // bufferResult.getRect().width, // bufferResult.getRect().height, // new RasterDataInfo( BandType.RGB, // DataType.BYTE, // InterleaveType.PIXEL ) ); // rd.setByteBuffer( bufferResult.getResult() ); // BufferedImage image = RasterFactory.rasterDataToImage( rd ); // ImageIO.write( image, "png", new File( "/tmp/" + tmpRect.toString() + ".png" ) ); // bufferResult.getRect().x -= tileRectInGrid.x; // bufferResult.getRect().y -= tileRectInGrid.y; return bufferResult; }
@Override public void setDoubles( int x, int y, int width, int height, int band, double[] values ) { RasterRect requestedRect = new RasterRect( x, y, width, height ); int[] interSectingTiles = getIntersectingTiles( requestedRect ); if ( interSectingTiles != null ) { final RasterRect tileRect = new RasterRect( 0, 0, tileWidth, tileHeight ); for ( int row = interSectingTiles[1]; row < interSectingTiles[3]; ++row ) { for ( int col = interSectingTiles[0]; col < interSectingTiles[2]; ++col ) { tileRect.y = row * tileHeight; final RasterRect intersectionWithTile = RasterRect.intersection( tileRect, requestedRect ); if ( intersectionWithTile != null ) { final int heightOffset = ( intersectionWithTile.y / tileHeight ); final RasterRect tReq = new RasterRect( ( intersectionWithTile.x - widthOffset ), ( intersectionWithTile.y - heightOffset ), intersectionWithTile.width,
private void copyValuesFromTile( int tileColumn, int tileRow, RasterRect dstRect, ByteBuffer srcBuffer, ByteBuffer dstBuffer ) throws IOException { int tileWidth = getTileRasterWidth(); int tileHeight = getTileRasterHeight(); RasterRect tileRect = new RasterRect( tileWidth * tileColumn, tileHeight * tileRow, tileWidth, tileHeight ); Rasters.copyValuesFromTile( tileRect, dstRect, srcBuffer, dstBuffer, sampleSize ); }
@Override public short[] getShorts( int x, int y, int width, int height, int band, short[] result ) { RasterRect requestedRect = new RasterRect( x, y, width, height ); int[] interSectingTiles = getIntersectingTiles( requestedRect ); short[] resultValues = result; final RasterRect tileRect = new RasterRect( 0, 0, tileWidth, tileHeight ); for ( int row = interSectingTiles[1]; row < interSectingTiles[3]; ++row ) { for ( int col = interSectingTiles[0]; col < interSectingTiles[2]; ++col ) { tileRect.y = row * tileHeight; final RasterRect intersectionWithTile = RasterRect.intersection( tileRect, requestedRect ); if ( intersectionWithTile != null ) { final int heightOffset = ( intersectionWithTile.y / tileHeight ); final RasterRect tReq = new RasterRect( ( intersectionWithTile.x - widthOffset ), ( intersectionWithTile.y - heightOffset ), intersectionWithTile.width,