Search in sources :

Example 16 with MapLayer

use of org.apache.sis.portrayal.MapLayer in project geotoolkit by Geomatys.

the class RasterSymbolizerTest method coverage_whose_grid_origin_is_lower_left_should_be_flipped.

/**
 * Source coverage will be a matrix <em>with origin lower-left</em>:
 * <table>
 *     <tr><td>4</td><td>3</td></tr>
 *     <tr><td>1</td><td>2</td></tr>
 * </table>
 * @throws PortrayalException
 */
@Test
public void coverage_whose_grid_origin_is_lower_left_should_be_flipped() throws PortrayalException {
    final BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_BYTE_GRAY);
    image.getRaster().setSample(0, 0, 0, 1);
    image.getRaster().setSample(1, 0, 0, 2);
    image.getRaster().setSample(1, 1, 0, 3);
    image.getRaster().setSample(0, 1, 0, 4);
    final GridGeometry geom = new GridGeometry(new GridExtent(2, 2), PixelInCell.CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 10, 10), CommonCRS.defaultGeographic());
    final GridCoverage baseData = new GridCoverage2D(geom, null, image);
    MapLayer layer = MapBuilder.createLayer(new InMemoryGridCoverageResource(baseData));
    final MapLayers ctx = MapBuilder.createContext();
    ctx.getComponents().add(layer);
    BufferedImage rendering = DefaultPortrayalService.portray(new CanvasDef(new Dimension(2, 2), geom.getEnvelope()), new SceneDef(ctx, new Hints(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR, RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)));
    // As display is oriented upper-left, output should be flipped on y axis. Also, the renderer will stretch values
    // along 256 colors, so we have to adapt comparison.
    final int[] pixels = rendering.getRaster().getPixels(0, 0, 2, 2, (int[]) null);
    final int[] expected = { 255, 255, 255, 255, 165, 165, 165, 255, 0, 0, 0, 255, 88, 88, 88, 255 };
    assertArrayEquals(expected, pixels);
    final ColorMap colorMap = SF.colorMap(SF.interpolateFunction(null, Arrays.asList(SF.interpolationPoint(1, FF.literal(Color.RED)), SF.interpolationPoint(2, FF.literal(Color.GREEN)), SF.interpolationPoint(3, FF.literal(Color.BLUE)), SF.interpolationPoint(4, FF.literal(Color.WHITE))), null, null, FF.literal(Color.BLACK)));
    final RasterSymbolizer symbol = SF.rasterSymbolizer(null, null, null, null, colorMap, null, null, null);
    ctx.getComponents().set(0, MapBuilder.createCoverageLayer(baseData, SF.style(symbol), "test"));
    rendering = DefaultPortrayalService.portray(new CanvasDef(new Dimension(2, 2), geom.getEnvelope()), new SceneDef(ctx, new Hints(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR, RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)));
    assertEquals(Color.WHITE.getRGB(), rendering.getRGB(0, 0));
    assertEquals(Color.BLUE.getRGB(), rendering.getRGB(1, 0));
    assertEquals(Color.RED.getRGB(), rendering.getRGB(0, 1));
    assertEquals(Color.GREEN.getRGB(), rendering.getRGB(1, 1));
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) Hints(org.geotoolkit.factory.Hints) RenderingHints(java.awt.RenderingHints) GO2Hints(org.geotoolkit.display2d.GO2Hints) ColorMap(org.opengis.style.ColorMap) MapLayer(org.apache.sis.portrayal.MapLayer) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) BufferedImage(java.awt.image.BufferedImage) RasterSymbolizer(org.opengis.style.RasterSymbolizer) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) SceneDef(org.geotoolkit.display2d.service.SceneDef) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 17 with MapLayer

