use of org.opengis.style.Graphic in project geotoolkit by Geomatys.
the class ProjectedGeometryTest method createProjectedGeometry.
private static ProjectedGeometry createProjectedGeometry(Geometry geometry, Dimension canvasBounds, AffineTransform objToDisp) throws NoninvertibleTransformException, TransformException, FactoryException {
final int canvasWidth = canvasBounds.width;
final int canvasHeight = canvasBounds.height;
// build a maplayer
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic());
final FeatureType type = ftb.build();
final Feature feature = type.newInstance();
JTS.setCRS(geometry, CommonCRS.WGS84.normalizedGeographic());
feature.setPropertyValue("geom", geometry);
final FeatureSet col = new InMemoryFeatureSet(type, Arrays.asList(feature));
final List<GraphicalSymbol> symbols = new ArrayList<>();
symbols.add(SF.mark(StyleConstants.MARK_SQUARE, SF.fill(Color.BLACK), SF.stroke(Color.BLACK, 0)));
final Graphic graphic = SF.graphic(symbols, StyleConstants.LITERAL_ONE_FLOAT, FF.literal(2), StyleConstants.LITERAL_ZERO_FLOAT, null, null);
final PointSymbolizer ps = SF.pointSymbolizer(graphic, null);
final MutableStyle style = SF.style(ps);
final MapLayer layer = MapBuilder.createLayer(col);
layer.setStyle(style);
// build a rendering canvas
final J2DCanvasBuffered canvas = new J2DCanvasBuffered(CommonCRS.WGS84.normalizedGeographic(), new Dimension(canvasWidth, canvasHeight));
canvas.applyTransform(objToDisp);
final RenderingContext2D context = canvas.prepareContext(new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB).createGraphics());
final ProjectedGeometry pg = new ProjectedGeometry(context);
pg.setDataGeometry(geometry, CommonCRS.WGS84.normalizedGeographic());
Envelope env = canvas.getVisibleEnvelope();
System.out.println(env.getMinimum(0) + " " + env.getMaximum(0));
System.out.println(env.getMinimum(1) + " " + env.getMaximum(1));
return pg;
}
use of org.opengis.style.Graphic in project geotoolkit by Geomatys.
the class CachedPointSymbolizerTest method testMargin.
@Test
public void testMargin() throws FactoryException {
final GridGeometry grid = new GridGeometry(new GridExtent(1, 1), CRS.getDomainOfValidity(CommonCRS.WGS84.normalizedGeographic()), GridOrientation.HOMOTHETY);
final RenderingContext2D ctx = new RenderingContext2D(grid, null);
{
// NO ANCHOR, NO DISPLACEMENT
final List<GraphicalSymbol> symbols = new ArrayList<>();
final Stroke stroke = SF.stroke(Color.BLACK, 2);
final Fill fill = SF.fill(Color.RED);
final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
symbols.add(mark);
final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, DEFAULT_DISPLACEMENT);
final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
final float margin = cached.getMargin(null, ctx);
// 12/2 + 2*2(stroke width)
assertEquals(8f, margin, DELTA);
}
{
// NO ANCHOR
final List<GraphicalSymbol> symbols = new ArrayList<>();
final Stroke stroke = SF.stroke(Color.BLACK, 2);
final Fill fill = SF.fill(Color.RED);
final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
symbols.add(mark);
final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, SF.displacement(10, 15));
final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
final float margin = cached.getMargin(null, ctx);
// 12/2 + 2*2(stroke width) + 15(disp)
assertEquals(23f, margin, DELTA);
}
{
final List<GraphicalSymbol> symbols = new ArrayList<>();
final Stroke stroke = SF.stroke(Color.BLACK, 2);
final Fill fill = SF.fill(Color.RED);
final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
symbols.add(mark);
final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, SF.anchorPoint(0, 1.7), SF.displacement(10, 15));
final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
final float margin = cached.getMargin(null, ctx);
// 12/2 + 2*2(stroke width) + 15(disp) + 16*(1.7-0.5)
assertEquals(23f + 19.2f, margin, DELTA);
}
}
use of org.opengis.style.Graphic in project geotoolkit by Geomatys.
the class CategoryStyleBuilder method analyze.
public void analyze(final MapLayer layer) {
Resource resource = layer.getData();
if (!(resource instanceof FeatureSet)) {
throw new IllegalArgumentException("Layer resource must be a FeatureSet");
}
this.layer = layer;
fts.rules().clear();
properties.clear();
if (layer != null) {
FeatureType schema;
try {
schema = ((FeatureSet) resource).getType();
} catch (DataStoreException ex) {
throw new FeatureStoreRuntimeException(ex.getMessage(), ex);
}
for (PropertyType desc : schema.getProperties(true)) {
if (desc instanceof AttributeType) {
Class<?> type = ((AttributeType) desc).getValueClass();
if (!Geometry.class.isAssignableFrom(type)) {
properties.add(ff.property(desc.getName().tip().toString()));
}
}
}
// find the geometry class for template
Class<?> geoClass = null;
try {
PropertyType geo = FeatureExt.getDefaultGeometry(schema);
geoClass = Features.toAttribute(geo).map(AttributeType::getValueClass).orElse(null);
} catch (PropertyNotFoundException ex) {
LOGGER.log(Level.FINE, "No sis:geometry property found", ex);
}
if (geoClass == null) {
return;
}
if (Polygon.class.isAssignableFrom(geoClass) || MultiPolygon.class.isAssignableFrom(geoClass)) {
Stroke stroke = sf.stroke(Color.BLACK, 1);
Fill fill = sf.fill(Color.BLUE);
template = sf.polygonSymbolizer(stroke, fill, null);
expectedType = PolygonSymbolizer.class;
} else if (LineString.class.isAssignableFrom(geoClass) || MultiLineString.class.isAssignableFrom(geoClass)) {
Stroke stroke = sf.stroke(Color.BLUE, 2);
template = sf.lineSymbolizer(stroke, null);
expectedType = LineSymbolizer.class;
} else {
Stroke stroke = sf.stroke(Color.BLACK, 1);
Fill fill = sf.fill(Color.BLUE);
List<GraphicalSymbol> symbols = new ArrayList<>();
symbols.add(sf.mark(StyleConstants.MARK_CIRCLE, fill, stroke));
Graphic gra = sf.graphic(symbols, ff.literal(1), ff.literal(12), ff.literal(0), sf.anchorPoint(), sf.displacement());
template = sf.pointSymbolizer(gra, null);
expectedType = PointSymbolizer.class;
}
// try to rebuild the previous analyze if it was one
List<? extends FeatureTypeStyle> ftss = layer.getStyle().featureTypeStyles();
if (ftss.size() == 1) {
FeatureTypeStyle fts = ftss.get(0);
// defensive copy avoid synchronization
List<? extends Rule> candidateRules = new ArrayList<>(fts.rules());
for (Rule r : candidateRules) {
// defensive copy avoid synchronization
List<? extends Symbolizer> candidateSymbols = new ArrayList<>(r.symbolizers());
if (candidateSymbols.size() != 1)
break;
Symbolizer symbol = candidateSymbols.get(0);
if (expectedType.isInstance(symbol)) {
if (r.isElseFilter()) {
// it looks like it's a valid classification "other" rule
this.fts.rules().add((MutableRule) r);
template = symbol;
other = true;
} else {
Filter f = r.getFilter();
if (f != null && f.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_EQUAL_TO) {
BinaryComparisonOperator equal = (BinaryComparisonOperator) f;
Expression exp1 = equal.getOperand1();
Expression exp2 = equal.getOperand2();
if (exp1 instanceof ValueReference && exp2 instanceof Literal) {
if (properties.contains(exp1)) {
// it looks like it's a valid classification property rule
this.fts.rules().add((MutableRule) r);
template = symbol;
currentProperty = (ValueReference) exp1;
} else {
// property is not in the schema
break;
}
} else if (exp2 instanceof ValueReference && exp1 instanceof Literal) {
if (properties.contains(exp2)) {
// it looks like it's a valid classification property rule
this.fts.rules().add((MutableRule) r);
template = symbol;
currentProperty = (ValueReference) exp2;
} else {
// property is not in the schema
break;
}
} else {
// mismatch analyze structure
break;
}
}
}
} else {
break;
}
}
}
}
}
use of org.opengis.style.Graphic in project geotoolkit by Geomatys.
the class IntervalStyleBuilder method derivateSymbolizer.
/**
* Derivate a symbolizer with a new color.
*/
private 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.Graphic in project geotoolkit by Geomatys.
the class IntervalStyleBuilder method createPointTemplate.
public static PointSymbolizer createPointTemplate() {
final MutableStyleFactory sf = GO2Utilities.STYLE_FACTORY;
final FilterFactory ff = GO2Utilities.FILTER_FACTORY;
final Stroke stroke = sf.stroke(Color.BLACK, 1);
final Fill fill = sf.fill(Color.BLUE);
final List<GraphicalSymbol> symbols = new ArrayList<GraphicalSymbol>();
symbols.add(sf.mark(StyleConstants.MARK_CIRCLE, fill, stroke));
final Graphic gra = sf.graphic(symbols, ff.literal(1), ff.literal(12), ff.literal(0), sf.anchorPoint(), sf.displacement());
return sf.pointSymbolizer(gra, null);
}
Aggregations