package org.mwc.debrief.lite.gui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import org.geotools.geometry.DirectPosition2D;
import org.geotools.map.MapContent;
import org.geotools.map.MapViewport;
import org.geotools.referencing.CRS;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

import Debrief.GUI.Frames.Application;
import MWC.Algorithms.PlainProjection;
import MWC.GUI.Layers;
import MWC.GUI.ToolParent;
import MWC.GenericData.WorldArea;
import MWC.GenericData.WorldLocation;

public class GeoToolMapProjection extends PlainProjection
{
  private static final String WORLD_PROJECTION = "EPSG:3395"; // 3395 for Mercator proj
  private static final String DATA_PROJECTION = "EPSG:4326";
  /**
   *
   */
  private static final long serialVersionUID = 3398817999418475368L;
  private MathTransform _degs2metres;
  private final MapViewport _view;
  private final DirectPosition2D _workDegs;
  private final DirectPosition2D _workMetres;
  private final DirectPosition2D _workScreen;
  private final Layers _layers;

  public GeoToolMapProjection(final MapContent map, final Layers data)
  {
    super("GeoTools Map");
    _view = map.getViewport();
    _layers = data;

    // initialise our working data stores
    _workDegs = new DirectPosition2D();
    _workMetres = new DirectPosition2D();
    _workScreen = new DirectPosition2D();
    // we'll tell GeoTools to use the projection that's used by most of our
    // charts,
    // so that the chart will be displayed undistorted
    try
    {
      final CoordinateReferenceSystem worldCoords = CRS.decode(
          WORLD_PROJECTION);

      // we also need a way to convert a location in degrees to that used by
      // the charts (metres)
      final CoordinateReferenceSystem worldDegs = CRS.decode(DATA_PROJECTION);
      _degs2metres = CRS.findMathTransform(worldDegs, worldCoords);
    }
    catch (final FactoryException e)
    {
      e.printStackTrace();
    }
  }

  @Override
  public WorldArea getDataArea()
  {
    return _layers.getBounds();
  }

  @Override
  public Dimension getScreenArea()
  {
    final Rectangle rect = _view.getScreenArea();
    return new Dimension((int) rect.getWidth(), (int) rect.getHeight());
  }

  @Override
  public WorldArea getVisibleDataArea()
  {
    return _layers.getBounds();
  }

  @Override
  public Point toScreen(final WorldLocation val)
  {
    Point res = null;
    // and now for the actual projection bit
    _workDegs.setLocation(val.getLong(), val.getLat());
    _view.getWorldToScreen().transform(_workDegs, _workScreen);
    // output the results
    res = new Point((int) _workScreen.getCoordinate()[0], (int) _workScreen
        .getCoordinate()[1]);
    return res;
  }

  @Override
  public WorldLocation toWorld(final Point val)
  {
    WorldLocation res = null;
    _workScreen.setLocation(val.x, val.y);
    try
    {
      // hmm, do we have an area?
      final WorldArea dArea = this.getDataArea();
      if (dArea.getWidth() > 0 || dArea.getHeight() > 0)
      {
        // now got to screen
        final AffineTransform currentTransform = _view.getScreenToWorld();
        if (currentTransform != null)
        {
          currentTransform.transform(_workScreen, _workDegs);
        }
        res = new WorldLocation(_workDegs.getCoordinate()[1], _workDegs
            .getCoordinate()[0], 0);
      }
    }
    catch (final MismatchedDimensionException e)
    {
      Application.logError2(ToolParent.ERROR,
          "Whilst trying to set convert to world coords", e);
    }
    return res;
  }

  @Override
  public void zoom(final double value)
  {
  }
}
@@ -487,127 +487,8 @@
 em;%0D
-%0Aimport org.opengis.referencing.operation.MathTransform;%0D%0Aimport org.opengis.referencing.operation.TransformException;%0D
 %0A%0D%0Ai
@@ -1001,47 +1001,8 @@
 L;%0D%0A
-  private MathTransform _degs2metres;%0D%0A
   pr
@@ -1082,55 +1082,8 @@
 s;%0D%0A
-  private final DirectPosition2D _workMetres;%0D%0A
   pr
@@ -1411,22 +1411,8 @@
 %0A   
- _workMetres =
  new
@@ -1930,28 +1930,13 @@
 );%0D%0A
+
      
- _degs2metres =
  CRS

remove unnecessary transformation