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);
}
Aggregations