Example #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
  }
Example #2
0
  /** Create a default Style for feature display */
  private Style createDefaultStyle() {
    Rule rule = createRule(LINE_COLOUR, FILL_COLOUR);

    FeatureTypeStyle fts = sf.createFeatureTypeStyle();
    fts.rules().add(rule);

    Style style = sf.createStyle();
    style.featureTypeStyles().add(fts);
    return style;
  }
  private Rule createRule(Color outlineColor, Color fillColor, boolean bSelected) {
    Symbolizer symbolizer = null;
    Fill fill = null;
    Stroke stroke = sf.createStroke(ff.literal(outlineColor), ff.literal(LINE_WIDTH));

    switch (geometryType) {
      case POLYGON:
        fill = sf.createFill(ff.literal(fillColor), ff.literal(OPACITY));
        symbolizer = sf.createPolygonSymbolizer(stroke, fill, geometryAttributeName);
        break;

      case LINE:
        symbolizer = sf.createLineSymbolizer(stroke, geometryAttributeName);
        break;

      case POINT:
        fill = sf.createFill(ff.literal(fillColor), ff.literal(OPACITY));

        Mark mark = sf.getTriangleMark();
        mark.setFill(fill);
        mark.setStroke(stroke);

        Graphic graphic = sf.createDefaultGraphic();
        graphic.graphicalSymbols().clear();
        graphic.graphicalSymbols().add(mark);
        graphic.setSize(ff.literal(bSelected ? SELECTED_POINT_SIZE : POINT_SIZE));

        symbolizer = sf.createPointSymbolizer(graphic, geometryAttributeName);
    }

    Rule rule = sf.createRule();
    rule.symbolizers().add(symbolizer);
    return rule;
  }
  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;
  }
Example #5
0
  /**
   * Create a Style where features with given IDs are painted yellow, while others are painted with
   * the default colors.
   */
  private Style createSelectedStyle(Set<FeatureId> IDs) {
    Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR);
    selectedRule.setFilter(ff.id(IDs));

    Rule otherRule = createRule(LINE_COLOUR, FILL_COLOUR);
    otherRule.setElseFilter(true);

    FeatureTypeStyle fts = sf.createFeatureTypeStyle();
    fts.rules().add(selectedRule);
    fts.rules().add(otherRule);

    Style style = sf.createStyle();
    style.featureTypeStyles().add(fts);
    return style;
  }
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated modifiable
   */
  public Object parse(ElementInstance instance, Node node, Object value) throws Exception {
    Expression color = null;
    Expression opacity = null;
    Graphic graphicFill = null;

    graphicFill = (Graphic) node.getChildValue("GraphicFill");

    // &quot;fill&quot; (color)
    // &quot;fill-opacity&quot;
    List params = node.getChildValues(CssParameter.class);

    for (Iterator itr = params.iterator(); itr.hasNext(); ) {
      CssParameter param = (CssParameter) itr.next();

      if ("fill".equals(param.getName())) {
        color = param.getExpression();
      }

      if ("fill-opacity".equals(param.getName())) {
        opacity = param.getExpression();
      }
    }

    Fill fill = styleFactory.createFill(color);

    if (opacity != null) {
      fill.setOpacity(opacity);
    }

    return fill;
  }
  private Style createDefaultStyle(int idx, Set<FeatureId> IDs) {
    Color fillColor = featureFills[idx];

    Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR, true);
    selectedRule.setFilter(ff.id(IDs));

    Rule rule = createRule(fillColor, fillColor, false);
    rule.setElseFilter(true);

    FeatureTypeStyle fts = sf.createFeatureTypeStyle();
    fts.rules().add(selectedRule);
    fts.rules().add(rule);

    Style style = sf.createStyle();
    style.featureTypeStyles().add(fts);
    return style;
  }
