Search in sources :

Example 11 with InMemoryFeatureSet

use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.

the class CellSymbolizerRenderer method presentations.

private Stream<Presentation> presentations(MapLayer layer, FeatureSet fs) {
    // calculate the cells
    final int cellSize = symbol.getSource().getCellSize();
    final AffineTransform trs = renderingContext.getDisplayToObjective();
    final double objCellSize = AffineTransforms2D.getScale(trs) * cellSize;
    // find min and max cols/rows
    final Envelope env = renderingContext.getCanvasObjectiveBounds2D();
    final CoordinateReferenceSystem crs = env.getCoordinateReferenceSystem();
    final int minCol = (int) (env.getMinimum(0) / objCellSize);
    final int maxCol = (int) ((env.getMaximum(0) / objCellSize) + 0.5);
    final int minRow = (int) (env.getMinimum(1) / objCellSize);
    final int maxRow = (int) ((env.getMaximum(1) / objCellSize) + 0.5);
    final int nbRow = maxRow - minRow;
    final int nbCol = maxCol - minCol;
    // create all cell contours
    final Polygon[][] contours = new Polygon[nbRow][nbCol];
    for (int r = 0; r < nbRow; r++) {
        for (int c = 0; c < nbCol; c++) {
            final double minx = (minCol + c) * objCellSize;
            final double maxx = minx + objCellSize;
            final double miny = (minRow + r) * objCellSize;
            final double maxy = miny + objCellSize;
            contours[r][c] = GF.createPolygon(new Coordinate[] { new Coordinate(minx, miny), new Coordinate(minx, maxy), new Coordinate(maxx, maxy), new Coordinate(maxx, miny), new Coordinate(minx, miny) });
            JTS.setCRS(contours[r][c], crs);
        }
    }
    FeatureType baseType = null;
    FeatureType cellType = null;
    String[] numericProperties = null;
    Statistics[][][] stats = null;
    try (final RenderingRoutines.GraphicIterator graphics = RenderingRoutines.getIterator(fs, renderingContext)) {
        while (graphics.hasNext()) {
            final ProjectedObject obj = graphics.next();
            final ProjectedFeature projFeature = (ProjectedFeature) obj;
            if (baseType == null) {
                // we expect all features to have the same type
                baseType = projFeature.getCandidate().getType();
                cellType = CellSymbolizer.buildCellType(baseType, crs);
                final List<String> props = new ArrayList<>();
                for (PropertyType desc : baseType.getProperties(true)) {
                    if (desc instanceof AttributeType) {
                        final AttributeType att = (AttributeType) desc;
                        final Class binding = att.getValueClass();
                        if (Number.class.isAssignableFrom(binding) || String.class.isAssignableFrom(binding)) {
                            props.add(att.getName().toString());
                        }
                    }
                }
                numericProperties = props.toArray(new String[props.size()]);
                stats = new Statistics[numericProperties.length][nbRow][nbCol];
                for (int i = 0; i < numericProperties.length; i++) {
                    for (int j = 0; j < nbRow; j++) {
                        for (int k = 0; k < nbCol; k++) {
                            stats[i][j][k] = new Statistics("");
                        }
                    }
                }
            }
            final ProjectedGeometry pg = projFeature.getGeometry(geomPropertyName);
            final Geometry[] geoms = pg.getObjectiveGeometryJTS();
            // find in which cell it intersects
            int row = -1;
            int col = -1;
            loop: for (Geometry g : geoms) {
                if (g == null)
                    continue;
                for (int r = 0; r < nbRow; r++) {
                    for (int c = 0; c < nbCol; c++) {
                        if (contours[r][c].intersects(g)) {
                            row = r;
                            col = c;
                            break loop;
                        }
                    }
                }
            }
            // fill stats
            if (row != -1) {
                final Feature feature = projFeature.getCandidate();
                for (int i = 0; i < numericProperties.length; i++) {
                    final Object value = feature.getProperty(numericProperties[i]).getValue();
                    try {
                        final Number num = ObjectConverters.convert(value, Number.class);
                        if (num != null) {
                            stats[i][row][col].accept(num.doubleValue());
                        }
                    } catch (UnconvertibleObjectException e) {
                        Logging.recoverableException(LOGGER, CellSymbolizerRenderer.class, "portray", e);
                    // TODO - do we really want to ignore?
                    }
                }
            }
        }
    } catch (DataStoreException | IOException | TransformException ex) {
        ExceptionPresentation ep = new ExceptionPresentation(ex);
        ep.setLayer(layer);
        ep.setResource(fs);
        return Stream.of(ep);
    }
    if (numericProperties == null) {
        // nothing in the iterator
        return Stream.empty();
    }
    // render the cell features
    final Object[] values = new Object[2 + 7 * numericProperties.length];
    final List<Feature> features = new ArrayList<>();
    for (int r = 0; r < nbRow; r++) {
        for (int c = 0; c < nbCol; c++) {
            final Feature feature = cellType.newInstance();
            feature.setPropertyValue(AttributeConvention.IDENTIFIER, "cell-n");
            values[0] = contours[r][c].getCentroid();
            JTS.setCRS(((Geometry) values[0]), crs);
            values[1] = contours[r][c];
            int k = 1;
            for (int b = 0, n = numericProperties.length; b < n; b++) {
                values[++k] = stats[b][r][c].count();
                values[++k] = stats[b][r][c].minimum();
                values[++k] = stats[b][r][c].mean();
                values[++k] = stats[b][r][c].maximum();
                values[++k] = stats[b][r][c].span();
                values[++k] = stats[b][r][c].rms();
                values[++k] = stats[b][r][c].sum();
            }
            features.add(feature);
        }
    }
    final Rule rule = symbol.getSource().getRule();
    final InMemoryFeatureSet subfs = new InMemoryFeatureSet(cellType, features);
    final MapLayer subLayer = MapBuilder.createLayer(subfs);
    subLayer.setStyle(GO2Utilities.STYLE_FACTORY.style(rule.symbolizers().toArray(new Symbolizer[0])));
    return DefaultPortrayalService.present(subLayer, subfs, renderingContext);
}
Also used : FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) MapLayer(org.apache.sis.portrayal.MapLayer) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Feature(org.opengis.feature.Feature) ProjectedFeature(org.geotoolkit.display2d.primitive.ProjectedFeature) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) AttributeType(org.opengis.feature.AttributeType) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(org.locationtech.jts.geom.Polygon) DataStoreException(org.apache.sis.storage.DataStoreException) TransformException(org.opengis.referencing.operation.TransformException) ProjectedFeature(org.geotoolkit.display2d.primitive.ProjectedFeature) IOException(java.io.IOException) Statistics(org.apache.sis.math.Statistics) Point(java.awt.Point) ProjectedGeometry(org.geotoolkit.display2d.primitive.ProjectedGeometry) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) ProjectedObject(org.geotoolkit.display2d.primitive.ProjectedObject) ProjectedGeometry(org.geotoolkit.display2d.primitive.ProjectedGeometry) AffineTransform(java.awt.geom.AffineTransform) ProjectedObject(org.geotoolkit.display2d.primitive.ProjectedObject) RenderingRoutines(org.geotoolkit.display2d.style.renderer.RenderingRoutines) Rule(org.opengis.style.Rule)

