Exemplo n.º 1
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
  }
  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;
  }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
0
  /**
   * 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;
  }
Exemplo n.º 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
  }