Beispiel #1
0
  @Test
  public void testSEStyleWithRelativePath() throws IOException {
    StyleInfo si = getCatalog().getStyleByName("relative");

    assertNotNull(si);
    Style style = si.getStyle();
    PolygonSymbolizer ps =
        (PolygonSymbolizer) style.featureTypeStyles().get(0).rules().get(0).symbolizers().get(0);
    ExternalGraphic eg = (ExternalGraphic) ps.getFill().getGraphicFill().graphicalSymbols().get(0);
    URI uri = eg.getOnlineResource().getLinkage();
    assertNotNull(uri);
    File actual = DataUtilities.urlToFile(uri.toURL()).getCanonicalFile();
    assertEquals(rockFillSymbolFile, actual);
  }
  static GraphicalSymbol cast(GraphicalSymbol item) {
    if (item == null) {
      return null;
    } else if (item instanceof ExternalGraphicImpl) {
      return (ExternalGraphic) item;
    } else if (item instanceof org.opengis.style.ExternalGraphic) {
      org.opengis.style.ExternalGraphic graphic = (org.opengis.style.ExternalGraphic) item;
      ExternalGraphicImpl copy = new ExternalGraphicImpl();
      copy.colorReplacements().addAll(graphic.getColorReplacements());
      copy.setFormat(graphic.getFormat());
      copy.setInlineContent(graphic.getInlineContent());
      copy.setOnlineResource(graphic.getOnlineResource());

      return copy;
    }
    return null;
  }
  /**
   * Paints a GraphicLegend in the supplied graphics
   *
   * @param graphics The graphics in which to draw.
   * @param shape The shape to draw.
   * @param legend The legend to apply.
   * @param symbolScale The scale of the symbol, if the legend graphic has to be rescaled
   */
  public void paint(
      final Graphics2D graphics,
      final LiteShape2 shape,
      final GraphicLegend legend,
      final double symbolScale,
      boolean isLabelObstacle) {
    if (legend == null) {
      // TODO: what's going on? Should not be reached...
      throw new NullPointerException("ShapePainter has been asked to paint a null legend!!");
    }
    Iterator<GraphicalSymbol> symbolIter = legend.graphicalSymbols().iterator();

    while (symbolIter.hasNext()) {

      GraphicalSymbol symbol = symbolIter.next();

      if (symbol instanceof ExternalGraphic) {
        float[] coords = new float[2];
        PathIterator iter = getPathIterator(shape);
        iter.currentSegment(coords);

        // Note: Converting to Radians here due to direct use of SLD Expressions which uses degrees
        double rotation =
            Math.toRadians(((Literal) legend.getRotation()).evaluate(null, Double.class));
        float opacity = ((Literal) legend.getOpacity()).evaluate(null, Float.class);
        AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);

        ExternalGraphic graphic = (ExternalGraphic) symbol;

        while (!(iter.isDone())) {
          iter.currentSegment(coords);
          try {
            BufferedImage image = ImageIO.read(graphic.getOnlineResource().getLinkage().toURL());
            if ((symbolScale > 0.0) && (symbolScale != 1.0)) {
              int w = (int) (image.getWidth() / symbolScale);
              int h = (int) (image.getHeight() / symbolScale);
              int imageType =
                  image.getType() == 0 ? BufferedImage.TYPE_4BYTE_ABGR : image.getType();
              BufferedImage rescaled = new BufferedImage(w, h, imageType);
              Graphics2D g = rescaled.createGraphics();
              g.setRenderingHint(
                  RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
              g.drawImage(image, 0, 0, w, h, 0, 0, image.getWidth(), image.getHeight(), null);
              g.dispose();
              image = rescaled;
            }

            renderImage(
                graphics,
                coords[0],
                coords[1],
                -image.getWidth() / 2.0,
                -image.getHeight() / 2.0,
                // Doesn't seem to work with SVGs
                // Looking at the SLDStyleFactory, they get the icon from an
                // ExternalGraphicFactory.
                image,
                rotation,
                composite,
                isLabelObstacle);
          } catch (IOException ex) {
            Logger.getLogger(StyledShapePainter.class.getName()).log(Level.SEVERE, null, ex);
          }
          iter.next();
        }
      }
    }
  }