Example 12 with InMemoryFeatureSet

use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.

the class QueryTest method reprojectTest.

@Test
public void reprojectTest() throws DataStoreException {
    CoordinateReferenceSystem inCrs = CommonCRS.WGS84.normalizedGeographic();
    CoordinateReferenceSystem outCrs = CommonCRS.WGS84.geographic();
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(Point.class).setName("geom").setCRS(inCrs).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = ftb.build();
    final Point geometry = GF.createPoint(new Coordinate(10, 30));
    geometry.setUserData(inCrs);
    final Feature feature = type.newInstance();
    feature.setPropertyValue("geom", geometry);
    FeatureSet fs = new InMemoryFeatureSet(type, Collections.singleton(feature));
    final FeatureQuery query = Query.reproject(type, outCrs);
    final FeatureSet rfs = fs.subset(query);
    final Feature rfeature = rfs.features(false).findFirst().get();
    Point geom1 = (Point) rfeature.getPropertyValue("sis:geometry");
    Point geom2 = (Point) rfeature.getPropertyValue("geom");
    assertEquals(outCrs, geom1.getUserData());
    assertEquals(outCrs, geom2.getUserData());
    Assert.assertEquals(30.0, geom1.getX(), 0.0);
    Assert.assertEquals(10.0, geom1.getY(), 0.0);
    Assert.assertEquals(30.0, geom2.getX(), 0.0);
    Assert.assertEquals(10.0, geom2.getY(), 0.0);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Coordinate(org.locationtech.jts.geom.Coordinate) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Point(org.locationtech.jts.geom.Point) FeatureQuery(org.apache.sis.storage.FeatureQuery) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 13 with InMemoryFeatureSet

use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.

the class MeridianTest method createFeatureLayer.

private static MapLayers createFeatureLayer(MultiPoint geometry) {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(MultiPoint.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = ftb.build();
    final Feature feature = type.newInstance();
    JTS.setCRS(geometry, CommonCRS.WGS84.normalizedGeographic());
    feature.setPropertyValue("geom", geometry);
    final FeatureSet col = new InMemoryFeatureSet(type, Arrays.asList(feature));
    final List<GraphicalSymbol> symbols = new ArrayList<>();
    symbols.add(SF.mark(StyleConstants.MARK_SQUARE, SF.fill(Color.RED), SF.stroke(Color.BLACK, 0)));
    final Graphic graphic = SF.graphic(symbols, StyleConstants.LITERAL_ONE_FLOAT, FF.literal(2), StyleConstants.LITERAL_ZERO_FLOAT, null, null);
    final PointSymbolizer ps = SF.pointSymbolizer(graphic, null);
    final MutableStyle style = SF.style(ps);
    final MapLayer layer = MapBuilder.createLayer(col);
    layer.setStyle(style);
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(layer);
    return context;
}
Also used : MultiPoint(org.locationtech.jts.geom.MultiPoint) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) PointSymbolizer(org.opengis.style.PointSymbolizer) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Graphic(org.opengis.style.Graphic) GraphicalSymbol(org.opengis.style.GraphicalSymbol) MapLayer(org.apache.sis.portrayal.MapLayer) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) MutableStyle(org.geotoolkit.style.MutableStyle) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 14 with InMemoryFeatureSet