use of org.apache.sis.portrayal.MapLayer 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);
}
Also used : FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Fill(org.opengis.style.Fill) Description(org.opengis.style.Description) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) MapLayer(org.apache.sis.portrayal.MapLayer) Unit(javax.measure.Unit) Feature(org.opengis.feature.Feature) Font(org.opengis.style.Font) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) SceneDef(org.geotoolkit.display2d.service.SceneDef) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) Raster(java.awt.image.Raster) Point(org.locationtech.jts.geom.Point) Dimension(java.awt.Dimension) Point(org.locationtech.jts.geom.Point) LabelPlacement(org.opengis.style.LabelPlacement) Coordinate(org.locationtech.jts.geom.Coordinate) Expression(org.opengis.filter.Expression) TextSymbolizer(org.opengis.style.TextSymbolizer) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) Halo(org.opengis.style.Halo) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 18 with MapLayer

use of org.apache.sis.portrayal.MapLayer in project geotoolkit by Geomatys.

the class GraduationTest method renderGraduationTest.

/**
 * Sanity test, only ensure the rendering is successfull without errors not the final result.
 */
@Test
public void renderGraduationTest() throws PortrayalException, FactoryException {
    final CoordinateReferenceSystem crs = CRS.forCode("EPSG:2154");
    final GraduationSymbolizer gs = new GraduationSymbolizer();
    final GraduationSymbolizer.Graduation gra = new GraduationSymbolizer.Graduation();
    gs.getGraduations().add(gra);
    final MutableStyle style = GO2Utilities.STYLE_FACTORY.style(gs);
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(String.class).setName("id").addRole(AttributeRole.IDENTIFIER_COMPONENT);
    ftb.addAttribute(LineString.class).setName("geom").setCRS(crs);
    final FeatureType type = ftb.build();
    final LineString geom = org.geotoolkit.geometry.jts.JTS.getFactory().createLineString(new Coordinate[] { new Coordinate(0, 0), new Coordinate(100, 0) });
    geom.setUserData(crs);
    final Feature f = type.newInstance();
    f.setPropertyValue("id", "id-0");
    f.setPropertyValue("geom", geom);
    final MapLayer layer = MapBuilder.createLayer(FeatureStoreUtilities.collection(f));
    layer.setStyle(style);
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(layer);
    final SceneDef sdef = new SceneDef(context);
    final CanvasDef cdef = new CanvasDef();
    cdef.setDimension(new Dimension(100, 100));
    cdef.setBackground(Color.darkGray);
    cdef.setEnvelope(CRS.getDomainOfValidity(crs));
    final BufferedImage img = DefaultPortrayalService.portray(cdef, sdef);
    Assert.assertNotNull(img);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) MapLayer(org.apache.sis.portrayal.MapLayer) Dimension(java.awt.Dimension) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) SceneDef(org.geotoolkit.display2d.service.SceneDef) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 19 with MapLayer

use of org.apache.sis.portrayal.MapLayer in project geotoolkit by Geomatys.

the class LegendSizeTest method testPolygonStyle.

@Test
public void testPolygonStyle() {
    final MapLayer layer = MapBuilder.createEmptyMapLayer();
    layer.setStyle(SF.style(StyleConstants.DEFAULT_POLYGON_SYMBOLIZER));
    final MapLayers ctx = MapBuilder.createContext();
    ctx.getComponents().add(layer);
    Dimension dim = DefaultLegendService.legendPreferredSize(null, ctx);
    assertEquals(30, dim.width);
    assertEquals(24, dim.height);
    // test with an empty template
    dim = DefaultLegendService.legendPreferredSize(NO_MARGIN_TEMPLATE, ctx);
    assertEquals(30, dim.width);
    assertEquals(24, dim.height);
}
Also used : MapLayer(org.apache.sis.portrayal.MapLayer) Dimension(java.awt.Dimension) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 20 with MapLayer

use of org.apache.sis.portrayal.MapLayer in project geotoolkit by Geomatys.

the class LegendSizeTest method testRecursiveStyle.

