Exemple #1
0
  private void sldExample() {
    // sldExample start
    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();

    StyledLayerDescriptor sld = styleFactory.createStyledLayerDescriptor();
    sld.setName("example");
    sld.setAbstract("Example Style Layer Descriptor");

    UserLayer layer = styleFactory.createUserLayer();
    layer.setName("layer");

    FeatureTypeConstraint constraint =
        styleFactory.createFeatureTypeConstraint("Feature", Filter.INCLUDE, null);

    layer.layerFeatureConstraints().add(constraint);

    Style style = styleFactory.createStyle();

    style.getDescription().setTitle("Style");
    style.getDescription().setAbstract("Definition of Style");

    // define feature type styles used to actually
    // define how features are rendered
    //
    layer.userStyles().add(style);

    sld.layers().add(layer);
    // sldExample end
  }
  public static StyledLayerDescriptor createDefaultSLD(Style style) {
    StyleFactory factory = CommonFactoryFinder.getStyleFactory();

    StyledLayerDescriptor sld = factory.createStyledLayerDescriptor();
    UserLayer layer = factory.createUserLayer();

    // FeatureTypeConstraint ftc =
    // styleFactory.createFeatureTypeConstraint(null, Filter.INCLUDE, null);
    layer.setLayerFeatureConstraints(new FeatureTypeConstraint[] {null});
    sld.addStyledLayer(layer);
    layer.addUserStyle((org.geotools.styling.Style) style);

    return sld;
  }
  /**
   * Generate SLD file. Creates everything but the FeatureTypeStyle that must be provided.
   *
   * @param fts FeatureTypeStyle. Contains all the rules and therefore the filters and symbolizers
   * @return StyledLayerDescriptor SLD file
   */
  private StyledLayerDescriptor createSLD(FeatureTypeStyle fts) {

    // create SLD
    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
    StyledLayerDescriptor sld = styleFactory.createStyledLayerDescriptor();

    // add named layer
    NamedLayer layer = styleFactory.createNamedLayer();
    // wfs-ng: ensures we recover the correct typename
    layer.setName(_command.getFeatureTypeName()); // name must match the layer name
    fts.setName(_command.getFeatureTypeName());
    sld.addStyledLayer(layer);

    // add a custom style to the user layer
    Style style = styleFactory.createStyle();
    style.setName(_command.getFeatureTypeName());
    style.setTitle(_command.getFeatureTypeName() + "_classification");
    style.addFeatureTypeStyle(fts);
    layer.addStyle(style);

    return sld;
  }
  private void convertToSLD_1_1() {
    if (style == null) {
      JOptionPane.showMessageDialog(this, "Style not defined");
      return;
    }
    // Wrap style as an SLD
    StyleFactory factory = CommonFactoryFinder.getStyleFactory();
    StyledLayerDescriptor sld = factory.createStyledLayerDescriptor();

    org.geotools.sld.v1_1.SLDConfiguration configuration =
        new org.geotools.sld.v1_1.SLDConfiguration();
    Encoder encoder = new org.geotools.xml.Encoder(configuration);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      encoder.encode(style, org.geotools.sld.bindings.SLD.STYLEDLAYERDESCRIPTOR, outputStream);
      String document = outputStream.toString("UTF-8");

      display(document, SLD_1_1);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemple #5
0
  private void styleFactoryExample() throws Exception {
    // styleFactoryExample start
    //
    // We are using the GeoTools styleFactory that allows access to get/set methods
    org.geotools.styling.StyleFactory sf = CommonFactoryFinder.getStyleFactory();
    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

    StyledLayerDescriptor sld = sf.createStyledLayerDescriptor();
    sld.setName("sld");
    sld.setTitle("Example");
    sld.setAbstract("Example Style Layer Descriptor");

    UserLayer layer = sf.createUserLayer();
    layer.setName("layer");

    //
    // define constraint limited what features the sld applies to
    FeatureTypeConstraint constraint =
        sf.createFeatureTypeConstraint("Feature", Filter.INCLUDE, null);

    layer.layerFeatureConstraints().add(constraint);

    //
    // create a "user defined" style
    Style style = sf.createStyle();
    style.setName("style");
    style.getDescription().setTitle("User Style");
    style.getDescription().setAbstract("Definition of Style");

    //
    // define feature type styles used to actually define how features are rendered
    FeatureTypeStyle featureTypeStyle = sf.createFeatureTypeStyle();

    // RULE 1
    // first rule to draw cities
    Rule rule1 = sf.createRule();
    rule1.setName("rule1");
    rule1.getDescription().setTitle("City");
    rule1.getDescription().setAbstract("Rule for drawing cities");
    rule1.setFilter(ff.less(ff.property("POPULATION"), ff.literal(50000)));

    //
    // create the graphical mark used to represent a city
    Stroke stroke = sf.stroke(ff.literal("#000000"), null, null, null, null, null, null);
    Fill fill = sf.fill(null, ff.literal(Color.BLUE), ff.literal(1.0));

    // OnLineResource implemented by gt-metadata - so no factory!
    OnLineResourceImpl svg = new OnLineResourceImpl(new URI("file:city.svg"));
    svg.freeze(); // freeze to prevent modification at runtime

    OnLineResourceImpl png = new OnLineResourceImpl(new URI("file:city.png"));
    png.freeze(); // freeze to prevent modification at runtime

    //
    // List of symbols is considered in order with the rendering engine choosing
    // the first one it can handle. Allowing for svg, png, mark order
    List<GraphicalSymbol> symbols = new ArrayList<GraphicalSymbol>();
    symbols.add(sf.externalGraphic(svg, "svg", null)); // svg preferred
    symbols.add(sf.externalGraphic(png, "png", null)); // png preferred
    symbols.add(sf.mark(ff.literal("circle"), fill, stroke)); // simple circle backup plan

    Expression opacity = null; // use default
    Expression size = ff.literal(10);
    Expression rotation = null; // use default
    AnchorPoint anchor = null; // use default
    Displacement displacement = null; // use default

    // define a point symbolizer of a small circle
    Graphic city = sf.graphic(symbols, opacity, size, rotation, anchor, displacement);
    PointSymbolizer pointSymbolizer =
        sf.pointSymbolizer("point", ff.property("the_geom"), null, null, city);

    rule1.symbolizers().add(pointSymbolizer);

    featureTypeStyle.rules().add(rule1);

    //
    // RULE 2 Default

    List<GraphicalSymbol> dotSymbols = new ArrayList<GraphicalSymbol>();
    dotSymbols.add(sf.mark(ff.literal("circle"), null, null));
    Graphic dotGraphic = sf.graphic(dotSymbols, null, ff.literal(3), null, null, null);
    PointSymbolizer dotSymbolizer = sf.pointSymbolizer("dot", null, null, null, dotGraphic);
    List<org.opengis.style.Symbolizer> symbolizers = new ArrayList<org.opengis.style.Symbolizer>();
    symbolizers.add(dotSymbolizer);
    Filter other = null; // null will mark this rule as "other" accepting all remaining features
    Rule rule2 =
        sf.rule("default", null, null, Double.MIN_VALUE, Double.MAX_VALUE, symbolizers, other);
    featureTypeStyle.rules().add(rule2);

    style.featureTypeStyles().add(featureTypeStyle);

    layer.userStyles().add(style);

    sld.layers().add(layer);
    // styleFactoryExample end
  }