use of org.opengis.style.PolygonSymbolizer in project geotoolkit by Geomatys.
the class MapContextTileGenerator method generate.
@Override
public void generate(TileMatrixSet pyramid, Envelope env, NumberRange resolutions, ProcessListener listener) throws DataStoreException, InterruptedException {
// check if we can optimize tiles generation
boolean rasterOptimisation = true;
search: for (MapLayer layer : MapBuilder.getLayers(sceneDef.getContext())) {
final Style style = layer.getStyle();
for (FeatureTypeStyle fts : style.featureTypeStyles()) {
for (Rule rule : fts.rules()) {
double scaleMin = rule.getMinScaleDenominator();
double scaleMax = rule.getMaxScaleDenominator();
// CanvasUtilities.computeSEScale
if (scaleMin != 0.0 || scaleMax < 5.0E9) {
rasterOptimisation = false;
break search;
}
for (Symbolizer symbolizer : rule.symbolizers()) {
if (symbolizer instanceof RasterSymbolizer || symbolizer instanceof DynamicRangeSymbolizer) {
// ok
} else if (symbolizer instanceof PolygonSymbolizer) {
PolygonSymbolizer ps = (PolygonSymbolizer) symbolizer;
// check if we have a plain fill
Displacement displacement = ps.getDisplacement();
Fill fill = ps.getFill();
Stroke stroke = ps.getStroke();
Expression perpendicularOffset = ps.getPerpendicularOffset();
if (displacement != null) {
Double dx = doubleValue(displacement.getDisplacementX());
Double dy = doubleValue(displacement.getDisplacementX());
if ((dx != null && dx != 0.0) || (dy != null && dy != 0.0)) {
rasterOptimisation = false;
break search;
}
}
if (perpendicularOffset != null) {
Double off = doubleValue(perpendicularOffset);
if (off != null && off != 0.0) {
rasterOptimisation = false;
break search;
}
}
if (stroke != null) {
Double op = doubleValue(stroke.getOpacity());
Double wd = doubleValue(stroke.getWidth());
if ((op == null || op == 0.0) || (wd == null || wd == 0.0)) {
// not visible
} else {
rasterOptimisation = false;
break search;
}
}
if (fill != null) {
if (fill.getGraphicFill() != null) {
rasterOptimisation = false;
break search;
}
}
} else {
rasterOptimisation = false;
break search;
}
}
}
}
}
if (rasterOptimisation) {
/*
We can generate the pyramid starting from the lowest level then going up
using the previously generated level.
*/
if (env != null) {
try {
CoordinateReferenceSystem targetCrs = pyramid.getCoordinateReferenceSystem();
Envelope baseEnv = env;
env = Envelopes.transform(env, targetCrs);
double[] minres = new double[] { resolutions.getMinDouble(), resolutions.getMinDouble() };
double[] maxres = new double[] { resolutions.getMaxDouble(), resolutions.getMaxDouble() };
minres = ReferencingUtilities.convertResolution(baseEnv, minres, targetCrs, null);
maxres = ReferencingUtilities.convertResolution(baseEnv, maxres, targetCrs, null);
resolutions = NumberRange.create(minres[0], true, maxres[0], true);
} catch (TransformException ex) {
throw new DataStoreException(ex.getMessage(), ex);
}
}
// generate lower level from data
final TileMatrix[] mosaics = pyramid.getTileMatrices().toArray(new TileMatrix[0]);
Arrays.sort(mosaics, (TileMatrix o1, TileMatrix o2) -> Double.compare(o1.getScale(), o2.getScale()));
MapLayers parent = sceneDef.getContext();
Hints hints = sceneDef.getHints();
final long total = TileMatrices.countTiles(pyramid, env, resolutions);
final AtomicLong al = new AtomicLong();
// send an event only every few seconds
final AtomicLong tempo = new AtomicLong(System.currentTimeMillis());
final String msg = " / " + NumberFormat.getIntegerInstance(Locale.FRANCE).format(total);
for (final TileMatrix mosaic : mosaics) {
if (resolutions == null || resolutions.contains(mosaic.getScale())) {
final Rectangle rect;
try {
rect = TileMatrices.getTilesInEnvelope(mosaic, env);
} catch (NoSuchDataException ex) {
continue;
}
final CanvasDef canvasDef = new CanvasDef();
canvasDef.setBackground(this.canvasDef.getBackground());
canvasDef.setEnvelope(mosaic.getEnvelope());
final SceneDef sceneDef = new SceneDef(parent, hints);
// one thread per line, the progressive image generates multiple tiles when drawing
// this approach is more efficient from profiling result then using tile by tile
// generation
Stream<Tile> stream = LongStream.range(rect.y, rect.y + rect.height).parallel().boxed().flatMap((Long y) -> {
try {
final ProgressiveImage img = new ProgressiveImage(canvasDef, sceneDef, mosaic.getGridSize(), mosaic.getTileSize(), mosaic.getScale(), 0);
return img.generate(rect.x, rect.x + rect.width, y.intValue(), skipEmptyTiles);
} catch (Exception ex) {
LOGGER.log(Level.INFO, "Failed to generate a tile {0}", ex.getMessage());
LOGGER.log(Level.FINER, "Failed to generate a tile ", ex);
return Stream.empty();
}
});
if (listener != null) {
final NumberFormat format = NumberFormat.getIntegerInstance(Locale.FRANCE);
stream = stream.map((Tile t) -> {
long n = al.incrementAndGet();
if (n % 1000 == 0) {
final long time = System.currentTimeMillis();
if (tempo.updateAndGet((long operand) -> ((time - operand) > 3000) ? time : operand) == time) {
listener.progressing(new ProcessEvent(DUMMY, format.format(n) + msg, (float) ((((double) n) / ((double) total)) * 100.0)));
}
}
return t;
});
}
mosaic.writeTiles(stream, null);
// last level event
final NumberFormat format = NumberFormat.getIntegerInstance(Locale.FRANCE);
long v = al.get();
if (listener != null) {
listener.progressing(new ProcessEvent(DUMMY, format.format(v) + msg, (float) ((((double) v) / ((double) total)) * 100.0)));
}
// modify context
final DefaultTileMatrixSet pm = new DefaultTileMatrixSet(pyramid.getCoordinateReferenceSystem());
pm.getMosaicsInternal().add(mosaic);
final InMemoryTiledGridCoverageResource r = new InMemoryTiledGridCoverageResource(NamesExt.create("test"));
r.setSampleDimensions(sampleDimensions);
r.getTileMatrixSets().add(pm);
final MapLayers mc = MapBuilder.createContext();
mc.getComponents().add(MapBuilder.createLayer(r));
parent = mc;
hints = new Hints(hints);
hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
}
}
} else {
super.generate(pyramid, env, resolutions, listener);
}
}
use of org.opengis.style.PolygonSymbolizer in project geotoolkit by Geomatys.
the class CategoryStyleBuilder method derivateSymbolizer.
/**
* Derivate a symbolizer with a new color.
*/
public Symbolizer derivateSymbolizer(final Symbolizer symbol, final Color color) {
if (symbol instanceof PolygonSymbolizer) {
PolygonSymbolizer ps = (PolygonSymbolizer) symbol;
Fill fill = sf.fill(sf.literal(color), ps.getFill().getOpacity());
return sf.polygonSymbolizer(ps.getName(), ps.getGeometryPropertyName(), ps.getDescription(), ps.getUnitOfMeasure(), ps.getStroke(), fill, ps.getDisplacement(), ps.getPerpendicularOffset());
} else if (symbol instanceof LineSymbolizer) {
LineSymbolizer ls = (LineSymbolizer) symbol;
Stroke oldStroke = ls.getStroke();
Stroke stroke = sf.stroke(sf.literal(color), oldStroke.getOpacity(), oldStroke.getWidth(), oldStroke.getLineJoin(), oldStroke.getLineCap(), oldStroke.getDashArray(), oldStroke.getDashOffset());
return sf.lineSymbolizer(ls.getName(), ls.getGeometryPropertyName(), ls.getDescription(), ls.getUnitOfMeasure(), stroke, ls.getPerpendicularOffset());
} else if (symbol instanceof PointSymbolizer) {
PointSymbolizer ps = (PointSymbolizer) symbol;
Graphic oldGraphic = ps.getGraphic();
Mark oldMark = (Mark) oldGraphic.graphicalSymbols().get(0);
Fill fill = sf.fill(sf.literal(color), oldMark.getFill().getOpacity());
List<GraphicalSymbol> symbols = new ArrayList<GraphicalSymbol>();
symbols.add(sf.mark(oldMark.getWellKnownName(), fill, oldMark.getStroke()));
Graphic graphic = sf.graphic(symbols, oldGraphic.getOpacity(), oldGraphic.getSize(), oldGraphic.getRotation(), oldGraphic.getAnchorPoint(), oldGraphic.getDisplacement());
return sf.pointSymbolizer(graphic, ps.getGeometryPropertyName());
} else {
throw new IllegalArgumentException("unexpected symbolizer type : " + symbol);
}
}
use of org.opengis.style.PolygonSymbolizer in project geotoolkit by Geomatys.
the class GeometryExpressionTest method bufferGeoTest.
/**
* Test a geo buffer expression around geometry.
*
* TODO : make a real test case
*/
@Ignore
@Test
public void bufferGeoTest() throws Exception {
final CoordinateReferenceSystem crs2154 = CRS.forCode("EPSG:2154");
final CoordinateReferenceSystem crs3857 = CRS.forCode("EPSG:3857");
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Point.class).setName("geom").setCRS(crs2154);
final FeatureType type = ftb.build();
final Point point = GO2Utilities.JTS_FACTORY.createPoint(new Coordinate(0, 0));
JTS.setCRS(point, crs2154);
final Feature feature = type.newInstance();
feature.setPropertyValue("geom", point);
final Expression geomExp = FF.function("bufferGeo", new Expression[] { FF.property("geom"), FF.literal(100), FF.literal("m") });
final Stroke stroke = SF.stroke(Color.BLACK, 10);
final Fill fill = SF.fill(Color.RED);
final PolygonSymbolizer symbolizer = SF.polygonSymbolizer("", geomExp, DEFAULT_DESCRIPTION, Units.METRE, stroke, fill, DEFAULT_DISPLACEMENT, LITERAL_ZERO_FLOAT);
final MutableStyle style = SF.style(symbolizer);
final MapLayers context = MapBuilder.createContext();
final MapLayer fml = MapBuilder.createLayer(FeatureStoreUtilities.collection(feature));
fml.setStyle(style);
context.getComponents().add(fml);
final GeneralEnvelope env = new GeneralEnvelope(crs3857);
final Point pt = (Point) org.apache.sis.internal.feature.jts.JTS.transform(point, CRS.findOperation(crs2154, crs3857, null).getMathTransform());
env.setRange(0, pt.getX() - 500, pt.getX() + 500);
env.setRange(1, pt.getY() - 500, pt.getY() + 500);
final CanvasDef canvasDef = new CanvasDef();
canvasDef.setEnvelope(env);
canvasDef.setDimension(new Dimension(100, 100));
canvasDef.setBackground(Color.WHITE);
final SceneDef sceneDef = new SceneDef(context);
final BufferedImage image = DefaultPortrayalService.portray(canvasDef, sceneDef);
ImageIO.write(image, "png", new File("test.png"));
// we must obtain a red point of 20pixel width at image center
final int red = Color.RED.getRGB();
final int white = Color.WHITE.getRGB();
Assert.assertEquals(white, image.getRGB(20, 9));
Assert.assertEquals(red, image.getRGB(20, 11));
Assert.assertEquals(red, image.getRGB(20, 20));
Assert.assertEquals(red, image.getRGB(20, 29));
Assert.assertEquals(white, image.getRGB(20, 31));
Assert.assertEquals(white, image.getRGB(9, 20));
Assert.assertEquals(red, image.getRGB(11, 20));
Assert.assertEquals(red, image.getRGB(20, 20));
Assert.assertEquals(red, image.getRGB(29, 20));
Assert.assertEquals(white, image.getRGB(31, 20));
}
use of org.opengis.style.PolygonSymbolizer in project geotoolkit by Geomatys.
the class SE100toGTTransformer method visitRule.
/**
* Trasnform SLD v1.0 rule in GT Rule.
*/
public MutableRule visitRule(final org.geotoolkit.sld.xml.v100.Rule rt) {
final MutableRule rule = styleFactory.rule();
rule.setName(rt.getName());
final InternationalString title = (rt.getTitle() == null) ? null : new SimpleInternationalString(rt.getTitle());
final InternationalString abs = (rt.getAbstract() == null) ? null : new SimpleInternationalString(rt.getAbstract());
rule.setDescription(styleFactory.description(title, abs));
rule.setElseFilter(rt.getElseFilter() != null);
rule.setFilter(visitFilter(rt.getFilter()));
rule.setLegendGraphic(visitLegend(rt.getLegendGraphic()));
rule.setMaxScaleDenominator((rt.getMaxScaleDenominator() == null) ? Double.MAX_VALUE : rt.getMaxScaleDenominator());
rule.setMinScaleDenominator((rt.getMinScaleDenominator() == null) ? 0 : rt.getMinScaleDenominator());
if (rt.getSymbolizer() == null || rt.getSymbolizer().isEmpty()) {
} else {
for (final JAXBElement<? extends org.geotoolkit.sld.xml.v100.SymbolizerType> jax : rt.getSymbolizer()) {
final org.geotoolkit.sld.xml.v100.SymbolizerType st = jax.getValue();
if (st == null) {
continue;
}
if (st instanceof org.geotoolkit.sld.xml.v100.PointSymbolizer) {
final org.geotoolkit.sld.xml.v100.PointSymbolizer pst = (org.geotoolkit.sld.xml.v100.PointSymbolizer) st;
rule.symbolizers().add(visit(pst));
} else if (st instanceof org.geotoolkit.sld.xml.v100.LineSymbolizer) {
final org.geotoolkit.sld.xml.v100.LineSymbolizer pst = (org.geotoolkit.sld.xml.v100.LineSymbolizer) st;
rule.symbolizers().add(visit(pst));
} else if (st instanceof org.geotoolkit.sld.xml.v100.PolygonSymbolizer) {
final org.geotoolkit.sld.xml.v100.PolygonSymbolizer pst = (org.geotoolkit.sld.xml.v100.PolygonSymbolizer) st;
rule.symbolizers().add(visit(pst));
} else if (st instanceof org.geotoolkit.sld.xml.v100.TextSymbolizer) {
final org.geotoolkit.sld.xml.v100.TextSymbolizer pst = (org.geotoolkit.sld.xml.v100.TextSymbolizer) st;
rule.symbolizers().add(visit(pst));
} else if (st instanceof org.geotoolkit.sld.xml.v100.RasterSymbolizer) {
final org.geotoolkit.sld.xml.v100.RasterSymbolizer pst = (org.geotoolkit.sld.xml.v100.RasterSymbolizer) st;
rule.symbolizers().add(visit(pst));
}
}
}
return rule;
}
use of org.opengis.style.PolygonSymbolizer in project geotoolkit by Geomatys.
the class GTtoSE110Transformer method visit.
/**
* Transform a GT rule in jaxb rule or OnlineResource
*/
@Override
public Object visit(final Rule rule, final Object data) {
if (rule.getOnlineResource() != null) {
// we store only the online resource
return visit(rule.getOnlineResource(), null);
}
final RuleType rt = se_factory.createRuleType();
rt.setName(rule.getName());
rt.setDescription(visit(rule.getDescription(), null));
if (rule.isElseFilter()) {
rt.setElseFilter(se_factory.createElseFilterType());
} else if (rule.getFilter() != null) {
rt.setFilter(apply(rule.getFilter()));
}
if (rule.getLegend() != null) {
rt.setLegendGraphic(visit(rule.getLegend(), null));
}
rt.setMaxScaleDenominator(rule.getMaxScaleDenominator());
rt.setMinScaleDenominator(rule.getMinScaleDenominator());
for (final Symbolizer symbol : rule.symbolizers()) {
if (symbol instanceof LineSymbolizer) {
rt.getSymbolizer().add(visit((LineSymbolizer) symbol, null));
} else if (symbol instanceof PolygonSymbolizer) {
rt.getSymbolizer().add(visit((PolygonSymbolizer) symbol, null));
} else if (symbol instanceof PointSymbolizer) {
rt.getSymbolizer().add(visit((PointSymbolizer) symbol, null));
} else if (symbol instanceof RasterSymbolizer) {
rt.getSymbolizer().add(visit((RasterSymbolizer) symbol, null));
} else if (symbol instanceof TextSymbolizer) {
rt.getSymbolizer().add(visit((TextSymbolizer) symbol, null));
} else if (symbol instanceof ExtensionSymbolizer) {
((List) rt.getSymbolizer()).add(visit((ExtensionSymbolizer) symbol, null));
}
}
return rt;
}
Aggregations