Saturday, September 19, 2015

Parsing and splitting openstreetmap file [ C ]


Its quite bizarre to me, more I'm trying to friendly with string / character processing with C programming, they are showing their envy to me.

I was/am need to split a / any big osm file to three parts, NODE, WAY and RELATION. You can do that by giving line number range to program. But I want to get the line number automatically.

This was seems very easy, just counting the new line till getting required string, i.e. <way id=

So I wrote,

 char way_compare[] = "<way id=";  
 FILE *fp = fopen("Kiel.osm", "r");  
 char ckchr[10];  
 int count_line=1;  
 char chr = getc(fp);  
 while(chr != EOF)  
   {  
   if(chr == '\n')  
     {  count_line++;  
       fgets(ckchr, 10, fp);  
       if(!strcmp(way_compare,ckchr))  
         { break; }  
     }  
     chr = getc(fp);  
   }  
   printf("Searched: %s found at %d\n",way_compare,count_line);  

Wednesday, September 2, 2015

Reading between two given line number of a File


 It is sometimes very important, to let program to read a big file only some specific part/s.

For example, I was and still trying with Openstreetmap with readosm library. Which really need such option.

As .OSM file is quite a large and readosm does read every single bit of the file, on other hand my projected implementation hardware is quite low in resources, badly needed to optimize readosm reading style.

I'd and still have a plan of doing indexing for .OSM file and apply program to read according it.

To do so, here comes the first part of virtually slicing the .OSM file and apply to program to read by given lines between.

My program is an extension of
http://rosettacode.org/wiki/Read_a_specific_line_from_a_file