use of org.geotoolkit.display.axis.NumberGraduation in project geotoolkit by Geomatys.
the class J2DGridUtilities method paint.
public static void paint(final RenderingContext2D context, final GridTemplate template) {
CoordinateReferenceSystem gridCRS = template.getCRS();
// use context crs if gridcrs is not defined
if (gridCRS == null)
gridCRS = context.getObjectiveCRS();
final Graphics2D g = context.getGraphics();
context.switchToDisplayCRS();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setComposite(GO2Utilities.ALPHA_COMPOSITE_1F);
// calculate the cliping zone for texts
final float xTextOffset = template.getXTextOffset();
final float yTextOffset = template.getYTextOffset();
final Rectangle clip = new Rectangle(context.getCanvasDisplayBounds());
clip.x += xTextOffset;
clip.height -= yTextOffset;
final Shape shp = new TransformedShape(clip, context.getDisplayToObjective());
final List<Coordinate> coords = new ArrayList<>();
final PathIterator ite = shp.getPathIterator(null);
final double[] vals = new double[3];
while (!ite.isDone()) {
ite.currentSegment(vals);
coords.add(new Coordinate(vals[0], vals[1]));
ite.next();
}
final GeometryFactory fact = JTS.getFactory();
final LinearRing ring = fact.createLinearRing(coords.toArray(new Coordinate[coords.size()]));
final Polygon bounds = fact.createPolygon(ring, new LinearRing[0]);
final LabelRenderer renderer = context.getLabelRenderer(true);
final LabelLayer layer = new DefaultLabelLayer(false, true);
final RenderingHints tickHint = new RenderingHints(null);
tickHint.put(Graduation.VISUAL_AXIS_LENGTH, context.getCanvasDisplayBounds().width);
tickHint.put(Graduation.VISUAL_TICK_SPACING, 200);
// number of point by line
final int nbPoint = 20;
final CoordinateReferenceSystem objectiveCRS = context.getObjectiveCRS2D();
try {
// reduce grid bounds to validity area
Envelope gridBounds = Envelopes.transform(context.getCanvasObjectiveBounds2D(), gridCRS);
if (new GeneralEnvelope(gridBounds).isEmpty()) {
// envelope likely contains NaN values
gridBounds = CRS.getDomainOfValidity(gridCRS);
if (gridBounds == null) {
// try to convert target CRS validity to grid crs
gridBounds = CRS.getDomainOfValidity(objectiveCRS);
if (gridBounds == null) {
// nothing we can do
return;
}
gridBounds = Envelopes.transform(gridBounds, gridCRS);
}
}
if (Math.abs(gridBounds.getSpan(0)) < MIN || Math.abs(gridBounds.getSpan(1)) < MIN) {
return;
}
Envelope validity = CRS.getDomainOfValidity(gridCRS);
if (validity != null) {
GeneralEnvelope env = new GeneralEnvelope(gridBounds);
env.intersect(validity);
gridBounds = env;
}
final MathTransform gridToObj = CRS.findOperation(gridCRS, objectiveCRS, null).getMathTransform();
final MathTransform objToGrid = gridToObj.inverse();
// grid on X axis ---------------------------------------------------
final NumberGraduation graduationX = new NumberGraduation(null);
graduationX.setRange(gridBounds.getMinimum(0), gridBounds.getMaximum(0), gridBounds.getCoordinateReferenceSystem().getCoordinateSystem().getAxis(0).getUnit());
TickIterator tickIte = graduationX.getTickIterator(tickHint, null);
while (!tickIte.isDone()) {
tickIte.next();
final String label = tickIte.currentLabel();
final double d = tickIte.currentPosition();
if (d > gridBounds.getMaximum(0))
continue;
final ArrayList<Coordinate> lineCoords = new ArrayList<>();
final double maxY = gridBounds.getMaximum(1);
final double step = gridBounds.getSpan(1) / nbPoint;
for (double k = Math.nextUp(gridBounds.getMinimum(1)); k < maxY; k += step) {
lineCoords.add(new Coordinate(d, k));
}
lineCoords.add(new Coordinate(d, Math.nextAfter(maxY, Double.NEGATIVE_INFINITY)));
Geometry geom = fact.createLineString(lineCoords.toArray(new Coordinate[lineCoords.size()]));
if (geom == null)
continue;
final ProjectedGeometry pg = new ProjectedGeometry(context);
pg.setDataGeometry(geom, gridCRS);
// draw line
if (tickIte.isMajorTick()) {
g.setPaint(template.getMainLinePaint());
g.setStroke(template.getMainLineStroke());
} else {
g.setPaint(template.getLinePaint());
g.setStroke(template.getLineStroke());
}
for (Shape ds : pg.getDisplayShape()) g.draw(ds);
// clip geometry to avoid text outside visible area
geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, gridToObj);
if (geom == null)
continue;
geom = geom.intersection(bounds);
pg.setDataGeometry(geom, objectiveCRS);
// draw text
final LinearLabelDescriptor desc;
if (tickIte.isMajorTick()) {
desc = new DefaultLinearLabelDescriptor(label, template.getMainLabelFont(), template.getMainLabelPaint(), template.getMainHaloWidth(), template.getMainHaloPaint(), 0, 10, 3, false, false, false, pg);
} else {
desc = new DefaultLinearLabelDescriptor(label, template.getLabelFont(), template.getLabelPaint(), template.getHaloWidth(), template.getHaloPaint(), 0, 10, 3, false, false, false, pg);
}
layer.labels().add(desc);
}
// grid on Y axis ---------------------------------------------------
final NumberGraduation graduationY = new NumberGraduation(null);
graduationY.setRange(gridBounds.getMinimum(1), gridBounds.getMaximum(1), gridBounds.getCoordinateReferenceSystem().getCoordinateSystem().getAxis(1).getUnit());
tickIte = graduationY.getTickIterator(tickHint, null);
while (!tickIte.isDone()) {
tickIte.next();
final String label = tickIte.currentLabel();
final double d = tickIte.currentPosition();
if (d > gridBounds.getMaximum(1))
continue;
final ArrayList<Coordinate> lineCoords = new ArrayList<>();
final double maxX = gridBounds.getMaximum(0);
final double step = gridBounds.getSpan(0) / nbPoint;
for (double k = Math.nextUp(gridBounds.getMinimum(0)); k < maxX; k += step) {
lineCoords.add(new Coordinate(k, d));
}
lineCoords.add(new Coordinate(Math.nextAfter(maxX, Double.NEGATIVE_INFINITY), d));
Geometry geom = fact.createLineString(lineCoords.toArray(new Coordinate[lineCoords.size()]));
final ProjectedGeometry pg = new ProjectedGeometry(context);
pg.setDataGeometry(geom, gridCRS);
// draw line
if (tickIte.isMajorTick()) {
g.setPaint(template.getMainLinePaint());
g.setStroke(template.getMainLineStroke());
} else {
g.setPaint(template.getLinePaint());
g.setStroke(template.getLineStroke());
}
for (Shape ds : pg.getDisplayShape()) g.draw(ds);
// clip geometry to avoid text outside visible area
geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, gridToObj);
if (geom == null)
continue;
geom = geom.intersection(bounds);
pg.setDataGeometry(geom, objectiveCRS);
// draw text
final LinearLabelDescriptor desc;
if (tickIte.isMajorTick()) {
desc = new DefaultLinearLabelDescriptor(label, template.getMainLabelFont(), template.getMainLabelPaint(), template.getMainHaloWidth(), template.getMainHaloPaint(), 0, 10, 3, false, false, false, pg);
} else {
desc = new DefaultLinearLabelDescriptor(label, template.getLabelFont(), template.getLabelPaint(), template.getHaloWidth(), template.getHaloPaint(), 0, 10, 3, false, false, false, pg);
}
layer.labels().add(desc);
}
} catch (Exception ex) {
// TODO: NO!!
ex.printStackTrace();
}
renderer.portrayImmidiately(layer);
}
Aggregations