/*
 * 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.IlvManagerView;
import ilog.views.maps.IlvFeatureRenderer;
import ilog.views.maps.IlvMapFeature;
import ilog.views.maps.IlvMapRenderException;
import ilog.views.maps.format.shapefile.IlvShapeFileReader;
import ilog.views.maps.format.shapefile.IlvShapeFileTileLoader;
import ilog.views.maps.srs.coordsys.IlvCoordinateSystem;
import ilog.views.maps.srs.coordsys.IlvGeographicCoordinateSystem;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;
import ilog.views.tiling.IlvTiledLayer;
import ilog.views.util.IlvProductUtil;

/**
 * Example of how to use the ShapeFile reader package.
 */
public class ShapeReader {
  IlvManager manager = new IlvManager();
  IlvManagerView view = new IlvManagerView(manager);

  /**
   * 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();
    }
    // Retreive 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 have then 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 on tile 0, 0.
    tiledLayer.fitTransformerToTile(view, 0, 0);
  }

  public static void main(String a[]) {
    // This sample uses JViews Maps features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Maps Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Maps_Deployment);

    ShapeReader reader = new ShapeReader();
    if (a.length == 0) {// no arguments, lets specify some
      a = new String[] { "data/myShapeFile.shp", null };
    }
    if (a.length == 2) {// load the shape file and the DBF data.
      try {
        reader.loadFile(a[0], a[1]);
        System.out.println("reader loaded " + reader.manager.getCardinal() + " objects");
      } catch (IOException e) {
        e.printStackTrace();
      } catch (IlvMapRenderException e) {
        e.printStackTrace();
      } catch (IlvCoordinateTransformationException e) {
        e.printStackTrace();
      }
    }
    if (a.length == 4) {// load the shape file on demand.
      try {
        reader.loadLOD(a[0], a[1], a[2], a[3]);
        System.out.println("reader loaded " + reader.manager.getCardinal() + " objects");
      } catch (IOException e1) {
        e1.printStackTrace();
      }
    }
  }
}