Search in sources :

Example 26 with MapLayers

use of org.apache.sis.portrayal.MapLayers 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 27 with MapLayers

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

the class RasterSymbolizerTest method renderInterpolationCoverage.

/**
 * Render a coverage with nearest and lanczos interpolation.
 */
@Test
public void renderInterpolationCoverage() throws FactoryException, PortrayalException, IOException {
    final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
    final BufferedImage image = new BufferedImage(36, 18, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setColor(Color.RED);
    g.fillRect(0, 0, 18, 9);
    g.setColor(Color.GREEN);
    g.fillRect(18, 0, 18, 9);
    g.setColor(Color.BLUE);
    g.fillRect(0, 9, 18, 9);
    g.setColor(Color.YELLOW);
    g.fillRect(18, 9, 18, 9);
    g.dispose();
    final GridExtent extent = new GridExtent(36, 18);
    final AffineTransform2D gridToCrs = new AffineTransform2D(10, 0, 0, -10, -180, 90);
    final GridGeometry grid = new GridGeometry(extent, PixelInCell.CELL_CORNER, gridToCrs, crs);
    final SampleDimension red = new SampleDimension.Builder().setName("1").build();
    final SampleDimension green = new SampleDimension.Builder().setName("2").build();
    final SampleDimension blue = new SampleDimension.Builder().setName("3").build();
    final GridCoverage2D coverage = new GridCoverage2D(grid, Arrays.asList(red, green, blue), image);
    final GridExtent queryextent = new GridExtent(360, 180);
    final GeneralEnvelope queryenv = new GeneralEnvelope(crs);
    queryenv.setRange(0, -180, 180);
    queryenv.setRange(1, -90, 90);
    final GridGeometry querygrid = new GridGeometry(queryextent, queryenv, GridOrientation.HOMOTHETY);
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(MapBuilder.createCoverageLayer(new InMemoryGridCoverageResource(coverage)));
    final BufferedImage nearest;
    final BufferedImage bicubic;
    final BufferedImage lanczos;
    {
        final Hints hints = new Hints();
        hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR);
        final CanvasDef cdef = new CanvasDef(querygrid);
        final SceneDef sdef = new SceneDef(context, hints);
        nearest = DefaultPortrayalService.portray(cdef, sdef);
    }
    {
        final Hints hints = new Hints();
        hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.BICUBIC2);
        final CanvasDef cdef = new CanvasDef(querygrid);
        final SceneDef sdef = new SceneDef(context, hints);
        bicubic = DefaultPortrayalService.portray(cdef, sdef);
    }
    {
        final Hints hints = new Hints();
        hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.LANCZOS);
        final CanvasDef cdef = new CanvasDef(querygrid);
        final SceneDef sdef = new SceneDef(context, hints);
        lanczos = DefaultPortrayalService.portray(cdef, sdef);
    }
    int nearestRgb = nearest.getRGB(179, 0);
    int bicubicRgb = bicubic.getRGB(179, 0);
    int naczosRgb = lanczos.getRGB(179, 0);
    assertTrue(nearestRgb != bicubicRgb);
    assertTrue(bicubicRgb != naczosRgb);
}
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) MapBuilder(org.geotoolkit.map.MapBuilder) SampleDimension(org.apache.sis.coverage.SampleDimension) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) SceneDef(org.geotoolkit.display2d.service.SceneDef) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) 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 28 with MapLayers

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

the class RasterSymbolizerTest method renderRGBIndexedCoverage.

/**
 * Render a coverage with :
 * - 3 sample dimensions R,G,B
 * - 1 byte indexed color model
 * - 1 raster, not tiled
 */
