Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/extras/find-fixme/fixme-finder.c
Parent Directory
|
Revision Log
Revision 1367 -
(show annotations)
(download)
(as text)
Sat Jun 1 18:56:25 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 9980 byte(s)
Sat Jun 1 18:56:25 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 9980 byte(s)
A tool to search an OSM file for "fixme" tags to create a database and display them on an interactive map.
1 | /*************************************** |
2 | OSM planet file fixme finder. |
3 | |
4 | Part of the Routino routing software. |
5 | ******************/ /****************** |
6 | This file Copyright 2008-2013 Andrew M. Bishop |
7 | |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU Affero General Public License as published by |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public License |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | ***************************************/ |
21 | |
22 | |
23 | #include <stdio.h> |
24 | #include <stdlib.h> |
25 | #include <unistd.h> |
26 | #include <string.h> |
27 | #include <errno.h> |
28 | #include <sys/time.h> |
29 | |
30 | #include "types.h" |
31 | #include "ways.h" |
32 | |
33 | #include "typesx.h" |
34 | #include "nodesx.h" |
35 | #include "waysx.h" |
36 | #include "relationsx.h" |
37 | |
38 | #include "files.h" |
39 | #include "logging.h" |
40 | #include "errorlogx.h" |
41 | #include "functions.h" |
42 | #include "osmparser.h" |
43 | #include "tagging.h" |
44 | #include "uncompress.h" |
45 | |
46 | |
47 | /* Global variables */ |
48 | |
49 | /*+ The name of the temporary directory. +*/ |
50 | char *option_tmpdirname=NULL; |
51 | |
52 | /*+ The amount of RAM to use for filesorting. +*/ |
53 | size_t option_filesort_ramsize=0; |
54 | |
55 | /*+ The number of threads to use for filesorting. +*/ |
56 | int option_filesort_threads=1; |
57 | |
58 | |
59 | /* Local functions */ |
60 | |
61 | static void print_usage(int detail,const char *argerr,const char *err); |
62 | |
63 | |
64 | /*++++++++++++++++++++++++++++++++++++++ |
65 | The main program for the find-fixme. |
66 | ++++++++++++++++++++++++++++++++++++++*/ |
67 | |
68 | int main(int argc,char** argv) |
69 | { |
70 | struct timeval start_time; |
71 | NodesX *OSMNodes; |
72 | WaysX *OSMWays; |
73 | RelationsX *OSMRelations; |
74 | ErrorLogsX *OSMErrorLogs; |
75 | char *dirname=NULL,*prefix=NULL,*tagging="fixme.xml",*errorlog="fixme.log"; |
76 | int option_keep=1; |
77 | int option_filenames=0; |
78 | int arg; |
79 | |
80 | gettimeofday(&start_time,NULL); |
81 | |
82 | /* Parse the command line arguments */ |
83 | |
84 | for(arg=1;arg<argc;arg++) |
85 | { |
86 | if(!strcmp(argv[arg],"--help")) |
87 | print_usage(1,NULL,NULL); |
88 | else if(!strncmp(argv[arg],"--dir=",6)) |
89 | dirname=&argv[arg][6]; |
90 | else if(!strncmp(argv[arg],"--sort-ram-size=",16)) |
91 | option_filesort_ramsize=atoi(&argv[arg][16]); |
92 | #if defined(USE_PTHREADS) && USE_PTHREADS |
93 | else if(!strncmp(argv[arg],"--sort-threads=",15)) |
94 | option_filesort_threads=atoi(&argv[arg][15]); |
95 | #endif |
96 | else if(!strncmp(argv[arg],"--tmpdir=",9)) |
97 | option_tmpdirname=&argv[arg][9]; |
98 | else if(!strncmp(argv[arg],"--tagging=",10)) |
99 | tagging=&argv[arg][10]; |
100 | else if(!strcmp(argv[arg],"--loggable")) |
101 | option_loggable=1; |
102 | else if(!strcmp(argv[arg],"--logtime")) |
103 | option_logtime=1; |
104 | else if(argv[arg][0]=='-' && argv[arg][1]=='-') |
105 | print_usage(0,argv[arg],NULL); |
106 | else |
107 | option_filenames++; |
108 | } |
109 | |
110 | /* Check the specified command line options */ |
111 | |
112 | if(!option_filesort_ramsize) |
113 | { |
114 | #if SLIM |
115 | option_filesort_ramsize=64*1024*1024; |
116 | #else |
117 | option_filesort_ramsize=256*1024*1024; |
118 | #endif |
119 | } |
120 | else |
121 | option_filesort_ramsize*=1024*1024; |
122 | |
123 | if(!option_tmpdirname) |
124 | { |
125 | if(!dirname) |
126 | option_tmpdirname="."; |
127 | else |
128 | option_tmpdirname=dirname; |
129 | } |
130 | |
131 | if(tagging) |
132 | { |
133 | if(!ExistsFile(tagging)) |
134 | { |
135 | fprintf(stderr,"Error: The '--tagging' option specifies a file that does not exist.\n"); |
136 | exit(EXIT_FAILURE); |
137 | } |
138 | } |
139 | else |
140 | { |
141 | if(ExistsFile(FileName(dirname,prefix,"tagging.xml"))) |
142 | tagging=FileName(dirname,prefix,"tagging.xml"); |
143 | else |
144 | { |
145 | fprintf(stderr,"Error: The '--tagging' option was not used and the default 'tagging.xml' does not exist.\n"); |
146 | exit(EXIT_FAILURE); |
147 | } |
148 | } |
149 | |
150 | if(ParseXMLTaggingRules(tagging)) |
151 | { |
152 | fprintf(stderr,"Error: Cannot read the tagging rules in the file '%s'.\n",tagging); |
153 | exit(EXIT_FAILURE); |
154 | } |
155 | |
156 | /* Create new node, segment, way and relation variables */ |
157 | |
158 | OSMNodes=NewNodeList(0,0); |
159 | |
160 | OSMWays=NewWayList(0,0); |
161 | |
162 | OSMRelations=NewRelationList(0,0); |
163 | |
164 | /* Create the error log file */ |
165 | |
166 | if(errorlog) |
167 | open_errorlog(FileName(dirname,prefix,errorlog),0,option_keep); |
168 | |
169 | /* Parse the file */ |
170 | |
171 | for(arg=1;arg<argc;arg++) |
172 | { |
173 | int fd; |
174 | char *filename,*p; |
175 | |
176 | if(argv[arg][0]=='-' && argv[arg][1]=='-') |
177 | continue; |
178 | |
179 | filename=strcpy(malloc(strlen(argv[arg])+1),argv[arg]); |
180 | |
181 | fd=ReOpenFile(filename); |
182 | |
183 | if((p=strstr(filename,".bz2")) && !strcmp(p,".bz2")) |
184 | { |
185 | fd=Uncompress_Bzip2(fd); |
186 | *p=0; |
187 | } |
188 | |
189 | if((p=strstr(filename,".gz")) && !strcmp(p,".gz")) |
190 | { |
191 | fd=Uncompress_Gzip(fd); |
192 | *p=0; |
193 | } |
194 | |
195 | printf("\nParse OSM Data [%s]\n==============\n\n",filename); |
196 | fflush(stdout); |
197 | |
198 | if((p=strstr(filename,".pbf")) && !strcmp(p,".pbf")) |
199 | { |
200 | if(ParsePBFFile(fd,OSMNodes,OSMWays,OSMRelations)) |
201 | exit(EXIT_FAILURE); |
202 | } |
203 | else if((p=strstr(filename,".o5m")) && !strcmp(p,".o5m")) |
204 | { |
205 | if(ParseO5MFile(fd,OSMNodes,OSMWays,OSMRelations)) |
206 | exit(EXIT_FAILURE); |
207 | } |
208 | else |
209 | { |
210 | if(ParseOSMFile(fd,OSMNodes,OSMWays,OSMRelations)) |
211 | exit(EXIT_FAILURE); |
212 | } |
213 | |
214 | CloseFile(fd); |
215 | |
216 | free(filename); |
217 | } |
218 | |
219 | DeleteXMLTaggingRules(); |
220 | |
221 | FinishNodeList(OSMNodes); |
222 | FinishWayList(OSMWays); |
223 | FinishRelationList(OSMRelations); |
224 | |
225 | /* Sort the data */ |
226 | |
227 | printf("\nSort OSM Data\n=============\n\n"); |
228 | fflush(stdout); |
229 | |
230 | /* Sort the nodes, ways and relations */ |
231 | |
232 | SortNodeList(OSMNodes); |
233 | |
234 | SortWayList(OSMWays); |
235 | |
236 | SortRelationList(OSMRelations); |
237 | |
238 | /* Process the data */ |
239 | |
240 | RenameFile(OSMNodes->filename_tmp,OSMNodes->filename); |
241 | RenameFile(OSMWays->filename_tmp,OSMWays->filename); |
242 | RenameFile(OSMRelations->rrfilename_tmp,OSMRelations->rrfilename); |
243 | RenameFile(OSMRelations->trfilename_tmp,OSMRelations->trfilename); |
244 | |
245 | close_errorlog(); |
246 | |
247 | printf("\nCreate Error Log\n================\n\n"); |
248 | fflush(stdout); |
249 | |
250 | OSMErrorLogs=NewErrorLogList(); |
251 | |
252 | ProcessErrorLogs(OSMErrorLogs,OSMNodes,OSMWays,OSMRelations); |
253 | |
254 | SortErrorLogsGeographically(OSMErrorLogs); |
255 | |
256 | SaveErrorLogs(OSMErrorLogs,FileName(dirname,prefix,"fixme.mem")); |
257 | |
258 | FreeErrorLogList(OSMErrorLogs); |
259 | |
260 | /* Free the memory (delete the temporary files) */ |
261 | |
262 | FreeNodeList(OSMNodes,0); |
263 | FreeWayList(OSMWays,0); |
264 | FreeRelationList(OSMRelations,0); |
265 | |
266 | /* Print the total time */ |
267 | |
268 | if(option_logtime) |
269 | { |
270 | printf("\n"); |
271 | fprintf_elapsed_time(stdout,&start_time); |
272 | printf("Complete\n"); |
273 | fflush(stdout); |
274 | } |
275 | |
276 | return(0); |
277 | } |
278 | |
279 | |
280 | /*++++++++++++++++++++++++++++++++++++++ |
281 | Print out the usage information. |
282 | |
283 | int detail The level of detail to use - 0 = low, 1 = high. |
284 | |
285 | const char *argerr The argument that gave the error (if there is one). |
286 | |
287 | const char *err Other error message (if there is one). |
288 | ++++++++++++++++++++++++++++++++++++++*/ |
289 | |
290 | static void print_usage(int detail,const char *argerr,const char *err) |
291 | { |
292 | fprintf(stderr, |
293 | "Usage: fixme-finder [--help]\n" |
294 | " [--dir=<dirname>]\n" |
295 | #if defined(USE_PTHREADS) && USE_PTHREADS |
296 | " [--sort-ram-size=<size>] [--sort-threads=<number>]\n" |
297 | #else |
298 | " [--sort-ram-size=<size>]\n" |
299 | #endif |
300 | " [--tmpdir=<dirname>]\n" |
301 | " [--tagging=<filename>]\n" |
302 | " [--loggable] [--logtime]\n" |
303 | " [<filename.osm> ...\n" |
304 | " | <filename.pbf> ...\n" |
305 | " | <filename.o5m> ..." |
306 | #if defined(USE_BZIP2) && USE_BZIP2 |
307 | "\n | <filename.(osm|o5m).bz2> ..." |
308 | #endif |
309 | #if defined(USE_GZIP) && USE_GZIP |
310 | "\n | <filename.(osm|o5m).gz> ..." |
311 | #endif |
312 | "]\n"); |
313 | |
314 | if(argerr) |
315 | fprintf(stderr, |
316 | "\n" |
317 | "Error with command line parameter: %s\n",argerr); |
318 | |
319 | if(err) |
320 | fprintf(stderr, |
321 | "\n" |
322 | "Error: %s\n",err); |
323 | |
324 | if(detail) |
325 | fprintf(stderr, |
326 | "\n" |
327 | "--help Prints this information.\n" |
328 | "\n" |
329 | "--dir=<dirname> The directory containing the fixme database.\n" |
330 | "\n" |
331 | "--sort-ram-size=<size> The amount of RAM (in MB) to use for data sorting\n" |
332 | #if SLIM |
333 | " (defaults to 64MB otherwise.)\n" |
334 | #else |
335 | " (defaults to 256MB otherwise.)\n" |
336 | #endif |
337 | #if defined(USE_PTHREADS) && USE_PTHREADS |
338 | "--sort-threads=<number> The number of threads to use for data sorting.\n" |
339 | #endif |
340 | "\n" |
341 | "--tmpdir=<dirname> The directory name for temporary files.\n" |
342 | " (defaults to the '--dir' option directory.)\n" |
343 | "\n" |
344 | "--tagging=<filename> The name of the XML file containing the tagging rules\n" |
345 | " (defaults to 'fixme.xml' with '--dir' option)\n" |
346 | "\n" |
347 | "--loggable Print progress messages suitable for logging to file.\n" |
348 | "--logtime Print the elapsed time for each processing step.\n" |
349 | "\n" |
350 | "<filename.osm>, <filename.pbf>, <filename.o5m>\n" |
351 | " The name(s) of the file(s) to read and parse.\n" |
352 | " Filenames ending '.pbf' read as PBF, filenames ending\n" |
353 | " '.o5m' read as O5M, others as XML.\n" |
354 | #if defined(USE_BZIP2) && USE_BZIP2 |
355 | " Filenames ending '.bz2' will be bzip2 uncompressed.\n" |
356 | #endif |
357 | #if defined(USE_GZIP) && USE_GZIP |
358 | " Filenames ending '.gz' will be gzip uncompressed.\n" |
359 | #endif |
360 | ); |
361 | |
362 | exit(!detail); |
363 | } |