| Programming with JViews Maps > Creating a Map Application Using the API > Using Readers > Writing a New Reader |
Writing a New Reader |
INDEX
PREVIOUS
NEXT
|
In 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 ASCII file to be read has been created especially for this example. Its format is very simple and its specifications are as follows:
.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 |
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:
<installdir>/jviews-maps81/codefragments/newreader/srchtml/java/SimplePolylineReader.java
| Note |
| Only the portions of code that require comments are reproduced here. |
As shown below, the SimplePolylineReader implements the IlvMapFeatureIterator interface:
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; } |
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; } |
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 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:
IlvMapLineString is created, which will be associated with the map feature. // Stores the line of text that is read. String line; // Reads a line. while ((line = file.readLine()) != null) { [...] |
IlvMapFormatException is thrown if a format error is detected while reading.// Add this point to geometry. geometry.addPoint(c); [...] |
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; |
// Initialize the map feature. feature.setGeometry(geometry); |
attributeInfo object, which is shared by the attributes of the map features, was initialized in the reader's constructor. // Returns the read map feature. return feature; } |
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. Legal terms. | PREVIOUS NEXT |