@Test
public void renderRGBIndexedCoverage() throws FactoryException, PortrayalException {
    final BufferedImage image = new BufferedImage(18, 9, BufferedImage.TYPE_BYTE_INDEXED);
    image.setRGB(0, 0, Color.RED.getRGB());
    image.setRGB(17, 0, Color.GREEN.getRGB());
    image.setRGB(0, 8, Color.BLUE.getRGB());
    final GridExtent extent = new GridExtent(18, 9);
    final AffineTransform2D gridToCrs = new AffineTransform2D(20, 0, 0, 20, -170, -80);
    final GridGeometry grid = new GridGeometry(extent, PixelInCell.CELL_CENTER, gridToCrs, CommonCRS.WGS84.normalizedGeographic());
    /*
         * We volontarely name samples 1 to avoid use of names as a hint.
         */
    final SampleDimension rgb = new SampleDimension.Builder().setName("1").build();
    final GridCoverage2D coverage = new GridCoverage2D(grid, Arrays.asList(rgb), image);
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(MapBuilder.createCoverageLayer(new InMemoryGridCoverageResource(coverage)));
    final CanvasDef cdef = new CanvasDef(grid);
    final SceneDef sdef = new SceneDef(context);
    BufferedImage result = DefaultPortrayalService.portray(cdef, sdef);
    assertEquals(Color.RED.getRGB(), result.getRGB(0, 0));
    assertEquals(Color.GREEN.getRGB(), result.getRGB(17, 0));
    assertEquals(Color.BLUE.getRGB(), result.getRGB(0, 8));
    // Now, test with a resample (flip axes):
    final GridExtent latLonExtent = new GridExtent(9, 18);
    AffineTransform2D latLonG2C = new AffineTransform2D(20, 0, 0, 20, -80, -170);
    GridGeometry latLonGrid = new GridGeometry(latLonExtent, PixelInCell.CELL_CENTER, latLonG2C, CommonCRS.WGS84.geographic());
    result = DefaultPortrayalService.portray(new CanvasDef(latLonGrid), sdef);
    assertEquals(Color.RED.getRGB(), result.getRGB(0, 0));
    assertEquals(Color.GREEN.getRGB(), result.getRGB(0, 17));
    assertEquals(Color.BLUE.getRGB(), result.getRGB(8, 0));
    latLonG2C = new AffineTransform2D(-20, 0, 0, 20, 80, -170);
    latLonGrid = new GridGeometry(latLonExtent, PixelInCell.CELL_CENTER, latLonG2C, CommonCRS.WGS84.geographic());
    result = DefaultPortrayalService.portray(new CanvasDef(latLonGrid), sdef);
    assertEquals(Color.RED.getRGB(), result.getRGB(8, 0));
    assertEquals(Color.GREEN.getRGB(), result.getRGB(8, 17));
    assertEquals(Color.BLUE.getRGB(), result.getRGB(0, 0));
}
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) MapBuilder(org.geotoolkit.map.MapBuilder) SceneDef(org.geotoolkit.display2d.service.SceneDef) SampleDimension(org.apache.sis.coverage.SampleDimension) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) BufferedImage(java.awt.image.BufferedImage) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 29 with MapLayers

use of org.apache.sis.portrayal.MapLayers 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 30 with MapLayers

use of org.apache.sis.portrayal.MapLayers 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)

Aggregations

MapLayers (org.apache.sis.portrayal.MapLayers)104 Dimension (java.awt.Dimension)61 BufferedImage (java.awt.image.BufferedImage)57 Test (org.junit.Test)57 MapLayer (org.apache.sis.portrayal.MapLayer)51 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)47 Rectangle (java.awt.Rectangle)26 CanvasDef (org.geotoolkit.display2d.service.CanvasDef)26 SceneDef (org.geotoolkit.display2d.service.SceneDef)26 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)22 Coordinate (org.locationtech.jts.geom.Coordinate)17 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)16 MutableStyle (org.geotoolkit.style.MutableStyle)16 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)16 Graphics2D (java.awt.Graphics2D)15 SampleDimension (org.apache.sis.coverage.SampleDimension)15 Feature (org.opengis.feature.Feature)14 FeatureType (org.opengis.feature.FeatureType)14 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)13 FeatureSet (org.apache.sis.storage.FeatureSet)13