Tuesday, October 27, 2015

XML or .osm special character issue and solution in C


You probably have experience while parsing openstreetmap .osm file - some name [TAG-NAME] appear as

C&C - which is in clear text "C&C". So while you are coding for a .osm or XML parser, it is important to consider such special character at XML specification.

If it need to present such value at literal form, it is important to replace those characters with some other clear text format.

For example, if it need to replace the above "&" a below C program can make an idea.

 char *special_ch_replace   
 (char *s, char ser, char rep)   
 {  
  char *p1;  
  for (p1=s; *p1; p1++)   
  {  
   if (*p1 == ser)  
      {  
    *p1 = rep;  
   }  
  }  
  return s;  
 }  

Saturday, October 3, 2015

C program to generate .osm[XML] with efficient memory management


It was a requirement to generate new .osm file with selectively extracted openstreetmap's data from a big file [e.g. a country / state .osm file].

Equally the requirement extended as no use of any pre-programmed library, only simple C programming and very importantly, the program should avoid -

1. memory leaking
2. buffer overflow
3. change of pointer address
4. extra memory allocation, and
5. efficient.

End output has to be something like this -

     <node id="73900462" lat="54.3884" lon="10.38285">  
         <tag k="name" v="Lidl"/>  
         <tag k="shop" v="supermarket"/>  
         <tag k="addr:city" v="Schönberg"/>  
         <tag k="addr:street" v="Große Mühlenstraße"/>  
         <tag k="addr:housenumber" v="51"/>  
     </node>  

The output will write to a file. Where, node_id, lat, lon, tag_value/s etc. will be dynamic.

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

Wednesday, August 26, 2015

Simple thing but difficult to find [ALSA wav file]


I was in need of ALSA and honestly, I'd just few testing style programming of it long before.

It need to take wav file as input and do a playback of that file using ALSA. However, I need to learn it form scratch, though to find any suitable example according how I can handle wav file to send as ALSA PCM playback, was difficult.

So I wish to write some explanation of it here, and will try to exclude those what you can find and learn by a short googling.

Below part, I presume you already know, or if you don't, I found -

http://www.linuxjournal.com/article/6735?page=0,0

         is the best and most informative.

Wednesday, August 12, 2015

GEO-Search - Faster approch between two point


Previous post, I port the calculation and formula from Federico

My search bring me another solution, which is more faster than previous ->

Below it is, [This one calculate for Kilometre]
  
struct arg_max_min_lo_lt

{
    double min_long;

    double min_lat;

    double max_long;

    double max_lat;
}; 

struct arg_max_min_lo_lt get_lo_lt_values()

{

        int eRadius = 12742;

        float radius=5;

        struct arg_max_min_lo_lt m_lo_lt_ptr;

        //each degree of lon or lat is approximately 69 miles or 111 kilometres difference

        double denominator = cos(deg2rad(curr_lat)) * 111;

        m_lo_lt_ptr.min_long = curr_long - (radius / denominator );

        m_lo_lt_ptr.max_long = curr_long + (radius / denominator );

        m_lo_lt_ptr.min_lat = curr_lat - (double)radius / 111;

        m_lo_lt_ptr.max_lat = curr_lat + (double)radius / 111;

        return m_lo_lt_ptr;

}

Friday, August 7, 2015

GEO-Search: The Haversine Equation


To calculate longitude & latitude of an area easiest way is to calculate The "Haversine Equation".
Implementation of this equation in various programming language was done at here ->

http://blog.fedecarg.com/2009/02/08/geo-proximity-search-the-haversine-equation/

I port it in basic C programming ->


 #include <stdio.h>  
 #include <math.h>  
 double deg2rad(double lat);  
 int main()  
 {  
 double curr_long=-2.708077,curr_lat = 53.754842;  
 int radius=20;  
 double denominator = cos(deg2rad(curr_lat)) * 69;  // 69 as calculation in Mile
 double min_long = curr_long - (radius / denominator );  
 double max_long = curr_long + (radius / denominator );  
 double min_lat = curr_lat - (double)radius / 69;  
 double max_lat = curr_lat + (double)radius / 69;  
 printf("Minimum Longitude :%1.9f\nMaximum Longitude: %1.9f\n",min_long,max_long);  
 printf("Minimum Latitude :%1.9f\nMaximum Latitude: %1.9f\n",min_lat,max_lat);  
 return 0;  
 }  
 double deg2rad(double lat)  
 {  
 return 0.0174532925*lat;  
 }