Search in sources :

Example 6 with Hints

use of org.geotoolkit.factory.Hints 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 7 with Hints

use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.

the class FeatureStreamsTest method testReprojectFeatureIterator.

@Test
public void testReprojectFeatureIterator() throws DataStoreException, FactoryException {
    Query query = new Query(collection.getType().getName());
    FeatureReader reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    final CoordinateReferenceSystem targetCRS = CommonCRS.WGS84.geographic();
    FeatureReader retyped = FeatureStreams.decorate(reader, new ReprojectMapper(reader.getFeatureType(), targetCRS), new Hints());
    int mask = 0;
    Feature f;
    while (retyped.hasNext()) {
        f = retyped.next();
        final Object id = f.getPropertyValue(AttributeConvention.IDENTIFIER);
        assertEquals(4, f.getType().getProperties(true).size());
        assertEquals(targetCRS, JTS.findCoordinateReferenceSystem((Geometry) f.getProperty("att_geom").getValue()));
        if (id1.equals(id)) {
            mask |= 1 << 0;
            assertEquals(GF.createPoint(new Coordinate(0, 3)).toString(), f.getProperty("att_geom").getValue().toString());
        } else if (id2.equals(id)) {
            mask |= 1 << 1;
            assertEquals(GF.createPoint(new Coordinate(0, 1)).toString(), f.getProperty("att_geom").getValue().toString());
        } else if (id3.equals(id)) {
            mask |= 1 << 2;
            assertEquals(GF.createPoint(new Coordinate(0, 2)).toString(), f.getProperty("att_geom").getValue().toString());
        }
    }
    if (mask != 7) {
        fail("missing features in iterations");
    }
    // check has next do not iterate
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    retyped = FeatureStreams.decorate(reader, new ReprojectMapper(reader.getFeatureType(), CommonCRS.WGS84.geographic()), new Hints());
    testIterationOnNext(retyped, 3);
    // check sub iterator is properly closed
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(reader);
    assertFalse(checkIte.isClosed());
    retyped = FeatureStreams.decorate(checkIte, new ReprojectMapper(checkIte.getFeatureType(), CommonCRS.WGS84.geographic()), new Hints());
    while (retyped.hasNext()) retyped.next();
    retyped.close();
    assertTrue(checkIte.isClosed());
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Query(org.geotoolkit.storage.feature.query.Query) Hints(org.geotoolkit.factory.Hints) Coordinate(org.locationtech.jts.geom.Coordinate) ReprojectMapper(org.geotoolkit.feature.ReprojectMapper) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Feature(org.opengis.feature.Feature) Point(org.locationtech.jts.geom.Point) CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) Test(org.junit.Test)

Example 8 with Hints

use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.

the class FeatureStreamsTest method testTransformFeatureIterator.

