Rogue Wave Views Maps Reader Framework > Renderers > Creating a Colored Line Renderer
 
Creating a Colored Line Renderer
This section shows how to write a new renderer that displays colored polylines from the numeric value of an attribute whose name is known.
The complete source code example for this renderer can be found in the following file:
<installdir>/samples/maps/userman/src/colorLineRenderer.cpp
Let’s suppose that we 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 ColorModel, a ColorModel being a class that allows mapping from integer values to RGB colors in a display independent way. 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 IlvMapGeneralPath graphic object.
IlvGraphic*
ColorLineRenderer::makeGraphic(const IlvMapFeature& feature,
const IlvMapInfo& targetMapInfo,
IlvMapsError& status) const {
const IlvMapGeometry* geometry = feature.getGeometry();
if (!geometry) {
status = IlvMaps::IllegalArgument();
return 0;
}
if (geometry->getClassInfo() != IlvMapLineString::ClassInfo()) {
status = IlvMaps::ClassError();
return 0;
}
 
const IlvMapLineString* lineStr = (const IlvMapLineString*) geometry;
 
int segCount = lineStr->getSegmentCount();
if (segCount == 0)
return 0;
 
IlvMapGeneralPath* genPath = new IlvMapGeneralPath(getDisplay());
 
const IlvMapSegment *segment;
IlvCoordinate c;
IlvPoint p;
 
segment = lineStr->getSegment(0);
c = segment -> getStartPoint();
status = targetMapInfo.toViews(c, feature.getProjection(), p);
genPath->moveTo(p);
 
for (int i = 0; i < segCount ; i++) {
c = segment -> getEndPoint();
status = targetMapInfo.toViews(c, feature.getProjection(), p);
genPath->lineTo(p);
}
 
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 coordinate system has it oriented downward. It might also imply a change of projection. In our example, the method toViews both converts the coordinates of points according to projections, if necessary, and corrects the orientation of the y-axis. Note that the source (feature::getProjection) and target projections can be set to 0, especially if the source data is not georeferenced (that is, its projection is not known), or do not need to be reprojected. For further information about projections, see Selecting a Target Projection and Map Projections.
Once the graphic object is created, we retrieve the attribute value for coloring the lines using a color model, as shown below:
IlvInt colorIndex = 0;
 
const IlvFeatureAttributeProperty* attributeList =
feature.getAttributes();
const IlvFeatureAttribute* fa =
attributeList->getAttribute(_attributeName);
const IlvMapClassInfo* clsinfo =
fa->getClassInfo();
if(clsinfo->isSubtypeOf(IlvIntegerAttribute::ClassInfo()))
colorIndex = ((IlvIntegerAttribute*)fa)->getValue();
else if(clsinfo->isSubtypeOf(IlvDoubleAttribute::ClassInfo()))
colorIndex = (IlvInt)((IlvDoubleAttribute*)fa)->getValue();
IlvColor* color = getDisplay()->
getColor((IlvUShort)_colorModel->getRed(colorIndex),
(IlvUShort)_colorModel->getGreen(colorIndex),
(IlvUShort)_colorModel->getBlue(colorIndex));
 
genPath->setForeground(color);
 
return genPath;
}
 

Version 5.7
Copyright © 2013, Rogue Wave Software, Inc. All Rights Reserved.