/* 
** geo2xml erzeugen
**
** 05.08.2008 routen nachfahren funktioniert, deshalb hier eine 
**            Verbesserung
**            Erzeugt direkt eine .ikt datei
** Quelle der trk files:
** http://212.66.132.199/cgi-bin/rph.cgi?formID=initForm&formURL=rphFrameset.html&frameMapParameter=-1,-1,-1,-1,,,1,,,Frankfurt,Galvanistra%C3%9Fe%2028
** ikt files werden mit magicmaps (Kartenwerk deutschland) eingelesen.
** von dort kann GARMIn Geko geladen werden
**
** 28.06.2011 li der radroutenplaner Hessen liefert keine .trk Dateien mehr. Deshalb Umstellung auf
**            kml -> itk
** ---------------------------- KML Input       
**  <LineString>
**                 Laenge   Breite    Höhe
**                 llllllll bbbbbbbbb hhhhh
**    <coordinates>8.643808,50.087271,97.54 8.644133,50.087307,97.61 8.643844,50.088193,98.01 8.643644,50.088777,97.02</coordinates>
**  </LineString>
*/


#include <string.h>             /* for memcpy() */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>

char buffer[64024];
char temp[ 256 ];
char temp2[ 256 ];
char infile[ 256 ];
char outfile[ 256 ];

