Add new files and modified some existing file to internationalizate OSRM turns instructions.

This commit is contained in:
Moises Arcos 2012-02-23 14:06:56 +01:00
parent 98ecc3c668
commit 93e20c1843
12 changed files with 214 additions and 24 deletions

View File

@ -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];

View File

@ -0,0 +1,138 @@
#ifndef TURNINSTRUCTIONSLIST_H_
#define TURNINSTRUCTIONSLIST_H_
#include <list>
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <cctype>
#include <boost/algorithm/string.hpp>
#include "./TurnInstructions.h"
struct TurnInstructionsListClass {
// Clase en la que recorreremos la estructura en directorios
list<TurnInstructionsClass> tilist;
vector<string> languages;
// Constructor vacio
TurnInstructionsListClass(){};
// Constructor
TurnInstructionsListClass(std::string path){
vector<string> archivos;
vector<string>::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<string> &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 <TurnInstructionsClass>::iterator pos;
int index = -1;
// Cuando no se le pasa ningún idioma
if(index == -1 && language == ""){
for(int i=0; i<languages.size(); i++){
if(languages.at(i).find("EN")==0){
index = i;
break;
}
}
}
// Cuando se le pasa por parametro lang=ES_ES y lo que tenemos es ES_ES
for(int i=0; i<languages.size(); i++){
if(language.compare(languages.at(i)) == 0){
index = i;
break;
}
}
// Cuando se le pasa por parametro lang=ES y lo que tenemos es ES_ES
if(index == -1){
for(int i=0; i<languages.size(); i++){
if(language != "" && languages.at(i).find(language)==0){
index = i;
break;
}
}
}
// Cuando se le pasa por parámetro lang=ES_ES y lo que tenemos es ES
if(index == -1){
for(int i=0; i<languages.size(); i++){
if(language.find(languages.at(i))==0){
index = i;
break;
}
}
}
// Cuando se le pasa por parámetro ES_AR y tenemos ES debe devolver ES
if(index == -1){
for(int i=0; i<languages.size(); i++){
int pos = language.find("_");
string lang = language.substr(0, pos);
if(languages.at(i).find(lang)==0){
index = i;
break;
}
}
}
// Cuando no encuentra idioma que tome el por defecto EN
if(index == -1){
for(int i=0; i<languages.size(); i++){
if(languages.at(i).find("EN")==0){
index = i;
break;
}
}
}
pos = tilist.begin();
for(int i=0; i<index; i++){
pos++;
}
return *pos;
}
};
#endif /* TURNINSTRUCTIONSLIST_H_ */

View File

@ -0,0 +1,15 @@
Continue
Turn slight right
Turn right
Turn sharp right
U-Turn
Turn sharp left
Turn left
Turn slight left
Reach via point
Head
Enter round-about
Leave round-about
Stay on round-about
Start

View File

@ -8,7 +8,7 @@ Girar a la izquierda
Gire a la izquierda
Gire a la izquierda
Llegar a un punto intermedio
cabeza
Cabeza
Introduzca rotonda
Deja rotonda
Permanezca en rotonda

View File

@ -0,0 +1,16 @@
Continuer
Tourner à droite
Tourner à droite
Tournez à droite forte
U-Turn
Tourner à gauche
Tourner à gauche
Tourner à gauche
Parvenir à un compromis
Tête
Entrez rond-point
Laissez rond-point
Rester sur rond-point
Commencer

View File

@ -29,6 +29,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../typedefs.h"
#include "../DataStructures/ExtractorStructs.h"
#include "../DataStructures/TurnInstructions.h"
#include "../DataStructures/HashTable.h"
#include "../Util/StringUtil.h"
@ -48,7 +49,8 @@ public:
BaseDescriptor() { }
//Maybe someone can explain the pure virtual destructor thing to me (dennis)
virtual ~BaseDescriptor() { }
virtual void Run(http::Reply & reply, RawRouteData &rawRoute, PhantomNodes &phantomNodes, SearchEngineT &sEngine, unsigned distance) = 0;
// Add translations
virtual void Run(http::Reply & reply, RawRouteData &rawRoute, PhantomNodes &phantomNodes, SearchEngineT &sEngine, unsigned distance, TurnInstructionsClass ti) = 0;
virtual void SetConfig(const _DescriptorConfig & config) = 0;
};