@Test
public void testTransformFeatureIterator() throws DataStoreException {
    FeatureType originalType = buildOriginalFT();
    final FeatureTypeBuilder builder = new FeatureTypeBuilder();
    final GenericName name = NamesExt.create("http://test.com", "TestSchema");
    builder.setName(name);
    builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY);
    builder.addAttribute(LineString.class).setName("att_geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = builder.build();
    final LineString geom = GF.createLineString(new Coordinate[] { new Coordinate(0, 0), // dx 15 , dy 12
    new Coordinate(15, 12), // dx 7 , dy 16
    new Coordinate(8, 28), // dx 1 , dy 3
    new Coordinate(9, 31), // dx 14 , dy 20
    new Coordinate(-5, 11), // dx 4 , dy 2
    new Coordinate(-1, 9) });
    final FeatureCollection collection = FeatureStoreUtilities.collection("id", type);
    Feature sf = type.newInstance();
    sf.setPropertyValue("att_geom", geom);
    collection.add(sf);
    // get the reader -------------------------------------------------------
    Query query = new Query(originalType.getName());
    FeatureReader reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    // create the decimate reader -------------------------------------------
    final Hints hints = new Hints();
    GeometryTransformer decim = new GeometryScaleTransformer(10, 10);
    final TransformMapper ttype = new TransformMapper(reader.getFeatureType(), decim);
    FeatureReader retyped = FeatureStreams.decorate(reader, ttype, hints);
    assertTrue(retyped.hasNext());
    LineString decimated = (LineString) retyped.next().getPropertyValue(AttributeConvention.GEOMETRY);
    assertFalse(retyped.hasNext());
    retyped.close();
    assertEquals(4, decimated.getNumPoints());
    assertEquals(geom.getGeometryN(0).getCoordinate(), decimated.getGeometryN(0).getCoordinate());
    assertEquals(geom.getGeometryN(1).getCoordinate(), decimated.getGeometryN(1).getCoordinate());
    assertEquals(geom.getGeometryN(2).getCoordinate(), decimated.getGeometryN(2).getCoordinate());
    assertEquals(geom.getGeometryN(4).getCoordinate(), decimated.getGeometryN(3).getCoordinate());
    // check the original geometry has not been modified
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    assertTrue(reader.hasNext());
    LineString notDecimated = (LineString) reader.next().getPropertyValue(AttributeConvention.GEOMETRY);
    assertEquals(6, notDecimated.getNumPoints());
    assertFalse(reader.hasNext());
    reader.close();
    // same test but with reuse hint ---------------------------------------
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    decim = new GeometryScaleTransformer(10, 10);
    retyped = FeatureStreams.decorate(reader, new TransformMapper(reader.getFeatureType(), decim), hints);
    assertTrue(retyped.hasNext());
    decimated = (LineString) retyped.next().getPropertyValue(AttributeConvention.GEOMETRY);
    assertFalse(retyped.hasNext());
    retyped.close();
    assertEquals(4, decimated.getNumPoints());
    assertEquals(geom.getGeometryN(0).getCoordinate(), decimated.getGeometryN(0).getCoordinate());
    assertEquals(geom.getGeometryN(1).getCoordinate(), decimated.getGeometryN(1).getCoordinate());
    assertEquals(geom.getGeometryN(2).getCoordinate(), decimated.getGeometryN(2).getCoordinate());
    assertEquals(geom.getGeometryN(4).getCoordinate(), decimated.getGeometryN(3).getCoordinate());
    // check the original geometry has not been modified
    reader = collection.getSession().getFeatureStore().getFeatureReader(query);
    assertTrue(reader.hasNext());
    notDecimated = (LineString) reader.next().getPropertyValue(AttributeConvention.GEOMETRY);
    assertEquals(6, notDecimated.getNumPoints());
    assertFalse(reader.hasNext());
    reader.close();
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) Query(org.geotoolkit.storage.feature.query.Query) Hints(org.geotoolkit.factory.Hints) GeometryTransformer(org.geotoolkit.geometry.jts.transform.GeometryTransformer) LineString(org.locationtech.jts.geom.LineString) TransformMapper(org.geotoolkit.feature.TransformMapper) Feature(org.opengis.feature.Feature) GeometryScaleTransformer(org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer) GenericName(org.opengis.util.GenericName) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Test(org.junit.Test)

Example 9 with Hints

use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.

the class DefaultPortrayalService method prepareCanvas.