int main(argc, argv)
int argc;
char **argv;
{
   FILE *fr;
   FILE *fw;
   
   char *s,*e,*e2;
   int i=0;
   int found = 0;
   int n,records,len,pos;
   char cor[]= "<coordinates>";
   char text[ 256 ];
   
   if ( argc < 2 )
   {
      printf("use geo2xml <infile>.kml \n");
      return(1);
   }

   if ( strlen(argv[1]) )
   {
      sprintf( infile ,"%s.kml", argv[1] );
      sprintf( outfile,"%s.ikt", argv[1] );
      printf("geo2xml Input : %s\n",infile);
      fr =fopen( infile, "r");
      if ( fr == NULL )
      {
         printf("open error inputfile");
         return(1);
      }
   }
   else
     return(1);
      
   printf("geo2xml Output: %s\n",outfile);
   fw =fopen( outfile, "w");
   if ( fw == NULL )
   {
      printf("open error outputfile");
      return(1);
   }   
   fr = fopen( infile, "r");      
                                                 // 1. Lauf Anzahl Punkte ermitteln
   while ( fgets( buffer, 64000, fr ) != NULL )
   {
	   len = strlen(buffer);
						
      if ( len > 0 )
      {
         s = &buffer[0];
         found = 0;
         for ( pos=0; pos < 10; pos++)         
         {
            if ( strncmp( s, cor, 13) == 0 )    // "<coordinates>" suchen 
            {
               found = 1;
               break;
            }
            else
            {
               s++;
            }                  
         }
         if ( found == 1 )       // "<coordinates>" gefunden
         {
            s += 13;            
            
            do                   // loop bis ende string 
            {
               found = 0;
               e = s;
               for ( pos=0; pos < 40; pos++)
               {
                  if ( *e == ' ' )
                  {
                     found = 1;
                     break;
                  }
                  else if ( *e == '<' )
                  {
                     found = 2;
                     break;
                  }                  
                  else
                  {
                     e++;
                  }       
               } 
               s += pos + 1;                 
               i++;                 
            } while ( found < 2 ); 
         }   
      }
   }   
   fclose(fr);

   printf(" Punkte = %d\n",i);    
   
   sprintf(temp,"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
   fputs(temp, fw);
   sprintf(temp," <Root FileFormat=\"IK3D-Project-File\" >\n");
   fputs(temp, fw);
   sprintf(temp," <Info>\n");
   fputs(temp, fw);
   sprintf(temp,"  <ProductVersion>\n");
   fputs(temp, fw);
   sprintf(temp,"   <Version>Germany TK25</Version>\n");
   fputs(temp, fw);
   sprintf(temp,"   <FileFormatVersion>1.1</FileFormatVersion>\n");
   fputs(temp, fw);
   sprintf(temp,"  </ProductVersion>\n");
   fputs(temp, fw);
   sprintf(temp,"  <Language>German</Language>\n");
   fputs(temp, fw);
   sprintf(temp," </Info>\n");
   fputs(temp, fw);
   sprintf(temp," <Header>\n");
   fputs(temp, fw);
   sprintf(temp,"  <Projectname>Undefiniert</Projectname>\n");
   fputs(temp, fw);
   sprintf(temp,"  <countOfObjects>1</countOfObjects>\n");
   fputs(temp, fw);
   sprintf(temp,"  <description><![CDATA[keine Beschreibung vorhanden]]></description>\n");
   fputs(temp, fw);
   sprintf(temp," </Header>\n");
   fputs(temp, fw);
   sprintf(temp," <Content>\n");
   fputs(temp, fw);
   sprintf(temp,"  <MMGeoObjects>\n");
   fputs(temp, fw);
   sprintf(temp,"   <count>1</count>\n");
   fputs(temp, fw);
   sprintf(temp,"   <MMGeoObject_0>\n");
   fputs(temp, fw);
   sprintf(temp,"    <GeoObjectType>1</GeoObjectType>\n");
   fputs(temp, fw);
   sprintf(temp,"    <Name>%s</Name>\n",argv[1]);
   fputs(temp, fw);
   sprintf(temp,"    <Remark></Remark>\n");
   fputs(temp, fw);
   sprintf(temp,"    <Link></Link>\n");
   fputs(temp, fw);
   sprintf(temp,"    <Priority>0</Priority>\n");
   fputs(temp, fw);
   sprintf(temp,"    <ID>396071682842359565</ID>\n");
   fputs(temp, fw);
   sprintf(temp,"    <Locked>False</Locked>\n");
   fputs(temp, fw);
   sprintf(temp,"    <PathWidthFactor>1.0000000000</PathWidthFactor>\n");
   fputs(temp, fw);
   sprintf(temp,"    <PathDrawType>5</PathDrawType>\n");
   fputs(temp, fw);
   sprintf(temp,"    <PathPoints>\n");       
   fputs(temp, fw);
   sprintf(temp,"    <count>%d</count>\n", i);         // deswegen 1. Lauf
   fputs(temp, fw);
   records = 29 ;
   
   i=0;
   n=0;
   fr =fopen( infile, "r");                            // jetzt auswerten 
   
   while ( fgets( buffer, 64000, fr ) != NULL )
   {	
      len = strlen(buffer);
						
      if ( len > 0 )
      {
         s = &buffer[0];
         found = 0;
         for ( pos=0; pos < 10; pos++)         
         {
            if ( strncmp( s, cor, 13) == 0 )    // "<coordinates>" suchen 
            {
               found = 1;
               break;
            }
            else
            {
               s++;
            }                  
         }
         if ( found == 1 )       // "<coordinates>" gefunden
         {
            s += 13;            

            do                   // loop bis ende string 
            {
               found = 0;
               e = s;
               for ( pos=0; pos < 40; pos++)
               {
                  if ( *e == ' ' )
                  {
                     found = 1;
                     break;
                  }
                  else if ( *e == '<' )
                  {
                     found = 2;
                     break;
                  }
                  else if ( *e == ',' )
                  {
                     *e = ' ';
                     e++;                     
                  }
                  else
                  {
                     e++;
                  }       
               }       
               memcpy( text, s, pos );
               text[pos] = '\0';
               
                                  // ------------------- Punkt erzeugen 
               sprintf(temp,"        <Point_%d>\n",i);
               fputs(temp, fw);
               sprintf(temp,"        <Link></Link>\n");
               fputs(temp, fw);
               
               /* Umwandlung coordinaten paar nach 4 records */ 
               
               /* soll: <Point_2>
                         <Link></Link>  
                         <GeoPosition Y="50.087269" X="8.643787" /> 
                        </Point_2>                      
               */                     
               sprintf(temp,"         <GeoPosition");                    
               e = text;
               n = 0; 
               while ( *e != ' ' )
               {
                  n++;
                  e++ ;
                  if ( n > 12 )
                  {
                     printf("blank not found\n");
                     break;
                  }  
               }
               *e = '\0';
               sprintf( temp2," X=\"%s\"", text);
               strcat( temp, temp2);
               
               e += 1;               
               e2 = e;
               n=0;
               while ( *e2 != ' ' )
               { 
                  n++;
                  e2++ ;
                  if ( n > 12 )
                  {
                     printf("blank not found\n");
                     break;
                  }  
               }
               *e2 = '\0';
               sprintf(temp2," Y=\"%s\" />\n",e);
               strcat(temp,temp2);                             
               fputs(temp,fw);
                             
               sprintf(temp,"        </Point_%d>\n",i);
               fputs(temp, fw);
               i++;
               n=0;
               records += 4 ;               
               s += pos + 1;  
            } while ( found < 2 ); 
         }   
      }
   }
   sprintf(temp,"    </PathPoints>\n"); 
   fputs(temp, fw);
   sprintf(temp,"    <PathDrawable2D>\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <PathColor Blue=\"1.0000\" Red=\"1.0000\" Green=\"0.3333\" />\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <PointColor Blue=\"0.0000\" Red=\"1.0000\" Green=\"1.0000\" />\n"); 
   fputs(temp, fw);      
   sprintf(temp,"     <MarkingColor Blue=\"1.0000\" Red=\"1.0000\" Green=\"0.0000\" />\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <Alpha>0.6800000000</Alpha>\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <Antialiasing>False</Antialiasing>\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <CirclePieces>12</CirclePieces>\n"); 
   fputs(temp, fw);
   sprintf(temp,"    </PathDrawable2D>\n"); 
   fputs(temp, fw);
   sprintf(temp,"    <PathDrawable3D>\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <PathColor Blue=\"1.0000\" Red=\"1.0000\" Green=\"0.3333\" />\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <PointColor Blue=\"0.0000\" Red=\"1.0000\" Green=\"1.0000\" />\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <MarkingColor Blue=\"1.0000\" Red=\"1.0000\" Green=\"0.0000\" />\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <Alpha>1.0000000000</Alpha>\n"); 
   fputs(temp, fw);
   sprintf(temp,"     <Antialiasing>False</Antialiasing>\n"); 
   fputs(temp, fw);
   sprintf(temp,"    </PathDrawable3D>\n"); 
   fputs(temp, fw);
   sprintf(temp,"   </MMGeoObject_0>\n"); 
   fputs(temp, fw);
   sprintf(temp,"  </MMGeoObjects>\n"); 
   fputs(temp, fw);
   sprintf(temp," </Content>\n"); 
   fputs(temp, fw);
   sprintf(temp,"</Root>\n");
   fputs(temp, fw);
   records += 20 ;
      
   fclose(fr);
   fclose(fw);
   printf("geo2xml %d records written\n",records);
   exit(0);
   return(0);
}
