The complete source code example for this renderer can
be found in the following file:
Suppose that you want to display contour lines of different
elevations with different colors. A simple solution would consist
of indexing a color using the elevation value by means of a java.awt.image.ColorModel
.
More generally, it would be useful to have a renderer class that applies
a color to graphic objects, such as lines, polygons, or text, by using
any of the attributes associated with a map feature.
The
makeGraphic
method in
the
ColorLineRenderer
class builds an
IlvPolyline
graphic
object from the interface
IlvMapMultiPointInterface. This interface is common to all geometries composed
of a set of points.
public IlvGraphic makeGraphic(IlvMapFeature feature,
IlvCoordinateTransformation tr)
throws IlvMapRenderException, IlvCoordinateTransformationException
{
// Check that this geometry can be processed.
IlvMapMultiPointInterface multiPoint;
try {
multiPoint = (IlvMapMultiPointInterface)feature.getGeometry();
} catch (Exception e) {
throw new IlvMapRenderException("not a multipoint geometry");
}
// Check that something has to be done.
int pointCount = multiPoint.getPointCount();
if (pointCount == 0)
return null;
// Allocate polyline point array.
IlvPoint p[] = new IlvPoint[pointCount];
// Convert points.
for (int i = 0; i < pointCount ; i++) {
p[i] = new IlvPoint();
IlvProjectionUtil.ToViews(tr,
multiPoint.getPoint(i),
p[i]);
}
// Create the graphic object, without duplicating the p[] array.
IlvPolyline poly = new IlvPolyline(p, false);
[...]
The map feature coordinates must be converted to the
manager coordinate system. This conversion implies a change of orientation
of the y-axis since cartographic data coordinate systems have the
positive portion of their y-axis oriented upward, whereas the manager
has it oriented downward. It might also imply a coordinate transformation
by changing the source coordinate system of the data into a target
coordinate system. In our example, the
ToViews method both transforms the coordinates, if necessary,
and corrects the orientation of the y-axis. Note that the
IlvCoordinateTransformation
can
be
identity
by setting the source (
feature.getCoordinateSystem()
)
and target coordinate systems to
null
,
especially if the source data is not georeferenced (that is, its projection
is not known), or does not need to be transformed. For further information
about coordinate systems, see
Handling spatial reference systems.
Once the graphic object is created, the attribute value
for coloring the lines using a color model is retrieved, as shown
below:
int colorIndex = 0;
// Get attribute list.
IlvFeatureAttributeProperty attributeList = feature.getAttributes();
if (attributeList != null) {
try {
IlvFeatureAttribute attribute = null;
attribute = attributeList.getAttribute(myAttributeName);
if (attribute instanceof IlvIntegerAttribute)
colorIndex = ((IlvIntegerAttribute)attribute).getValue();
else if (attribute instanceof IlvDoubleAttribute)
colorIndex = (int)((IlvDoubleAttribute)attribute).getValue();
} catch (IllegalArgumentException e) {
// No attribute found.
colorIndex = 0;
}
}
Color color = new Color(myColorModel.getRed(colorIndex),
myColorModel.getGreen(colorIndex),
myColorModel.getBlue(colorIndex));
// Sets the color of graphic.
poly.setForeground(color);