private void layerGridSubSets(StringBuilder str, TileLayer layer) {
    Iterator<GridSubset> gridSubsets = layer.getGridSubsets().values().iterator();

    while (gridSubsets.hasNext()) {
      GridSubset gridSubset = gridSubsets.next();

      str.append("    <TileMatrixSetLink>");
      str.append("      <TileMatrixSet>" + gridSubset.getName() + "</TileMatrixSet>\n");

      if (!gridSubset.fullGridSetCoverage()) {
        String[] levelNames = gridSubset.getGridNames();
        long[][] wmtsLimits = gridSubset.getWMTSCoverages();

        str.append("      <TileMatrixSetLimits>\n");
        for (int i = 0; i < levelNames.length; i++) {
          str.append("        <TileMatrixLimits>\n");
          str.append("          <TileMatrix>" + levelNames[i] + "</TileMatrix>\n");
          str.append("          <MinTileRow>" + wmtsLimits[i][1] + "</MinTileRow>\n");
          str.append("          <MaxTileRow>" + wmtsLimits[i][3] + "</MaxTileRow>\n");
          str.append("          <MinTileCol>" + wmtsLimits[i][0] + "</MinTileCol>\n");
          str.append("          <MaxTileCol>" + wmtsLimits[i][2] + "</MaxTileCol>\n");
          str.append("        </TileMatrixLimits>\n");
        }
        str.append("      </TileMatrixSetLimits>\n");
      }
      str.append("    </TileMatrixSetLink>");
    }
  }
 @Override
 public synchronized void addGridSubset(GridSubset gridSubset) {
   removeGridSubset(gridSubset.getName());
   gridSubsets.add(new XMLGridSubset(gridSubset));
   subSets.put(gridSubset.getName(), gridSubset);
 }
  /**
   * Initializes the layer, creating internal structures for calculating grid location and so forth.
   *
   * <p>Subclasses shall implement {@link #initializeInternal(GridSetBroker)} for anything else
   */
  @Override
  public final boolean initialize(GridSetBroker gridSetBroker) {

    if (this.expireCacheList == null) {
      this.expireCacheList = new ArrayList<ExpirationRule>(1);

      if (this.expireCache == null) {
        expireCacheList.add(new ExpirationRule(0, GWCVars.CACHE_NEVER_EXPIRE));
      } else {
        int expireCacheInt = Integer.parseInt(expireCache);
        if (expireCacheInt == GWCVars.CACHE_USE_WMS_BACKEND_VALUE) {
          saveExpirationHeaders = true;
        }
        expireCacheList.add(new ExpirationRule(0, expireCacheInt));
      }
    }

    if (this.expireClientsList == null) {
      this.expireClientsList = new ArrayList<ExpirationRule>(1);

      if (this.expireClients == null) {
        expireClientsList.add(new ExpirationRule(0, 7200));
      } else {
        int expireClientsInt = Integer.parseInt(expireClients);

        if (expireClientsInt == GWCVars.CACHE_USE_WMS_BACKEND_VALUE) {
          saveExpirationHeaders = true;
        } else if (expireClientsInt == GWCVars.CACHE_NEVER_EXPIRE) {
          // One year should do
          expireClientsInt = 3600 * 24 * 365;
        }
        expireClientsList.add(new ExpirationRule(0, expireClientsInt));
      }
    }

    try {
      // mimetypes
      this.formats = new ArrayList<MimeType>();
      if (mimeFormats != null) {
        for (String fmt : mimeFormats) {
          formats.add(MimeType.createFromFormat(fmt));
        }
      }
      if (formats.size() == 0) {
        formats.add(0, MimeType.createFromFormat("image/png"));
        formats.add(1, MimeType.createFromFormat("image/jpeg"));
      }
    } catch (GeoWebCacheException gwce) {
      log.error(gwce.getMessage());
      gwce.printStackTrace();
    }

    if (subSets == null) {
      subSets = new HashMap<String, GridSubset>();
    }

    if (this.gridSubsets != null) {
      Iterator<XMLGridSubset> iter = gridSubsets.iterator();
      while (iter.hasNext()) {
        XMLGridSubset xmlGridSubset = iter.next();
        GridSubset gridSubset = xmlGridSubset.getGridSubSet(gridSetBroker);

        if (gridSubset == null) {
          log.error(
              xmlGridSubset.getGridSetName()
                  + " is not known by the GridSetBroker, skipping for layer "
                  + name);
        } else {
          subSets.put(gridSubset.getName(), gridSubset);
        }
      }

      this.gridSubsets = null;
    }

    // Convert version 1.1.x and 1.0.x grid objects
    if (grids != null && !grids.isEmpty()) {
      Iterator<XMLOldGrid> iter = grids.values().iterator();
      while (iter.hasNext()) {
        GridSubset converted = iter.next().convertToGridSubset(gridSetBroker);
        subSets.put(converted.getSRS().toString(), converted);
      }

      // Null it for the garbage collector
      grids = null;
    }

    if (this.subSets.size() == 0) {
      subSets.put(
          gridSetBroker.WORLD_EPSG4326.getName(),
          GridSubsetFactory.createGridSubSet(gridSetBroker.WORLD_EPSG4326));
      subSets.put(
          gridSetBroker.WORLD_EPSG3857.getName(),
          GridSubsetFactory.createGridSubSet(gridSetBroker.WORLD_EPSG3857));
    }

    return initializeInternal(gridSetBroker);
  }
