/** * @param env * @param width * @param height * @return max(resx, resy) */ public static double calcResolution( Envelope env, int width, int height ) { return max( env.getSpan0() / width, env.getSpan1() / height ); }
/** * @param env * @param width * @param height * @return max(resx, resy) */ public static double calcResolution( Envelope env, int width, int height ) { return max( env.getSpan0() / width, env.getSpan1() / height ); }
/** * @return the lowest resolution of the grid */ public double getResolution() { double xRes = targetEnvelope.getSpan0() / width; double yRes = targetEnvelope.getSpan1() / height; double zRes = Double.MAX_VALUE; if ( depth != Integer.MIN_VALUE ) { double envDepth = getEnvDepth( targetEnvelope ); if ( !Double.isNaN( envDepth ) ) { zRes = envDepth / depth; } } return min( xRes, min( yRes, zRes ) ); }
/** * Calculate the resolution for the first raster level. * * @param srcWidth * the width of the source raster (in pixel) * @param srcHeight * the height of the source raster (in pixel) * @param dstEnv * the envelope of the target raster (in target SRS) * @param tileSize * the size of each tile (in pixel) * @return the resolution in unit/pixel */ public static double calculateBaseResolution( int srcWidth, int srcHeight, Envelope dstEnv, int tileSize ) { int xTiles = (int) Math.ceil( srcWidth / (double) tileSize ); int yTiles = (int) Math.ceil( srcHeight / (double) tileSize ); double xRes = dstEnv.getSpan0() / srcWidth; double yRes = dstEnv.getSpan1() / srcHeight; double deltaX = xTiles * tileSize * xRes; double deltaY = yTiles * tileSize * yRes; double diagSize = Math.sqrt( deltaX * deltaX + deltaY * deltaY ); double pixelSize = diagSize / Math.sqrt( Math.pow( xTiles * tileSize, 2 ) + Math.pow( yTiles * tileSize, 2 ) ); return pixelSize; }
/** * Calculate the optimal tile size, so that the last tile in a raster tree consist of exactly one tile (no borders). * * @param srcWidth * the width of the source raster (in pixel) * @param srcHeight * the height of the source raster (in pixel) * @param dstEnv * the envelope of the target raster (in target SRS) * @param maxTileSize * the maximum size of each tile (in pixel) * @return the optimal tile size */ public static int calculateOptimalTileSize( int srcWidth, int srcHeight, Envelope dstEnv, int maxTileSize ) { // calculate the new size, consider the aspect ratio to get square pixels double deltaX = dstEnv.getSpan0(); double deltaY = dstEnv.getSpan1(); double diagSize = Math.sqrt( deltaX * deltaX + deltaY * deltaY ); // pixelSize for calculation of the new image size double pixelSize = diagSize / Math.sqrt( Math.pow( srcWidth, 2 ) + Math.pow( srcHeight, 2 ) ); int dstHeight; int dstWidth; int levels = 0; do { levels += 1; dstHeight = (int) ( deltaY / ( pixelSize * pow( 2, levels ) ) + 0.5 ); dstWidth = (int) ( deltaX / ( pixelSize * pow( 2, levels ) ) + 0.5 ); } while ( Math.max( dstHeight, dstWidth ) > maxTileSize ); return Math.max( dstHeight, dstWidth ); }
private TileMatrix buildTileMatrix( SpatialMetadata envelopeAndCrs, Band overview ) { int tw = overview.GetBlockXSize(); int th = overview.GetBlockYSize(); int width = overview.getXSize(); int height = overview.getYSize(); int numx = (int) Math.ceil( (double) width / (double) tw ); int numy = (int) Math.ceil( (double) height / (double) th ); Envelope env = envelopeAndCrs.getEnvelope(); double res = Math.max( env.getSpan0() / width, env.getSpan1() / height ); String id = Double.toString( res / DEFAULT_PIXEL_SIZE ); return new TileMatrix( id, envelopeAndCrs, BigInteger.valueOf( tw ), BigInteger.valueOf( th ), res, BigInteger.valueOf( numx ), BigInteger.valueOf( numy ) ); } }
private float[] calcOptimalEye( Envelope bBox ) { float[] eye = new float[] { 0, 1, 1 }; if ( bBox != null ) { double[] min = bBox.getMin().getAsArray(); double[] max = bBox.getMax().getAsArray(); double centerX = min[0] + ( ( max[0] - min[0] ) * 0.5f ); double centerY = min[1] + ( ( max[1] - min[1] ) * 0.5f ); // float centerZ = bBox[2] + ( ( bBox[2] - bBox[5] ) * 0.5f ); double eyeZ = 2 * ( ( Math.max( bBox.getSpan0(), bBox.getSpan1() ) / 2 ) / Math.tan( Math.toRadians( fov * 0.5 ) ) ); eye = new float[] { (float) centerX, (float) centerY, (float) eyeZ }; } return eye; }
/** * Create a Gird for the given pixel resolution and output envelope * * @param resx * resolution of a pixel (ie. the width of each grid cell in CRS units) * @param resy * resolution of a pixel (ie. the height of each grid cell in CRS units) * @param resz * resolution of a pixel (ie. the depth of each grid cell in CRS units) * @param targetEnvelope * the envelope of the output grid * @return a new grid */ public static Grid fromRes( double resx, double resy, double resz, Envelope targetEnvelope ) { int width = (int) Math.round( targetEnvelope.getSpan0() / resx ); int height = (int) Math.round( targetEnvelope.getSpan1() / resy ); int depth = Integer.MIN_VALUE; if ( !Double.isNaN( resz ) ) { if ( targetEnvelope.getCoordinateDimension() == 3 ) { double envDepth = getEnvDepth( targetEnvelope ); depth = (int) Math.round( envDepth / resz ); } } return new Grid( width, height, depth, targetEnvelope ); }
public GetMap( List<Pair<String, String>> layers, int width, int height, Envelope boundingBox, String format, boolean transparent ) throws OWSException { for ( Pair<String, String> layer : layers ) { this.layers.add( new LayerRef( layer.first ) ); this.styles.add( layer.second != null ? new StyleRef( layer.second ) : null ); } this.width = width; this.height = height; this.bbox = boundingBox; this.crs = boundingBox.getCoordinateSystem(); this.bgcolor = white; this.format = format; this.transparent = transparent; try { scale = RenderHelper.calcScaleWMS130( width, height, bbox, crs, pixelSize ); LOG.debug( "GetMap request has a WMS 1.3.0/SLD scale of '{}' (adapted to pixel size of {}).", scale, pixelSize ); resolution = max( bbox.getSpan0() / width, bbox.getSpan1() / height ); LOG.debug( "Resolution per pixel is {}.", resolution ); } catch ( ReferenceResolvingException e ) { LOG.trace( "Stack trace:", e ); throw new OWSException( e.getLocalizedMessage(), "InvalidParameterValue" ); } }
/** * @param map * @param version * @param service * @throws OWSException */ public GetMap( Map<String, String> map, Version version, MapOptionsMaps exts ) throws OWSException { if ( version.equals( VERSION_111 ) ) { parse111( map, exts ); } if ( version.equals( VERSION_130 ) ) { parse130( map, exts ); } parameterMap.putAll( map ); try { scale = RenderHelper.calcScaleWMS130( width, height, bbox, crs, pixelSize ); LOG.debug( "GetMap request has a WMS 1.3.0/SLD scale of '{}' (adapted to pixel size of {}).", scale, pixelSize ); resolution = max( bbox.getSpan0() / width, bbox.getSpan1() / height ); LOG.debug( "Resolution per pixel is {}.", resolution ); } catch ( ReferenceResolvingException e ) { LOG.trace( "Stack trace:", e ); LOG.warn( "The scale of a GetMap request could not be calculated: '{}'.", e.getLocalizedMessage() ); } }
private boolean isDefaultBBox() { Envelope env = getDefaultBBox(); return ( Math.abs( bbox.getSpan0() - env.getSpan0() ) < 1E-11 ) && ( Math.abs( bbox.getSpan1() - env.getSpan1() ) < 1E-11 ) && ( Math.abs( bbox.getMin().get0() - env.getMin().get0() ) < 1E-11 ) && ( Math.abs( bbox.getMin().get1() - env.getMin().get1() ) < 1E-11 ) && ( Math.abs( bbox.getMin().get2() - env.getMin().get2() ) < 1E-11 ); }
public static Pair<Envelope, DoublePair> getWorldToScreenTransform( AffineTransform worldToScreen, Envelope bbox, int width, int height ) { // we have to flip horizontally, so invert y-axis and move y-axis with screen height worldToScreen.scale( 1, -1 ); worldToScreen.translate( 0, -height ); // calculate scalex, scaley and swap axis if necessary final double scalex, scaley; final ICRS crs = bbox.getCoordinateSystem(); if ( isXyOrdered( crs ) ) { scalex = width / bbox.getSpan0(); scaley = height / bbox.getSpan1(); } else { worldToScreen.scale( -1, 1 ); worldToScreen.rotate( Math.PI / 2 ); scalex = height / bbox.getSpan0(); scaley = width / bbox.getSpan1(); } worldToScreen.scale( scalex, scaley ); worldToScreen.translate( -bbox.getMin().get0(), -bbox.getMin().get1() ); return new Pair<Envelope, DoublePair>( bbox, new DoublePair( scalex, scaley ) ); }
/** * Creates a new {@link GdalDataset} from the given file (which must be supported by GDAL). * * @param file * raster image file (format must be supported by GDAL), never <code>null</code> * @param crs * native CRS, can be <code>null</code> (unknown) * @throws UnknownCRSException * @throws IOException */ public GdalDataset( File file, ICRS crs ) throws UnknownCRSException, IOException { this.file = file.getCanonicalFile(); this.crs = crs; dataset = gdal.Open( file.getPath() ); datasetEnvelope = readEnvelope(); width = datasetEnvelope.getSpan0(); height = datasetEnvelope.getSpan1(); datasetPixelsX = dataset.getRasterXSize(); datasetPixelsY = dataset.getRasterYSize(); unitsPerPixelX = width / (double) datasetPixelsX; unitsPerPixelY = height / (double) datasetPixelsY; }
/** * Create a tile grid based on the output envelope, tile size and target pixel resolution. * * @param dstEnv * the envelope of the target raster (in target SRS) * @param tileSize * the size of each tile (in pixel) * @param resolution * the pixel resolution (units/pixel) * @return a new TileGrid */ static TileGrid createTileGrid( Envelope dstEnv, int tileSize, float resolution ) { // TODO // double precision = dstEnv.getPrecision(); double precision = 0.0; float tileDimension = tileSize * resolution; int xTiles = (int) Math.ceil( dstEnv.getSpan0() / tileDimension ); int yTiles = (int) Math.ceil( dstEnv.getSpan1() / tileDimension ); double x0 = dstEnv.getMin().get0(); double y0 = dstEnv.getMin().get1(); ICRS crs = dstEnv.getCoordinateSystem(); return new TileGrid( x0, y0, tileDimension, tileDimension, xTiles, yTiles, crs, precision ); }
public Envelope calcClickBox( int radius ) { radius = parameters.get( "RADIUS" ) == null ? radius : parseInt( parameters.get( "RADIUS" ) ); GeometryFactory fac = new GeometryFactory(); double dw = envelope.getSpan0() / width; double dh = envelope.getSpan1() / height; int r2 = radius / 2; r2 = r2 <= 0 ? 1 : r2; return fac.createEnvelope( new double[] { envelope.getMin().get0() + ( x - r2 ) * dw, envelope.getMax().get1() - ( y + r2 ) * dh }, new double[] { envelope.getMin().get0() + ( x + r2 ) * dw, envelope.getMax().get1() - ( y - r2 ) * dh }, envelope.getCoordinateSystem() ); } }
private double[] getGeoTransform( Envelope bbox, int pixelsX, int pixelsY ) { if ( isXy( bbox.getCoordinateSystem() ) ) { double originX = bbox.getMin().get0(); double pixelSizeX = bbox.getSpan0() / pixelsX; double originY = bbox.getMax().get1(); double pixelSizeY = -bbox.getSpan1() / pixelsY; return new double[] { originX, pixelSizeX, 0.0, originY, 0.0, pixelSizeY }; } double originX = bbox.getMin().get1(); double pixelSizeX = bbox.getSpan1() / pixelsX; double originY = bbox.getMax().get0(); double pixelSizeY = -bbox.getSpan0() / pixelsY; return new double[] { originX, pixelSizeX, 0.0, originY, 0.0, pixelSizeY }; }
TileDataSet buildTileDataSet( GeoTIFFTileStoreJAXB.TileDataSet cfg, ResourceLocation<TileStore> location, Envelope envelope ) { String filename = cfg.getFile(); String format = cfg.getImageFormat(); String tmsId = cfg.getTileMatrixSetId(); File file = location.resolveToFile( filename ); TileMatrixSet tms = workspace.getResource( TileMatrixSetProvider.class, tmsId ); List<TileDataLevel> levels = new ArrayList<TileDataLevel>(); double x = envelope.getMin().get0() - tms.getSpatialMetadata().getEnvelope().getMin().get0(); double y = envelope.getMax().get1() - tms.getSpatialMetadata().getEnvelope().getMax().get1(); int idx = 0; for ( TileMatrix tm : tms.getTileMatrices() ) { int xoff = (int) Math.round( x / tm.getTileWidth() ); int yoff = (int) Math.round( y / tm.getTileHeight() ); int numx = (int) Math.ceil( envelope.getSpan0() / tm.getTileWidth() ); int numy = (int) Math.ceil( envelope.getSpan1() / tm.getTileHeight() ); levels.add( new GeoTIFFTileDataLevel( tm, file, idx++, xoff, yoff, numx, numy ) ); } return new DefaultTileDataSet( levels, tms, format ); }
/** * @param bbox * the bbox to set */ public final void setBbox( Envelope bbox ) { if ( bbox == null ) { throw new NullPointerException( "Bbox may not be null" ); } this.bbox = bbox; Point p = bbox.getCentroid(); double[] min = bbox.getMin().getAsArray(); double[] max = bbox.getMax().getAsArray(); position = new float[] { (float) p.get0(), (float) p.get1(), (float) ( ( p.getCoordinateDimension() == 3 ) ? p.get2() : 0 ) }; if ( bbox.getMin().getCoordinateDimension() == 2 ) { min = new double[] { min[0], min[1], 0 }; } if ( bbox.getMax().getCoordinateDimension() == 2 ) { max = new double[] { max[0], max[1], 0 }; } this.error = (float) Vectors3d.length( Vectors3d.sub( max, min ) ); this.height = (float) bbox.getSpan1(); this.groundLevel = (float) min[2]; }
/** * @return the envelope that should be used for backend queries */ public Envelope getQueryBox() { if ( queryBoxSize < 0 ) { return bbox; } double minx = bbox.getMin().get0(); double miny = bbox.getMin().get1(); double maxx = bbox.getMax().get0(); double maxy = bbox.getMax().get1(); double w = bbox.getSpan0(); double h = bbox.getSpan1(); double dx = ( w * queryBoxSize - w ) / 2; double dy = ( h * queryBoxSize - h ) / 2; minx -= dx; miny -= dy; maxx += dx; maxy += dy; return new GeometryFactory().createEnvelope( minx, miny, maxx, maxy, bbox.getCoordinateSystem() ); }
/** * @param env * @return a polygon */ public static Polygon envelopeToPolygon( Envelope env ) { GeometryFactory fac = new GeometryFactory(); Point a = env.getMin(); Point b = fac.createPoint( null, a.get0() + env.getSpan0(), a.get1(), env.getCoordinateSystem() ); Point c = env.getMax(); Point d = fac.createPoint( null, a.get0(), a.get1() + env.getSpan1(), env.getCoordinateSystem() ); LinearRing ring = fac.createLinearRing( null, env.getCoordinateSystem(), new PointsArray( a, b, c, d, a ) ); return fac.createPolygon( null, env.getCoordinateSystem(), ring, null ); }