Integer findClosestZoom(GridSet gridSet, WMSMapContent map) {
    double reqScale =
        RendererUtilities.calculateOGCScale(bounds(map), gridSet.getTileWidth(), null);

    int i = 0;
    double error = Math.abs(gridSet.getGrid(i).getScaleDenominator() - reqScale);
    while (i < gridSet.getNumLevels() - 1) {
      Grid g = gridSet.getGrid(i + 1);
      double e = Math.abs(g.getScaleDenominator() - reqScale);

      if (e > error) {
        break;
      } else {
        error = e;
      }
      i++;
    }

    return Math.max(i, 0);
  }
 private void tileMatrixSet(StringBuilder str, GridSet gridSet) {
   str.append("  <TileMatrixSet>\n");
   str.append("    <ows:Identifier>" + gridSet.getName() + "</ows:Identifier>\n");
   // If the following is not good enough, please get in touch and we will try to fix it :)
   str.append(
       "    <ows:SupportedCRS>urn:ogc:def:crs:EPSG::"
           + gridSet.getSrs().getNumber()
           + "</ows:SupportedCRS>\n");
   // TODO detect these str.append("
   // <WellKnownScaleSet>urn:ogc:def:wkss:GlobalCRS84Pixel</WellKnownScaleSet>\n");
   Grid[] grids = gridSet.getGrids();
   for (int i = 0; i < grids.length; i++) {
     double[] tlCoordinates = gridSet.getOrderedTopLeftCorner(i);
     tileMatrix(
         str,
         grids[i],
         tlCoordinates,
         gridSet.getTileWidth(),
         gridSet.getTileHeight(),
         gridSet.isScaleWarning());
   }
   str.append("  </TileMatrixSet>\n");
 }