/[zanavi_public1]/navit/navit/script/mapextract.php
ZANavi

Contents of /navit/navit/script/mapextract.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 5 months ago) by zoff99
File size: 5500 byte(s)
import files
1 #!/usr/local/bin/php –q
2 <?php
3 function getmercator($sx,$sy,$ex,$ey) {
4
5 $sx = $sx*6371000.0*M_PI/180;
6 $sy = log(tan(M_PI_4+$sy*M_PI/360))*6371000.0;
7
8 $ex = $ex*6371000.0*M_PI/180;
9 $ey = log(tan(M_PI_4+$ey*M_PI/360))*6371000.0;
10
11 return array(
12 'l' => array(
13 'x' => $sx,
14 'y' => $sy
15 ),
16 'h' => array(
17 'x' => $ex,
18 'y' => $ey
19 )
20 );
21
22 }
23 function contains_bbox($c, &$r) {
24 if ($c['l']['x'] > $r['h']['x'])
25 return false;
26 elseif ($c['h']['x'] < $r['l']['x'])
27 return false;
28 elseif ($c['l']['y'] > $r['h']['y'])
29 return false;
30 elseif ($c['h']['y'] < $r['l']['y'])
31 return false;
32 else
33 return true;
34 }
35
36 $fetch_bbox = getmercator(11.3, 47.9, 11.4, 48.0);
37
38 $files = array();
39 $files['input'] = '/home/burner/carputer/navit/src/maps/osm_bbox_11.3,47.9,11.7,48.2.bin';
40 $files['output'] = 'myarea.bin';
41
42 $formats = array();
43 $formats['ziphpack'] = "lssssslLLSS";
44 $formats['zipheader'] = "l" . "ziplocsig"; # Signature (is always the same)
45 $formats['zipheader'] .= "/s" . "zipver"; # zip version needed
46 $formats['zipheader'] .= "/s" . "zipgenfld";# type of os that generated the file
47 $formats['zipheader'] .= "/s" . "zipmthd"; #
48 $formats['zipheader'] .= "/s" . "ziptime"; # time
49 $formats['zipheader'] .= "/s" . "zipdate"; # date
50 $formats['zipheader'] .= "/l" . "zipcrc"; # crc checksum
51 $formats['zipheader'] .= "/L" . "zipsize"; # data size
52 $formats['zipheader'] .= "/L" . "zipuncmp"; # uncompressed size
53 $formats['zipheader'] .= "/S" . "zipfnln"; # length of filename
54 $formats['zipheader'] .= "/S" . "zipxtraln";# length of extra data (always 0)
55
56 $formats['zipcd'] = "".
57 "i" . "zipcensig/".
58 "c" . "zipcver/".
59 "c" . "zipcos/".
60 "c" . "zipcvxt/".
61 "c" . "zipcexos/".
62 "s" . "zipcflg/".
63 "s" . "zipcmthd/".
64 "s" . "ziptim/".
65 "s" . "zipdat/".
66 "i" . "zipccrc/".
67 "I" . "zipcsiz/".
68 "I" . "zipcunc/".
69 "S" . "zipcfnl/".
70 "S" . "zipcxtl/".
71 "S" . "zipccml/".
72 "S" . "zipdsk/".
73 "S" . "zipint/".
74 "I" . "zipext/".
75 "I" . "zipofst/".
76 $formats['zipcdpack'] = "iccccssssiIISSSSSII";
77
78 $formats['zipcontent'] = "i5x/i5y/ii";
79
80 $world_bbox = array();
81 $world_bbox['l']['x'] = -20000000;
82 $world_bbox['l']['y'] = -20000000;
83 $world_bbox['h']['x'] = 20000000;
84 $world_bbox['h']['y'] = 20000000;
85
86 $fp = fopen($files['input'], 'r');
87 $sp = fopen($files['output'], 'w');
88
89 $files = array();
90 $offset = 0;
91
92 /**
93 * Read through zipheaders
94 *
95 */
96 while (!feof($fp)) {
97
98 $buffer = fread($fp, 30);
99 $tileinfo = unpack($formats['zipheader'], $buffer);
100
101 if ($tileinfo['zipfnln'] <= 0)
102 break;
103
104 $filename = fread($fp, $tileinfo['zipfnln']);
105 $x=0;
106 $done=false;
107
108 $r = $world_bbox;
109
110 while (!$done) {
111 $c['x'] = floor( ($r['l']['x'] + $r['h']['x'])/2 );
112 $c['y'] = floor( ($r['l']['y'] + $r['h']['y'])/2 );
113
114 switch($filename[$x]) {
115 case 'a':
116 $r['l']['x'] = $c['x'];
117 $r['l']['y'] = $c['y'];
118 break;
119 case 'b':
120 $r['h']['x'] = $c['x'];
121 $r['l']['y'] = $c['y'];
122 break;
123 case 'c':
124 $r['l']['x'] = $c['x'];
125 $r['h']['y'] = $c['y'];
126 break;
127 case 'd':
128 $r['h']['x'] = $c['x'];
129 $r['h']['y'] = $c['y'];
130 break;
131 default:
132 $done=true;
133 }
134 $x++;
135 }
136
137 $tilecontent = fread($fp, $tileinfo['zipsize']);
138
139 /* Area inside box, save it! */
140 if (contains_bbox($fetch_bbox, $r)) {
141 #echo "In box. ";
142 #echo $filename . " ";
143 $zipheader = $buffer;
144 #echo "\n";
145
146 /* Area outside of box, set zipcontent=0 */
147 } else {
148 $tileinfo['zipmthd'] = $tileinfo['zipcrc'] = $tileinfo['zipsize'] = $tileinfo['zipuncmp'] = 0;
149 #echo "Out of box";
150 $zipheader = $tileinfo;
151 $tilecontent = '';
152 $zipheader = pack($formats['ziphpack'],
153 $tileinfo['ziplocsig'],
154 $tileinfo['zipver'],
155 $tileinfo['zipgenfld'],
156 $tileinfo['zipmthd'],
157 $tileinfo['ziptime'],
158 $tileinfo['zipdate'],
159 $tileinfo['zipcrc'],
160 $tileinfo['zipsize'],
161 $tileinfo['zipuncmp'],
162 $tileinfo['zipfnln'],
163 $tileinfo['zipxtraln']
164 );
165 }
166
167 $put = $zipheader.$filename.$tilecontent;
168 $files[$filename]['header'] = $tileinfo;
169 $files[$filename]['size'] = strlen($put);
170
171 $zipcd = array();
172 $zipcd['zipcensig'] = 0x02014b50;
173 $zipcd['zipcver'] = $tileinfo['zipver'];
174 $zipcd['zipcos'] = 0x00;
175 $zipcd['zipcvxt'] = 0x0a;
176 $zipcd['zipcexos'] = 0x00;
177 $zipcd['zipcflg'] = 0x00;
178 $zipcd['zipcmthd'] = $tileinfo['zipmthd'];
179 $zipcd['ziptim'] = $tileinfo['ziptime'];
180 $zipcd['zipdat'] = $tileinfo['zipdate'];
181 $zipcd['zipccrc'] = $tileinfo['zipcrc'];
182 $zipcd['zipcsiz'] = $tileinfo['zipsize'];
183 $zipcd['zipcunc'] = $tileinfo['zipuncmp'];
184 $zipcd['zipcfnl'] = $tileinfo['zipfnln'];
185 $zipcd['zipcxtl'] = 0x00;
186 $zipcd['zipccml'] = 0x00;
187 $zipcd['zipdsk'] = 0x00;
188 $zipcd['zipint'] = 0x00;
189 $zipcd['zipext'] = 0x00;
190 $zipcd['zipofst'] = $offset;
191
192 $zipcd_data .= pack($formats['zipcdpack'],
193 $zipcd['zipcensig'],
194 $zipcd['zipcver'],
195 $zipcd['zipcos'],
196 $zipcd['zipcvxt'],
197 $zipcd['zipcexos'],
198 $zipcd['zipcflg'],
199 $zipcd['zipcmthd'],
200 $zipcd['ziptim'],
201 $zipcd['zipdat'],
202 $zipcd['zipccrc'],
203 $zipcd['zipcsiz'],
204 $zipcd['zipcunc'],
205 $zipcd['zipcfnl'],
206 $zipcd['zipcxtl'],
207 $zipcd['zipccml'],
208 $zipcd['zipdsk'],
209 $zipcd['zipint'],
210 $zipcd['zipext'],
211 $zipcd['zipofst']
212 ) . $filename;
213
214
215 fwrite($sp, $put);
216 $offset += strlen($put);
217
218 }
219
220 fwrite($sp, $zipcd_data);
221
222 fclose($fp);
223 fclose($sp);
224
225 ?>

   
Visit the ZANavi Wiki