// ReferencePoint.java
//
// Copyright 1998 & 1999 Henrik Bjorkman. All rights reserved.
// No responsibilities taken. Use on your own risk
// www.stacken.kth.se/~bjorkman
//
// This class stores one reference point.
//
// Some static functions for parsing latitudes and
// longitudes are also found here.
//
//
// History
//
// 1999-06-19
// Moved class ReferencePoint to its own file.
// Henrik Bjorkman
//
// 1999-08-07
// Added an error message if Lat/Lng parse failed.
// Henrik Bjorkman
//
// 2000-06-13
// Changed package name to chartplotter_package.
// Henrik Bjorkman
//

package chartplotter_package;

import java.io.*;
import java.awt.*;
import java.text.*;




public class ReferencePoint
{
  public int x,y; /* in pixels */
  public double lat,lng; /* in minutes */

  public static final double MAX_NORTH=90*60;
  public static final double MAX_SOUTH=-90*60;
  public static final double MAX_WEST=-180*60;
  public static final double MAX_EAST=180*60;

  static void error(String s)
  {
    System.err.println("ReferencePoint: " + s);
    System.exit(0);
  }

  static void debug(String s)
  {
    //System.out.println("ReferencePoint: " + s);
  }

  public static int numLen(String str)
  {
    int i=0;
    while (i<str.length() && Character.isDigit(str.charAt(i))) {i++;}
    return(i);
  }

  static public String toStringLatLng(double minutes, char pos, char neg)
  {
    char sign=pos;
    int l=(new Double(minutes*1000)).intValue();
    if (l<0) {l=-l;sign=neg;}
    int d=l/60000;
    int m=(l%60000)/1000;
    int m1000=l%1000;
    return(ChartWord.addZero(""+d,2)+sign+
     ChartWord.addZero(""+m,2)+"."+
     ChartWord.addZero(""+m1000,3));
  }

  static public double parseLatLng(String str, char pos, char neg) throws NumberFormatException
  {
    int l=numLen(str);

    if ((l==0) || (l==str.length())) 
    {
      throw new NumberFormatException("No lat/lng found in: "+str);
    }

    double hr=Double.valueOf(str.substring(0,l)).doubleValue();
    double min=Double.valueOf(str.substring(l+1)).doubleValue();

    if (hr>180.1)
    {
      throw new NumberFormatException("Out of range lat/lng: "+str);
    }

    if (min>60.1) 
    {
      throw new NumberFormatException("Bad minutes in lat/lng: "+str);
    }

    double m=hr*60d+min;

    if (str.charAt(l)==neg)
    {
      m=-m;
    }
    else if (str.charAt(l)!=pos)
    {
      throw new NumberFormatException("Wrong format lat/lng: "+str);
    }

    return(m);
  }

  public void setLat(String str)
  {
    lat=parseLatLng(str,'N','S');    
  }

  public void setLng(String str)
  {
    lng=parseLatLng(str,'E','W');
  }

  public String toString()
  {
    return(""+x+" "+y+" "+toStringLatLng(lat,'N','S')+" "+toStringLatLng(lng,'E','W'));
  }

  public ReferencePoint(String str)
  {
    debug("str: "+str);
    x=Integer.parseInt(ChartWord.getWord(str,0));
    y=Integer.parseInt(ChartWord.getWord(str,1));
    setLat(ChartWord.getWord(str,2));
    setLng(ChartWord.getWord(str,3));
    debug("x "+x+" y "+y+" lat "+lat+ " lng "+lng);
  }
}


