The
getNextFeature method reads the geometry of a map feature and creates
an
IlvMapFeature object that will hold all the information required
to process the geometry. The geometry read in the code example that
follows is an
IlvMapLineString,
which is the class to define polyline geometries.
public IlvMapFeature getNextFeature()
throws IOException
{
return readPolyline();
}
The polyline points are read by the private method readPolyline
.
This method reads each line in the file to extract the coordinates
of the points and the related comments, if any.
It is broken up as follows:
A geometry of the type
IlvMapLineString is
created, which will be associated with the map feature.
private IlvMapFeature readPolyline()
throws IOException
{
// Concatenates all the comment lines.
StringBuffer buffer = new StringBuffer();
// Reads the current map feature.
IlvMapFeature feature = new IlvMapFeature();
// Reads the current line string geometry.
IlvMapLineString geometry = new IlvMapLineString();
The points making up this line string
are read and the related comment is stored as an attribute.
// Stores the line of text that is read.
String line;
// Reads a line.
while ((line = file.readLine()) != null) {
[...]
The longitude coordinate values are
read. Note that an exception of type
IlvMapFormatException is thrown if a format error is detected while reading.
// Process longitude.
IlvCoordinate c = new IlvCoordinate();
if (tokenizer.hasMoreElements() == false)
throw new IlvMapFormatException("Longitude coordinate expected");
try {
currentToken = (String)tokenizer.nextElement();
c.x = decimalParser.parse(currentToken).doubleValue();
} catch (ParseException e) {
throw new IlvMapFormatException("Error while parsing longitude");
}
These comments also apply to latitude coordinates.
Each point read from the file is added
to the line string geometry.
// Add this point to geometry.
geometry.addPoint(c);
[...]
The following if
statement
tests whether the end of the file has been reached. In this case,
the getNextFeature
method should return
a null
pointer.
// End of file.
if ((line == null) && (geometry.getPointCount() == 0))
return null;
The geometry is associated with the
map feature.
// Initialize the map feature.
feature.setGeometry(geometry);
The comments are extracted to form attributes,
which are associated with the map feature. The attributeInfo
object,
which is shared by the attributes of the map features, was initialized
in the reader’s constructor.
// Set attribute.
IlvStringAttribute[] attribute = new IlvStringAttribute[1];
if (buffer.length() > 0) {
attribute[0] = new IlvStringAttribute();
attribute[0].setString(buffer.toString());
} else {
attribute[0] = null;
}
feature.setAttributeInfo(attributeInfo);
feature.setAttributes(new IlvFeatureAttributeProperty(attributeInfo,
attribute));
The read map feature is returned.
// Returns the read map feature.
return feature;
}