use of org.geotoolkit.storage.memory.InMemoryFeatureSet 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.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.
the class MeridianTest method createFeatureLayer.
private static <T extends Geometry> MapLayers createFeatureLayer(T geometry, Class<T> geomClass) {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(geomClass).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
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 PolygonSymbolizer symbol = SF.polygonSymbolizer(SF.stroke(Color.BLACK, 0), SF.fill(Color.RED), null);
final MutableStyle style = SF.style(symbol);
final MapLayer layer = MapBuilder.createLayer(col);
layer.setStyle(style);
final MapLayers context = MapBuilder.createContext();
context.getComponents().add(layer);
return context;
}
use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.
the class PortrayalServiceTest method testPropertyPreservation.
@Test
public void testPropertyPreservation() throws Exception {
final Point location = GO2Utilities.JTS_FACTORY.createPoint(new Coordinate(2, 3));
final FeatureTypeBuilder builder = new FeatureTypeBuilder().setName("Donaldville");
builder.addAttribute(Point.class).setName("geometry");
builder.addAttribute(String.class).setName("firstName");
builder.addAttribute(String.class).setName("lastName");
final FeatureType duckType = builder.build();
final Feature duck = duckType.newInstance();
duck.setPropertyValue("firstName", "Donald");
duck.setPropertyValue("lastName", "Duck");
duck.setPropertyValue("geometry", location);
final MapLayer layer = MapBuilder.createLayer(new InMemoryFeatureSet(duckType, Collections.singleton(duck)));
final CanvasDef canvas = new CanvasDef(new Dimension(16, 16), new Envelope2D(CommonCRS.defaultGeographic(), 0, 0, 16, 16));
final MapLayers ctx = MapBuilder.createContext();
ctx.getComponents().add(layer);
final SceneDef scene = new SceneDef(ctx);
scene.getHints().put(GO2Hints.KEY_PRESERVE_PROPERTIES, true);
final List<Presentation> result = DefaultPortrayalService.present(canvas, scene).collect(Collectors.toList());
assertEquals(1, result.size());
final Presentation presentation = result.get(0);
final Feature picked = (Feature) presentation.getCandidate();
assertNotNull(picked);
Map<String, Object> expectedProperties = new HashMap<>();
expectedProperties.put("firstName", "Donald");
expectedProperties.put("lastName", "Duck");
expectedProperties.put("geometry", location);
final Map<String, Object> properties = picked.getType().getProperties(true).stream().filter(p -> p instanceof AttributeType).map(attr -> attr.getName().tip().toString()).collect(Collectors.toMap(Function.identity(), name -> picked.getPropertyValue(name)));
assertEquals(String.format("Returned values do not match.%nExpected: %s%nActual: %s%n", expectedProperties.entrySet().stream().map(entry -> String.format("%s -> %s", entry.getKey(), entry.getValue())).collect(Collectors.joining(", ")), properties.entrySet().stream().map(entry -> String.format("%s -> %s", entry.getKey(), entry.getValue())).collect(Collectors.joining(", "))), expectedProperties, properties);
// By default, don't load un-necessary properties
scene.getHints().remove(GO2Hints.KEY_PRESERVE_PROPERTIES);
final List<Object> geometries = DefaultPortrayalService.present(canvas, scene).map(p -> (Feature) p.getCandidate()).peek(Assert::assertNotNull).peek(f -> assertEquals(1, f.getType().getProperties(true).stream().filter(p -> p instanceof AttributeType).count())).map(f -> f.getPropertyValue("geometry")).collect(Collectors.toList());
assertEquals(1, geometries.size());
assertEquals(geometries.get(0), location);
}
use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.
the class PortrayalServiceTest method testCoveragePropertyRendering.
/**
* Test rendering of a coverage inside a feature property.
*/
@Test
public void testCoveragePropertyRendering() throws Exception {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(GridCoverage.class).setName("coverage");
final FeatureType ft = ftb.build();
final BufferedImage img = new BufferedImage(90, 90, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g = img.createGraphics();
g.setColor(Color.GREEN);
g.fillRect(0, 0, 90, 90);
g.dispose();
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setDomain(new GridGeometry(null, PixelInCell.CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0.5, 0.5), CommonCRS.WGS84.normalizedGeographic()));
gcb.setValues(img);
final Feature f = ft.newInstance();
f.setPropertyValue("coverage", gcb.build());
final FeatureSet collection = new InMemoryFeatureSet(ft, Arrays.asList(f));
final String name = "mySymbol";
final Description desc = DEFAULT_DESCRIPTION;
final String geometry = "coverage";
final Unit unit = Units.POINT;
final Expression opacity = LITERAL_ONE_FLOAT;
final ChannelSelection channels = null;
final OverlapBehavior overlap = null;
final ColorMap colormap = null;
final ContrastEnhancement enhance = null;
final ShadedRelief relief = null;
final Symbolizer outline = null;
final RasterSymbolizer symbol = SF.rasterSymbolizer(name, geometry, desc, unit, opacity, channels, overlap, colormap, enhance, relief, outline);
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 SceneDef sdef = new SceneDef(context);
final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
env.setRange(0, -180, +180);
env.setRange(1, -90, +90);
final CanvasDef cdef = new CanvasDef(new Dimension(360, 180), env);
final BufferedImage result = DefaultPortrayalService.portray(cdef, sdef);
final Raster raster = result.getData();
final int[] pixel = new int[4];
final int[] trans = new int[] { 0, 0, 0, 0 };
final int[] green = new int[] { 0, 255, 0, 255 };
assertNotNull(result);
raster.getPixel(0, 0, pixel);
assertArrayEquals(trans, pixel);
raster.getPixel(179, 45, pixel);
assertArrayEquals(trans, pixel);
raster.getPixel(181, 45, pixel);
assertArrayEquals(green, pixel);
}
use of org.geotoolkit.storage.memory.InMemoryFeatureSet 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);
}
Aggregations