Example #8
0
 static Style makeStyle(StyleFactory factory, List<List<MiniRule>> ftStyles) {
   Style style = factory.createStyle();
   for (List<MiniRule> rules : ftStyles) {
     FeatureTypeStyle ftStyle = factory.createFeatureTypeStyle();
     for (MiniRule miniRule : rules) {
       if (!miniRule.symbolizers.isEmpty()) {
         Rule realRule = factory.createRule();
         for (Symbolizer sym : miniRule.symbolizers) {
           realRule.symbolizers().add(sym);
         }
         ftStyle.rules().add(realRule);
       }
     }
     style.featureTypeStyles().add(ftStyle);
   }
   return style;
 }
  /**
   * Creates a FeatureTypeStyle (core part of a SLD file). It is composed by Rules (tag <sld:Rule>)
   * and each Rule can contain one Filter (filters values) and one Symbolizer (what's displayed). It
   * Needs 2 factories: <br>
   * - I_FilterFactory: gives Filters (tag <ogc:Filter>) <br>
   * - I_SymbolizerFactory: gives Symbolizers (tag: <sld:PolygonSymbolizer> or
   * <sld:PointSymbolizer>) <br>
   *
   * @param filterFact Filters Factory
   * @param symbolizerFact Symbolizers Factory
   * @return FeatureTypeStyle (List of Rules)
   */
  private FeatureTypeStyle createFeatureTypeStyle(
      I_FilterFactory filterFact, I_SymbolizerFactory symbolizerFact) {

    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
    FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
    Iterator<Symbolizer> symbolizers = symbolizerFact.iterator();
    Iterator<Filter> filters = filterFact.iterator();

    // use each factory iterator to get their items (Filter and Symbolizer)
    // there should be as many symbolizers as many filters
    while (symbolizers.hasNext() && filters.hasNext()) {

      // create a Rule. A Rule is composed by one Filter and one Symbolizer
      Rule rule = styleFactory.createRule();
      Filter filter = filters.next();
      rule.setSymbolizers(new Symbolizer[] {symbolizers.next()});
      rule.setFilter(filter.getGISFilter());
      rule.setName(filter.getName());
      rule.setTitle(filter.getName());
      fts.addRule(rule);
    }

    if (filters.hasNext()) {
      throw new RuntimeException("BUG: more filters than symbolizers");
    }

    /*
     * This piece of code can be added to add a stroke around the polygons.
     * This rule does not include a filter, therefore, it is globally applied.
     */

    /*
    StyleBuilder styleBuilder = new StyleBuilder();
    Rule rule = styleFactory.createRule();
    Stroke stroke = styleFactory.createStroke(
            styleBuilder.literalExpression(Color.BLACK),  // color
            styleBuilder.literalExpression(1),  // width
            styleBuilder.literalExpression(1.0)); // opacity
    LineSymbolizer ls = styleFactory.createLineSymbolizer(stroke, "");
    rule.setSymbolizers(new Symbolizer[] {ls});
    fts.addRule(rule);
    */

    return fts;
  }
Example #10
0
  @SuppressWarnings("unchecked")
  private static Style createStyle(SimpleFeatureSource source, String fieldName) throws Exception {

    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

    Function colourFn = ff.function("colorlookup", ff.literal(source), ff.property(fieldName));

    Stroke stroke =
        styleFactory.createStroke(
            colourFn,
            ff.literal(1.0f), // line
            // width
            ff.literal(1.0f)); // opacity

    Fill fill = styleFactory.createFill(colourFn, ff.literal(1.0f)); // opacity

    Class<?> geomClass = source.getSchema().getGeometryDescriptor().getType().getBinding();
    Symbolizer sym = null;
    Geometries geomType = Geometries.getForBinding((Class<? extends Geometry>) geomClass);

    switch (geomType) {
      case POLYGON:
      case MULTIPOLYGON:
        sym = styleFactory.createPolygonSymbolizer(stroke, fill, null);
        break;

      case LINESTRING:
      case MULTILINESTRING:
        sym = styleFactory.createLineSymbolizer(stroke, null);
        break;

      case POINT:
      case MULTIPOINT:
        Graphic gr = styleFactory.createDefaultGraphic();
        gr.graphicalSymbols().clear();
        Mark mark = styleFactory.getCircleMark();
        mark.setFill(fill);
        mark.setStroke(stroke);
        gr.graphicalSymbols().add(mark);
        gr.setSize(ff.literal(10.0f));
        sym = styleFactory.createPointSymbolizer(gr, null);
        break;

      default:
        throw new IllegalArgumentException("Unsupported geometry type");
    }

    Style style = SLD.wrapSymbolizers(sym);

    return style;
  }
