1#!/usr/local/bin/perl
2use warnings;
3
4
5# Copyright (C) 2000-2019 The Xastir Group.
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; either version 2
10# of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20# 02111-1307, USA.
21#
22# Look at the README for more information on the program.
23
24
25# This will retrieve the 1:250k map images from the "Department of
26# Natural Resources Geomatics Canada" site.  These are topographic
27# map files for the whole of Canada!
28#
29#
30# - "cd /usr/local/share/xastir/maps"
31#
32# - Assure you have write privileges in the directory above either
33#   by becoming root using the "su" command, using "sudo", or
34#   temporarily changing ownership and/or privileges on the
35#   "maps" directory.
36#
37# - "/usr/local/lib/xastir/toporama250k.pl"
38#
39# - The script will create/populate this directory heirarchy:
40#   .../maps/toporama/images/
41#   .../maps/toporama/images/b250k/
42#
43# - Move/rename directories/contents as you wish.
44#
45# - In Xastir:  "Map->Configure->Index: Reindex ALL Maps!"
46#
47#
48# Note that the same map files are also available from Steve Dimse's
49# site:  "http://mm.aprs.net/maps/geo/toporama"
50#
51#
52# Code for this script contributed by Adi Linden, VA3ADI.
53# Modifications for latitudes above 67 degrees north contributed by Tom Tessier, VE4TRT
54
55
56
57# Create the "toporama" directory
58mkdir "toporama";
59chdir "toporama";
60
61
62# Retrieve the 1:250,000 map images.  Skipping those that we've
63# already downloaded.
64`wget -nv -np -nH -N -r -m -l 0 -T 10 http://toporama.cits.rncan.gc.ca/images/b250k/`;
65
66
67# Remove index.html files
68`rm -rf \`find ./images -type f -name index.html\*\``;
69# Remove *.asc files
70`rm -rf \`find ./images -type f -name \*.asc\``;
71
72
73
74# Define some stuff
75$base_dir = "images/b250k";
76
77# This defines the top left corner of the 0 grid up to 67 degrees north latitude.
78$basex = '-56';
79$basey = '44';
80%offsetx = ( 'a'  => 3,
81             'b'  => 2,
82             'c'  => 1,
83             'd'  => 0,
84             'e'  => 0,
85             'f'  => 1,
86             'g'  => 2,
87             'h'  => 3,
88             'i'  => 3,
89             'j'  => 2,
90             'k'  => 1,
91             'l'  => 0,
92             'm'  => 0,
93             'n'  => 1,
94             'o'  => 2,
95             'p'  => 3,
96             '1'  => 3,
97             '2'  => 2,
98             '3'  => 1,
99             '4'  => 0,
100             '5'  => 0,
101             '6'  => 1,
102             '7'  => 2,
103             '8'  => 3,
104             '9'  => 3,
105             '10' => 2,
106             '11' => 1,
107             '12' => 0,
108             '13' => 0,
109             '14' => 1,
110             '15' => 2,
111             '16' => 3 );
112%offsety = ( 'a'  => 3,
113             'b'  => 3,
114             'c'  => 3,
115             'd'  => 3,
116             'e'  => 2,
117             'f'  => 2,
118             'g'  => 2,
119             'h'  => 2,
120             'i'  => 1,
121             'j'  => 1,
122             'k'  => 1,
123             'l'  => 1,
124             'm'  => 0,
125             'n'  => 0,
126             'o'  => 0,
127             'p'  => 0,
128             '1'  => 3,
129             '2'  => 3,
130             '3'  => 3,
131             '4'  => 3,
132             '5'  => 2,
133             '6'  => 2,
134             '7'  => 2,
135             '8'  => 2,
136             '9'  => 1,
137             '10' => 1,
138             '11' => 1,
139             '12' => 1,
140             '13' => 0,
141             '14' => 0,
142             '15' => 0,
143             '16' => 0 );
144
145# This defines the top left corner of the 0 grid between 68 and 84 degrees north latitude.
146%offsetx68 = ( 'a'  => 4,
147             'b'  => 0,
148             'c'  => 0,
149             'd'  => 4,
150             'e'  => 4,
151             'f'  => 0,
152             'g'  => 0,
153             'h'  => 4,
154             '1'  => 4,
155             '2'  => 0,
156             '3'  => 0,
157             '4'  => 4,
158             '5'  => 4,
159             '6'  => 0,
160             '7'  => 0,
161             '8'  => 4 );
162%offsety68 = ( 'a'  => 3,
163             'b'  => 3,
164             'c'  => 2,
165             'd'  => 2,
166             'e'  => 1,
167             'f'  => 1,
168             'g'  => 0,
169             'h'  => 0,
170             '1'  => 3,
171             '2'  => 3,
172             '3'  => 2,
173             '4'  => 2,
174             '5'  => 1,
175             '6'  => 1,
176             '7'  => 0,
177             '8'  => 0 );
178
179#def
180
181use File::Find;
182
183print "Writing .geo files\n";
184
185find(\&process, $base_dir);
186
187print "\nDone.\n";
188
189
190
191# Run for each file found
192sub process
193{
194    # Only look for .gif files
195    if ( /.gif$/ ) {
196
197        # Get the map sheet designation from the file name
198        $file = "$_";
199        $grida = substr($file, 0, 3);
200        $gridb = substr($file, 3, 1);
201	$hilat = substr($file, 2, 1);
202
203	# .geo calculations for lattitudes greater than 78 degrees north latitude.
204        if ( $grida > 119 ) {
205
206           # Calculate top left coordinates of the map sheet
207            use integer;
208            $topx = $basex - ((($grida/10) +10 ) / 22 * 16) + $offsetx68{$gridb} * 2;
209            $topy = 84 - $offsety68{$gridb} * 1;
210            $botx = $topx + 8;
211            $boty = $topy - 2;
212
213            # Create the output file
214            $out = substr($file, 0, 4) . ".geo";
215            open (OUT, ">$out") or die "Can't open $OUT: $!\n";
216            print OUT "# Automatically created by toporama250k.pl\n";
217            print OUT "FILENAME\t$file\n";
218            print OUT "#\t\tx\ty\tlon\tlat\n";
219            print OUT "TIEPOINT\t0\t0\t$topx\t$topy\n";
220            print OUT "TIEPOINT\t6399\t1599\t$botx\t$boty\n";
221            print OUT "IMAGESIZE\t6400\t1600\n";
222            close OUT;
223
224            print ".";
225            next;
226        }
227
228	# .geo calculations for lattitudes between 68 and 78 degrees north latitude.
229	if ( $hilat > 6 ) {
230
231            # Calculate top left coordinates of the map sheet
232            use integer;
233            $topx = $basex - $grida / 10 * 8 + $offsetx68{$gridb} * 1;
234            $topy = $basey + ($grida % 10) * 4 - $offsety68{$gridb} * 1;
235            $botx = $topx + 4;
236            $boty = $topy - 1;
237
238            # Create the output file
239            $out = substr($file, 0, 4) . ".geo";
240            open (OUT, ">$out") or die "Can't open $OUT: $!\n";
241            print OUT "# Automatically created by toporama250k.pl\n";
242            print OUT "FILENAME\t$file\n";
243            print OUT "#\t\tx\ty\tlon\tlat\n";
244            print OUT "TIEPOINT\t0\t0\t$topx\t$topy\n";
245            print OUT "TIEPOINT\t6399\t1599\t$botx\t$boty\n";
246            print OUT "IMAGESIZE\t6400\t1600\n";
247            close OUT;
248
249            print ".";
250	    next;
251
252	}
253
254        # Calculate top left coordinates of the map sheet
255        use integer;
256        $topx = $basex - $grida / 10 * 8 + $offsetx{$gridb} * 2;
257        $topy = $basey + ($grida % 10) * 4 - $offsety{$gridb} * 1;
258        $botx = $topx + 2;
259        $boty = $topy - 1;
260
261        # Create the output file
262        $out = substr($file, 0, 4) . ".geo";
263        open (OUT, ">$out") or die "Can't open $OUT: $!\n";
264        print OUT "# Automatically created by toporama250k.pl\n";
265        print OUT "FILENAME\t$file\n";
266        print OUT "#\t\tx\ty\tlon\tlat\n";
267        print OUT "TIEPOINT\t0\t0\t$topx\t$topy\n";
268        print OUT "TIEPOINT\t3199\t1599\t$botx\t$boty\n";
269        print OUT "IMAGESIZE\t3200\t1600\n";
270        close OUT;
271
272        print ".";
273    }
274
275}
276