Beispiel #4
0
  private static String generateHTML(
      TileLayer layer, String gridSetStr, String formatStr, boolean asPlugin)
      throws GeoWebCacheException {
    String layerName = layer.getName();

    GridSubset gridSubset = layer.getGridSubset(gridSetStr);

    BoundingBox bbox = gridSubset.getGridSetBounds();
    BoundingBox zoomBounds = gridSubset.getOriginalExtent();

    String res = "resolutions: " + Arrays.toString(gridSubset.getResolutions()) + ",\n";

    String units = "units: \"" + gridSubset.getGridSet().guessMapUnits() + "\",\n";

    String openLayersPath;
    if (asPlugin) {
      openLayersPath = "../openlayers/OpenLayers.js";
    } else {
      openLayersPath = "../openlayers/OpenLayers.js";
    }

    String page =
        "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>\n"
            + "<meta http-equiv=\"imagetoolbar\" content=\"no\">\n"
            + "<title>"
            + layerName
            + " "
            + gridSubset.getName()
            + " "
            + formatStr
            + "</title>\n"
            + "<style type=\"text/css\">\n"
            + "body { font-family: sans-serif; font-weight: bold; font-size: .8em; }\n"
            + "body { border: 0px; margin: 0px; padding: 0px; }\n"
            + "#map { width: 85%; height: 85%; border: 0px; padding: 0px; }\n"
            + "</style>\n"
            + "<script src=\""
            + openLayersPath
            + "\"></script>    \n"
            + "<script type=\"text/javascript\">               \n"
            + "var map, demolayer;                               \n"
            + "  // sets the chosen modifiable parameter        \n"
            + "  function setParam(name, value){                \n"
            + "   str = \"demolayer.mergeNewParams({\" + name + \": '\" + value + \"'})\" \n"
            + "   // alert(str);                                   \n"
            + "   eval(str);                                    \n"
            + "  }                                              \n"
            + "OpenLayers.DOTS_PER_INCH = "
            + gridSubset.getDotsPerInch()
            + ";\n"
            + "OpenLayers.Util.onImageLoadErrorColor = 'transparent';\n"
            + "function init(){\n"
            + "var mapOptions = { \n"
            + res
            + "projection: new OpenLayers.Projection('"
            + gridSubset.getSRS().toString()
            + "'),\n"
            + "maxExtent: new OpenLayers.Bounds("
            + bbox.toString()
            + "),\n"
            + units
            + "controls: []\n"
            + "};\n"
            + "map = new OpenLayers.Map('map', mapOptions );\n"
            + "map.addControl(new OpenLayers.Control.PanZoomBar({\n"
            + "		position: new OpenLayers.Pixel(2, 15)\n"
            + "}));\n"
            + "map.addControl(new OpenLayers.Control.Navigation());\n"
            + "map.addControl(new OpenLayers.Control.Scale($('scale')));\n"
            + "map.addControl(new OpenLayers.Control.MousePosition({element: $('location')}));\n"
            + "demolayer = new OpenLayers.Layer.WMS(\n"
            + "\""
            + layerName
            + "\",\"../service/wms\",\n"
            + "{layers: '"
            + layerName
            + "', format: '"
            + formatStr
            + "' },\n"
            + "{ tileSize: new OpenLayers.Size("
            + gridSubset.getTileWidth()
            + ","
            + gridSubset.getTileHeight()
            + ")";

    /*
     * If the gridset has a top left tile origin, lets tell that to open layers. Otherwise it'll
     * calculate tile bounds based on the bbox bottom left corner, leading to misaligned
     * requests.
     */
    GridSet gridSet = gridSubset.getGridSet();
    if (gridSet.isTopLeftAligned()) {
      page +=
          ",\n tileOrigin: new OpenLayers.LonLat(" + bbox.getMinX() + ", " + bbox.getMaxY() + ")";
    }

    page +=
        "});\n"
            + "map.addLayer(demolayer);\n"
            + "map.zoomToExtent(new OpenLayers.Bounds("
            + zoomBounds.toString()
            + "));\n"
            + "// The following is just for GetFeatureInfo, which is not cached. Most people do not need this \n"
            + "map.events.register('click', map, function (e) {\n"
            + "  document.getElementById('nodelist').innerHTML = \"Loading... please wait...\";\n"
            + "  var params = {\n"
            + "    REQUEST: \"GetFeatureInfo\",\n"
            + "    EXCEPTIONS: \"application/vnd.ogc.se_xml\",\n"
            + "    BBOX: map.getExtent().toBBOX(),\n"
            + "    X: e.xy.x,\n"
            + "    Y: e.xy.y,\n"
            + "    INFO_FORMAT: 'text/html',\n"
            + "    QUERY_LAYERS: map.layers[0].params.LAYERS,\n"
            + "    FEATURE_COUNT: 50,\n"
            + "    Layers: '"
            + layerName
            + "',\n"
            + "    Styles: '',\n"
            + "    Srs: '"
            + gridSubset.getSRS().toString()
            + "',\n"
            + "    WIDTH: map.size.w,\n"
            + "    HEIGHT: map.size.h,\n"
            + "    format: \""
            + formatStr
            + "\" };\n"
            + "  OpenLayers.loadURL(\"../service/wms\", params, this, setHTML, setHTML);\n"
            + "  OpenLayers.Event.stop(e);\n"
            + "  });\n"
            + "}\n"
            + "function setHTML(response){\n"
            + "    document.getElementById('nodelist').innerHTML = response.responseText;\n"
            + "};\n"
            + "</script>\n"
            + "</head>\n"
            + "<body onload=\"init()\">\n"
            + "<div id=\"params\">"
            + makeModifiableParameters(layer)
            + "</div>\n"
            + "<div id=\"map\"></div>\n"
            + "<div id=\"nodelist\"></div>\n"
            + "</body>\n"
            + "</html>";
    return page;
  }
