-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmkwaypoint.pl
executable file
·265 lines (206 loc) · 5.83 KB
/
mkwaypoint.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/perl -w
#(c) Bernhard Tittelbach, 2007-2010
use utf8;
use Getopt::Long;
use Pod::Usage;
use POSIX qw(strftime);
#Defaults
my $desc="";
my $name="Geocache";
my $city="Graz";
my $state="Steiermark";
my $country="Austria";
my $lat=0;
my $long=0;
GetOptions(
"mkgpx!"=>\$mkgpx,
"mklmx!"=>\$mklmx,
"tfgpx!"=>\$transfer_gpx,
"overwrite!"=>\$overwriteflag,
"help!"=>\$helpflag,
"latitude:s"=>\$latitudearg,
"longitude:s"=>\$longitudearg,
"coordinates:s"=> \$latitudelongitudearg,
"description:s"=> \$desc,
"city:s"=> \$city,
"state:s"=> \$state,
"country:s"=> \$country) or pod2usage(1);
$name = join(" ",@ARGV);
$name =~ s/\//_/g;
my $lmx_file = $name.".lmx";
my $gpx_file = $name.".gpx";
if (defined $helpflag)
{
pod2usage(-verbose => 2, -exitval => 0, -message => "--------HELP--------\n");
}
sub convert_single
{
my $c=uc(shift(@_));
$c=~s/,/./;
if ($c=~/(N|S|W|E|O)?\s*(\d{2,3})\D+(\d{1,2}\.\d{3})/) {
my $nv = ($2+($3/60));
$nv = 0.0-$nv if ($1 eq "S" or $1 eq "W");
return $nv;
}
else
{
return $c;
}
}
sub convert_both
{
my $c = shift(@_);
if ($c =~ /((?:N|S).+)\s+((?:W|E|O).+)/i)
{
return (convert_single($1),convert_single($2));
}
elsif ($c =~ /((?:W|E|O).+)\s+((?:N|S).+)/i)
{
return (convert_single($2),convert_single($1));
}
else
{
print "Error, couldn't parse Coordinates\n\n";
exit 2;
}
}
if (defined $latitudelongitudearg)
{
($lat,$long) = convert_both($latitudelongitudearg);
}
elsif (defined $latitudearg and defined $longitudearg)
{
$lat=convert_single($latitudearg);
$long=convert_single($longitudearg);
}
else
{
print "Error, you must define --latitude and --longitude or --coordinates\n\n";
exit 1;
}
#define lmx string with known variables
my $lmx_contents = <<END
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<lm:lmx xmlns:lm=\"http://www.nokia.com/schemas/location/landmarks/1/0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.nokia.com/schemas/location/landmarks/1/0/ lmx.xsd\">
<lm:landmarkCollection>
<lm:landmark>
<lm:name>$name</lm:name>
<lm:description>$desc</lm:description>
<lm:coordinates>
<lm:latitude>$lat</lm:latitude>
<lm:longitude>$long</lm:longitude>
</lm:coordinates>
<lm:addressInfo>
<lm:street></lm:street>
<lm:postalCode></lm:postalCode>
<lm:city>$city</lm:city>
<lm:state>$state</lm:state>
<lm:country>$country</lm:country>
<lm:phoneNumber></lm:phoneNumber>
</lm:addressInfo>
<lm:category>
<lm:name>Geocache Punkt</lm:name>
</lm:category>
</lm:landmark>
</lm:landmarkCollection>
</lm:lmx>
END
;
$currtime=strftime("%Y-%m-%dT%TZ",localtime());
#define gpx string with known variables
my $gpx_contents = <<END
<?xml version="1.0" encoding="UTF-8"?>
<gpx
version="1.0"
creator="mkwaypoint Perl Script"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
<time>$currtime</time>
<bounds minlat="$lat" minlon="$long" maxlat="$lat" maxlon="$long"/>
<wpt lat="$lat" lon="$long">
<ele>0.000000</ele>
<name>$name</name>
<cmt>$desc</cmt>
<desc>$desc</desc>
<sym>Geocache</sym>
</wpt>
</gpx>
END
;
print "Lat: $lat\nLong: $long\n";
sub write_file
{
$filename=shift;
$contents=shift;
$overwriteflag=shift;
if (not -e $filename or $overwriteflag)
{
open($fh, ">$filename") or die("error: couldn't open file $filename");
print $fh $contents;
close $fh ;
print "$filename written\n";
}
else
{
print "$filename exists, not overwriting unless you specify --overwrite\n";
}
}
sub gpsbabel_transfer_file
{
$contents=shift;
open($fh, "| gpsbabel -i gpx -f - -o garmin -F usb:") or die("error: couldn't run gpsbabel");
print $fh $contents;
close $fh ;
print "GPX transfered to GPS\n";
}
write_file($lmx_file, $lmx_contents, $overwriteflag) if $mklmx;
write_file($gpx_file, $gpx_contents, $overwriteflag) if $mkgpx;
gpsbabel_transfer_file($gpx_contents) if $transfer_gpx;
__END__
=head1 NAME
mknokiawaypoint.pl
=head1 SYNOPSIS
Supply name, longitute and latitude as arguments and the script will create a .lmx Nokia-compatible WayPoint File which you can then send to your phone via bluetooth, usb or infrared.
You can abbreviate the options down to one char as long as they not ambigious
mknokiawaypoint.pl --coordinates "<coordinates>" "<Waypoint Name>"
=head1 OPTIONS
=over
=item B<--help>
Displays Help Man Page
=item B<--mklmx>
Create Nokia LMX Waypoint File
=item B<--mkgpx>
Create GPX Waypoint File
=item B<--tfgpx>
Transfer GPX to connected Garmin GPS
=item B<--latitude>
Give Latitude in WGS84
=item B<--longitude>
Give Longitude in WGS84
=item B<--coordinates>
Give both Longitude and Latitude in one String in WGS84
Degrees or Degrees and Minutes are accepted
=item B<--city>
City Field of Waypoint
=item B<--state>
State Field of Waypoint
=item B<--country>
Country Field of Waypoint
=item B<--description>
Description of WayPoint
=item B<--overwrite>
Overwrite Waypoint File if it already exists
=back
=head1 EXAMPLES
mknokiawaypoint.pl "Graz Top" --country Austria --city Graz \
--coord "N 47° 05.344 E 015° 23.120" \
--desc "Regular, D:2 T:2.5" --mkgpx --mklmx --tfgpx
mknokiawaypoint.pl -o --country Germany --city Paderborn --state "" \
--coord "N 51° 42.538 E 008° 46569" \
"Unter Dampf No. #3 - Die alte UHSe" \
--desc "Das Eckige muss ins Runde." --tfgpx
mknokiawaypoint.pl --coordinates "N 51° 43.212 E 008° 45.339" \
--country Germany --city Paderborn --region "" \
"The springs of Paderborn" --mklmx
=cut