In addition to the predefined readers available in JViews Maps, you can write
your own reader and customize it.
This section contains an example of an IlvMapFeatureIterator that you can use to
read polylines that were saved in an ASCII file.
Note
The classes that implement the
IlvMapFeatureIterator
interface are not necessarily file readers. They can also iterate,
for example, over the result of a query to a map server.
The file to be read
The ASCII file to be read has been created
especially for this example. Its format is very simple and its
specifications are as follows:
- It has a header specifying its format.
- There is one pair of coordinates (latitude and longitude) per line. These coordinates are expressed in degrees.
- Lines can contain comments. These comments, when they exist, are merged to form an attribute.
- Polylines are separated by a blank line.
- The file has the
.pol
extension.
The ASCII file is shown below:
ascii polylines -1.0 40.0 A 1x1 degree rectangle centered on the 1.0 40.0 (0,39) point 1.0 38.0 -1.0 38.0 -1.0 40.0 0.0 90.0 A meridian extending from the North pole to the South pole 0.0 -90.0
The reader
This section shows the reader you can use
to read this polyline file.
The complete source code for this example
can be found in the following file:
Note
Only the portions of code that require
comments are reproduced here.
As shown below, the
SimplePolylineReader
implements the IlvMapFeatureIterator interface:
class SimplePolygonReader implements IlvMapFeatureIterator { ... member variables ... /** * Constructor */ public SimplePolygonReader (String fileName) throws FileNotFoundException { Initializing member variables } ... methods ... }
The georeferencing methods
Since latitude and longitude in the polyline file are expressed
in degrees, the coordinate system is geographic. This is why the
isGeoreferenced method returns
true
and the getCoordinateSystem method returns
IlvGeographicCoordinateSystem.WGS84
. The
getCoordinateSystem
method would return
null
if the projection of the file to be read was unknown. See the
description of the
isGeoreferenced
method in The IlvMapFeatureIterator interface.
public boolean isGeoreferenced() { return true; } public IlvCoordinateSystem getCoordinateSystem() { return IlvGeographicCoordinateSystem.WGS84; }
Bounding box methods
Because of the data format, the bounding box of the polyline
cannot be retrieved until the data has been read. Here, the
methods getUpperLeftCorner and getLowerRightCorner return
null
to indicate that these points are not known. Another option is to
read the data, place it in an array, and then compute the
bounding box.
public IlvCoordinate getLowerRightCorner() { return null; }
Rendering methods
The getDefaultFeatureRenderer method must
return a renderer able to transform into graphic objects all the
map features read by this feature iterator. The IlvDefaultCurveRenderer can process map
features whose geometry is of type IlvMapLineString.
public IlvFeatureRenderer getDefaultFeatureRenderer() { return new IlvDefaultCurveRenderer(); }
If the geometries of the returned map features are not predefined
but instead are instances of a derived class, or if the map
feature attributes store drawing parameters to be used in
rendering operations such as color or line width, it is necessary
to provide renderers that can process these attributes or derived
geometries. See the section Creating a colored line renderer.
The getNextFeature method
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, thegetNextFeature
method should return anull
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; }