public static void prepareCanvas(final J2DCanvas canvas, final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
    final ContextContainer2D renderer = new ContextContainer2D(canvas);
    canvas.setContainer(renderer);
    final Color bgColor = canvasDef.getBackground();
    if (bgColor != null) {
        canvas.setBackgroundPainter(new SolidColorPainter(bgColor));
    }
    final CanvasMonitor monitor = canvasDef.getMonitor();
    if (monitor != null) {
        canvas.setMonitor(monitor);
    }
    final Hints hints = sceneDef.getHints();
    if (hints != null) {
        for (Entry<?, ?> entry : hints.entrySet()) {
            canvas.setRenderingHint((Key) entry.getKey(), entry.getValue());
        }
    }
    final MapLayers context = sceneDef.getContext();
    renderer.setContext(context);
    GridGeometry gridGeometry = canvasDef.getGridGeometry();
    if (gridGeometry != null) {
        try {
            canvas.setGridGeometry(gridGeometry);
        } catch (FactoryException ex) {
            throw new PortrayalException("Could not set objective crs", ex);
        }
    } else {
        final Envelope contextEnv = canvasDef.getEnvelope();
        final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
        try {
            canvas.setObjectiveCRS(crs);
        } catch (TransformException | FactoryException ex) {
            throw new PortrayalException("Could not set objective crs", ex);
        }
        // we specifically say to not repect X/Y proportions
        canvas.setAxisProportions(!canvasDef.isStretchImage());
        // setVisibleArea -> setAxisRange -> setRange.
        if (contextEnv != null) {
            try {
                canvas.setGridGeometry(canvasDef.getOrCreateGridGeometry());
            } catch (Exception e) {
                // Rollback to previous behavior
                try {
                    canvas.setVisibleArea(contextEnv);
                    if (canvasDef.getAzimuth() != 0) {
                        canvas.rotate(-Math.toRadians(canvasDef.getAzimuth()));
                    }
                } catch (NoninvertibleTransformException | TransformException ex) {
                    ex.addSuppressed(e);
                    throw new PortrayalException(ex);
                }
            }
        }
    }
    // paints all extensions
    final List<PortrayalExtension> extensions = sceneDef.extensions();
    if (extensions != null) {
        for (final PortrayalExtension extension : extensions) {
            if (extension != null)
                extension.completeCanvas(canvas);
        }
    }
}
Also used : SolidColorPainter(org.geotoolkit.display2d.canvas.painter.SolidColorPainter) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) FactoryException(org.opengis.util.FactoryException) ContextContainer2D(org.geotoolkit.display2d.container.ContextContainer2D) Color(java.awt.Color) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) Envelope(org.opengis.geometry.Envelope) PortrayalException(org.geotoolkit.display.PortrayalException) FactoryException(org.opengis.util.FactoryException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 10 with Hints

use of org.geotoolkit.factory.Hints in project geotoolkit by Geomatys.

the class DefaultPortrayalService method visit.

public static void visit(final CanvasDef canvasDef, final SceneDef sceneDef, final VisitDef visitDef) throws PortrayalException {
    final Envelope contextEnv = canvasDef.getEnvelope();
    final Dimension canvasDimension = canvasDef.getDimension();
    final Hints hints = sceneDef.getHints();
    final MapLayers context = sceneDef.getContext();
    final boolean strechImage = canvasDef.isStretchImage();
    final J2DCanvasBuffered canvas = new J2DCanvasBuffered(contextEnv.getCoordinateReferenceSystem(), canvasDimension, hints);
    final ContextContainer2D renderer = new ContextContainer2D(canvas);
    canvas.setContainer(renderer);
    renderer.setContext(context);
    try {
        canvas.setObjectiveCRS(contextEnv.getCoordinateReferenceSystem());
    } catch (TransformException | FactoryException ex) {
        throw new PortrayalException("Could not set objective crs", ex);
    }
    // we specifically say to not repect X/Y proportions
    canvas.setAxisProportions(!strechImage);
    try {
        canvas.setVisibleArea(contextEnv);
    } catch (NoninvertibleTransformException | TransformException ex) {
        throw new PortrayalException(ex);
    }
    final Shape selectedArea = visitDef.getArea();
    final GraphicVisitor visitor = visitDef.getVisitor();
    try {
        canvas.getGraphicsIn(selectedArea, visitor);
    } catch (Exception ex) {
        if (ex instanceof PortrayalException) {
            throw (PortrayalException) ex;
        } else {
            throw new PortrayalException(ex);
        }
    } finally {
        visitor.endVisit();
        canvas.clearCache();
    }
}
Also used : Shape(java.awt.Shape) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) FactoryException(org.opengis.util.FactoryException) GraphicVisitor(org.geotoolkit.display2d.GraphicVisitor) ContextContainer2D(org.geotoolkit.display2d.container.ContextContainer2D) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) Dimension(java.awt.Dimension) Envelope(org.opengis.geometry.Envelope) PortrayalException(org.geotoolkit.display.PortrayalException) FactoryException(org.opengis.util.FactoryException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Aggregations

Hints (org.geotoolkit.factory.Hints)30 RenderingHints (java.awt.RenderingHints)10 DataStoreException (org.apache.sis.storage.DataStoreException)10 GO2Hints (org.geotoolkit.display2d.GO2Hints)10 Dimension (java.awt.Dimension)9 MapLayers (org.apache.sis.portrayal.MapLayers)9 Test (org.junit.Test)9 BufferedImage (java.awt.image.BufferedImage)8 FeatureType (org.opengis.feature.FeatureType)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)7 Filter (org.opengis.filter.Filter)7 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)7 MapLayer (org.apache.sis.portrayal.MapLayer)6 PortrayalException (org.geotoolkit.display.PortrayalException)6 CanvasDef (org.geotoolkit.display2d.service.CanvasDef)6 SceneDef (org.geotoolkit.display2d.service.SceneDef)6 Query (org.apache.sis.storage.Query)5 FeatureReader (org.geotoolkit.storage.feature.FeatureReader)5