/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

import java.io.IOException;

import ilog.views.IlvGraphic;
import ilog.views.IlvManager;
import ilog.views.maps.IlvFeatureRenderer;
import ilog.views.maps.IlvMapFeature;
import ilog.views.maps.IlvMapFeatureIterator;
import ilog.views.maps.IlvMapRenderException;
import ilog.views.maps.format.IlvMapLoader;
import ilog.views.maps.format.shapefile.IlvShapeFileReader;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;

/**
 * Example showing generic way to use a JViews Maps reader.
 */
public class GenericReader {
  IlvMapFeatureIterator featureIterator;
  IlvManager manager;
  int layerIndex;
  String shapeFileName;
  String dbfFileName;

  public GenericReader(String shapeFileName, String dbfFileName) {
    manager = new IlvManager();
    this.shapeFileName = shapeFileName;
    this.dbfFileName = dbfFileName;
  }

  /**
   * Create a Shape file reader as an example of a generic reader.
   */
  public void useReader() {
    try {
      // Instantiate the reader. As an example, we use the IlvShapeFileReader.
      featureIterator = new IlvShapeFileReader(shapeFileName, dbfFileName);
    } catch (IOException e) {
      System.err.println("IOError while instantiating reader");
    }
    // Retrieve the feature renderer from the reader.
    IlvFeatureRenderer renderer = featureIterator.getDefaultFeatureRenderer();

    // No transformation.
    IlvCoordinateTransformation identity = IlvCoordinateTransformation.CreateTransformation(null, null);

    // Retrieve the first map feature.
    IlvMapFeature feature = null;
    try {
      feature = featureIterator.getNextFeature();
    } catch (IOException io) {
      System.err.println("IOExeption while getting next feature" + io.getMessage());
    }

    // Loop on all the available map features.
    while (feature != null) {
      try {
        // Create the graphic object representing the map feature.
        IlvGraphic graphic = renderer.makeGraphic(feature, identity);

        // Add the graphic object into the manager.
        manager.addObject(graphic, layerIndex, false);

        // Handle exceptions.
      } catch (IlvMapRenderException e) {
        // Should not occur: renderer provided by the feature iterator.
        System.out.println("Rendering Exception " + e.getMessage());
      } catch (IlvCoordinateTransformationException cte) {
        System.err.println("Coordinate transformation exeception " + cte.getMessage());
      }
    }
  }

  /**
   * Use of the IlvMapLoader to read a simple file.
   */
  public void useMapLoader() {
    IlvMapLoader mapLoader = new IlvMapLoader(manager);
    try {
      mapLoader.load(shapeFileName);
    } catch (IOException e) {
      System.err.println("IOExeption while loading " + e.getMessage());
    }
  }

  /**
   * Use of the IlvMapLoader and IlvFeatureIterator to read a file.
   */
  public void useMapLoaderAndFeatureIterator() {
    IlvMapLoader mapLoader = new IlvMapLoader(manager);
    try {
      mapLoader.load(featureIterator);
    } catch (IOException e) {
      System.err.println("IOExeption while loading " + e.getMessage());
    }
  }

}