示例#1
0
  /**
   * Creates a new sample file for the given style and stores it on disk. The sample dimensions
   * (width x height) are returned.
   *
   * @param style
   * @param pngOutputFormat
   * @return
   * @throws Exception
   */
  private Dimension createNewSample(StyleInfo style, GetLegendGraphicOutputFormat pngOutputFormat)
      throws Exception {
    GetLegendGraphicRequest legendGraphicRequest = new GetLegendGraphicRequest();
    File sampleLegendFolder = getSamplesFolder();

    legendGraphicRequest.setLayers(Arrays.asList((FeatureType) null));
    legendGraphicRequest.setStyles(Arrays.asList(style.getStyle()));
    legendGraphicRequest.setFormat(pngOutputFormat.getContentType());
    Object legendGraphic = pngOutputFormat.produceLegendGraphic(legendGraphicRequest);
    if (legendGraphic instanceof BufferedImageLegendGraphic) {
      BufferedImage image = ((BufferedImageLegendGraphic) legendGraphic).getLegend();

      PNGWriter writer = new PNGWriter();
      FileOutputStream outStream = null;
      try {
        File sampleFile =
            new File(
                sampleLegendFolder.getAbsolutePath() + File.separator + getSampleFileName(style));
        if (!sampleFile.getParentFile().exists()) {
          sampleFile.getParentFile().mkdirs();
        }
        outStream = new FileOutputStream(sampleFile);
        writer.writePNG(image, outStream, 0.0f, FilterType.FILTER_NONE);
        removeStyleSampleInvalidation(style);
        return new Dimension(image.getWidth(), image.getHeight());
      } finally {
        if (outStream != null) {
          outStream.close();
        }
      }
    }

    return null;
  }
示例#2
0
 @Test
 public void testPointRenderer() throws Exception {
   StyleInfo pointInfo = getGeoServer().getCatalog().getStyleByName("Buildings");
   assertNotNull(pointInfo);
   Style point = pointInfo.getStyle();
   assertNotNull(point);
   Renderer pointRenderer = StyleEncoder.styleToRenderer((org.geotools.styling.Style) point);
   assertNotNull(point);
   JSONBuilder json = new JSONStringer();
   StyleEncoder.encodeRenderer(json, pointRenderer);
 }
示例#3
0
 @Test
 public void testLineRenderer() throws Exception {
   StyleInfo lineInfo = getGeoServer().getCatalog().getStyleByName("Streams");
   assertNotNull(lineInfo);
   Style line = lineInfo.getStyle();
   assertNotNull(line);
   Renderer lineRenderer = StyleEncoder.styleToRenderer((org.geotools.styling.Style) line);
   assertNotNull(lineRenderer);
   JSONBuilder json = new JSONStringer();
   StyleEncoder.encodeRenderer(json, lineRenderer);
 }
