Jump to content

Convert DegMinSec to Decimal Degrees

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
neophyte

neophyte

    Newbie

  • Members
  • Pip
  • 6 posts
Hello,

I'm new to Java but have one college semester of C under my belt. My IDE is NetBeans 6.5, which I really like. I'm trying to write a function that converts a number in DegMinSec format (sexagesimal) to decimal degrees in order to perform trig functions (sin, cos, tan) on it. I have copied and pasted the code that I have come up with so far. I changed the input (double deg_min_sec) several times to check the results but I haven't been 100 % successful. My post contains the output below the code so you can see what's going on. Any suggestions would be greatly appreciated! FYI, I'm not a college student looking for someone to do my homework. I just do this as a hobby.

Regards,

Mark

package dms_to_dd;


/*

 * Date: 12 June 2009

 * Author: neophyte

 * Purpose: Convert DMS to DD

 */


public class DMS_to_DD {


   public static void main(String[] args) {


      int degrees = 0;   /* integer part DMS */

      double deg_min_sec = 36.5212;   /* DMS */

      double minutes_seconds = 0;   /* fractional part DMS */

      double minutes = 0, seconds = 0;   /* minutes, seconds DD */

      

      degrees = (int) deg_min_sec;

      minutes_seconds = deg_min_sec - degrees;

      String min_sec = Double.toString(minutes_seconds);

      String min = min_sec.substring(2,4);   /* minutes */

      String sec = min_sec.substring(4,6);   /* seconds */

      minutes = Double.valueOf(min) / 60;   /* minutes DD */

      seconds = Double.valueOf(sec) / 3600;   /* seconds DD */


      System.out.format("%.4f%n", degrees + minutes + seconds);

      }

}

----------------------------------------
double deg_min_sec = 36.5212;
run:
36.8700 /* correct */
BUILD SUCCESSFUL (total time: 0 seconds)
-----------------------------------------
double deg_min_sec = 90.0000;
run:
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException:
String index out of range: 4
at java.lang.String.substring(String.java:1935)
at dms_to_dd.DMS_to_DD.main(DMS_to_DD.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
-----------------------------------------
double deg_min_sec = 0.2015;
run:
0.3375 /* correct */
BUILD SUCCESSFUL (total time: 0 seconds)
-----------------------------------------
double deg_min_sec = 90.0006;
run:
90.0000 /* wrong! */
BUILD SUCCESSFUL (total time: 2 seconds)

Edited by WingedPanther, 20 June 2009 - 09:06 AM.
add code tags (the # button)


#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
I don't what the hell I have done, so lol...
public class DMS_to_DD {


   public static void main(String[] args) {


      int degrees = 0;   /* integer part DMS */

      double deg_min_sec = 36.5212;   /* DMS */

      double minutes_seconds = 0;   /* fractional part DMS */

      double minutes = 0, seconds = 0;   /* minutes, seconds DD */

      

      degrees = (int) deg_min_sec;

      minutes_seconds = deg_min_sec - degrees;

      String min_sec = Double.toString(minutes_seconds);

      

     if(min_sec.equals("0.0")) {

    	  String min = min_sec.substring(0,1);   /* minutes */

          String sec = min_sec.substring(0,2);   /* seconds */

          minutes = Double.valueOf(min) / (double)60;   /* minutes DD */

          seconds = Double.valueOf(sec) / (double)3600;   /* seconds DD */

      }

      else if(!min_sec.equals("0.0")) {

      String min = min_sec.substring(2,4);   /* minutes */

      String sec = min_sec.substring(4,6);   /* seconds */

      minutes = Double.valueOf(min) / (double)60;   /* minutes DD */

      seconds = Double.valueOf(sec) / (double)3600;   /* seconds DD */

      }

      System.out.format("%.4f%n", degrees + minutes + seconds);

      }

}
Now you should be able to do 90.000000000000000000000000000
Technically, just putting in 90 or 30 makes min_sec = 0.0, which results to string out of etc... for your substringing...
I can't more due to I have no idea what I am doing...
Posted Image

#3
neophyte

neophyte

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks Turk4n! That solves the problem with using fractional parts equaling zero. Still, I don't know why 90.0006 converts to 90.0000 and not to 90.0017.

#4
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

neophyte said:

Thanks Turk4n! That solves the problem with using fractional parts equaling zero. Still, I don't know why 90.0006 converts to 90.0000 and not to 90.0017.

You can't substring... 0.0006 to 0.0006 and that should equals to 90.0017...

Take a look here and try out their program, you will see that 90.0006 will equal to 90.0000.
Degrees, Minutes, Seconds

P.S - Don't thank me I didn't know what the hell I was doing :)

Cheers!
Posted Image

#5
neophyte

neophyte

    Newbie

  • Members
  • Pip
  • 6 posts
Well, I've abandoned the idea of parsing a string to do the conversion; it was getting too messy. Instead, I wrote something else that works 100% with all of the input I've tried. I would appreciate any constructive criticism from the community.

package dms_to_dd;

/*

 * Date: 21 June 2009

 * Author: neophyte

 * Purpose: Convert DMS to DD

 */


public class DMS_to_DD {


   public static void main(String[] args) {


      int deg = 0;

      double dms = 36.5212;

      double frac_part = 0, min_sec = 0, min = 0, sec = 0, min_dd = 0, sec_dd;


      deg = (int) dms;   /* 36 */

      frac_part = dms - deg;   /* 0.5212 */

      min_sec = frac_part * 100;   /* 52.12 */

      min = (int) min_sec;   /* 52 */

      sec = (min_sec - min) * 100;   /* 12 */

      min_dd = min / 60;   /* 0.8667 */

      sec_dd = sec / 3600;   /* 0.0033 */


      System.out.format("%.4f%n", deg + min_dd + sec_dd);

      }

}

Edited by WingedPanther, 21 June 2009 - 04:21 PM.


#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Looks good, but please use code tags (the # button) when posting code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
neophyte

neophyte

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks for the thumbs up WingedPanther! I noticed someone has been tidying up my code so it doesn't appear all left justified. How do I use the code tags and where do I find them?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
When you're editing a message, press the # button. Then put your code inside the tags that appear.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog