Code examples for using readers

For more information on the predefined readers, see Readers and writers.

The Shapefile reader

The complete source code for this example can be found in the following file:
/**
* Simple file reader for ESRI shape file format. 
* Attributes are read from the dbf file.
*/
  public void loadFile(String shapeFileName, String dbfFileName)
  throws IOException, 
         IlvMapRenderException, 
         IlvCoordinateTransformationException {
         
    IlvShapeFileReader reader = null;
    // Instantiate a new reader.
    try {
      reader = new IlvShapeFileReader(shapeFileName, dbfFileName);
    } catch (IOException e) {
      e.printStackTrace();
    }
    // Retrieve the default feature renderer.
    IlvFeatureRenderer renderer = reader.getDefaultFeatureRenderer();

     //  Coordinate Transformation from IlvGeographicCoordinateSystem.WGS84
    // to IlvGeographicCoordinateSystem.WGS84 (no transformation)
    IlvCoordinateSystem sourceCoordinateSystem = 
                           IlvGeographicCoordinateSystem.WGS84;
    IlvCoordinateSystem targetCoordinateSystem = 
                           IlvGeographicCoordinateSystem.WGS84;
    IlvCoordinateTransformation transform = 
              IlvCoordinateTransformation.CreateTransformation
                (sourceCoordinateSystem, targetCoordinateSystem);
    IlvMapFeature mapFeature;
    // Loop on all map features.
    while ((mapFeature = reader.getNextFeature()) != null) {
      IlvGraphic graphic = renderer.makeGraphic(mapFeature, transform);
      if (mapFeature.getAttributes() != null) 
        graphic.setNamedProperty(mapFeature.getAttributes().copy());
      // IlvGraphic has to be stored in an IlvManager.
      manager.addObject(graphic, false);
    }
  }

/**
* Create a Shape load-on-demand layer.
*/
  public void loadLOD(String shpFileName, 
                      String dbfFileName, 
                      String shxFileName, 
                      String idxFileName)
  throws IOException {
  
    // Instantiate tile loader.
    IlvShapeFileTileLoader shpTileLoader = new IlvShapeFileTileLoader(
        shpFileName, 
        dbfFileName, 
        shxFileName, 
        idxFileName);
    // instantiate tiled layer.
    IlvTiledLayer tiledLayer = 
             new IlvTiledLayer(shpTileLoader.getTileOrigin());
    // Affect tile loader.
    tiledLayer.setTileLoader(shpTileLoader);
    // Add the layer to the IlvManager.
    manager.addLayer(tiledLayer, 0);
    // Fit to tile 0, 0.
    tiledLayer.fitTransformerToTile(view, 0, 0);
  }

The MID/MIF reader

The complete source code for this example can be found in the following file:
/**
 * Example of how to use the MIDMIF reader package.
 */
public class MIDMIFReader {
  IlvManager manager = new IlvManager();

  /**
    * Load a MIDMIF file by providing the MIF file name and the MID file name.
   */
  public void loadMIDMIF(String mif, String mid)
  throws IlvMapFormatException, 
         IOException, 
         IlvMapRenderException, 
         IlvCoordinateTransformationException {

    // Create the MIDMIF reader.
    IlvMapFeatureIterator reader = new IlvMIDMIFReader(mif, mid);
    // Retrieve the default renderer. 
    IlvFeatureRenderer renderer = reader.getDefaultFeatureRenderer();
    // Initialize the coordinate transformation.
    IlvCoordinateSystem managerCS =
        IlvCoordinateSystemProperty.GetCoordinateSystem(manager);
    // The MIDMIF file can contain coordinate system information.
    IlvCoordinateSystem fileCS = reader.getCoordinateSystem();
    if ((managerCS == null) && (fileCS != null)) 
      manager.setNamedProperty(new IlvCoordinateSystemProperty(fileCS));
    // Create the transformation accordingly.
    IlvCoordinateTransformation tr = 
           IlvCoordinateTransformation.CreateTransformation(fileCS, managerCS);
    // Retrieve the first feature.
    IlvMapFeature f = reader.getNextFeature();
    int layersCount = manager.getLayersCount();
    manager.setContentsAdjusting(true);
    while (f != null) {
      // Create graphic object.
      IlvGraphic g = renderer.makeGraphic(f, tr);
      if (g != null) {
        // Add it to the manager.
        manager.addObject(g, layersCount, false);
        // Retrieve attributes.
        IlvFeatureAttributeProperty ap = f.getAttributes();
        if (ap != null) {
          // Copy the attributes into the graphic, if any.
          g.setNamedProperty(ap.copy()); 
        }
      }
      // Loop on all features.
      f = reader.getNextFeature();
    }
  }

The DTED file reader

The complete source code for the DTED® file reader example can be found in the following file:
/**
 * Example of how to use the DTED reader package. 
 */
public class DTEDReader {
  IlvManager manager = new IlvManager();
  IlvManagerView view = new IlvManagerView(manager);
  