示例#4
0
  public MockCatalogBuilder style(String name) {
    String filename = name + ".sld";
    if (getClass().getResourceAsStream(filename) == null) {
      return this;
    }

    String sId = newId();
    Version version = Styles.Handler.SLD_10.getVersion();

    final StyleInfo s = createNiceMock(StyleInfo.class);
    styles.add(s);

    expect(s.getId()).andReturn(sId);
    expect(s.getName()).andReturn(name).anyTimes();
    expect(s.getFilename()).andReturn(filename).anyTimes();
    expect(s.getSLDVersion()).andReturn(version).anyTimes();
    try {
      expect(s.getStyle())
          .andReturn(
              Styles.style(Styles.parse(getClass().getResourceAsStream(filename), null, version)))
          .anyTimes();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    expect(catalog.getStyle(sId)).andReturn(s).anyTimes();
    expect(catalog.getStyleByName(name)).andReturn(s).anyTimes();

    s.accept((CatalogVisitor) anyObject());
    expectLastCall()
        .andAnswer(
            new VisitAnswer() {
              @Override
              protected void doVisit(CatalogVisitor visitor) {
                visitor.visit(s);
              }
            })
        .anyTimes();

    callback.onStyle(name, s, this);
    replay(s);
    return this;
  }
    /**
     * Calls super.handleFeatureType to add common FeatureType content such as Name, Title and
     * LatLonBoundingBox, and then writes WMS specific layer properties as Styles, Scale Hint, etc.
     *
     * @throws IOException
     * @task TODO: write wms specific elements.
     */
    @SuppressWarnings("deprecation")
    protected void handleLayer(final LayerInfo layer) {
      // HACK: by now all our layers are queryable, since they reference
      // only featuretypes managed by this server
      AttributesImpl qatts = new AttributesImpl();
      boolean queryable = wmsConfig.isQueryable(layer);
      qatts.addAttribute("", "queryable", "queryable", "", queryable ? "1" : "0");
      start("Layer", qatts);
      element("Name", layer.getResource().getNamespace().getPrefix() + ":" + layer.getName());
      // REVISIT: this is bad, layer should have title and anbstract by itself
      element("Title", layer.getResource().getTitle());
      element("Abstract", layer.getResource().getAbstract());
      handleKeywordList(layer.getResource().getKeywords());

      /**
       * @task REVISIT: should getSRS() return the full URL? no - the spec says it should be a set
       *     of <SRS>EPSG:#</SRS>...
       */
      final String srs = layer.getResource().getSRS();
      element("SRS", srs);

      // DJB: I want to be nice to the people reading the capabilities
      // file - I'm going to get the
      // human readable name and stick it in the capabilities file
      // NOTE: this isnt well done because "comment()" isnt in the
      // ContentHandler interface...
      try {
        CoordinateReferenceSystem crs = layer.getResource().getCRS();
        String desc = "WKT definition of this CRS:\n" + crs;
        comment(desc);
      } catch (Exception e) {
        if (LOGGER.isLoggable(Level.WARNING)) {
          LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
        }
      }

      Envelope bbox;
      try {
        bbox = layer.getResource().boundingBox();
      } catch (Exception e) {
        throw new RuntimeException(
            "Unexpected error obtaining bounding box for layer " + layer.getName(), e);
      }
      Envelope llbbox = layer.getResource().getLatLonBoundingBox();

      handleLatLonBBox(llbbox);
      // the native bbox might be null
      if (bbox != null) {
        handleBBox(bbox, srs);
      }

      // handle dimensions
      String timeMetadata = null;
      String elevationMetadata = null;

      if (layer.getType() == Type.VECTOR) {
        dimensionHelper.handleVectorLayerDimensions(layer);
      } else if (layer.getType() == Type.RASTER) {
        dimensionHelper.handleRasterLayerDimensions(layer);
      }

      // handle data attribution
      handleAttribution(layer);

      // handle metadata URLs
      handleMetadataList(layer.getResource().getMetadataLinks());

      if (layer.getResource() instanceof WMSLayerInfo) {
        // do nothing for the moment, we may want to list the set of cascaded named styles
        // in the future (when we add support for that)
      } else {
        // add the layer style
        start("Style");

        StyleInfo defaultStyle = layer.getDefaultStyle();
        if (defaultStyle == null) {
          throw new NullPointerException("Layer " + layer.getName() + " has no default style");
        }
        Style ftStyle;
        try {
          ftStyle = defaultStyle.getStyle();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        element("Name", defaultStyle.getName());
        element("Title", ftStyle.getTitle());
        element("Abstract", ftStyle.getAbstract());
        handleLegendURL(layer.getName(), layer.getLegend(), null);
        end("Style");

        Set<StyleInfo> styles = layer.getStyles();

        for (StyleInfo styleInfo : styles) {
          try {
            ftStyle = styleInfo.getStyle();
          } catch (IOException e) {
            throw new RuntimeException(e);
          }
          start("Style");
          element("Name", styleInfo.getName());
          element("Title", ftStyle.getTitle());
          element("Abstract", ftStyle.getAbstract());
          handleLegendURL(layer.getName(), null, styleInfo);
          end("Style");
        }
      }

      end("Layer");
    }
示例#6
0
 public Style getStyleByName(String styleName) throws IOException {
   StyleInfo styleInfo = getCatalog().getStyleByName(styleName);
   return styleInfo == null ? null : styleInfo.getStyle();
 }