/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Methods/Mapping/scripts/do.pl

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
use Switch;
 
6
use Error qw(:try);
 
7
 
 
8
my %conf = (
 
9
    'path', "srtm2osm",
 
10
    'minor', 20,
 
11
    'medium', 100,
 
12
    'major', 200
 
13
);
 
14
 
 
15
sub get_header {
 
16
    my ($ncols, $nrows, $xllcorner, $yllcorner, $cellsize);
 
17
    my (@head, $i);
 
18
    my ($file) = @_;
 
19
    
 
20
    open ASC, $file;
 
21
    for($i=0;$i<5;$i++){
 
22
        $head[$i]=<ASC>;
 
23
        chomp $head[$i];
 
24
        $head[$i]=~s/[a-zA-Z_\s]//g;
 
25
    }
 
26
    close ASC;
 
27
 
 
28
    $ncols = $head[0]+0;
 
29
    $nrows = $head[1]+0;
 
30
    $xllcorner = $head[2]+0;
 
31
    $yllcorner = $head[3]+0;
 
32
    $cellsize = $head[4]+0;
 
33
 
 
34
    return ($xllcorner, $yllcorner, $ncols, $nrows, $cellsize);
 
35
}
 
36
 
 
37
sub get_rectangle {
 
38
    my ($file) = @_;
 
39
    my ($xstart, $ystart, $ncols, $nrows, $cellsize) = get_header($file);
 
40
    
 
41
    my $xend = $xstart + $ncols * $cellsize;
 
42
    my $yend = $ystart + $nrows * $cellsize;
 
43
    
 
44
    return ($xstart, $ystart, $xend, $yend);
 
45
}
 
46
 
 
47
sub select_area {
 
48
    my ($file, $xmin, $ymin, $xmax, $ymax, $size) = @_;
 
49
    my ($x1, $y1, $x2, $y2) = get_rectangle($file);
 
50
 
 
51
#    print " $x1, $y1, $x2, $y2\n";
 
52
    
 
53
 
 
54
    if ($x1 < $xmin) { $x1 = $xmin; }
 
55
    if ($x2 > $xmax) { $x2 = $xmax; }
 
56
    if ($y1 < $ymin) { $y1 = $ymin; }
 
57
    if ($y2 > $ymax) { $y2 = $ymax; }
 
58
 
 
59
#    print " $x1, $y1, $x2, $y2\n";
 
60
    
 
61
    if (($x1 >= $x2)||($y1 >= $y2)) {
 
62
        throw Error::Simple("Nothing to process");
 
63
    }
 
64
 
 
65
    return ($x1, $y1, $x2, $y2);
 
66
}
 
67
 
 
68
 
 
69
sub process_file {
 
70
    my $notfound = 1;
 
71
    my ($xi, $yi);
 
72
    my ($file, $x1, $y1, $x2, $y2, $size) = @_;
 
73
    
 
74
    try {
 
75
         ($x1, $y1, $x2, $y2) = select_area($file, $x1, $y1, $x2, $y2);
 
76
         $notfound = 0;
 
77
    } catch Error with {
 
78
        print "Skipping file: $file\n";
 
79
        return; # Looks just exiting from block
 
80
    };  # This semicolon is really-really needed.
 
81
 
 
82
    if ($notfound) { return; }
 
83
 
 
84
    print "Processing file: $file\n";
 
85
    printf (" Extract area: (%.2f,%.2f) - (%.2f,%.2f)\n", $x1, $y1, $x2, $y2);
 
86
    for ($yi = $y1; $yi < $y2; $yi += $size) {
 
87
        for ($xi = $x1; $xi < $x2; $xi += $size) {
 
88
            my $xj = (($xi+$size)<$x2)?($xi+$size):$x2;
 
89
            my $yj = (($yi+$size)<$y2)?($yi+$size):$y2;
 
90
            my $qwidth = $xj - $xi;
 
91
            my $qheight = $yj - $yi;
 
92
            my $resfile = sprintf("dem_data/$file.countours.%.2fx%.2f.osm", $xi, $yi);
 
93
            printf ("  $file: Qudrate %.2f,%.2f of size %.2fx%.2f\n", $xi, $yi, $qwidth, $qheight);
 
94
            
 
95
            system("$conf{'path'}/extractdata.pl $file $yi $xi $yj $xj > extract.txt");
 
96
            system("$conf{'path'}/mkcntr.pl ./extract.txt $conf{'minor'} $conf{'medium'} $conf{'major'} > $resfile");
 
97
        }
 
98
    }
 
99
}
 
100
 
 
101
sub usage {
 
102
    print "Usage: $0 <directory> [ymin xmin ymax xmax] [size]\n";
 
103
    print "                      x - longtitude, y - latitude\n";
 
104
    exit;
 
105
}
 
106
 
 
107
my ($dir, $y1, $x1, $y2, $x2, $size) = @ARGV;
 
108
 
 
109
switch (int(@ARGV)) {
 
110
    case 1 {
 
111
        $x1 = -360;
 
112
        $y1 = -360;
 
113
        $x2 = 360;
 
114
        $y2 = 360;
 
115
    }
 
116
    case [4,5] {
 
117
    }
 
118
    default {
 
119
        usage();
 
120
    }
 
121
}
 
122
if (!defined($size)) { $size = 1; }
 
123
 
 
124
 
 
125
my (@files, $f, $cdir);
 
126
     
 
127
opendir $cdir, $dir;
 
128
@files = readdir($cdir);
 
129
closedir $cdir;
 
130
 
 
131
mkdir "dem_data";
 
132
foreach $f(@files) {
 
133
    if ($f !~ /\.ASC$/) { next; }
 
134
    process_file($f, $x1, $y1, $x2, $y2, $size);
 
135
}
 
136
 
 
137
 
 
138
 
 
139
#srtm2osm/extractdata.pl srtm_38_02.ASC 50 5 51 6 > extract.txt
 
140
#srtm2osm/mkcntr.pl ./extract.txt 20 100 200 > countours
 
141
 
 
142
#for name in *.ASC; do
 
143
#    srtm2osm/extractdata.pl $name 47.24 6.32 54.7 13.46 > extract.txt
 
144
#    srtm2osm/mkcntr.pl ./extract.txt 20 100 200 > $name.countours
 
145
#done
 
146
 
 
147
#46.56 2.8 55.41 17.26
 
148
 
 
149
#for name in 
 
 
b'\\ No newline at end of file'