use of org.opengis.filter.Expression in project geotoolkit by Geomatys.
the class GeometryExpressionTest method bufferTest.
/**
* Test a buffer expression around geometry.
*/
@Test
public void bufferTest() throws PortrayalException, IOException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Point.class).setName("geom").setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = ftb.build();
final Point point = GO2Utilities.JTS_FACTORY.createPoint(new Coordinate(0, 0));
JTS.setCRS(point, crs);
final Feature feature = type.newInstance();
feature.setPropertyValue("geom", point);
final Expression geomExp = FF.function("buffer", FF.property("geom"), FF.literal(10));
final Fill fill = SF.fill(Color.RED);
final PolygonSymbolizer symbolizer = SF.polygonSymbolizer("", geomExp, DEFAULT_DESCRIPTION, DEFAULT_UOM, null, 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(crs);
env.setRange(0, -20, +20);
env.setRange(1, -20, +20);
final CanvasDef canvasDef = new CanvasDef();
canvasDef.setEnvelope(env);
canvasDef.setDimension(new Dimension(40, 40));
canvasDef.setBackground(Color.WHITE);
final SceneDef sceneDef = new SceneDef(context);
final BufferedImage image = DefaultPortrayalService.portray(canvasDef, sceneDef);
// we must obtain a red point of 10pixel 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.filter.Expression in project geotoolkit by Geomatys.
the class TextSymbolizerTest method pointLabelTest.
/**
* Render a label at check it is correctly located in the image.
*/
@Test
public void pointLabelTest() throws Exception {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Point.class).setName("geom").setCRS(CommonCRS.defaultGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = ftb.build();
final Feature feature = type.newInstance();
feature.setPropertyValue("geom", GF.createPoint(new Coordinate(0, 0)));
final FeatureSet collection = new InMemoryFeatureSet(type, Arrays.asList(feature));
// text symbolizer style
final String name = "mySymbol";
final Description desc = DEFAULT_DESCRIPTION;
// use the default geometry of the feature
final String geometry = null;
final Unit unit = Units.POINT;
final Expression label = FF.literal("LABEL");
final Font font = SF.font(FF.literal("Arial"), FONT_STYLE_ITALIC, FONT_WEIGHT_BOLD, FF.literal(14));
final LabelPlacement placement = SF.pointPlacement();
final Halo halo = SF.halo(Color.WHITE, 0);
final Fill fill = SF.fill(Color.BLUE);
final TextSymbolizer symbol = SF.textSymbolizer(name, geometry, desc, unit, label, font, placement, halo, fill);
final MutableStyle style = SF.style(symbol);
final MapLayer layer = MapBuilder.createLayer(collection);
layer.setStyle(style);
final MapLayers context = MapBuilder.createContext();
context.getComponents().add(layer);
final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.defaultGeographic());
env.setRange(0, -180, +180);
env.setRange(1, -90, +90);
final Hints hints = new Hints();
hints.put(GO2Hints.KEY_COLOR_MODEL, ColorModel.getRGBdefault());
final SceneDef scenedef = new SceneDef(context, hints);
final CanvasDef canvasdef = new CanvasDef(new Dimension(360, 180), env);
canvasdef.setBackground(Color.WHITE);
final BufferedImage buffer = DefaultPortrayalService.portray(canvasdef, scenedef);
// ImageIO.write(buffer, "PNG", new File("test.png"));
// we expect to have a blue label at the center of the image
final int[] pixel = new int[4];
final int[] blue = new int[] { 0, 0, 255, 255 };
final Raster raster = buffer.getData();
boolean found = false;
for (int x = 160; x < 200; x++) {
// should be exactly at the center
raster.getPixel(x, 90, pixel);
if (Arrays.equals(blue, pixel)) {
found = true;
}
}
assertTrue("label not found", found);
}
use of org.opengis.filter.Expression in project geotoolkit by Geomatys.
the class StyleCacheTest method GraphicCacheTest.
@Test
public void GraphicCacheTest() throws Exception {
// Test a complex graphic
final Expression Lookup = FF.property("POP_CNTRY");
final List<InterpolationPoint> values = new ArrayList<InterpolationPoint>();
// test color interpolation ---------------------------------------------
values.clear();
values.add(new DefaultInterpolationPoint(0d, FF.literal(3d)));
values.add(new DefaultInterpolationPoint(500000000d, FF.literal(50d)));
Interpolate interpolate = new DefaultInterpolate(Lookup, values, Method.COLOR, Mode.CUBIC, null);
List<GraphicalSymbol> symbols = new ArrayList<GraphicalSymbol>();
symbols.add(SF.mark(StyleConstants.MARK_CIRCLE, SF.fill(Color.RED), SF.stroke()));
Graphic graphic = SF.graphic(symbols, StyleConstants.DEFAULT_GRAPHIC_OPACITY, interpolate, StyleConstants.DEFAULT_GRAPHIC_ROTATION, StyleConstants.DEFAULT_ANCHOR_POINT, StyleConstants.DEFAULT_DISPLACEMENT);
CachedGraphic cached = CachedGraphic.cache(graphic);
assertFalse(cached.isStatic());
assertEquals(VisibilityState.DYNAMIC, cached.isStaticVisible());
}
use of org.opengis.filter.Expression in project geotoolkit by Geomatys.
the class PatternSymbolizerTest method testXml.
/**
* Test Jaxb xml support.
*/
@Test
public void testXml() throws JAXBException, IOException {
final MutableStyleFactory SF = GO2Utilities.STYLE_FACTORY;
final FilterFactory2 FF = GO2Utilities.FILTER_FACTORY;
final Map<Expression, List<Symbolizer>> ranges = new LinkedHashMap<>();
ranges.put(FF.literal(-1000), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.BLUE), null)));
ranges.put(FF.literal(-500), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.RED), null)));
ranges.put(FF.literal(-100), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.GREEN), null)));
ranges.put(FF.literal(100), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.YELLOW), null)));
ranges.put(FF.literal(1000), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.GRAY), null)));
final PatternSymbolizer ps = new PatternSymbolizer(FF.literal(0), ranges, ThreshholdsBelongTo.PRECEDING);
final MutableStyle style = GO2Utilities.STYLE_FACTORY.style(ps);
final Path path = Files.createTempFile("xml", ".xml");
IOUtilities.deleteOnExit(path);
new StyleXmlIO().writeStyle(path, style, Specification.StyledLayerDescriptor.V_1_1_0);
}
use of org.opengis.filter.Expression in project geotoolkit by Geomatys.
the class CachedStrokeSimple method getJ2DStroke.
/**
* Get the java2D Stroke for the given feature.
*
* @param candidate : evaluate stroke with the given feature
* @param coeff : use to adjust stroke size, if in display unit value equals 1
* @return Java2D Stroke
*/
public java.awt.Stroke getJ2DStroke(final Object candidate, float coeff) {
evaluate();
coeff = Math.abs(coeff);
java.awt.Stroke j2dStroke = cachedStroke;
// if stroke is null it means something is dynamic
if (j2dStroke == null || coeff != 1) {
float[] candidateDashes = cachedDashes;
float candidateOffset = cachedDashOffset;
int candidateCap = cachedCap;
int candidateJoin = cachedJoin;
float candidateWidth = cachedWidth;
if (Float.isNaN(candidateOffset)) {
final Expression expOffset = styleElement.getDashOffset();
candidateOffset = GO2Utilities.evaluate(expOffset, candidate, Float.class, 1f);
}
if (candidateCap == Integer.MAX_VALUE) {
final Expression expCap = styleElement.getLineCap();
final String cap = GO2Utilities.evaluate(expCap, null, String.class, STROKE_CAP_BUTT_STRING);
if (STROKE_CAP_BUTT_STRING.equalsIgnoreCase(cap))
candidateCap = BasicStroke.CAP_BUTT;
else if (STROKE_CAP_SQUARE_STRING.equalsIgnoreCase(cap))
candidateCap = BasicStroke.CAP_SQUARE;
else if (STROKE_CAP_ROUND_STRING.equalsIgnoreCase(cap))
candidateCap = BasicStroke.CAP_ROUND;
else
candidateCap = BasicStroke.CAP_BUTT;
}
if (candidateJoin == Integer.MAX_VALUE) {
final Expression expJoin = styleElement.getLineJoin();
final String join = GO2Utilities.evaluate(expJoin, null, String.class, STROKE_JOIN_BEVEL_STRING);
if (STROKE_JOIN_BEVEL_STRING.equalsIgnoreCase(join))
candidateJoin = BasicStroke.JOIN_BEVEL;
else if (STROKE_JOIN_MITRE_STRING.equalsIgnoreCase(join))
candidateJoin = BasicStroke.JOIN_MITER;
else if (STROKE_JOIN_ROUND_STRING.equalsIgnoreCase(join))
candidateJoin = BasicStroke.JOIN_ROUND;
else
candidateJoin = BasicStroke.JOIN_BEVEL;
}
if (Float.isNaN(candidateWidth)) {
final Expression expWidth = styleElement.getWidth();
candidateWidth = GO2Utilities.evaluate(expWidth, candidate, Float.class, 1f);
if (candidateWidth < 0) {
candidateWidth = 0f;
}
}
if (candidateDashes != null) {
float[] s = candidateDashes.clone();
for (int i = 0; i < s.length; i++) {
s[i] = s[i] * coeff;
}
j2dStroke = new BasicStroke(candidateWidth * coeff, candidateCap, candidateJoin, 1f, s, candidateOffset);
} else {
j2dStroke = new BasicStroke(candidateWidth * coeff, candidateCap, candidateJoin, 10f);
}
}
return j2dStroke;
}
Aggregations