Example #11
0
  private void twoFeatureTypeStyles() {
    // twoFeatureTypeStyles start
    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
    FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2();

    Style style = styleFactory.getDefaultStyle();

    // Feature type style 1
    FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
    fts.featureTypeNames().add(new NameImpl("feature-type-1"));
    style.featureTypeStyles().add(fts);

    // Feature type style 2
    FeatureTypeStyle fts2 = styleFactory.createFeatureTypeStyle();
    fts2.featureTypeNames().add(new NameImpl("feature-type-2"));

    // creating the rule 1
    Rule rule1 = styleFactory.createRule();
    rule1.setName("rule1");
    Filter aFilter = filterFactory.id(Collections.singleton(filterFactory.featureId("FID")));
    rule1.setFilter(aFilter);
    fts2.rules().add(rule1);

    // creating the rule 2
    Rule rule2 = styleFactory.createRule();
    rule2.setIsElseFilter(true);
    rule2.setName("rule2");
    fts2.rules().add(rule2);

    style.featureTypeStyles().add(fts2);
    // twoFeatureTypeStyles end
  }
  /**
   * 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();
    }
  }
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated modifiable
   */
  public Object parse(ElementInstance instance, Node node, Object value) throws Exception {
    TextSymbolizer ts = styleFactory.createTextSymbolizer();

    // &lt;xsd:element ref="sld:Geometry" minOccurs="0"/&gt;
    if (node.hasChild("Geometry")) {
      Expression geometry = (Expression) node.getChildValue("Geometry");
      if (geometry instanceof PropertyName) {
        PropertyName propertyName = (PropertyName) geometry;
        ts.setGeometryPropertyName(propertyName.getPropertyName());
      } else {
        ts.setGeometry(geometry);
      }
    }

    // &lt;xsd:element ref="sld:Label" minOccurs="0"/&gt;
    if (node.hasChild("Label")) {
      ts.setLabel((Expression) node.getChildValue("Label"));
    }

    // &lt;xsd:element ref="sld:Font" minOccurs="0"/&gt;
    if (node.hasChild("Font")) {
      ts.setFonts(new Font[] {(Font) node.getChildValue("Font")});
    }

    // &lt;xsd:element ref="sld:LabelPlacement" minOccurs="0"/&gt;
    if (node.hasChild("LabelPlacement")) {
      ts.setPlacement((LabelPlacement) node.getChildValue("LabelPlacement"));
    }

    // &lt;xsd:element ref="sld:Halo" minOccurs="0"/&gt;
    if (node.hasChild("Halo")) {
      ts.setHalo((Halo) node.getChildValue("Halo"));
    }

    // &lt;xsd:element ref="sld:Fill" minOccurs="0"/&gt;
    if (node.hasChild("Fill")) {
      ts.setFill((Fill) node.getChildValue("Fill"));
    }

    if (node.hasChild("Priority")) {
      ts.setPriority((Expression) node.getChildValue("Priority"));
    }

    // &lt;xsd:element ref="sld:VendorOption" minOccurs="0" maxOccurs="unbounded"/&gt;
    for (CssParameter param : (List<CssParameter>) node.getChildValues(CssParameter.class)) {
      ts.getOptions().put(param.getName(), param.getExpression().evaluate(null, String.class));
    }
    return ts;
  }
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated modifiable
   */
  public Object parse(ElementInstance instance, Node node, Object value) throws Exception {
    RasterSymbolizer rs = styleFactory.createRasterSymbolizer();

    // &lt;xsd:element ref="sld:Geometry" minOccurs="0"/&gt;
    if (node.hasChild("Geometry")) {
      PropertyName propertyName = (PropertyName) node.getChildValue("Geometry");
      rs.setGeometryPropertyName(propertyName.getPropertyName());
    }

    // &lt;xsd:element ref="sld:Opacity" minOccurs="0"/&gt;
    if (node.hasChild("Opacity")) {
      rs.setOpacity((Expression) node.getChildValue("Opacity"));
    }

    // &lt;xsd:element ref="sld:ChannelSelection" minOccurs="0"/&gt;
    if (node.hasChild("ChannelSelection")) {
      rs.setChannelSelection((ChannelSelection) node.getChildValue("ChannelSelection"));
    }

    // &lt;xsd:element ref="sld:OverlapBehavior" minOccurs="0"/&gt;
    if (node.hasChild("OverlapBehavior")) {
      rs.setOverlapBehavior((OverlapBehavior) node.getChildValue("OverlapBehavior"));
    }

    // &lt;xsd:element ref="sld:ColorMap" minOccurs="0"/&gt;
    if (node.hasChild("ColorMap")) {
      rs.setColorMap((ColorMap) node.getChildValue("ColorMap"));
    }

    // &lt;xsd:element ref="sld:ContrastEnhancement" minOccurs="0"/&gt;
    if (node.hasChild("ContrastEnhancement")) {
      rs.setContrastEnhancement((ContrastEnhancement) node.getChildValue("ContrastEnhancement"));
    }

    // &lt;xsd:element ref="sld:ShadedRelief" minOccurs="0"/&gt;
    if (node.hasChild("ShadedRelief")) {
      rs.setShadedRelief((ShadedRelief) node.getChildValue("ShadedRelief"));
    }

    // &lt;xsd:element ref="sld:ImageOutline" minOccurs="0"/&gt;
    if (node.hasChild("ImageOutline")) {
      ImageOutline imageOutput = (ImageOutline) node.getChildValue("ImageOutline");
      rs.setImageOutline(imageOutput.getSymbolizer());
    }

    return rs;
  }
Example #16
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
  }