use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.

the class PortrayalServiceTest method testMarginRendering.

/**
 * Test that a large graphic outside the map area is still rendered.
 */
@Test
public void testMarginRendering() throws Exception {
    final List<GraphicalSymbol> symbols = new ArrayList<>();
    final Stroke stroke = SF.stroke(Color.BLACK, 0);
    final Fill fill = SF.fill(Color.BLACK);
    final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
    symbols.add(mark);
    final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(8), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, DEFAULT_DISPLACEMENT);
    final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
    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 ft = ftb.build();
    final Feature feature = ft.newInstance();
    final Point pt = GF.createPoint(new Coordinate(12, 5));
    JTS.setCRS(pt, crs);
    feature.setPropertyValue("geom", pt);
    final FeatureSet col = new InMemoryFeatureSet(ft, Arrays.asList(feature));
    final MapLayer layer = MapBuilder.createLayer(col);
    layer.setStyle(SF.style(symbolizer));
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(layer);
    final GeneralEnvelope env = new GeneralEnvelope(crs);
    env.setRange(0, 0, 10);
    env.setRange(1, 0, 10);
    final CanvasDef cdef = new CanvasDef(new Dimension(10, 10), env);
    cdef.setBackground(Color.WHITE);
    final SceneDef sdef = new SceneDef(context);
    final BufferedImage img = DefaultPortrayalService.portray(cdef, sdef);
    assertEquals(Color.BLACK.getRGB(), img.getRGB(9, 5));
    assertEquals(Color.BLACK.getRGB(), img.getRGB(8, 5));
    assertEquals(Color.WHITE.getRGB(), img.getRGB(7, 5));
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) MapLayer(org.apache.sis.portrayal.MapLayer) ArrayList(java.util.ArrayList) Point(org.locationtech.jts.geom.Point) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) Coordinate(org.locationtech.jts.geom.Coordinate) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 15 with InMemoryFeatureSet

use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.

the class VisitorTest method intersectionFeatureTest.

/**
 * Feature visitor test.
 */
