1# -*- perl -*-
2
3#
4# $Id: Polar.pm,v 1.16 2005/11/10 21:03:11 eserte Exp $
5# Author: Slaven Rezic
6#
7# Copyright (C) 1998 Slaven Rezic. All rights reserved.
8# This package is free software; you can redistribute it and/or
9# modify it under the same terms as Perl itself.
10#
11# Mail: eserte@cs.tu-berlin.de
12# WWW:  http://user.cs.tu-berlin.de/~eserte/
13#
14
15# Die X0 etc.-Parameter basieren auf Dezimalgraden, die Ausgabe auf
16# Grad/Minuten
17
18package Karte::Polar;
19use Karte;
20use strict;
21use vars qw(@ISA $obj);
22
23@ISA = qw(Karte);
24
25sub new {
26    my $class = shift;
27    my $self =
28      {
29       Name     => 'Polare Koordinaten (WGS-84)',
30       Token    => 'polar',
31       Coordsys => 'P',
32
33# alte Daten:
34#         X0 => -790086.391400205,
35#         X1 => 67845.9393462968,
36#         X2 => -2073.78902640448,
37#         Y0 => -5824916.26148241,
38#         Y1 => 1021.55512610816,
39#         Y2 => 110888.031499432,
40
41#  # alte @polar_data-Daten, allerdings ohne die Brandenburger Daten:
42#         X0 => -722207.53657807,
43#         X1 => 68791.4431271489,
44#         X2 => -3606.02363962925,
45#         Y0 => -5908514.40301391,
46#         Y1 => 797.096731883998,
47#         Y2 => 112538.820481108,
48
49#  # erzeugt aus meinem ersten Tracklog:
50#         X0 => -794367.964940472,
51#         X1 => 67839.1895690972,
52#         X2 => -1990.98433751565,
53#         Y0 => -5743124.94026287,
54#         Y1 => 1318.09644368911,
55#         Y2 => 109256.047141788,
56
57#  # erzeugt aus dem Tracklog bis zum 2002-1-20:
58#         X0 => -770895.051743501,
59#         X1 => 67944.117707078,
60#         X2 => -2464.85696704533,
61#         Y0 => -5839995.54068123,
62#         Y1 => 1199.84534625717,
63#         Y2 => 111131.212045654,
64
65# Tracklog bis 2002-1-26:
66       X0 => -780761.760862528,
67       X1 => 67978.2421158527,
68       X2 => -2285.59137120724,
69       Y0 => -5844741.03397902,
70       Y1 => 1214.24447469596,
71       Y2 => 111217.945663725,
72
73# von T2001 abgeleitete Daten:
74#         X0 => -799812.068665913,
75#         X1 => 67920.5433536305,
76#         X2 => -1910.14108336354,
77#         Y0 => -5839468.36531781,
78#         Y1 => 1319.94853184892,
79#         Y2 => 111087.609558648,
80
81#  # von 20000510.tracks abgeleitete Daten:
82#         X0 => -798590.696343232,
83#         X1 => 67705.589136054,
84#         X2 => -1876.40600064877,
85#         Y0 => -5621170.36905198,
86#         Y1 => -458.511708212376,
87#         Y2 => 107384.253345884,
88
89#       Scrollregion => [0, 0, 11778, 9724],
90#       Scrollregion => [0, -3000, 11778, 9724],
91       Scrollregion => [0,0,60,30],
92      };
93    bless $self, $class;
94}
95
96# input: deg (decimal)
97# output: deg, min, sec
98# deg is signed
99sub ddd2dms {
100    my($ddd) = @_;
101
102    my $north_east = $ddd >= 0;
103    $ddd = -$ddd if !$north_east;
104    my $deg = int($ddd);
105    my $min = ($ddd-$deg)*60;
106    my $sec = ($min-int($min))*60;
107    $min = int($min);
108    (($north_east ? $deg : -$deg), $min, $sec);
109}
110
111# input: deg (decimal)
112# output: deg, min (with fraction)
113# deg is signed
114sub ddd2dmm {
115    my($ddd) = @_;
116
117    my $north_east = $ddd >= 0;
118    $ddd = -$ddd if !$north_east;
119    my $deg = int($ddd);
120    my $min = ($ddd-$deg)*60;
121    (($north_east ? $deg : -$deg), $min);
122}
123
124sub dms_human_readable {
125    my($type, @dms) = @_;
126    my $s = "";
127    if (defined $type) {
128	if ($type eq 'lat') {
129	    if ($dms[0] >= 0) {
130		$s .= "N ";
131	    } else {
132		$s .= "S ";
133		$dms[0] *= -1;
134	    }
135	} elsif ($type eq 'long') {
136	    if ($dms[0] >= 0) {
137		$s .= "E ";
138	    } else {
139		$s .= "W ";
140		$dms[0] *= -1;
141	    }
142	} else {
143	    die "Unknown type, should be lat or long";
144	}
145    }
146    $s . $dms[0] . "�" . sprintf("%02d", $dms[1]) . "'" . sprintf("%04.1f",$dms[2]) . "\"";
147}
148
149sub dmm_human_readable {
150    my($type, @dmm) = @_;
151    my $s = "";
152    if (defined $type) {
153	if ($type eq 'lat') {
154	    if ($dmm[0] >= 0) {
155		$s .= "N ";
156	    } else {
157		$s .= "S ";
158		$dmm[0] *= -1;
159	    }
160	} elsif ($type eq 'long') {
161	    if ($dmm[0] >= 0) {
162		$s .= "E ";
163	    } else {
164		$s .= "W ";
165		$dmm[0] *= -1;
166	    }
167	} else {
168	    die "Unknown type, should be lat or long";
169	}
170    }
171    $s . $dmm[0] . "�" . sprintf("%05.2f",$dmm[1]) . "'";
172}
173
174# input: deg, min, sec
175# deg should be signed
176# output: deg (decimal)
177sub dms2ddd {
178    my(@dms) = @_;
179    die "Overflow" if ($dms[1] >= 60 || $dms[2] >= 60);
180    $dms[0] + $dms[1]/60 + $dms[2]/3600;
181}
182
183# input: deg, min
184# deg should be signed
185# output: deg (decimal)
186sub dmm2ddd {
187    my(@dmm) = @_;
188    die "Overflow" if $dmm[1] >= 60;
189    $dmm[0] + $dmm[1]/60;
190}
191
192# input: a dms styled string e.g. N51 12 56.2
193sub dms_string2ddd {
194    my($s) = @_;
195    if ($s =~ /([NSEW])(\d+)\s(\d+)\s([\d\.]+)/) {
196	my($sgn) = $1 eq 'N' || $1 eq 'E' ? +1 : -1;
197	dms2ddd($sgn*$2, $3, $4);
198    } else {
199	die "Can't parse the dms styled string <$s>";
200    }
201}
202
203# input: a dmm styled string e.g. N51 12.1234
204sub dmm_string2ddd {
205    my($s) = @_;
206    if ($s =~ /([NSEW])(\d+)\s([\d\.]+)/) {
207	my($sgn) = $1 eq 'N' || $1 eq 'E' ? +1 : -1;
208	dmm2ddd($sgn*$2, $3);
209    } else {
210	die "Can't parse the dms styled string <$s>";
211    }
212}
213
214sub trim_accuracy {
215    my(undef, $x, $y) = @_;
216    (sprintf("%.6f", $x), sprintf("%.6f", $y));
217}
218
219$obj = new Karte::Polar;
220
2211;
222
223__END__
224
225=head1 NAME
226
227Karte::Polar - convert between BBBike and WGS84 coordinates
228
229=head1 SYNOPSIS
230
231See L<Karte>.
232
233=head1 AUTHOR
234
235Slaven Rezic.
236
237=cut
238