Search in sources :

Example 21 with MapLayers

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

the class ColorModelTest method testAlphaColorBackground.

@Test
public void testAlphaColorBackground() throws NoSuchAuthorityCodeException, FactoryException, PortrayalException {
    final MapLayers context = MapBuilder.createContext();
    final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.geographic());
    env.setRange(0, -180, 180);
    env.setRange(1, -90, 90);
    final CanvasDef cdef = new CanvasDef(new Dimension(800, 600), env);
    cdef.setBackground(new Color(0.5f, 0.1f, 0.7f, 0.6f));
    final BufferedImage img = DefaultPortrayalService.portray(cdef, new SceneDef(context));
    // background is not opaque we should obtain an RGBA color model
    assertTrue(!(img.getColorModel() instanceof IndexColorModel));
    assertEquals(ColorSpace.TYPE_RGB, img.getColorModel().getColorSpace().getType());
    assertEquals(4, img.getColorModel().getNumComponents());
    assertEquals(3, img.getColorModel().getNumColorComponents());
}
Also used : Color(java.awt.Color) SceneDef(org.geotoolkit.display2d.service.SceneDef) Dimension(java.awt.Dimension) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) BufferedImage(java.awt.image.BufferedImage) MapLayers(org.apache.sis.portrayal.MapLayers) IndexColorModel(java.awt.image.IndexColorModel) Test(org.junit.Test)

Example 22 with MapLayers

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

the class ColorModelTest method testReprojectionCoverageRGB.

@Test
public void testReprojectionCoverageRGB() throws TransformException, PortrayalException, NoSuchAuthorityCodeException, FactoryException {
    // create a test coverage
    final BufferedImage img = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g2d = img.createGraphics();
    g2d.setColor(Color.GREEN);
    g2d.fillRect(0, 0, 500, 500);
    final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    env.setRange(0, 0, 20);
    env.setRange(1, 0, 20);
    final GridCoverage coverage = new GridCoverage2D(new GridGeometry(null, env, GridOrientation.HOMOTHETY), null, img);
    // display it
    final MapLayers context = MapBuilder.createContext();
    final MapLayer cl = MapBuilder.createCoverageLayer(coverage, SF.style(StyleConstants.DEFAULT_RASTER_SYMBOLIZER), "coverage");
    context.getComponents().add(cl);
    final Envelope envelope = Envelopes.transform(env, CRS.forCode("EPSG:3031"));
    final CanvasDef cdef = new CanvasDef(new Dimension(800, 600), envelope);
    cdef.setBackground(Color.WHITE);
    final BufferedImage result = DefaultPortrayalService.portray(cdef, new SceneDef(context));
    // background is opaque we should obtain an RGB color model since raster styles
    // are unpredictable
    assertTrue(!(result.getColorModel() instanceof IndexColorModel));
    assertEquals(ColorSpace.TYPE_RGB, result.getColorModel().getColorSpace().getType());
    assertEquals(3, result.getColorModel().getNumComponents());
    assertEquals(3, result.getColorModel().getNumColorComponents());
    // check we don't have any black reprojection pixels
    int[] buffer = new int[4];
    final Raster raster = result.getData();
    for (int x = 0; x < raster.getWidth(); x++) {
        for (int y = 0; y < raster.getHeight(); y++) {
            raster.getPixel(x, y, buffer);
            if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0) {
                // black pixel
                fail("reprojection should not have generated black pixels.");
            }
        }
    }
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) MapLayer(org.apache.sis.portrayal.MapLayer) Raster(java.awt.image.Raster) Dimension(java.awt.Dimension) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) BufferedImage(java.awt.image.BufferedImage) Point(org.locationtech.jts.geom.Point) Graphics2D(java.awt.Graphics2D) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) SceneDef(org.geotoolkit.display2d.service.SceneDef) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) MapLayers(org.apache.sis.portrayal.MapLayers) IndexColorModel(java.awt.image.IndexColorModel) Test(org.junit.Test)

Example 23 with MapLayers

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

the class CoverageImageTest method testImageLayer.

/**
 * Compute and compare result image from {@link MapContext} build with {@link CoverageMapLayer},
 * and sourceImage.
 *
 * @param sourceImage expected image will be tested.
 * @param cml {@link CoverageMapLayer} use to build {@link MapContext}.
 * @throws PortrayalException
 */
