John Allen - GEO.Coordinate-0.03

Documentation | Source

NAME

GEO.Coordinate - Geographic coordinate manipulation class

SYNOPSIS

    // Import From JSAN
    JSAN.use( 'GEO.Coordinate' );

        coord = new GEO.Coordinate(51.5007);
        coord.toDMS();

        coord = new GEO.Coordinate(51, 30, 2.52);
        coord.toDecDeg();

DESCRIPTION

GEO.Coordinate is a class for manipulating geographic corodinates.

CONSTRUCTOR

  var coord = new GEO.Coordinate(decimaldegrees);
  var coord = new GEO.Coordinate(degrees, decimalminutes);
  var coord = new GEO.Coordinate(degrees, minutes, seconds);

Returns a new GEO.Coordinate object. The following parameters can be passed to the constructor:

METHODS

toDMS(precision)

This method will return the coordinate in degrees, minutes and seconds, the seconds will be returned at the specified precision.

Example:

        var coord = new GEO.Coordinate(51.5007);
        coord.toDMS(2);

Returns: 51 30' 2.52"

toDecDeg(precision)

This method will return the coordinate in decimal degrees, the decimals will be returned at the specified precision.

Example:

        var coord = new GEO.Coordinate(51, 30, 2.52);
        coord.toDecDeg(2);

Returns: 51.50

toDecMin(precision)

This method will return the coordinate in decimal minutes, the minute decimals will be returned at the specified precision.

Example:

        var coord = new GEO.Coordinate(51, 30, 2.52);
        coord.toDecMin(2);

Returns: 51 30.04'

AUTHOR

John Allen <john@thoofooz.com>

COPYRIGHT

  Copyright (c) 2005 John Allen.  All rights reserved.
  This library is free software; you can redistribute it and/or modify it 
  under the same terms as Perl.

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 37:

=over should be: '=over' or '=over positive_number'

Around line 124:

You forgot a '=back' before '=head1'

/*
 * A JavaScript geographic coordinate manipulation class.
 * Version 0.03 Copyright (C) John Allen <john@thoofooz.com>.
 * Distributed under the Perl License
 */

/*

=head1 NAME

GEO.Coordinate - Geographic coordinate manipulation class

=head1 SYNOPSIS

    // Import From JSAN
    JSAN.use( 'GEO.Coordinate' );

	coord = new GEO.Coordinate(51.5007);
	coord.toDMS();

	coord = new GEO.Coordinate(51, 30, 2.52);
	coord.toDecDeg();

=head1 DESCRIPTION

GEO.Coordinate is a class for manipulating geographic corodinates.
	
=head1 CONSTRUCTOR

  var coord = new GEO.Coordinate(decimaldegrees);
  var coord = new GEO.Coordinate(degrees, decimalminutes);
  var coord = new GEO.Coordinate(degrees, minutes, seconds);

Returns a new C<GEO.Coordinate> object. The following parameters can be
passed to the constructor:

=over 4
=item *
one parameter a coordinate in decimal degrees e.g. 51.5007
=item *
two parameters are decimal minutes e.g. 51 30.04
=item *
three parameters are degrees, minutes and seconds e.g. 51 30 2.52
=back

=cut

*/

if (!GEO) var GEO = {};

GEO.Coordinate = function (p1, p2, p3) {
	if (p1!=null && p2!=null && p3!=null) {
		this._deg = parseFloat(p1);
		this._min = parseFloat(p2);
		this._sec = parseFloat(p3);
		this._dmstodecdeg();
		this._dmstodecmin();
		if (p1.toString().match(/-0/g)) {
			// this is a negative parameter, eg.
			this._dec_deg_deg = this._dec_deg_deg*-1
			this._decdegtodms();
			this._dmstodecmin();
		}
	} else {
		if (p1!=null && p2!=null) {
			this._decmin_deg = parseFloat(p1);
			this._decmin_min = parseFloat(p2);
			this._decmintodecdeg();
			this._decdegtodms();
			if (p1.toString().match(/-0/g)) {
				// this is a negative parameter, eg.
				this._dec_deg_deg = this._dec_deg_deg*-1
				this._decdegtodms();
				this._dmstodecmin();
			}
		} else {
			this._dec_deg_deg = parseFloat(p1);
			this._decdegtodms();
			this._dmstodecmin();
		}
	}
};

GEO.Coordinate.VERSION = '0.03';

GEO.Coordinate.prototype = {
	_deg: null,
	_min: null,
	_sec: null,
	_decmin_deg: null,
	_decmin_min: null,
	_dec_deg_deg: null,

	_dmstodecdeg: function () {
		if (this._deg >= 0) {
			this._dec_deg_deg = this._deg + this._min/60 + this._sec/3600;
		} else {
			this._dec_deg_deg = this._deg - this._min/60 - this._sec/3600;
		}
	},

	_dmstodecmin: function () {
		this._decmin_deg = parseInt(this._dec_deg_deg);
		this._decmin_min = (Math.abs(this._dec_deg_deg - this._deg) * 3600) / 60;
	},

	_decmintodecdeg: function () {
		if (this._decmin_deg >= 0) {
			this._dec_deg_deg = this._decmin_deg + this._decmin_min/60;
		} else {
			this._dec_deg_deg = this._decmin_deg - this._decmin_min/60;
		}
	},

	_decdegtodms: function () {
		this._deg = parseInt(this._dec_deg_deg);
		this._min = parseInt((Math.abs(this._dec_deg_deg - this._deg) * 3600) / 60);
		this._sec = (Math.abs(this._dec_deg_deg - this._deg) * 3600) - (this._min * 60);
	},

/*

=head1 METHODS

=head2 toDMS(precision)

This method will return the coordinate in degrees, minutes and seconds, the 
seconds will be returned at the specified precision.

Example:

	var coord = new GEO.Coordinate(51.5007);
	coord.toDMS(2);

Returns: B<51 30' 2.52">

=head2 toDecDeg(precision)

This method will return the coordinate in decimal degrees, the 
decimals will be returned at the specified precision.

Example:

	var coord = new GEO.Coordinate(51, 30, 2.52);
	coord.toDecDeg(2);

Returns: B<51.50>

=head2 toDecMin(precision)

This method will return the coordinate in decimal minutes, the 
minute decimals will be returned at the specified precision.

Example:

	var coord = new GEO.Coordinate(51, 30, 2.52);
	coord.toDecMin(2);

Returns: B<51 30.04'>

=cut

*/
	toDMS: function (precision) {
		var p = precision || 0;
		var res = Math.abs(this._deg) + ' ' + this._min + '\' ' + this._sec.toFixed(p) + '"'
		if (this.toDecDeg(5)<0) {
			res = "-"+res;
		}
		return res;
	},

	toDecDeg: function (precision) {
		var p = precision || 0;
		return this._dec_deg_deg.toFixed(p);
	},

	toDecMin: function (precision) {
		var p = precision || 0;
		var res = Math.abs(this._decmin_deg) + ' ' + this._decmin_min.toFixed(p) + '\'';
		if (this.toDecDeg(5)<0) {
			res = "-"+res;
		}
		return res;
	}

};

/*

=head1 AUTHOR

John Allen <john@thoofooz.com>

=head1 COPYRIGHT

  Copyright (c) 2005 John Allen.  All rights reserved.
  This library is free software; you can redistribute it and/or modify it 
  under the same terms as Perl.

=cut

*/