  /**
   * Loads a single DTED tile.
   */
  public IlvGraphic loadSingleFrame(String fileName) 
  throws IlvMapFormatException, 
         FileNotFoundException, 
         IOException, 
         IlvMapRenderException, 
         IlvCoordinateTransformationException {
    // Instantiate the reader.
    IlvDTEDReader reader = new IlvDTEDReader(fileName);
    // Retrieve the unique feature.
    IlvMapFeature feature = reader.getNextFeature();
    // Retrieve the image renderer.
    IlvFeatureRenderer renderer = reader.getDefaultFeatureRenderer();
    // Transformation.
    IlvGeographicCoordinateSystem gcs = IlvGeographicCoordinateSystem.WGS84;
    IlvCoordinateTransformation tr = 
               IlvCoordinateTransformation.CreateTransformation
               (feature.getCoordinateSystem(),gcs);
    // Make the graphic object to be inserted in a IlvManager. 
    IlvGraphic graphic = renderer.makeGraphic(feature, tr);
    // Returns it.
    return graphic;
  }
  
  private int level = 0;
  
  /**
   * Create a load-on-demand DTED layer providing the directory name. 
   */
  public void makeLODLayer(String dirName) {
    // New DTED layer.
    IlvDTEDLayer layer = new IlvDTEDLayer(dirName, level);
    // Add it to the manager.
    manager.addLayer(layer, -1);
    // Center on tile (0, 0).
    layer.fitTransformerToTile(view, 0, 0);
  }
  
  public static void main(String a[]) {
    javax.swing.SwingUtilities.invokeLater(
     new Runnable() {
      public void run() {
        DTEDReader reader = new DTEDReader();
        try {
          reader.loadSingleFrame(a[0]);
        } catch (IlvMapFormatException e) {
          e.printStackTrace();
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } catch (IlvMapRenderException e) {
          e.printStackTrace();
        } catch (IlvCoordinateTransformationException e) {
          e.printStackTrace();
        }
      }
    }
   );
  }
}

The image file reader

The complete source code for this example can be found in the following file:
/**
 * Example showing how to use the Image reader package.
 */
public class ImageReader {
  IlvManager manager = new IlvManager();
  
  /**
   * Load a single image. Upper left and lower right coordinates 
   * have to be provided.
   */
  public void loadImage(String imageName, IlvCoordinate ul, IlvCoordinate lr) 
  throws IlvMapFormatException, 
         FileNotFoundException, 
         IOException, 
         IlvMapRenderException,
         IlvCoordinateTransformationException {
    // Create image reader.
    IlvImageReader reader = new IlvImageReader(imageName, ul, lr);
    // Retrieve the unique feature.
    IlvMapFeature feature = reader.getNextFeature();
    // Retrieve the default renderer.
    IlvFeatureRenderer renderer = reader.getDefaultFeatureRenderer();
    // No reprojection for images.
    IlvCoordinateTransformation tr = 
      new IlvCoordinateTransformation(IlvGeographicCoordinateSystem.WGS84,
                                      IlvGeographicCoordinateSystem.WGS84,
                                      new IlvMapAffineTransform());
    // Create the graphic object.
    IlvGraphic graphic = renderer.makeGraphic(feature, tr);
  }
  
  /**
   * Create a load-on-demand image layer.
    * The pattern, colFmt, rowFmt are used to retrieve the image file name 
   * from the tile coordinates 
   */
  public void loadLOD(IlvRect tileOrigin, 
                      String pattern, 
                      String colFmt, 
                      String rowFmt) 
  {
    // Create the tile loader.
    IlvImageTileLoader tileLoader = new IlvImageTileLoader(pattern, 
                                                       colFmt,
                                                       rowFmt);
    // Create the tiled layer.
    IlvTiledLayer tiledLayer = new IlvTiledLayer(tileOrigin);
    // Affect the tile loader to the tiled layer.
    tiledLayer.setTileLoader(tileLoader);
    // Add the layer into the manager.
    manager.addLayer(tiledLayer, -1);
  }