• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • tools
  • osm-sisyphus
job.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2011 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
11 #include "job.h"
12 #include "logger.h"
13 #include "upload.h"
14 
15 #include <QDebug>
16 #include <QDateTime>
17 #include <QProcess>
18 
19 Job::Job(const Region &region, const JobParameters &parameters, QObject *parent) :
20  QObject(parent), m_status(Waiting), m_region(region), m_parameters(parameters)
21 {
22  // nothing to do
23 }
24 
25 Job::Status Job::status() const
26 {
27  return m_status;
28 }
29 
30 QString Job::statusMessage() const
31 {
32  return m_statusMessage;
33 }
34 
35 Region Job::region() const
36 {
37  return m_region;
38 }
39 
40 void Job::setTransport(const QString &transport)
41 {
42  m_transport = transport;
43 }
44 
45 QString Job::transport() const
46 {
47  return m_transport;
48 }
49 
50 void Job::setProfile(const QString &profile)
51 {
52  m_profile = profile;
53 }
54 
55 void Job::setMonavSettings(const QString &filename)
56 {
57  m_monavSettings = filename;
58 }
59 
60 bool Job::operator ==(const Job &other) const
61 {
62  return m_transport == other.m_transport && m_region == other.m_region;
63 }
64 
65 void Job::run()
66 {
67  if (download() && monav() && search() && package() && upload()) {
68  // Nothing to do.
69  }
70 
71  cleanup();
72  emit finished(this);
73 }
74 
75 void Job::changeStatus(Job::Status status, const QString &message)
76 {
77  QString statusType;
78  switch (status) {
79  case Waiting: statusType = "waiting"; break;
80  case Downloading: statusType = "downloading"; break;
81  case Routing: statusType = "routing"; break;
82  case Search: statusType = "search"; break;
83  case Packaging: statusType = "packaging"; break;
84  case Uploading: statusType = "uploading"; break;
85  case Finished: statusType = "finished"; break;
86  case Error: statusType = "error"; break;
87  }
88 
89  Logger::instance().setStatus(m_region.id() + '_' + m_transport,
90  m_region.name() + QLatin1String( " (" ) + m_transport + ')', statusType, message);
91  m_statusMessage = message;
92  m_status = status;
93 }
94 
95 bool Job::download()
96 {
97  changeStatus(Downloading, "Downloading data.");
98  qDebug() << "Saving file to " << osmFile().absoluteFilePath();
99  if (osmFile().exists()) {
100  QDateTime now = QDateTime::currentDateTime();
101  if (osmFile().lastModified().daysTo(now) > 7) {
102  qDebug() << "Old file is outdated, re-downloading " << osmFile().absoluteFilePath();
103  QFile::remove(osmFile().absoluteFilePath());
104  } else {
105  qDebug() << "Old file is still ok, reusing" << osmFile().absoluteFilePath();
106  return true;
107  }
108  }
109 
110  QProcess wget;
111  QStringList arguments;
112  QString url = m_region.pbfFile();
113  arguments << "-O" << osmFile().absoluteFilePath() << url;
114  qDebug() << "Downloading " << url;
115  wget.start("wget", arguments);
116  wget.waitForFinished(1000 * 60 * 60 * 12); // wait up to 12 hours for download to complete
117  if (wget.exitStatus() == QProcess::NormalExit && wget.exitCode() == 0) {
118  return true;
119  } else {
120  qDebug() << "Failed to download " << url;
121  QFile::remove(osmFile().absoluteFilePath());
122  changeStatus(Error, "Error downloading .osm.pbf file: " + wget.readAllStandardError());
123  return false;
124  }
125 }
126 
127 //bool Job::marble()
128 //{
129 // changeStatus(Routing, "Extracting bounding box.");
130 // QStringList arguments;
131 // arguments << "--name" << m_region.name();
132 // arguments << "--version" << "0.2";
133 // arguments << "--date" << QDateTime::currentDateTime().toString("yyyy/dd/MM");
134 // arguments << "--transport" << m_transport;
135 // arguments << "--payload" << targetFile().fileName();
136 // arguments << m_parameters.base().absoluteFilePath("poly/" + m_region.polyFile());
137 // arguments << monavDir().absoluteFilePath() + "/marble.kml";
138 // QProcess poly2kml;
139 // poly2kml.start("poly2kml", arguments);
140 // poly2kml.waitForFinished(1000 * 60 * 30); // wait up to half an hour for poly2kml to convert the data
141 // if (poly2kml.exitStatus() == QProcess::NormalExit && poly2kml.exitCode() == 0) {
142 // qDebug() << "Processed kml file for marble";
143 // return true;
144 // } else {
145 // qDebug() << "poly2kml exiting with status " << poly2kml.exitCode();
146 // changeStatus(Error, "Error creating marble.kml: " + poly2kml.readAllStandardError());
147 // return false;
148 // }
149 //}
150 
151 bool Job::monav()
152 {
153  QString const status = QString("Generating offline routing map from %1 (%2).").arg(osmFile().fileName()).arg(Region::fileSize(osmFile()));
154  changeStatus(Routing, status);
155  QStringList arguments;
156  arguments << "-s=" + m_monavSettings;
157  arguments << "-i=" + osmFile().absoluteFilePath();
158  arguments << "-o=" + monavDir().absoluteFilePath();
159  arguments << "-pi=OpenStreetMap Importer" << "-pro=Contraction Hierarchies";
160  arguments << "-pg=GPS Grid" << "-di";
161  arguments << "-dro=" + m_transport;
162  arguments << "--profile=" + m_profile;
163  arguments << "-dd" /*<< "-dc"*/;
164  QProcess monav;
165  monav.start("monav-preprocessor", arguments);
166  monav.waitForFinished(1000 * 60 * 60 * 6); // wait up to 6 hours for monav to convert the data
167  if (monav.exitStatus() == QProcess::NormalExit && monav.exitCode() == 0) {
168  qDebug() << "Processed osm file for monav";
169  } else {
170  qDebug() << "monav exiting with status " << monav.exitCode();
171  changeStatus(Error, "Routing map conversion failed: " + monav.readAllStandardError());
172  return false;
173  }
174 
175  QFile pluginsFile(monavDir().absoluteFilePath() + "/plugins.ini");
176  pluginsFile.open(QFile::WriteOnly | QFile::Truncate);
177  QTextStream pluginsStream(&pluginsFile);
178  pluginsStream << "[General]\nrouter=Contraction Hierarchies\nrenderer=Mapnik Renderer\ngpsLookup=GPS Grid\naddressLookup=Unicode Tournament Trie\n";
179  pluginsFile.close();
180 
181  QFileInfo subdir = QFileInfo(monavDir().absoluteFilePath() + "/routing_" + m_transport.toLower());
182  if (subdir.exists() && subdir.isDir()) {
183  QFileInfoList files = QDir(subdir.absoluteFilePath()).entryInfoList(QDir::Files);
184  foreach(const QFileInfo &file, files) {
185  if (!QFile::rename(file.absoluteFilePath(), monavDir().absoluteFilePath() + '/' + file.fileName())) {
186  changeStatus(Error, "Unable to move monav files to target directory.");
187  return false;
188  }
189  }
190  QDir("/").rmdir(subdir.absoluteFilePath());
191  } else {
192  changeStatus(Error, "Unable to find files created by monav");
193  return false;
194  }
195 
196  return true;
197 }
198 
199 bool Job::search()
200 {
201  QString const status = QString("Generating offline search database from %1 (%2).").arg(osmFile().fileName()).arg(Region::fileSize(osmFile()));
202  changeStatus(Search, status);
203  QStringList arguments;
204  arguments << "--name" << m_region.name();
205  arguments << "--version" << "0.3";
206  arguments << "--date" << QDateTime::currentDateTime().toString("MM/dd/yy");
207  arguments << "--transport" << m_transport;
208  arguments << "--payload" << targetFile().fileName();
209  arguments << osmFile().absoluteFilePath();
210  arguments << searchFile().absoluteFilePath();
211  QFileInfo kmlFile(monavDir().absoluteFilePath() + "/marble.kml");
212  arguments << kmlFile.absoluteFilePath();
213  QProcess osmAddresses;
214  osmAddresses.start("osm-addresses", arguments);
215  osmAddresses.waitForFinished(1000 * 60 * 60 * 18); // wait up to 18 hours for osm-addresses to convert the data
216  if (osmAddresses.exitStatus() == QProcess::NormalExit && osmAddresses.exitCode() == 0) {
217  searchFile().refresh();
218  if (!searchFile().exists()) {
219  qDebug() << "osm-addresses did not create the .sqlite file";
220  changeStatus(Error, "Unknown error when creating the search database");
221  return false;
222  } else if (searchFile().size() < 8000) {
223  qDebug() << "The .sqlite database has a suspiciously small size.";
224  changeStatus(Error, "Search database is too small. Too little memory?");
225  return false;
226  }
227 
228  kmlFile.refresh();
229  if (!kmlFile.exists()) {
230  qDebug() << "File marble.kml has not been generated.";
231  changeStatus(Error, "Failed to generate marble.kml. Too little memory?");
232  return false;
233  }
234 
235  return true;
236  } else {
237  qDebug() << "osm-addresses exiting with status " << osmAddresses.exitCode();
238  changeStatus(Error, "Error creating search database: " + osmAddresses.readAllStandardError());
239  return false;
240  }
241 }
242 
243 bool Job::package()
244 {
245  changeStatus(Packaging, "Creating archive.");
246  QStringList arguments;
247  arguments << "czf" << targetFile().absoluteFilePath() << "earth/monav/" << "earth/placemarks";
248  QProcess tar;
249  tar.setWorkingDirectory(m_parameters.base().absolutePath() + "/data/" + m_region.id());
250  tar.start("tar", arguments);
251  tar.waitForFinished(1000 * 60 * 60); // wait up to 1 hour for tar to package things
252  if (tar.exitStatus() == QProcess::NormalExit && tar.exitCode() == 0) {
253  qDebug() << "Packaged tar file";
254  return true;
255  } else {
256  changeStatus(Error, "Packaging failed: " + tar.readAllStandardError());
257  return false;
258  }
259 }
260 
261 bool Job::upload()
262 {
263  changeStatus(Uploading, "Uploading file");
264  if (targetFile().exists()) {
265  Upload::instance().uploadAndDelete(m_region, targetFile(), m_transport);
266  return true;
267  }
268 
269  changeStatus(Error, "Target file does not exist.");
270  return false;
271 }
272 
273 bool Job::cleanup()
274 {
275  if (!m_parameters.cacheData()) {
276  QFile::remove(osmFile().absoluteFilePath());
277  }
278 
279  QFileInfo subdir = QFileInfo(monavDir().absoluteFilePath());
280  if (subdir.exists() && subdir.isDir()) {
281  QFileInfoList files = QDir(subdir.absoluteFilePath()).entryInfoList(QDir::Files);
282  foreach(const QFileInfo &file, files) {
283  QFile::remove(file.absoluteFilePath());
284  }
285  }
286 
287  QFile::remove(searchFile().absoluteFilePath());
288  return true;
289 }
290 
291 QFileInfo Job::osmFile()
292 {
293  m_parameters.base().mkdir("download");
294  QFileInfo result(m_parameters.base(), QString("download/") + m_region.id() + ".osm.pbf");
295  return result;
296 }
297 
298 QFileInfo Job::monavDir()
299 {
300  QString const subdir = "data/" + m_region.id() + "/earth/monav/" + m_transport.toLower() + '/' + m_region.path();
301  m_parameters.base().mkpath(subdir);
302  QFileInfo result(m_parameters.base(), subdir);
303  return result;
304 }
305 
306 QFileInfo Job::targetFile()
307 {
308  m_parameters.base().mkdir("finished");
309  QFileInfo result(m_parameters.base(), "finished/" + m_region.id() + '_' + m_transport.toLower() + ".tar.gz");
310  return result;
311 }
312 
313 QFileInfo Job::searchFile()
314 {
315  QString const subdir = "data/" + m_region.id() + "/earth/placemarks/" + QFileInfo(m_region.path()).path();
316  m_parameters.base().mkpath(subdir);
317  QFileInfo result(m_parameters.base(), subdir + '/' + m_region.id() + ".sqlite");
318  return result;
319 }
320 
321 #include "job.moc"
Job::setTransport
void setTransport(const QString &transport)
Definition: job.cpp:40
Job::Finished
Definition: job.h:32
Job::Downloading
Definition: job.h:27
Job::Status
Status
Definition: job.h:25
Upload::uploadAndDelete
void uploadAndDelete(const Region &region, const QFileInfo &file, const QString &transport)
Definition: upload.cpp:254
Job::setProfile
void setProfile(const QString &profile)
Definition: job.cpp:50
upload.h
Job::setMonavSettings
void setMonavSettings(const QString &filename)
Definition: job.cpp:55
Job::Waiting
Definition: job.h:26
search
Definition: google-search.qml:12
Job::transport
QString transport() const
Definition: job.cpp:45
Region::pbfFile
QString pbfFile
Definition: region.h:23
Job
Definition: job.h:21
Job::finished
void finished(Job *job)
QObject
Job::status
Status status() const
Definition: job.cpp:25
Routing
Definition: Routing.qml:15
Region
Definition: region.h:18
Job::run
virtual void run()
Definition: job.cpp:65
Job::Uploading
Definition: job.h:31
Job::Packaging
Definition: job.h:30
Logger::setStatus
void setStatus(const QString &id, const QString &name, const QString &status, const QString &message)
Definition: logger.cpp:69
JobParameters
Definition: jobparameters.h:16
Search
Definition: Search.qml:15
Region::path
QString path
Definition: region.h:25
Upload::instance
static Upload & instance()
Definition: upload.cpp:271
job.h
Region::id
QString id
Definition: region.h:24
Job::statusMessage
QString statusMessage() const
Definition: job.cpp:30
logger.h
Logger::instance
static Logger & instance()
Definition: logger.cpp:53
Job::region
Region region() const
Definition: job.cpp:35
JobParameters::base
QDir base()
Definition: jobparameters.cpp:18
Job::Job
Job(const Region &region, const JobParameters &parameters, QObject *parent=0)
Definition: job.cpp:19
Job::Error
Definition: job.h:33
Job::operator==
bool operator==(const Job &other) const
Definition: job.cpp:60
Region::fileSize
static QString fileSize(const QFileInfo &file)
Definition: region.cpp:90
Region::name
QString name
Definition: region.h:22
JobParameters::cacheData
bool cacheData() const
Definition: jobparameters.cpp:28
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal