From 93e20c18436b4b057428db3a2c15acaa2b84b354 Mon Sep 17 00:00:00 2001 From: Moises Arcos Date: Thu, 23 Feb 2012 14:06:56 +0100 Subject: [PATCH] Add new files and modified some existing file to internationalizate OSRM turns instructions. --- DataStructures/TurnInstructions.h | 12 +- DataStructures/TurnInstructionsList.h | 138 ++++++++++++++++++ DataStructures/languages/EN_EN | 15 ++ DataStructures/languages/{ES_ES.txt => ES_ES} | 2 +- DataStructures/languages/fr | 16 ++ Descriptors/BaseDescriptor.h | 4 +- Descriptors/GPXDescriptor.h | 3 +- Descriptors/JSONDescriptor.h | 16 +- Plugins/ObjectForPluginStruct.h | 6 +- Plugins/ViaRoutePlugin.h | 10 +- routed.cpp | 4 +- server.ini | 12 +- 12 files changed, 214 insertions(+), 24 deletions(-) create mode 100644 DataStructures/TurnInstructionsList.h create mode 100644 DataStructures/languages/EN_EN rename DataStructures/languages/{ES_ES.txt => ES_ES} (97%) create mode 100644 DataStructures/languages/fr diff --git a/DataStructures/TurnInstructions.h b/DataStructures/TurnInstructions.h index a3bcc7643..e9fbc1bb9 100644 --- a/DataStructures/TurnInstructions.h +++ b/DataStructures/TurnInstructions.h @@ -48,18 +48,20 @@ struct TurnInstructionsClass { std::string TurnStrings[15]; std::string Ordinals[11]; + TurnInstructionsClass(){ + }; + //This is a hack until c++0x is available enough to use initializer lists. - TurnInstructionsClass(){ - cout << "########## Entra en TurnInstructionsClass\n"; + TurnInstructionsClass(string nameFile){ // Ahora mismo la ruta estñá puesta de manera manual - char nameFile[] = "/home/usuario/workspace/Project-OSRM/DataStructures/languages/ES_ES.txt"; + //char nameFile[] = "/home/usuario/workspace/Project-OSRM/DataStructures/languages/ES_ES.txt"; // Declare an input file stream ifstream fread; // Open a file for only read - fread.open(nameFile, ifstream::in); + fread.open(nameFile.c_str(), ifstream::in); // Check if there have been any error if (!fread){ - cout << "fault" << endl; + cout << "Fault to read: " << nameFile.c_str() << endl; } // Create a buffer to use for read char buffer[128]; diff --git a/DataStructures/TurnInstructionsList.h b/DataStructures/TurnInstructionsList.h new file mode 100644 index 000000000..4a0abe822 --- /dev/null +++ b/DataStructures/TurnInstructionsList.h @@ -0,0 +1,138 @@ +#ifndef TURNINSTRUCTIONSLIST_H_ +#define TURNINSTRUCTIONSLIST_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "./TurnInstructions.h" + +struct TurnInstructionsListClass { + + // Clase en la que recorreremos la estructura en directorios + list tilist; + vector languages; + + // Constructor vacio + TurnInstructionsListClass(){}; + + // Constructor + TurnInstructionsListClass(std::string path){ + vector archivos; + vector::iterator posArchivo; + posArchivo = archivos.begin(); + FindFile(path.c_str(), archivos); + TurnInstructionsClass ti; + // Vector con los nombres de los archivos a leer + for (int i = 0; i < archivos.size(); ++i){ + ti = TurnInstructionsClass(archivos.at(i)); + posArchivo++; + tilist.push_back(ti); + } + }; + + void FindFile (const char dirname[], vector &archivos){ + static DIR *dir; + static struct dirent *mi_dirent; + // Comprobar que se abre el directorio + if((dir = opendir(dirname)) == NULL) + cout << "Fault in opendir" << endl;; + // Recorremos el contenido del directorio + while ((mi_dirent = readdir(dir)) != NULL){ + // Si no es él mismo (.) o el padre (..) + if (strcmp (mi_dirent->d_name, ".") != 0 && strcmp (mi_dirent->d_name, "..") != 0){ + struct stat structura; + // Leer la ruta completa + string dirnameString = dirname; + string nameFile = mi_dirent->d_name; + string pathFile = dirnameString + "/" + nameFile; + if (stat(pathFile.c_str(), &structura) < 0){ + cout << "Fault in stat" << endl; + }else if (structura.st_mode & S_IFREG){ + archivos.reserve(archivos.size() + 2); + archivos.push_back(pathFile); + boost::to_upper(nameFile); + languages.reserve(languages.size() + 2); + languages.push_back(nameFile); + }else if(structura.st_mode & S_IFDIR){ + FindFile(mi_dirent->d_name, archivos); + } + } + } + if(closedir(dir) == -1){ + cout << "Fault in closedir" << endl; + } + } + + TurnInstructionsClass getTurnInstructions(std::string language){ + boost::to_upper(language); + list ::iterator pos; + int index = -1; + // Cuando no se le pasa ningún idioma + if(index == -1 && language == ""){ + for(int i=0; i #include "BaseDescriptor.h" +#include "../DataStructures/TurnInstructions.h" template class GPXDescriptor : public BaseDescriptor{ @@ -33,7 +34,7 @@ private: std::string tmp; public: void SetConfig(const _DescriptorConfig& c) { config = c; } - void Run(http::Reply & reply, RawRouteData &rawRoute, PhantomNodes &phantomNodes, SearchEngineT &sEngine, unsigned distance) { + void Run(http::Reply & reply, RawRouteData &rawRoute, PhantomNodes &phantomNodes, SearchEngineT &sEngine, unsigned distance, TurnInstructionsClass ti) { reply.content += (""); reply.content += " #include "../DataStructures/StaticGraph.h" +#include "../DataStructures/TurnInstructionsList.h" #include "../Util/GraphLoader.h" @@ -37,8 +38,9 @@ struct ObjectsForQueryStruct { std::vector * names; QueryGraph * graph; unsigned checkSum; + TurnInstructionsListClass turnInstructionsList; - ObjectsForQueryStruct(std::string hsgrPath, std::string ramIndexPath, std::string fileIndexPath, std::string nodesPath, std::string namesPath, std::string psd = "route") { + ObjectsForQueryStruct(std::string hsgrPath, std::string ramIndexPath, std::string fileIndexPath, std::string nodesPath, std::string namesPath, std::string translationsPath, std::string psd = "route") { INFO("loading graph data"); std::ifstream hsgrInStream(hsgrPath.c_str(), ios::binary); //Deserialize road network graph @@ -74,6 +76,8 @@ struct ObjectsForQueryStruct { hsgrInStream.close(); namesInStream.close(); INFO("All query data structures loaded"); + INFO("Loading translations"); + turnInstructionsList = TurnInstructionsListClass(translationsPath); } ~ObjectsForQueryStruct() { diff --git a/Plugins/ViaRoutePlugin.h b/Plugins/ViaRoutePlugin.h index d5ba127cb..6be7ac4d5 100644 --- a/Plugins/ViaRoutePlugin.h +++ b/Plugins/ViaRoutePlugin.h @@ -38,6 +38,8 @@ or see http://www.gnu.org/licenses/agpl.txt. #include "../DataStructures/HashTable.h" #include "../DataStructures/StaticGraph.h" #include "../DataStructures/SearchEngine.h" +#include "../DataStructures/TurnInstructions.h" +#include "../DataStructures/TurnInstructionsList.h" #include "../Util/StringUtil.h" @@ -48,6 +50,7 @@ private: StaticGraph * graph; HashTable descriptorTable; std::string pluginDescriptorString; + TurnInstructionsListClass til; SearchEngine > * searchEngine; public: @@ -56,6 +59,8 @@ public: nodeHelpDesk = objects->nodeHelpDesk; graph = objects->graph; names = objects->names; + // Add translations + til = objects->turnInstructionsList; searchEngine = new SearchEngine >(graph, nodeHelpDesk, names); @@ -80,6 +85,9 @@ public: //Get start and end Coordinate std::string start = routeParameters.options["start"]; std::string dest = routeParameters.options["dest"]; + // Add translations + std::string lang = routeParameters.options["lang"]; + TurnInstructionsClass ti = til.getTurnInstructions(lang); std::vector textCoord = split (start, ','); @@ -196,7 +204,7 @@ public: // INFO("Number of segments: " << rawRoute.segmentEndCoordinates.size()); desc->SetConfig(descriptorConfig); - desc->Run(reply, rawRoute, phantomNodes, *searchEngine, distance); + desc->Run(reply, rawRoute, phantomNodes, *searchEngine, distance, ti); if("" != JSONParameter) { reply.content += ")\n"; diff --git a/routed.cpp b/routed.cpp index ec8a5bc04..2b7a31b5e 100644 --- a/routed.cpp +++ b/routed.cpp @@ -93,7 +93,9 @@ int main (int argc, char *argv[]) { serverConfig.GetParameter("ramIndex"), serverConfig.GetParameter("fileIndex"), serverConfig.GetParameter("nodesData"), - serverConfig.GetParameter("namesData")); + serverConfig.GetParameter("namesData"), + // Add to translate + serverConfig.GetParameter("translations")); h.RegisterPlugin(new HelloWorldPlugin()); diff --git a/server.ini b/server.ini index fbf0108d2..1b9b24d6a 100644 --- a/server.ini +++ b/server.ini @@ -2,8 +2,10 @@ Threads = 8 IP = 0.0.0.0 Port = 5000 -hsgrData=/opt/osm/germany.osrm.hsgr -nodesData=/opt/osm/germany.osrm.nodes -ramIndex=/opt/osm/germany.osrm.ramIndex -fileIndex=/opt/osm/germany.osrm.fileIndex -namesData=/opt/osm/germany.osrm.names +hsgrData=/home/usuario/workspace/osm/comunidad_de_madrid.osrm.hsgr +nodesData=/home/usuario/workspace/osm/comunidad_de_madrid.osrm.nodes +ramIndex=/home/usuario/workspace/osm/comunidad_de_madrid.osrm.ramIndex +fileIndex=/home/usuario/workspace/osm/comunidad_de_madrid.osrm.fileIndex +namesData=/home/usuario/workspace/osm/comunidad_de_madrid.osrm.names + +translations=/home/usuario/workspace/Project-OSRM/DataStructures/languages