@Test
public void intersectionFeatureTest() throws Exception {
    final MutableStyleFactory sf = new DefaultStyleFactory();
    final GeographicCRS crs = CommonCRS.WGS84.normalizedGeographic();
    final FeatureTypeBuilder sftb = new FeatureTypeBuilder();
    sftb.setName("testingIntersect");
    sftb.addAttribute(String.class).setName("id").addRole(AttributeRole.IDENTIFIER_COMPONENT);
    sftb.addAttribute(Polygon.class).setName("geom").setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType sft = sftb.build();
    final WritableFeatureSet collection = new InMemoryFeatureSet("id", sft);
    final Feature f = sft.newInstance();
    final GeometryFactory gf = org.geotoolkit.geometry.jts.JTS.getFactory();
    LinearRing ring = gf.createLinearRing(new Coordinate[] { new Coordinate(10, 10), new Coordinate(20, 10), new Coordinate(20, 20), new Coordinate(10, 20), new Coordinate(10, 10) });
    Polygon pol = gf.createPolygon(ring, new LinearRing[0]);
    pol.setUserData(crs);
    f.setPropertyValue("id", "id-0");
    f.setPropertyValue("geom", pol);
    collection.add(Arrays.asList(f).iterator());
    MapLayer layer = MapBuilder.createLayer(collection);
    layer.setStyle(sf.style(sf.polygonSymbolizer()));
    layer.setVisible(true);
    MapLayers context = MapBuilder.createContext(CommonCRS.WGS84.normalizedGeographic());
    context.getComponents().add(layer);
    final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    env.setRange(0, -180, 180);
    env.setRange(1, -90, 90);
    final Dimension dim = new Dimension(360, 180);
    // starting at top left corner
    Shape shparea = new Rectangle(195, 75, 2, 2);
    ListVisitor visitor = new ListVisitor();
    // ensure we can paint image
    DefaultPortrayalService.portray(context, env, dim, true);
    DefaultPortrayalService.visit(context, env, dim, true, null, shparea, visitor);
    assertEquals(1, visitor.features.size());
    assertEquals("id-0", FeatureExt.getId(visitor.features.get(0)).getIdentifier());
    // starting at top left corner
    shparea = new Rectangle(30, 12, 2, 2);
    visitor = new ListVisitor();
    // ensure we can paint image
    DefaultPortrayalService.portray(context, env, dim, true);
    DefaultPortrayalService.visit(context, env, dim, true, null, shparea, visitor);
    assertTrue(visitor.features.size() == 0);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Shape(java.awt.Shape) MapLayer(org.apache.sis.portrayal.MapLayer) Rectangle(java.awt.Rectangle) MutableStyleFactory(org.geotoolkit.style.MutableStyleFactory) SampleDimension(org.apache.sis.coverage.SampleDimension) Dimension(java.awt.Dimension) Feature(org.opengis.feature.Feature) Coordinate(org.locationtech.jts.geom.Coordinate) GeographicCRS(org.opengis.referencing.crs.GeographicCRS) Polygon(org.locationtech.jts.geom.Polygon) LinearRing(org.locationtech.jts.geom.LinearRing) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) DefaultStyleFactory(org.geotoolkit.style.DefaultStyleFactory) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Aggregations

InMemoryFeatureSet (org.geotoolkit.storage.memory.InMemoryFeatureSet)18 Feature (org.opengis.feature.Feature)18 FeatureType (org.opengis.feature.FeatureType)17 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)11 FeatureSet (org.apache.sis.storage.FeatureSet)11 MapLayer (org.apache.sis.portrayal.MapLayer)10 ArrayList (java.util.ArrayList)9 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)8 Coordinate (org.locationtech.jts.geom.Coordinate)8 MapLayers (org.apache.sis.portrayal.MapLayers)7 MutableStyle (org.geotoolkit.style.MutableStyle)7 Test (org.junit.Test)7 Dimension (java.awt.Dimension)6 WritableFeatureSet (org.apache.sis.storage.WritableFeatureSet)6 BufferedImage (java.awt.image.BufferedImage)5 IOException (java.io.IOException)5 DataStoreException (org.apache.sis.storage.DataStoreException)5 SampleDimension (org.apache.sis.coverage.SampleDimension)4 Geometry (org.locationtech.jts.geom.Geometry)4 Point (org.locationtech.jts.geom.Point)4