Steefmaester

GPS to KML

Jan 16th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. //trame GPS to KML by Steef
  2.  
  3. //main.cpp :
  4. #include <string>
  5. #include <iostream>
  6. #include "GPS.h"
  7.  
  8. using namespace std;
  9.  
  10.  
  11.  
  12. int main()
  13. {
  14.     GPS test;
  15.  
  16.     if(test.extraction_localisation("G:/2015 2016/gps/GPS/Debug/TraceGPS.txt") ==false)
  17.         cout<<" PB extraction"<<endl;
  18.     else cout<<"Succes d'extraction"<<endl;
  19.  
  20.     if(test.ecrire_kml("G:/2015 2016/gps/GPS/Debug/test.kml") == false)
  21.         cout<<" PB ecriture"<<endl;
  22.     else cout<<"Succes d'ecriture"<<endl;
  23.  
  24.     return 0;
  25. }
  26.  
  27. //GPS.h :
  28. #include <string>
  29. #include <vector>
  30. #include <fstream>
  31. #include <sstream>
  32.  
  33. using namespace std;
  34.  
  35. class GPS {
  36.  
  37.     struct trajet
  38.     {
  39.         float latitude;
  40.         float longitude;
  41.     };
  42.  
  43.     private:
  44.  
  45.         vector<trajet> trajet_complet;
  46.  
  47.     public:
  48.  
  49.         GPS();
  50.         bool extraction_localisation(string Nom_fichier);//lire le fichier source et recuprer latitude et longitude de chaque trame et stocker dans trajet_complet
  51.         float GPStoKML( string lati);
  52.         bool ecrire_kml(string Nom_fichier);//ecrire le fichier kml
  53.         vector<string> split(string str, char delimiter);
  54.  
  55. };
  56.  
  57. //GPS.cpp
  58.  
  59. #include "GPS.h"
  60.  
  61.  
  62. GPS::GPS(){
  63.  
  64. }
  65.  
  66. bool GPS::extraction_localisation(string Nom_fichier)
  67. {
  68.     //ouverture d'un flux de lecture
  69.     ifstream FichierSource(Nom_fichier);
  70.     string trame;
  71.     vector <string> decoupage_de_la_trame;
  72.     trajet latitude_longitude;
  73.    
  74.     if (FichierSource)
  75.     {  
  76.         //lecture des lignes et recuperation des localisation
  77.         while (getline(FichierSource, trame)) {
  78.        
  79.             //trouver les coordonnées
  80.             if(trame.find("$GPRMC") != -1)
  81.             {
  82.                 decoupage_de_la_trame = split(trame,',');
  83.  
  84.                 float latitude =GPStoKML(decoupage_de_la_trame.at(3));//recuperation et conversion de la latitude
  85.                 if(decoupage_de_la_trame.at(4) =="S")//chamgement du signe en fonction N/S
  86.                     latitude = latitude * -1;
  87.  
  88.                 float longitude =GPStoKML(decoupage_de_la_trame.at(5));//recuperation et conversion de la longitude
  89.                 if(decoupage_de_la_trame.at(6) =="W")//chamgement du signe en fonction W/E
  90.                     longitude = longitude * -1;
  91.                    
  92.                 latitude_longitude.latitude = latitude ;
  93.                 latitude_longitude.longitude = longitude ;
  94.  
  95.                 trajet_complet.push_back(latitude_longitude);//envoie des coordonnées dans le vecteur
  96.  
  97.             }
  98.  
  99.  
  100.         }
  101.         return true;
  102.  
  103.     }
  104.     else return false;
  105.  
  106.  
  107.  
  108.  
  109. }
  110.  
  111. float GPS::GPStoKML( string lati){
  112.  
  113.     int emplacement_point = lati.find('.');
  114.     string degres = lati.substr (0,emplacement_point-2);
  115.     float Val_degres = stof(degres);//degres
  116.  
  117.     string minutes = lati.substr (degres.size(),2);
  118.     float Val_minutes =stof(minutes);//minutes
  119.  
  120.     string secondes = lati.substr (emplacement_point+1,lati.size()-emplacement_point);
  121.     secondes ="0."+secondes;
  122.     float Val_secondes = stof(secondes)*60;//secondes
  123.  
  124.     float Val_final = Val_degres+ Val_minutes/60 + Val_secondes/3600;//calcul final
  125.  
  126.     return Val_final ;
  127.  
  128.  
  129. }
  130. bool GPS::ecrire_kml(string Nom_fichier){
  131.  
  132.     string debutkml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://www.opengis.net/kml/2.2\"> <Document> <name>Paths</name> <description>Examples of paths. Note that the tessellate tag is by default set to 0. If you want to create tessellated lines, they must be authored(or edited) directly in KML.</description> <Style id=\"yellowLineGreenPoly\"> <LineStyle>  <color>7f00ffff</color><width>4</width> </LineStyle> <PolyStyle>  <color>7f00ff00</color>  </PolyStyle> </Style> <Placemark>  <name>Absolute Extruded</name> <description>Transparent green wall with yellow outlines</description> <styleUrl>#yellowLineGreenPoly</styleUrl> <LineString> <extrude>1</extrude> <tessellate>1</tessellate> <altitudeMode>absolute</altitudeMode><coordinates> ";
  133.     string finkml = "</coordinates></LineString></Placemark></Document></kml>";
  134.  
  135.     //ouverture d'un flux d'écriture
  136.     ofstream fichier_destination(Nom_fichier);
  137.  
  138.     if (fichier_destination)
  139.     {
  140.         fichier_destination<<debutkml<<endl;//Entête du fichier kml
  141.         for (int i = 0; i < trajet_complet.size();i++)
  142.             fichier_destination<<trajet_complet.at(i).longitude<<","<<trajet_complet.at(i).latitude<<",1000"<<endl;//longitude + latitude + hauteur
  143.         fichier_destination<<finkml<<endl;//fin du fichier kml
  144.         return true;
  145.     }
  146.  
  147.     else  return false;
  148.  
  149.  
  150. }
  151.  
  152. vector<string> GPS::split(string str, char delimiter) { //fonction permettant de decouper un string
  153.     vector<string> internal;
  154.     stringstream ss(str);
  155.     string tok;
  156.  
  157.     while(getline(ss, tok, delimiter)) {
  158.         internal.push_back(tok);
  159.     }
  160.  
  161.     return internal;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment