Given a tile for a layer, what are the tiles bounds in world space.

@param tileConfiguration The basic tile configuration.
@param tileCode          The tile code.
@return The tile bounds in map CRS.
498712
public static Bbox getWorldBoundsForTile(TileConfiguration tileConfiguration, TileCode tileCode) {
		double resolution = tileConfiguration.getResolution(tileCode.getTileLevel());
		double worldTileWidth = tileConfiguration.getTileWidth() * resolution;
		double worldTileHeight = tileConfiguration.getTileHeight() * resolution;

		double x = tileConfiguration.getTileOrigin().getX() + tileCode.getX() * worldTileWidth;
		double y = tileConfiguration.getTileOrigin().getY() + tileCode.getY() * worldTileHeight;
		return new Bbox(x, y, worldTileWidth, worldTileHeight);
	}
898696
private static Set<GeoPosition> getMapGeoBounds(JXMapViewer mapViewer)
    {
        Set<GeoPosition> set = new HashSet<GeoPosition>();
        TileFactory tileFactory = mapViewer.getTileFactory();
        int zoom = mapViewer.getZoom();
        Rectangle2D bounds = mapViewer.getViewportBounds();
        Point2D pt = new Point2D.Double(bounds.getX(), bounds.getY());
        set.add(tileFactory.pixelToGeo(pt, zoom));
        pt = new Point2D.Double(bounds.getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight());
        set.add(tileFactory.pixelToGeo(pt, zoom));
        return set;
    }
351603
public void init(Envelope maxExtent, double scale) {
		double[] layerSize = TileUtil.getTileLayerSize(code, maxExtent, scale);
		tileWidth = layerSize[0];
		tileHeight = layerSize[1];
		int[] screenSize = TileUtil.getTileScreenSize(layerSize, scale);
		screenWidth = screenSize[0];
		screenHeight = screenSize[1];
		bounds = TileUtil.getTileBounds(code, maxExtent, scale);
	}
1284420
private int calculateTileLevel() {
		double baseX = layerBounds.getWidth();
		double baseY = layerBounds.getHeight();
		// choose the tile level so the area is between minimumTileSize and the next level (minimumTileSize * 4)
		double baseArea = baseX * baseY;
		double scale = layer.getMapModel().getMapView().getCurrentScale();
		double osmArea = preferredTileSize / (scale * scale);
		int tileLevel = (int) Math.round(Math.log(baseArea / osmArea) / Math.log(4.0));
		if (tileLevel < 0) {
			tileLevel = 0;
		}
		return tileLevel;
	}
868388
public int[] getBoundsInTileIndex( int zoomlevel ) throws Exception {
        String sql = "select min(tile_column), max(tile_column), min(tile_row), max(tile_row) from tiles where zoom_level="
                + zoomlevel;
        return database.execOnConnection(connection -> {
            try (IHMStatement statement = connection.createStatement(); IHMResultSet resultSet = statement.executeQuery(sql);) {
                if (resultSet.next()) {
                    int minTx = resultSet.getInt(1);
                    int maxTx = resultSet.getInt(2);
                    int minTy = resultSet.getInt(3);
                    int maxTy = resultSet.getInt(4);
                    return new int[]{minTx, maxTx, minTy, maxTy};
                }
            }
            return null;
        });
    }
1190960
private static TileConfiguration createTileConfiguration(MapConfiguration mapConfig,
			ClientVectorLayerInfo layerInfo, final ViewPort viewPort) {
		TileConfiguration tileConfig = new TileConfiguration();
		ClientMapInfo mapInfo = mapConfig.getHintValue(GeomajasServerExtension.MAPINFO);
		tileConfig.setTileWidth(mapInfo.getPreferredPixelsPerTile().getWidth());
		tileConfig.setTileHeight(mapInfo.getPreferredPixelsPerTile().getHeight());
		List<Double> resolutions = new ArrayList<Double>();
		for (int i = 0; i < viewPort.getResolutionCount(); i++) {
			resolutions.add(viewPort.getResolution(i));
		}
		tileConfig.setResolutions(resolutions);
		if (layerInfo.getMaxExtent() != null) {
			tileConfig.setTileOrigin(BboxService.getOrigin(layerInfo.getMaxExtent()));
		} else {
			tileConfig.setTileOrigin(new Coordinate());
		}
		return tileConfig;
	}
919332
private static int readGeoPackageFormatTiles(GeoPackage geoPackage,
			String tileTable, String imageFormat, boolean rawImage,
			TileDirectory tileDirectory) throws SQLException, IOException {

		int created = 0;

		TileProperties properties = new TileProperties(tileDirectory.directory);
		properties.load();

		int epsg = properties.getIntegerProperty(
				TileProperties.GEOPACKAGE_PROPERTIES_EPSG, true);
		double minX = properties.getDoubleProperty(
				TileProperties.GEOPACKAGE_PROPERTIES_MIN_X, true);
		double maxX = properties.getDoubleProperty(
				TileProperties.GEOPACKAGE_PROPERTIES_MAX_X, true);
		double minY = properties.getDoubleProperty(
				TileProperties.GEOPACKAGE_PROPERTIES_MIN_Y, true);
		double maxY = properties.getDoubleProperty(
				TileProperties.GEOPACKAGE_PROPERTIES_MAX_Y, true);

		// Create the user tile table
		List<TileColumn> columns = TileTable.createRequiredColumns();
		TileTable table = new TileTable(tileTable, columns);
		geoPackage.createTileTable(table);

		// Get SRS value
		SpatialReferenceSystemDao srsDao = geoPackage
				.getSpatialReferenceSystemDao();
		SpatialReferenceSystem srs = srsDao.getOrCreateFromEpsg(epsg);

		// Create the Tile Matrix Set and Tile Matrix tables
		geoPackage.createTileMatrixSetTable();
		geoPackage.createTileMatrixTable();

		// Create new Contents
		ContentsDao contentsDao = geoPackage.getContentsDao();

		Contents contents = new Contents();
		contents.setTableName(tileTable);
		contents.setDataType(ContentsDataType.TILES);
		contents.setIdentifier(tileTable);
		// contents.setDescription("");
		// contents.setLastChange(new Date());
		contents.setMinX(minX);
		contents.setMinY(minY);
		contents.setMaxX(maxX);
		contents.setMaxY(maxY);
		contents.setSrs(srs);

		// Create the contents
		contentsDao.create(contents);
		table.setContents(contents);

		// Create new Tile Matrix Set
		TileMatrixSetDao tileMatrixSetDao = geoPackage.getTileMatrixSetDao();

		TileMatrixSet tileMatrixSet = new TileMatrixSet();
		tileMatrixSet.setContents(contents);
		tileMatrixSet.setSrs(srs);
		tileMatrixSet.setMinX(minX);
		tileMatrixSet.setMinY(minY);
		tileMatrixSet.setMaxX(maxX);
		tileMatrixSet.setMaxY(maxY);
		tileMatrixSetDao.create(tileMatrixSet);

		// Create new Tile Matrix and tile table rows by going through each zoom
		// level
		TileMatrixDao tileMatrixDao = geoPackage.getTileMatrixDao();
		TileDao tileDao = geoPackage.getTileDao(tileMatrixSet);

		Integer lastZoom = null;
		Integer lastMatrixWidth = null;
		Integer lastMatrixHeight = null;

		for (ZoomDirectory zoomDirectory : tileDirectory.zooms.values()) {

			int zoomCount = 0;

			Integer tileWidth = null;
			Integer tileHeight = null;

			// Determine the matrix width and height
			Integer matrixWidth = properties.getIntegerProperty(
					TileProperties.getMatrixWidthProperty(zoomDirectory.zoom),
					false);
			Integer matrixHeight = properties.getIntegerProperty(
					TileProperties.getMatrixHeightProperty(zoomDirectory.zoom),
					false);

			// If the matrix width is not configured as a property, try to
			// determine it
			if (matrixWidth == null) {
				if (lastZoom != null) {
					// Determine the width by a factor of 2 for each zoom level
					matrixWidth = lastMatrixWidth;
					for (int i = lastZoom; i < zoomDirectory.zoom; i++) {
						matrixWidth *= 2;
					}
				} else {
					// Assume the max x is the width
					matrixWidth = zoomDirectory.maxX + 1;
				}
			}

			// If the matrix height is not configured as a property, try to
			// determine it
			if (matrixHeight == null) {
				if (lastZoom != null) {
					// Determine the height by a factor of 2 for each zoom level
					matrixHeight = lastMatrixHeight;
					for (int i = lastZoom; i < zoomDirectory.zoom; i++) {
						matrixHeight *= 2;
					}
				} else {
					// Assume the max y is the height
					matrixHeight = zoomDirectory.maxY + 1;
				}
			}

			// Set values for the next zoom level
			lastZoom = zoomDirectory.zoom;
			lastMatrixWidth = matrixWidth;
			lastMatrixHeight = matrixHeight;

			LOGGER.log(Level.INFO, "Zoom Level: " + zoomDirectory.zoom
					+ ", Width: " + matrixWidth + ", Height: " + matrixHeight
					+ ", Max Tiles: " + (matrixWidth * matrixHeight));

			for (XDirectory xDirectory : zoomDirectory.xValues.values()) {

				for (YFile yFile : xDirectory.yValues.values()) {

					BufferedImage image = null;

					// Set the tile width and height
					if (tileWidth == null || tileHeight == null) {
						image = ImageIO.read(yFile.file);
						tileWidth = image.getWidth();
						tileHeight = image.getHeight();
					}

					TileRow newRow = tileDao.newRow();

					newRow.setZoomLevel(zoomDirectory.zoom);
					newRow.setTileColumn(xDirectory.x);
					newRow.setTileRow(yFile.y);
					if (rawImage) {
						byte[] rawImageBytes = GeoPackageIOUtils
								.fileBytes(yFile.file);
						newRow.setTileData(rawImageBytes);
					} else {
						if (image == null) {
							image = ImageIO.read(yFile.file);
						}
						newRow.setTileData(image, imageFormat);
					}

					tileDao.create(newRow);

					zoomCount++;

					if (zoomCount % ZOOM_PROGRESS_FREQUENCY == 0) {
						LOGGER.log(Level.INFO, "Zoom " + zoomDirectory.zoom
								+ " Tile Progress... " + zoomCount);
					}
				}
			}

			LOGGER.log(Level.INFO, "Zoom " + zoomDirectory.zoom + " Tiles: "
					+ zoomCount);

			// If tiles were saved for the zoom level, create the tile matrix
			// row
			if (zoomCount > 0) {

				double pixelXSize = (maxX - minX) / matrixWidth / tileWidth;
				double pixelYSize = (maxY - minY) / matrixHeight / tileHeight;

				TileMatrix tileMatrix = new TileMatrix();
				tileMatrix.setContents(contents);
				tileMatrix.setZoomLevel(zoomDirectory.zoom);
				tileMatrix.setMatrixWidth(matrixWidth);
				tileMatrix.setMatrixHeight(matrixHeight);
				tileMatrix.setTileWidth(tileWidth);
				tileMatrix.setTileHeight(tileHeight);
				tileMatrix.setPixelXSize(pixelXSize);
				tileMatrix.setPixelYSize(pixelYSize);
				tileMatrixDao.create(tileMatrix);

				created += zoomCount;
			}

		}

		return created;
	}
436827
public boolean isWithinBoundingBox(int x, int y, int zoom) {
        boolean withinBounds = true;

        // If a bounding box is set, check if it overlaps with the request
        if (webMercatorBoundingBox != null) {

            // Get the bounding box of the requested tile
            BoundingBox tileWebMercatorBoundingBox = TileBoundingBoxUtils
                    .getWebMercatorBoundingBox(x, y, zoom);

            // Adjust the bounding box if needed
            BoundingBox adjustedWebMercatorBoundingBox = getWebMercatorBoundingBox(tileWebMercatorBoundingBox);

            // Check if the request overlaps
            withinBounds = adjustedWebMercatorBoundingBox.intersects(
                    tileWebMercatorBoundingBox, true);
        }

        return withinBounds;
    }
149392
public static BoundingBox getWGS84BoundingBox(TileGrid tileGrid, int zoom) {

		int tilesPerLat = tilesPerWGS84LatSide(zoom);
		int tilesPerLon = tilesPerWGS84LonSide(zoom);

		double tileSizeLat = tileSizeLatPerWGS84Side(tilesPerLat);
		double tileSizeLon = tileSizeLonPerWGS84Side(tilesPerLon);

		double minLon = (-1 * ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH)
				+ (tileGrid.getMinX() * tileSizeLon);
		double maxLon = (-1 * ProjectionConstants.WGS84_HALF_WORLD_LON_WIDTH)
				+ ((tileGrid.getMaxX() + 1) * tileSizeLon);
		double minLat = ProjectionConstants.WGS84_HALF_WORLD_LAT_HEIGHT
				- ((tileGrid.getMaxY() + 1) * tileSizeLat);
		double maxLat = ProjectionConstants.WGS84_HALF_WORLD_LAT_HEIGHT
				- (tileGrid.getMinY() * tileSizeLat);

		BoundingBox box = new BoundingBox(minLon, minLat, maxLon, maxLat);

		return box;
	}