Beispiel #5
0
  private static String tableRows(
      TileLayerDispatcher tileLayerDispatcher, GridSetBroker gridSetBroker)
      throws GeoWebCacheException {
    StringBuffer buf = new StringBuffer();

    Set<String> layerList = new TreeSet<String>(tileLayerDispatcher.getLayerNames());
    for (String layerName : layerList) {
      TileLayer layer = tileLayerDispatcher.getTileLayer(layerName);
      buf.append(
          "<tr><td style=\"min-width: 100px;\"><strong>" + layer.getName() + "</strong><br />\n");
      buf.append("<a href=\"rest/seed/" + layer.getName() + "\">Seed this layer</a>\n");
      buf.append("</td><td>" + layer.isEnabled() + "</td>");
      buf.append("<td><table width=\"100%\">");

      int count = 0;
      for (String gridSetId : layer.getGridSubsets()) {
        GridSubset gridSubset = layer.getGridSubset(gridSetId);
        String gridSetName = gridSubset.getName();
        if (gridSetName.length() > 20) {
          gridSetName = gridSetName.substring(0, 20) + "...";
        }
        buf.append("<tr><td style=\"width: 170px;\">").append(gridSetName);

        buf.append("</td><td>OpenLayers: [");
        Iterator<MimeType> mimeIter = layer.getMimeTypes().iterator();
        boolean prependComma = false;
        while (mimeIter.hasNext()) {
          MimeType mime = mimeIter.next();
          if (mime instanceof ImageMime) {
            if (prependComma) {
              buf.append(", ");
            } else {
              prependComma = true;
            }
            buf.append(generateDemoUrl(layer.getName(), gridSubset.getName(), (ImageMime) mime));
          }
        }
        buf.append("]</td><td>\n");

        if (gridSubset.getName().equals(gridSetBroker.WORLD_EPSG4326.getName())) {
          buf.append(" &nbsp; KML: [");
          String prefix = "";
          prependComma = false;
          Iterator<MimeType> kmlIter = layer.getMimeTypes().iterator();
          while (kmlIter.hasNext()) {
            MimeType mime = kmlIter.next();
            if (mime instanceof ImageMime || mime == XMLMime.kml) {
              if (prependComma) {
                buf.append(", ");
              } else {
                prependComma = true;
              }
              buf.append(
                  "<a href=\""
                      + prefix
                      + "service/kml/"
                      + layer.getName()
                      + "."
                      + mime.getFileExtension()
                      + ".kml\">"
                      + mime.getFileExtension()
                      + "</a>");
            } else if (mime == XMLMime.kmz) {
              if (prependComma) {
                buf.append(", ");
              } else {
                prependComma = true;
              }
              buf.append(
                  "<a href=\"" + prefix + "service/kml/" + layer.getName() + ".kml.kmz\">kmz</a>");
            }
          }
          buf.append("]");
        } else {
          // No Google Earth support
        }
        buf.append("</td></tr>");
        count++;
      }

      // if(count == 0) {
      // buf.append("<tr><td colspan=\"2\"><i>None</i></td></tr>\n");
      // }

      buf.append("</table></td>\n");
      buf.append("</tr>\n");
    }

    return buf.toString();
  }