View File

@ -23,6 +23,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include <boost/foreach.hpp>
#include "BaseDescriptor.h"
#include "../DataStructures/TurnInstructions.h"
template<class SearchEngineT>
class GPXDescriptor : public BaseDescriptor<SearchEngineT>{
@ -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 += ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
reply.content += "<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" "
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "

View File

@ -47,7 +47,7 @@ public:
JSONDescriptor() {}
void SetConfig(const _DescriptorConfig & c) { config = c; }
void Run(http::Reply & reply, RawRouteData &rawRoute, PhantomNodes &phantomNodes, SearchEngineT &sEngine, const unsigned durationOfTrip) {
void Run(http::Reply & reply, RawRouteData &rawRoute, PhantomNodes &phantomNodes, SearchEngineT &sEngine, const unsigned durationOfTrip, TurnInstructionsClass ti) {
WriteHeaderToOutput(reply.content);
if(durationOfTrip != INT_MAX) {
descriptionFactory.SetStartSegment(phantomNodes.startPhantom);
@ -102,8 +102,8 @@ public:
std::string tmpDist, tmpLength, tmpDuration, tmpBearing;
//Fetch data from Factory and generate a string from it.
BOOST_FOREACH(SegmentInformation & segment, descriptionFactory.pathDescription) {
if(TurnInstructions.TurnIsNecessary( segment.turnInstruction) ) {
if(TurnInstructions.EnterRoundAbout == segment.turnInstruction) {
if(ti.TurnIsNecessary( segment.turnInstruction) ) {
if(ti.EnterRoundAbout == segment.turnInstruction) {
roundAbout.nameID = segment.nameID;
roundAbout.startIndex = prefixSumOfNecessarySegments;
} else {
@ -111,14 +111,14 @@ public:
reply.content += ",";
reply.content += "[\"";
if(TurnInstructions.LeaveRoundAbout == segment.turnInstruction) {
reply.content += TurnInstructions.TurnStrings[TurnInstructions.EnterRoundAbout];
if(ti.LeaveRoundAbout == segment.turnInstruction) {
reply.content += ti.TurnStrings[ti.EnterRoundAbout];
reply.content += " and leave at ";
reply.content += TurnInstructions.Ordinals[roundAbout.leaveAtExit+1];
reply.content += ti.Ordinals[roundAbout.leaveAtExit+1];
reply.content += " exit";
roundAbout.leaveAtExit = 0;
} else {
reply.content += TurnInstructions.TurnStrings[segment.turnInstruction];
reply.content += ti.TurnStrings[segment.turnInstruction];
}
reply.content += "\",\"";
reply.content += sEngine.GetEscapedNameForNameID(segment.nameID);
@ -141,7 +141,7 @@ public:
reply.content += tmpBearing;
reply.content += "]";
}
} else if(TurnInstructions.StayOnRoundAbout == segment.turnInstruction) {
} else if(ti.StayOnRoundAbout == segment.turnInstruction) {
++roundAbout.leaveAtExit;
}
if(segment.necessary)

View File

@ -26,6 +26,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include<string>
#include "../DataStructures/StaticGraph.h"
#include "../DataStructures/TurnInstructionsList.h"
#include "../Util/GraphLoader.h"
@ -37,8 +38,9 @@ struct ObjectsForQueryStruct {
std::vector<std::string> * 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() {

View File

@ -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<EdgeData> * graph;
HashTable<std::string, unsigned> descriptorTable;
std::string pluginDescriptorString;
TurnInstructionsListClass til;
SearchEngine<EdgeData, StaticGraph<EdgeData> > * searchEngine;
public:
@ -56,6 +59,8 @@ public:
nodeHelpDesk = objects->nodeHelpDesk;
graph = objects->graph;
names = objects->names;
// Add translations
til = objects->turnInstructionsList;
searchEngine = new SearchEngine<EdgeData, StaticGraph<EdgeData> >(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<std::string> 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";

View File

@ -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());

View File

@ -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