Search in sources :

Example 1 with ProjectedObject

use of org.geotoolkit.display2d.primitive.ProjectedObject 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)

Aggregations

Point (java.awt.Point)1 AffineTransform (java.awt.geom.AffineTransform)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)1 ExceptionPresentation (org.apache.sis.internal.map.ExceptionPresentation)1 Statistics (org.apache.sis.math.Statistics)1 MapLayer (org.apache.sis.portrayal.MapLayer)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)1 ProjectedFeature (org.geotoolkit.display2d.primitive.ProjectedFeature)1 ProjectedGeometry (org.geotoolkit.display2d.primitive.ProjectedGeometry)1 ProjectedObject (org.geotoolkit.display2d.primitive.ProjectedObject)1 RenderingRoutines (org.geotoolkit.display2d.style.renderer.RenderingRoutines)1 InMemoryFeatureSet (org.geotoolkit.storage.memory.InMemoryFeatureSet)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 Polygon (org.locationtech.jts.geom.Polygon)1 AttributeType (org.opengis.feature.AttributeType)1 Feature (org.opengis.feature.Feature)1