@Test
public void testRecursiveStyle() {
    // Polygon layer, glyph size : w30, h24
    final MapLayer leaf1 = MapBuilder.createEmptyMapLayer();
    leaf1.setStyle(SF.style(StyleConstants.DEFAULT_POLYGON_SYMBOLIZER));
    final MapLayer leaf2 = MapBuilder.createEmptyMapLayer();
    leaf2.setStyle(SF.style(StyleConstants.DEFAULT_RASTER_SYMBOLIZER));
    final MapLayers node1 = MapBuilder.createItem();
    node1.setIdentifier("bouh");
    node1.getComponents().add(leaf1);
    node1.getComponents().add(leaf2);
    final MapLayers node2 = MapBuilder.createItem();
    node2.getComponents().add(leaf1);
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(node1);
    context.getComponents().add(node2);
    Dimension dim = DefaultLegendService.legendPreferredSize(NO_MARGIN_TEMPLATE, context);
    // We've got 3 glyph vertically aligned, no margin, no title and no insets between them.
    final int glyphHeight = 24;
    final int glyphWidth = 30;
    assertEquals(glyphWidth, dim.width);
    assertEquals(glyphHeight * 3, dim.height);
    // A new template to test legend estimation using titles and left insets to reflect tree structure.
    final int leftInset = 10;
    final DefaultBackgroundTemplate backTemplate = new DefaultBackgroundTemplate(new BasicStroke(0), Color.BLACK, Color.RED, new Insets(0, leftInset, 0, 0), 0);
    final LegendTemplate titleTemplate = new DefaultLegendTemplate(backTemplate, NO_MARGIN_TEMPLATE.getGapSize(), NO_MARGIN_TEMPLATE.getGlyphSize(), NO_MARGIN_TEMPLATE.getRuleFont(), true, NO_MARGIN_TEMPLATE.getLayerFont(), true);
    // Same test than above, but here we've got titles and left insets.
    dim = DefaultLegendService.legendPreferredSize(titleTemplate, context);
    // Get text pixel size.
    final BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2d = img.createGraphics();
    final FontMetrics font = g2d.getFontMetrics(titleTemplate.getLayerFont());
    final int fontHeight = font.getHeight();
    final int fontWidth = font.stringWidth(node1.getIdentifier());
    final int gap = (int) titleTemplate.getGapSize();
    final int legendHeight = // node1
    // title
    fontHeight + gap + glyphHeight + // leaf1
    gap + glyphHeight + // leaf2
    gap + // node2
    glyphHeight;
    /* Inset multiply 3 times because :
         * <-Inset->MapContext
         *          <-Inset->node1
         *                   <-Inset->leaf1
         * ...
         */
    final int legendWidth = leftInset * 3 + Math.max(fontWidth, glyphWidth);
    assertEquals("Legend width", legendWidth, dim.width);
    assertEquals("Legend height", legendHeight, dim.height);
}
Also used : BasicStroke(java.awt.BasicStroke) DefaultBackgroundTemplate(org.geotoolkit.display2d.ext.DefaultBackgroundTemplate) Insets(java.awt.Insets) FontMetrics(java.awt.FontMetrics) MapLayer(org.apache.sis.portrayal.MapLayer) Dimension(java.awt.Dimension) BufferedImage(java.awt.image.BufferedImage) MapLayers(org.apache.sis.portrayal.MapLayers) Graphics2D(java.awt.Graphics2D) Test(org.junit.Test)

Aggregations

MapLayer (org.apache.sis.portrayal.MapLayer)75 MapLayers (org.apache.sis.portrayal.MapLayers)51 Dimension (java.awt.Dimension)31 Test (org.junit.Test)27 BufferedImage (java.awt.image.BufferedImage)26 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)22 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)21 MutableStyle (org.geotoolkit.style.MutableStyle)21 FeatureSet (org.apache.sis.storage.FeatureSet)20 Feature (org.opengis.feature.Feature)19 FeatureType (org.opengis.feature.FeatureType)17 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)15 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)14 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)14 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)14 Graphics2D (java.awt.Graphics2D)12 DataStoreException (org.apache.sis.storage.DataStoreException)12 GridCoverageBuilder (org.apache.sis.coverage.grid.GridCoverageBuilder)11 InMemoryFeatureSet (org.geotoolkit.storage.memory.InMemoryFeatureSet)11 ArrayList (java.util.ArrayList)10