Beispiel #6
0
  protected void renderCanvas() throws OutsideCoverageException, GeoWebCacheException, IOException {
    // Now we loop over all the relevant tiles and write them to the canvas,
    // Starting at the bottom, moving to the right and up

    // Bottom row of tiles, in tile coordinates
    long starty = srcRectangle[1];

    // gridy is the tile row index
    for (long gridy = starty; gridy <= srcRectangle[3]; gridy++) {

      int tiley = 0;
      int canvasy = (int) (srcRectangle[3] - gridy) * gridSubset.getTileHeight();
      int tileHeight = gridSubset.getTileHeight();

      if (canvOfs.top > 0) {
        // Add padding
        canvasy += canvOfs.top;
      } else {
        // Top tile is cut off
        if (gridy == srcRectangle[3]) {
          // This one starts at the top, so canvasy remains 0
          tileHeight = tileHeight + canvOfs.top;
          tiley = -canvOfs.top;
        } else {
          // Offset that the first tile contributed,
          // rather, we subtract what it did not contribute
          canvasy += canvOfs.top;
        }
      }

      if (gridy == srcRectangle[1] && canvOfs.bottom < 0) {
        // Check whether we only use part of the first tiles (bottom row)
        // Offset is negative, slice the bottom off the tile
        tileHeight += canvOfs.bottom;
      }

      long startx = srcRectangle[0];
      for (long gridx = startx; gridx <= srcRectangle[2]; gridx++) {

        long[] gridLoc = {gridx, gridy, srcIdx};

        ConveyorTile tile =
            new ConveyorTile(
                sb,
                layer.getName(),
                gridSubset.getName(),
                gridLoc,
                ImageMime.png,
                fullParameters,
                null,
                null);

        // Check whether this tile is to be rendered at all
        try {
          layer.applyRequestFilters(tile);
        } catch (RequestFilterException e) {
          log.debug(e.getMessage());
          continue;
        }

        layer.getTile(tile);

        BufferedImage tileImg = ImageIO.read(tile.getBlob().getInputStream());

        int tilex = 0;
        int canvasx = (int) (gridx - startx) * gridSubset.getTileWidth();
        int tileWidth = gridSubset.getTileWidth();

        if (canvOfs.left > 0) {
          // Add padding
          canvasx += canvOfs.left;
        } else {
          // Leftmost tile is cut off
          if (gridx == srcRectangle[0]) {
            // This one starts to the left top, so canvasx remains 0
            tileWidth = tileWidth + canvOfs.left;
            tilex = -canvOfs.left;
          } else {
            // Offset that the first tile contributed,
            // rather, we subtract what it did not contribute
            canvasx += canvOfs.left;
          }
        }

        if (gridx == srcRectangle[2] && canvOfs.right < 0) {
          // Check whether we only use part of the first tiles (bottom row)
          // Offset is negative, slice the bottom off the tile
          tileWidth = tileWidth + canvOfs.right;
        }

        // TODO We should really ensure we can never get here
        if (tileWidth == 0 || tileHeight == 0) {
          log.debug("tileWidth: " + tileWidth + " tileHeight: " + tileHeight);
          continue;
        }

        // Cut down the tile to the part we want
        if (tileWidth != gridSubset.getTileWidth() || tileHeight != gridSubset.getTileHeight()) {
          log.debug(
              "tileImg.getSubimage("
                  + tilex
                  + ","
                  + tiley
                  + ","
                  + tileWidth
                  + ","
                  + tileHeight
                  + ")");
          tileImg = tileImg.getSubimage(tilex, tiley, tileWidth, tileHeight);
        }

        // Render the tile on the big canvas
        log.debug(
            "drawImage(subtile," + canvasx + "," + canvasy + ",null) " + Arrays.toString(gridLoc));
        gfx.drawImage(tileImg, canvasx, canvasy, null); // imageObserver
      }
    }

    gfx.dispose();
  }
  GridSubset findBestGridSubset(WMSMapContent map) {
    GetMapRequest req = map.getRequest();
    Map formatOpts = req.getFormatOptions();

    GridSetBroker gridSetBroker = gwc.getGridSetBroker();
    GridSet gridSet = null;

    // first check format options to see if explicitly specified
    if (formatOpts.containsKey("gridset")) {
      gridSet = gridSetBroker.get(formatOpts.get("gridset").toString());
    }

    // next check srs
    if (gridSet == null) {
      gridSet = gridSetBroker.get(req.getSRS().toUpperCase());
    }

    if (gridSet != null) {
      return GridSubsetFactory.createGridSubSet(gridSet);
    }

    CoordinateReferenceSystem crs = map.getCoordinateReferenceSystem();

    // look up epsg code
    Integer epsgCode = null;
    try {
      epsgCode = CRS.lookupEpsgCode(crs, false);
    } catch (Exception e) {
      throw new ServiceException("Unable to determine epsg code for " + crs, e);
    }
    if (epsgCode == null) {
      throw new ServiceException("Unable to determine epsg code for " + crs);
    }

    SRS srs = SRS.getSRS(epsgCode);

    // figure out the appropriate grid sub set
    Set<GridSubset> gridSubsets = new LinkedHashSet<GridSubset>();
    for (MapLayerInfo l : req.getLayers()) {
      TileLayer tl = gwc.getTileLayerByName(l.getName());
      if (tl == null) {
        throw new ServiceException("No tile layer for " + l.getName());
      }

      List<GridSubset> theseGridSubsets = tl.getGridSubsetsForSRS(srs);
      if (gridSubsets.isEmpty()) {
        gridSubsets.addAll(theseGridSubsets);
      } else {
        gridSubsets.retainAll(theseGridSubsets);
      }

      if (gridSubsets.isEmpty()) {
        throw new ServiceException(
            "No suitable " + epsgCode + " grid subset for " + req.getLayers());
      }
    }

    if (gridSubsets.size() > 1) {
      if (LOGGER.isLoggable(Level.WARNING)) {
        StringBuilder msg = new StringBuilder("Found multiple grid subsets: ");
        for (GridSubset gs : gridSubsets) {
          msg.append(gs.getName()).append(", ");
        }
        msg.setLength(msg.length() - 2);
        msg.append(". Choosing first.");
        LOGGER.warning(msg.toString());
      }
    }

    return gridSubsets.iterator().next();
  }