568842
function(viewPortPx) {
        var maxExtent = this.maxExtent;
        var resolution = this.getResolution();
        var tileMapWidth = resolution * this.tileSize.w;
        var tileMapHeight = resolution * this.tileSize.h;
        var mapPoint = this.getLonLatFromViewPortPx(viewPortPx);
        var tileLeft = maxExtent.left + (tileMapWidth *
                                         Math.floor((mapPoint.lon -
                                                     maxExtent.left) /
                                                    tileMapWidth));
        var tileBottom = maxExtent.bottom + (tileMapHeight *
                                             Math.floor((mapPoint.lat -
                                                         maxExtent.bottom) /
                                                        tileMapHeight));
        return new OpenLayers.Bounds(tileLeft, tileBottom,
                                     tileLeft + tileMapWidth,
                                     tileBottom + tileMapHeight);
    }
107942
function (bounds) {
        bounds = this.adjustBounds(bounds);
        var res = this.getServerResolution();
        var x = Math.round((bounds.left - this.tileOrigin.lon) / (res * this.tileSize.w));
        var y = Math.round((bounds.bottom - this.tileOrigin.lat) / (res * this.tileSize.h));
        var z = this.getServerZoom();
        var path = this.serviceVersion + "/" + this.layername + "/" + z + "/" + x + "/" + y + "." + this.type; 
        var url = this.url;
        if (OpenLayers.Util.isArray(url)) {
            url = this.selectUrl(path, url);
        }
        return url + path;
    }
64676
public BoundingBox getBoundingBox() {
        if (this.boundingBox == null) {
            double minLatitude = Math.max(MercatorProjection.LATITUDE_MIN, MercatorProjection.tileYToLatitude(tileY + 1, zoomLevel));
            double minLongitude = Math.max(-180, MercatorProjection.tileXToLongitude(this.tileX, zoomLevel));
            double maxLatitude = Math.min(MercatorProjection.LATITUDE_MAX, MercatorProjection.tileYToLatitude(this.tileY, zoomLevel));
            double maxLongitude = Math.min(180, MercatorProjection.tileXToLongitude(tileX + 1, zoomLevel));
            if (maxLongitude == -180) {
                // fix for dateline crossing, where the right tile starts at -180 and causes an invalid bbox
                maxLongitude = 180;
            }
            this.boundingBox = new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude);
        }
        return this.boundingBox;
    }
180234
public void setTileFormat(TileFormatType tileFormat) {
		if (tileFormat == null) {
			tileFormat = TileFormatType.STANDARD;
		} else {
			switch (tileFormat) {
			case STANDARD:
			case TMS:
				this.tileFormat = tileFormat;
				break;
			default:
				throw new GeoPackageException(
						"Unsupported Tile Format Type for URL Tile Generation: "
								+ tileFormat);
			}
		}
	}
976765
public static Tile getUpperLeft(BoundingBox boundingBox, byte zoomLevel, int tileSize) {
        int tileLeft = MercatorProjection.longitudeToTileX(boundingBox.minLongitude, zoomLevel);
        int tileTop = MercatorProjection.latitudeToTileY(boundingBox.maxLatitude, zoomLevel);
        return new Tile(tileLeft, tileTop, zoomLevel, tileSize);
    }
1349951
public int getTileSetID (String tileSetName)
        throws PersistenceException
    {
        Integer tsid = _map.get(tileSetName);
        if (tsid == null) {
            tsid = Integer.valueOf(++_nextTileSetID);
            _map.put(tileSetName, tsid);
        }
        return tsid.intValue();
    }
367921
public static BoundingBox getBoundingBox(int x, int y, int zoom) {

		int tilesPerSide = tilesPerSide(zoom);
		double tileWidthDegrees = tileWidthDegrees(tilesPerSide);
		double tileHeightDegrees = tileHeightDegrees(tilesPerSide);

		double minLon = -180.0 + (x * tileWidthDegrees);
		double maxLon = minLon + tileWidthDegrees;

		double maxLat = 90.0 - (y * tileHeightDegrees);
		double minLat = maxLat - tileHeightDegrees;

		BoundingBox box = new BoundingBox(minLon, minLat, maxLon, maxLat);

		return box;
	}
1265803
def tileBounds(self, zoom, tileCol, tileRow):
        ""
        assert zoom in range(0, len(self.RESOLUTIONS))

        # 0,0 at top left: y axis down and x axis right
        tileSize = self.tileSize(zoom)
        minX = self.MINX + tileCol * tileSize
        maxX = self.MINX + (tileCol + 1) * tileSize
        if self.originCorner == 'bottom-left':
            minY = self.MINY + tileRow * tileSize
            maxY = self.MINY + (tileRow + 1) * tileSize
        elif self.originCorner == 'top-left':
            minY = self.MAXY - (tileRow + 1) * tileSize
            maxY = self.MAXY - tileRow * tileSize
        return [minX, minY, maxX, maxY]