Search in sources :

Example 11 with MapLayers

use of org.apache.sis.portrayal.MapLayers 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);
}
Also used : Color(java.awt.Color) Expression(org.opengis.filter.Expression) POSITIVE_INFINITY(java.lang.Double.POSITIVE_INFINITY) J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) Hints(org.geotoolkit.factory.Hints) Arrays(java.util.Arrays) Envelope2D(org.apache.sis.geometry.Envelope2D) Unit(javax.measure.Unit) GridOrientation(org.apache.sis.coverage.grid.GridOrientation) CRS(org.apache.sis.referencing.CRS) PixelInCell(org.opengis.referencing.datum.PixelInCell) PortrayalException(org.geotoolkit.display.PortrayalException) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) StopOnErrorMonitor(org.geotoolkit.display.canvas.control.StopOnErrorMonitor) Coordinate(org.locationtech.jts.geom.Coordinate) NEGATIVE_INFINITY(java.lang.Double.NEGATIVE_INFINITY) FilterFactory(org.opengis.filter.FilterFactory) Envelope(org.opengis.geometry.Envelope) GridCoverage2D(org.apache.sis.coverage.grid.GridCoverage2D) FeatureType(org.opengis.feature.FeatureType) DEFAULT_DISPLACEMENT(org.geotoolkit.style.StyleConstants.DEFAULT_DISPLACEMENT) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) StyleConstants(org.geotoolkit.style.StyleConstants) MapLayers(org.apache.sis.portrayal.MapLayers) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) DEFAULT_ANCHOR_POINT(org.geotoolkit.style.StyleConstants.DEFAULT_ANCHOR_POINT) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Feature(org.opengis.feature.Feature) MutableStyleFactory(org.geotoolkit.style.MutableStyleFactory) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) AxesConvention(org.apache.sis.referencing.cs.AxesConvention) AttributeType(org.opengis.feature.AttributeType) Point(org.locationtech.jts.geom.Point) org.opengis.style(org.opengis.style) AbstractCRS(org.apache.sis.referencing.crs.AbstractCRS) Collectors(java.util.stream.Collectors) SearchArea(org.geotoolkit.display.SearchArea) FilterUtilities(org.geotoolkit.filter.FilterUtilities) Dimension(java.awt.Dimension) List(java.util.List) Presentation(org.apache.sis.internal.map.Presentation) AttributeRole(org.apache.sis.feature.builder.AttributeRole) MapBuilder(org.geotoolkit.map.MapBuilder) LITERAL_ONE_FLOAT(org.geotoolkit.style.StyleConstants.LITERAL_ONE_FLOAT) DEFAULT_DESCRIPTION(org.geotoolkit.style.StyleConstants.DEFAULT_DESCRIPTION) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.util.FactoryException) Rectangle(java.awt.Rectangle) SampleDimension(org.apache.sis.coverage.SampleDimension) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) GO2Utilities(org.geotoolkit.display2d.GO2Utilities) HashMap(java.util.HashMap) RenderingContext(org.geotoolkit.display.canvas.RenderingContext) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Graphics2D(java.awt.Graphics2D) MARK_CIRCLE(org.geotoolkit.style.StyleConstants.MARK_CIRCLE) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Raster(java.awt.image.Raster) CommonCRS(org.apache.sis.referencing.CommonCRS) DataStoreException(org.apache.sis.storage.DataStoreException) DefaultStyleFactory(org.geotoolkit.style.DefaultStyleFactory) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) GO2Hints(org.geotoolkit.display2d.GO2Hints) MutableStyle(org.geotoolkit.style.MutableStyle) Units(org.apache.sis.measure.Units) MapLayer(org.apache.sis.portrayal.MapLayer) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) GraphicVisitor(org.geotoolkit.display2d.GraphicVisitor) Assert.assertNotNull(org.junit.Assert.assertNotNull) J2DCanvas(org.geotoolkit.display2d.canvas.J2DCanvas) JTS(org.geotoolkit.geometry.jts.JTS) Test(org.junit.Test) Assert.assertNull(org.junit.Assert.assertNull) ColorModel(java.awt.image.ColorModel) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) FeatureSet(org.apache.sis.storage.FeatureSet) WritableRaster(java.awt.image.WritableRaster) GridExtent(org.apache.sis.coverage.grid.GridExtent) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) HashMap(java.util.HashMap) MapLayer(org.apache.sis.portrayal.MapLayer) Point(org.locationtech.jts.geom.Point) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) Presentation(org.apache.sis.internal.map.Presentation) Feature(org.opengis.feature.Feature) Envelope2D(org.apache.sis.geometry.Envelope2D) Coordinate(org.locationtech.jts.geom.Coordinate) AttributeType(org.opengis.feature.AttributeType) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 12 with MapLayers

use of org.apache.sis.portrayal.MapLayers 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);
}
Also used : FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) MapLayer(org.apache.sis.portrayal.MapLayer) Unit(javax.measure.Unit) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) Graphics2D(java.awt.Graphics2D) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) Expression(org.opengis.filter.Expression) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 13 with MapLayers

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

the class PortrayalServiceTest method testPortrayalCoverageResource.

/**
 * Test the CoverageReader view of a scene.
 */
@Test
public void testPortrayalCoverageResource() throws DataStoreException {
    // create a test coverage
    final BufferedImage img = new BufferedImage(360, 180, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2d = img.createGraphics();
    g2d.setColor(Color.GREEN);
    g2d.fillRect(0, 0, 360, 180);
    final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    env.setRange(0, -180, 180);
    env.setRange(1, -90, 90);
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setDomain(env);
    gcb.setValues(img);
    final GridCoverage coverage = gcb.build();
    // 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 SceneDef sceneDef = new SceneDef(context);
    final GridCoverageResource resource = DefaultPortrayalService.asResource(sceneDef);
    final GridGeometry gridGeom = resource.getGridGeometry();
    assertNotNull(gridGeom);
    final GridCoverage result = resource.read(null);
    final RenderedImage image = result.render(null);
    assertEquals(1000, image.getWidth());
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) MapLayer(org.apache.sis.portrayal.MapLayer) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) RenderedImage(java.awt.image.RenderedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 14 with MapLayers

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

the class PortrayalServiceTest method testCoverageVisit0_360.

/**
 * Test picking on a coverage in range 0-360.
 * @throws PortrayalException
 */
@Test
public void testCoverageVisit0_360() throws PortrayalException {
    // Create 0-360 coverage
    final BufferedImage img = new BufferedImage(350, 180, BufferedImage.TYPE_INT_ARGB);
    CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
    crs = AbstractCRS.castOrCopy(crs).forConvention(AxesConvention.POSITIVE_RANGE);
    final AffineTransform2D gridToCrs = new AffineTransform2D(1, 0, 0, -1, 0, 90);
    final GridExtent extent = new GridExtent(350, 180);
    final GridGeometry gg = new GridGeometry(extent, PixelInCell.CELL_CORNER, gridToCrs, crs);
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setValues(img);
    gcb.setDomain(gg);
    final GridCoverage coverage = gcb.build();
    final GridCoverageResource gcr = new InMemoryGridCoverageResource(coverage);
    final MapLayer layer = MapBuilder.createCoverageLayer(gcr);
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(layer);
    final GeneralEnvelope viewEnv = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    viewEnv.setRange(0, -180, +180);
    viewEnv.setRange(1, -90, +90);
    final AtomicInteger count = new AtomicInteger();
    GraphicVisitor gv = new GraphicVisitor() {

        @Override
        public void startVisit() {
        }

        @Override
        public void endVisit() {
        }

        @Override
        public void visit(Presentation graphic, RenderingContext context, SearchArea area) {
            count.incrementAndGet();
        }

        @Override
        public boolean isStopRequested() {
            return false;
        }
    };
    final SceneDef scene = new SceneDef(context);
    final CanvasDef canvas = new CanvasDef(new Dimension(360, 180), viewEnv);
    final VisitDef visit = new VisitDef(new Rectangle(10, 80, 2, 2), gv);
    DefaultPortrayalService.visit(canvas, scene, visit);
    assertEquals(1, count.get());
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) RenderingContext(org.geotoolkit.display.canvas.RenderingContext) GridExtent(org.apache.sis.coverage.grid.GridExtent) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) SearchArea(org.geotoolkit.display.SearchArea) GraphicVisitor(org.geotoolkit.display2d.GraphicVisitor) MapLayer(org.apache.sis.portrayal.MapLayer) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) Presentation(org.apache.sis.internal.map.Presentation) BufferedImage(java.awt.image.BufferedImage) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 15 with MapLayers

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

the class PortrayalServiceTest method testRendering.

private void testRendering(final MapLayer layer) throws Exception {
    final StopOnErrorMonitor monitor = new StopOnErrorMonitor();
    final MapLayers context = MapBuilder.createContext(CommonCRS.WGS84.normalizedGeographic());
    context.getComponents().add(layer);
    assertEquals(1, context.getComponents().size());
    for (final Envelope env : envelopes) {
        for (double[] drange : timestamps) {
            for (double[] erange : elevations) {
                final GeneralEnvelope cenv = new GeneralEnvelope(CRS.compound(env.getCoordinateReferenceSystem(), CommonCRS.Vertical.ELLIPSOIDAL.crs(), CommonCRS.Temporal.JAVA.crs()));
                cenv.subEnvelope(0, 2).setEnvelope(env);
                cenv.setRange(2, erange[0], erange[1]);
                cenv.setRange(3, drange[0], drange[1]);
                final CanvasDef cdef = new CanvasDef(new Dimension(800, 600), cenv);
                cdef.setAzimuth(0);
                cdef.setMonitor(monitor);
                final BufferedImage img = DefaultPortrayalService.portray(cdef, new SceneDef(context));
                assertNull(monitor.getLastException());
                assertNotNull(img);
            }
        }
    }
}
Also used : StopOnErrorMonitor(org.geotoolkit.display.canvas.control.StopOnErrorMonitor) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) BufferedImage(java.awt.image.BufferedImage) MapLayers(org.apache.sis.portrayal.MapLayers)

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