private void testImageLayer(RenderedImage sourceImage, MapLayer cml) throws PortrayalException {
    // create a mapcontext
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(cml);
    outputImgDim.setSize(proportionalityCoefficient * srcWidth, proportionalityCoefficient * srcHeight);
    hints = new Hints(GO2Hints.KEY_COLOR_MODEL, sourceImage.getColorModel(), RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    cdef.setDimension(outputImgDim);
    sdef.setContext(context);
    sdef.setHints(hints);
    cdef.setEnvelope(resEnv);
    final BufferedImage imgResult = DefaultPortrayalService.portray(cdef, sdef);
    checkImage(sourceImage, imgResult, proportionalityCoefficient);
}
Also used : Hints(org.geotoolkit.factory.Hints) RenderingHints(java.awt.RenderingHints) BufferedImage(java.awt.image.BufferedImage) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 24 with MapLayers

use of org.apache.sis.portrayal.MapLayers 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));
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Fill(org.opengis.style.Fill) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) MapLayer(org.apache.sis.portrayal.MapLayer) Point(org.locationtech.jts.geom.Point) Dimension(java.awt.Dimension) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) Point(org.locationtech.jts.geom.Point) MutableStyle(org.geotoolkit.style.MutableStyle) Coordinate(org.locationtech.jts.geom.Coordinate) Expression(org.opengis.filter.Expression) SceneDef(org.geotoolkit.display2d.service.SceneDef) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 25 with MapLayers

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

the class RasterSymbolizerTest method UTM32632Test.

/**
 * Check proper image reprojection in UTM
 */
@Ignore
@Test
public void UTM32632Test() throws Exception {
    final BufferedImage img = new BufferedImage(120, 90, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2d = img.createGraphics();
    g2d.setColor(Color.GREEN);
    g2d.fillRect(0, 0, 120, 90);
    // set it's envelope
    final GeneralEnvelope gridEnv = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    gridEnv.setRange(0, 0, 120);
    gridEnv.setRange(1, 0, 90);
    // create the coverage
    final GridCoverage coverage = new GridCoverage2D(new GridGeometry(null, gridEnv, GridOrientation.HOMOTHETY), null, img);
    final MapLayers context = MapBuilder.createContext();
    final MapLayer cl = MapBuilder.createCoverageLayer(coverage, SF.style(StyleConstants.DEFAULT_RASTER_SYMBOLIZER), "coverage");
    context.getComponents().add(cl);
    final GeneralEnvelope env = new GeneralEnvelope(CRS.forCode("EPSG:32632"));
    env.setRange(0, -2574823.6832217844, 5487970.783439655);
    env.setRange(1, 4289777.45228916, 1.0491927042028729E7);
    final Hints hints = new Hints();
    final SceneDef scenedef = new SceneDef(context, hints);
    final CanvasDef canvasdef = new CanvasDef(new Dimension(800, 800), env);
    canvasdef.setBackground(Color.WHITE);
    final BufferedImage buffer = DefaultPortrayalService.portray(canvasdef, scenedef);
    ImageIO.write(buffer, "PNG", new File("test.png"));
    // We should obtain a green triangle crossing the image looking like this :
    // 
    // |\
    // |_\
    // we can't test the shape so we test we found more and more green pixels on each line
    // we expect to have a blue label at the center of the image
    final int[] pixel = new int[4];
    final int[] green = new int[] { 0, 255, 0, 255 };
    int nbGreen = 0;
    final Raster raster = buffer.getData();
    for (int y = 0; y < 800; y++) {
        int nb = 0;
        for (int x = 0; x < 800; x++) {
            raster.getPixel(x, y, pixel);
            if (Arrays.equals(green, pixel)) {
                nb++;
            }
        }
        assertTrue("expected at least one green pixel", nb > 0);
        assertTrue(nb >= nbGreen);
        nbGreen = nb;
    }
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) Hints(org.geotoolkit.factory.Hints) RenderingHints(java.awt.RenderingHints) GO2Hints(org.geotoolkit.display2d.GO2Hints) MapLayer(org.apache.sis.portrayal.MapLayer) Raster(java.awt.image.Raster) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) SceneDef(org.geotoolkit.display2d.service.SceneDef) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) File(java.io.File) MapLayers(org.apache.sis.portrayal.MapLayers) Ignore(org.junit.Ignore) 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