Search in sources :

Example 6 with MutableStyle

use of org.geotoolkit.style.MutableStyle 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 7 with MutableStyle

use of org.geotoolkit.style.MutableStyle 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 8 with MutableStyle

use of org.geotoolkit.style.MutableStyle 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 9 with MutableStyle

use of org.geotoolkit.style.MutableStyle in project geotoolkit by Geomatys.

the class PatternSymbolizerTest method testXml.

/**
 * Test Jaxb xml support.
 */
@Test
public void testXml() throws JAXBException, IOException {
    final MutableStyleFactory SF = GO2Utilities.STYLE_FACTORY;
    final FilterFactory2 FF = GO2Utilities.FILTER_FACTORY;
    final Map<Expression, List<Symbolizer>> ranges = new LinkedHashMap<>();
    ranges.put(FF.literal(-1000), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.BLUE), null)));
    ranges.put(FF.literal(-500), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.RED), null)));
    ranges.put(FF.literal(-100), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.GREEN), null)));
    ranges.put(FF.literal(100), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.YELLOW), null)));
    ranges.put(FF.literal(1000), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.GRAY), null)));
    final PatternSymbolizer ps = new PatternSymbolizer(FF.literal(0), ranges, ThreshholdsBelongTo.PRECEDING);
    final MutableStyle style = GO2Utilities.STYLE_FACTORY.style(ps);
    final Path path = Files.createTempFile("xml", ".xml");
    IOUtilities.deleteOnExit(path);
    new StyleXmlIO().writeStyle(path, style, Specification.StyledLayerDescriptor.V_1_1_0);
}
Also used : Path(java.nio.file.Path) MutableStyle(org.geotoolkit.style.MutableStyle) Expression(org.opengis.filter.Expression) StyleXmlIO(org.geotoolkit.sld.xml.StyleXmlIO) List(java.util.List) MutableStyleFactory(org.geotoolkit.style.MutableStyleFactory) FilterFactory2(org.geotoolkit.filter.FilterFactory2) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 10 with MutableStyle

use of org.geotoolkit.style.MutableStyle in project geotoolkit by Geomatys.

the class OwcXmlIO method readEntry.

private static MapItem readEntry(final EntryType entry) throws JAXBException, FactoryException, DataStoreException {
    final List<Object> entryContent = entry.getAuthorOrCategoryOrContent();
    String layerName = "";
    String layerTitle = "";
    String layerAbstract = "";
    boolean visible = true;
    boolean selectable = true;
    double layerOpacity = 1.0;
    MapItem mapItem = null;
    MutableStyle baseStyle = null;
    MutableStyle selectionStyle = null;
    final List<MapItem> children = new ArrayList<>();
    for (Object o : entryContent) {
        QName name = null;
        if (o instanceof JAXBElement) {
            final JAXBElement jax = (JAXBElement) o;
            name = jax.getName();
            o = jax.getValue();
            if (GEOTK_FACTORY._Visible_QNAME.equals(name)) {
                visible = (Boolean) o;
            } else if (GEOTK_FACTORY._Selectable_QNAME.equals(name)) {
                selectable = (Boolean) o;
            } else if (GEOTK_FACTORY._Opacity_QNAME.equals(name)) {
                layerOpacity = (Double) o;
            }
        }
        if (o instanceof OfferingType) {
            final OfferingType offering = (OfferingType) o;
            for (OwcExtension ext : getExtensions()) {
                if (ext.getCode().equals(offering.getCode())) {
                    mapItem = ext.createLayer(offering);
                    break;
                }
            }
            // search for styles
            baseStyle = readStyle(offering, true);
            selectionStyle = readStyle(offering, false);
        } else if (o instanceof ContentType) {
            // decode children
            final ContentType content = (ContentType) o;
            final List<Object> contentContent = content.getContent();
            for (Object co : contentContent) {
                if (co instanceof JAXBElement) {
                    co = ((JAXBElement) o).getValue();
                }
                if (co instanceof EntryType) {
                    children.add(readEntry((EntryType) co));
                }
            }
        } else if (o instanceof IdType) {
            final IdType idType = (IdType) o;
            final String value = idType.getValue();
            layerName = value;
        } else if (o instanceof TextType) {
            final TextType tt = (TextType) o;
            if (ATOM_FACTORY._EntryTypeTitle_QNAME.equals(name)) {
                if (!tt.getContent().isEmpty()) {
                    layerTitle = (String) tt.getContent().get(0);
                }
            } else if (ATOM_FACTORY._EntryTypeSummary_QNAME.equals(name)) {
                if (!tt.getContent().isEmpty()) {
                    layerAbstract = (String) tt.getContent().get(0);
                }
            }
        }
    }
    if (mapItem == null) {
        mapItem = MapBuilder.createItem();
    } else if (mapItem instanceof MapLayer) {
        if (baseStyle != null) {
            ((MapLayer) mapItem).setStyle(baseStyle);
        }
        ((MapLayer) mapItem).setOpacity(layerOpacity);
    }
    mapItem.setIdentifier(layerName);
    mapItem.setTitle(layerTitle);
    mapItem.setAbstract(layerAbstract);
    mapItem.setVisible(visible);
    if (mapItem instanceof MapLayers) {
        ((MapLayers) mapItem).getComponents().addAll(children);
    } else if (!children.isEmpty()) {
        throw new IllegalArgumentException("MapLayer can not have children layers.");
    }
    return mapItem;
}
Also used : ContentType(org.geotoolkit.owc.xml.v10.ContentType) QName(javax.xml.namespace.QName) MapLayer(org.apache.sis.portrayal.MapLayer) ArrayList(java.util.ArrayList) JAXBElement(javax.xml.bind.JAXBElement) IdType(org.w3._2005.atom.IdType) TextType(org.w3._2005.atom.TextType) OfferingType(org.geotoolkit.owc.xml.v10.OfferingType) EntryType(org.w3._2005.atom.EntryType) MutableStyle(org.geotoolkit.style.MutableStyle) List(java.util.List) ArrayList(java.util.ArrayList) MapItem(org.apache.sis.portrayal.MapItem) MapLayers(org.apache.sis.portrayal.MapLayers)

Aggregations

MutableStyle (org.geotoolkit.style.MutableStyle)55 Expression (org.opengis.filter.Expression)20 MapLayer (org.apache.sis.portrayal.MapLayer)19 Unit (javax.measure.Unit)16 MapLayers (org.apache.sis.portrayal.MapLayers)15 Description (org.opengis.style.Description)15 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)11 Feature (org.opengis.feature.Feature)11 FeatureType (org.opengis.feature.FeatureType)11 Fill (org.opengis.style.Fill)11 GraphicalSymbol (org.opengis.style.GraphicalSymbol)11 Stroke (org.opengis.style.Stroke)11 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)10 FeatureSet (org.apache.sis.storage.FeatureSet)10 GraphicStroke (org.opengis.style.GraphicStroke)10 PointSymbolizer (org.opengis.style.PointSymbolizer)10 BufferedImage (java.awt.image.BufferedImage)9 MutableFeatureTypeStyle (org.geotoolkit.style.MutableFeatureTypeStyle)9 PolygonSymbolizer (org.opengis.style.PolygonSymbolizer)9