Ejemplo n.º 1
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;
  }