示例#1
0
  /**
   * This transforms all external graphics references that are relative to absolute. This is a
   * workaround to be able to visualize png and svg in relative mode, which doesn't work right now
   * in geotools. See: http://jira.codehaus.org/browse/GEOT-3235
   *
   * <p>This will not be necessary any more as soon as the geotools bug is fixed.
   *
   * @param relatedFile the related shapefile.
   * @param style the style to check.
   */
  private void makeGraphicsAbsolute(File relatedFile, Style style) {
    File parentFolder = relatedFile.getParentFile();
    Rule[] rules = SLDs.rules(style);
    for (Rule rule : rules) {
      Symbolizer[] onlineResource = rule.getSymbolizers();

      for (Symbolizer symbolizer : onlineResource) {
        if (symbolizer instanceof PointSymbolizer) {
          PointSymbolizer pointSymbolizer = (PointSymbolizer) symbolizer;
          Graphic graphic = SLDs.graphic(pointSymbolizer);
          List<GraphicalSymbol> graphicalSymbols = graphic.graphicalSymbols();
          for (GraphicalSymbol graphicalSymbol : graphicalSymbols) {
            if (graphicalSymbol instanceof ExternalGraphic) {
              ExternalGraphic externalGraphic = (ExternalGraphic) graphicalSymbol;
              try {
                URL location = externalGraphic.getLocation();
                File urlToFile = URLUtils.urlToFile(location);
                if (urlToFile != null && !urlToFile.exists()) {
                  File newFile = new File(parentFolder, urlToFile.getPath());
                  if (newFile.exists()) {
                    externalGraphic.setLocation(newFile.toURI().toURL());
                  }
                }
              } catch (MalformedURLException e) {
                e.printStackTrace();
              }
            }
          }
        }
      }
    }
  }
  private void setStylingElements(RasterSymbolizer symbolizer) {
    double number = SLDs.rasterOpacity(symbolizer);
    int opacity = (new Double(number * 100)).intValue();

    opacityScale.setSelection(opacity);
    opacityText.setText(Integer.toString(opacity) + "%"); // $NON-NLS-1$
    opacityText.pack(true);
  }
 private boolean isPointStyle(Style style) {
   Symbolizer[] symbolizers = SLDs.symbolizers(style);
   for (Symbolizer symbolizer : symbolizers) {
     if (symbolizer instanceof PointSymbolizer) {
       return true;
     }
   }
   return false;
 }
 public Object[] getChildren(Object parentElement) {
   if (parentElement == null) return EMPTY_ARRAY;
   if (parentElement instanceof Style) {
     Style style = (Style) parentElement;
     Rule[] rule = SLDs.rules(style);
     return rule; // return rules
   } else if (parentElement instanceof FeatureTypeStyle) {
     FeatureTypeStyle fts = (FeatureTypeStyle) parentElement;
     List<Rule> rules = fts.rules();
     Rule[] rulesArray = (Rule[]) rules.toArray(new Rule[rules.size()]);
     return rulesArray;
   }
   return EMPTY_ARRAY;
 }
  // XXX: Consider doing more SLD when WMS post is a go
  public boolean canStyle(String SyleID, Object value) {
    if (value == null) return false;
    if (value instanceof Style) return !Double.isNaN(SLDs.rasterOpacity((Style) value));

    if (value instanceof StyleImpl && getRenderContext().getGeoResource().canResolve(Layer.class)) {
      try {
        Layer layer =
            getRenderContext()
                .getGeoResource()
                .resolve(Layer.class, ProgressManager.instance().get());
        if (layer.getStyles().contains(value)) {
          return true;
        } else {
          return false;
        }
      } catch (IOException e) {
        return false;
      }
    }

    return false;
  }
  /**
   * Start editing the provided symbolizer.
   *
   * @param schema
   * @param sym
   * @param mode
   */
  public void set(SimpleFeatureType schema, TextSymbolizer sym, Mode mode) {
    listen(false);
    try {
      this.schema = schema;
      this.enabled = (mode != Mode.NONE && sym != null);

      this.font = SLDs.textFont(sym);
      if (this.font == null || this.font.length == 0) {
        this.font = new FontData[] {new FontData("Arial", 12, SWT.NORMAL)}; // $NON-NLS-1$
      }
      this.labelType = SLDs.textLabelString(sym);
      this.colour = SLDs.textFontFill(sym);
      if (this.colour == null) {
        this.colour = Color.BLACK;
      }

      this.on.setEnabled(mode != Mode.NONE);
      this.fonter.setColorValue(this.colour);
      this.fonter.setFontList(this.font);

      if (schema != null) {
        AttributeDescriptor[] attrs =
            schema.getAttributeDescriptors().toArray(new AttributeDescriptor[0]);
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < attrs.length; i++) {
          Class cls = attrs[i].getType().getBinding();
          if (String.class.isAssignableFrom(cls)) {
            list.add(attrs[i].getName().getLocalPart());
          } else if (Number.class.isAssignableFrom(cls)) {
            list.add(attrs[i].getName().getLocalPart());
          }
        }
        this.field.removeAll();
        this.field.setItems(list.toArray(new String[0]));
        if (this.labelType == null) {
          this.field.select(0);
        } else {
          this.field.setText(this.labelType);
        }
      }

      this.on.setSelection(this.enabled);
      this.field.setEnabled(this.enabled);
      this.fonter.setEnabled(this.enabled);

      if (schema != null && (SLD.isLine(schema) == pointPlacement || this.place == null)) {
        pointPlacement = !SLD.isLine(schema);
        if (pointPlacement) {
          initPlacementContentsPoint();
        } else {
          initPlacementContentsLine();
        }
      }
      this.place.setEnabled(this.enabled);
      if (pointPlacement) {
        // PointPlacement
        this.place2.setEnabled(this.enabled);
        this.place3.setEnabled(this.enabled);
        if (this.labelPlacement == null || !(this.labelPlacement instanceof PointPlacement)) {
          // defaults
          if (mode == Mode.POINT) {
            // don't cover the point!
            this.place.select(2); // top
            this.place2.select(2); // right
          } else {
            this.place.select(1); // middle
            this.place2.select(1); // center
          }
          this.place3.select(0); // 0 degrees rotation
        } else {
          AnchorPoint anchor = ((PointPlacement) labelPlacement).getAnchorPoint();
          String anchorX = anchor.getAnchorPointX().toString();
          String anchorY = anchor.getAnchorPointY().toString();
          // use labels if 0, 0.5, or 1, otherwise use value for align
          if (anchorX.equals(Double.toString(SLDs.ALIGN_LEFT))) {
            this.place2.select(0);
          } else if (anchorX.equals(Double.toString(SLDs.ALIGN_CENTER))) {
            this.place2.select(1);
          } else if (anchorX.equals(Double.toString(SLDs.ALIGN_RIGHT))) {
            this.place2.select(2);
          } else {
            this.place2.setText(anchorX);
          }
          if (anchorY.equals(Double.toString(SLDs.ALIGN_BOTTOM))) {
            this.place.select(0);
          } else if (anchorY.equals(Double.toString(SLDs.ALIGN_MIDDLE))) {
            this.place.select(1);
          } else if (anchorY.equals(Double.toString(SLDs.ALIGN_TOP))) {
            this.place.select(2);
          } else {
            this.place.setText(anchorY);
          }
          // rotation
          this.place3.setText(((PointPlacement) labelPlacement).getRotation().toString());
        }
      } else {
        // LinePlacement
        if (this.labelPlacement == null || !(this.labelPlacement instanceof LinePlacement)) {
          // defaults
          this.place.setText(
              ProjectPlugin.getPlugin()
                  .getPreferenceStore()
                  .getString(PreferenceConstants.P_STYLE_DEFAULT_PERPENDICULAR_OFFSET));
        } else {
          String offset = ((LinePlacement) labelPlacement).getPerpendicularOffset().toString();
          this.place.setText(offset);
        }
      }
    } finally {
      listen(true);
    }
  }
  public boolean canAddLayer(ILayer layer) {

    if (!layer.hasResource(Layer.class)) return false;

    try {
      if (!layer
          .findGeoResource(Layer.class)
          .parent(ProgressManager.instance().get())
          .equals(getRenderContext().getGeoResource().parent(ProgressManager.instance().get())))
        return false;
    } catch (IOException e2) {
      return false;
    }

    double opacity = Double.NaN;

    ICompositeRenderContext context1 = (ICompositeRenderContext) context;

    IRenderContext[] contexts =
        context1.getContexts().toArray(new IRenderContext[context1.getContexts().size()]);
    Arrays.sort(contexts);
    List<Layer> owsLayers = new ArrayList<Layer>();
    IService currentService;
    try {
      owsLayers.add(layer.getResource(Layer.class, new NullProgressMonitor()));
      currentService = layer.getResource(IService.class, null);
    } catch (IOException e1) {
      WMSPlugin.log("", e1); // $NON-NLS-1$
      return false;
    }
    for (IRenderContext renderContext : contexts) {
      ILayer previousLayer = renderContext.getLayer();

      try {
        owsLayers.add(previousLayer.getResource(Layer.class, new NullProgressMonitor()));
        IService previousService = previousLayer.getResource(IService.class, null);
        if (currentService != previousService) {
          return false;
        }
      } catch (IOException e) {
        WMSPlugin.log("Error while retrieving service.", e); // $NON-NLS-1$
        return false;
      }

      if (BasicWMSRenderer2.findRequestCRS(owsLayers, context.getCRS(), context.getMap()) == null)
        return false;

      Style style = (Style) previousLayer.getStyleBlackboard().get(SLDContent.ID);
      if (style != null) {
        opacity = SLDs.rasterOpacity(SLDs.rasterSymbolizer(style));
      }
    }

    Style style = (Style) layer.getStyleBlackboard().get(SLDContent.ID);
    if (style == null && Double.isNaN(opacity)) {
      return true;
    }

    double result = SLDs.rasterOpacity(SLDs.rasterSymbolizer(style));

    if (result == opacity) {
      return true;
    }
    return false;
  }