Commit b34de11f authored by Sutirtha Ghosh's avatar Sutirtha Ghosh Committed by Albert Astals Cid

Automatic download of .osm files in the "Edit Maps" Panel

I have added an icon in the Edits Maps Panel for downloading .osm file
for the current viewport(visible region)

    I have created a Dialog which opens on clicking the download icon(in
Edits Map Panel) and then in the Dialog on clicking download it tries to
download the .osm file from server.
    In case of network error or too large visible region an error
message will be shown telling to zoom in .
    It shows a Busy indicator progress bar since server does not support
reporting the size of the file.
    The .osm file is saved in a temporary directory
    If the download is successful then the file is opened automatically
using loadAnnotationfile(const QString& )

Differential Revision: https://phabricator.kde.org/D836
parent 1b8698a6
......@@ -114,5 +114,6 @@
<file alias="icons/animator.png">icons/16x16/tool-animator.png</file>
<file>icons/map-gnomonic.png</file>
<file>icons/edit-select.png</file>
<file>icons/download.png</file>
</qresource>
</RCC>
......@@ -59,6 +59,7 @@
#include "ParsingRunnerManager.h"
#include "ViewportParams.h"
#include "osm/OsmPlacemarkData.h"
#include "DownloadOsmDialog.h"
namespace Marble
{
......@@ -406,14 +407,30 @@ void AnnotatePlugin::loadAnnotationFile()
if ( filename.isNull() ) {
return;
}
else{
openAnnotationFile(filename);
}
}
void AnnotatePlugin::downloadOsm()
{
QPointer<DownloadOsmDialog> dialog=new DownloadOsmDialog(m_marbleWidget,this);
dialog->show();
}
ParsingRunnerManager manager( m_marbleWidget->model()->pluginManager() );
GeoDataDocument *document = manager.openFile( filename );
void AnnotatePlugin::openAnnotationFile(const QString& filename)
{
if ( filename.isNull() ) {
return;
}
ParsingRunnerManager manager( m_marbleWidget->model()->pluginManager(),this );
GeoDataDocument *document = manager.openFile( filename);
Q_ASSERT( document );
// FIXME: The same problem as in the case of copying/cutting graphic items applies here:
// the files do not load properly because the geometry copy is not a deep copy.
for ( GeoDataFeature *feature: document->featureList() ) {
foreach ( GeoDataFeature *feature, document->featureList() ) {
if (const GeoDataPlacemark *placemark = geodata_cast<GeoDataPlacemark>(feature)) {
GeoDataPlacemark *newPlacemark = new GeoDataPlacemark( *placemark );
......@@ -943,6 +960,11 @@ void AnnotatePlugin::setupActions( MarbleWidget *widget )
QAction *clearAnnotations = new QAction( QIcon(QStringLiteral(":/icons/remove.png")),
tr("Clear all Annotations"),
this );
QAction *downloadOsm = new QAction( QIcon(":/icons/download.png"),
tr("Download OpenStreetMap Data"),
this );
connect( downloadOsm, SIGNAL(triggered(bool)), this, SLOT(downloadOsm()) );
downloadOsm->setToolTip(tr("Download OpenStreetMap data of the visible region"));
connect( drawPolygon, SIGNAL(toggled(bool)), clearAnnotations, SLOT(setDisabled(bool)) );
connect( clearAnnotations, SIGNAL(triggered()), this, SLOT(clearAnnotations()) );
......@@ -972,6 +994,7 @@ void AnnotatePlugin::setupActions( MarbleWidget *widget )
group->addAction( removeItem );
group->addAction( sep3 );
group->addAction( clearAnnotations );
group->addAction(downloadOsm);
group->addAction( sep4 );
m_actions.append( group );
......
......@@ -96,6 +96,7 @@ private Q_SLOTS:
void clearAnnotations();
void saveAnnotationFile();
void loadAnnotationFile();
void openAnnotationFile(const QString&);
void copyItem();
void cutItem();
void pasteItem();
......@@ -126,6 +127,7 @@ private Q_SLOTS:
void setPolylineAvailable();
void addRelation( const OsmPlacemarkData &relationOsmData );
void downloadOsm();
protected:
bool eventFilter( QObject *watched, QEvent *event ) override;
......
......@@ -11,11 +11,13 @@ set( annotate_UI
EditGroundOverlayDialog.ui
EditPolygonDialog.ui
EditPolylineDialog.ui
DownloadOsmDialog.ui
)
set( annotate_SRCS
AnnotatePlugin.cpp
AreaAnnotation.cpp
DownloadOsmDialog.cpp
EditGroundOverlayDialog.cpp
EditPolygonDialog.cpp
NodeItemDelegate.cpp
......
//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2017 Sutirtha Ghosh <ghsutirtha@gmail.com>
//
//Self
#include "DownloadOsmDialog.h"
//Qt
#include <QMessageBox>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QTemporaryFile>
#include <QTemporaryFile>
#include <QDir>
//Marble
#include "ui_DownloadOsmDialog.h"
#include "MarbleWidget.h"
#include "ViewParams.h"
#include "ViewportParams.h"
#include "GeoDataLatLonAltBox.h"
#include "GeoDataLatLonBox.h"
#include "MarbleGlobal.h"
#include "AnnotatePlugin.h"
#include "LatLonBoxWidget.h"
namespace Marble
{
DownloadOsmDialog::DownloadOsmDialog(MarbleWidget *parent,AnnotatePlugin *annotatePlugin) :
QDialog(parent),
m_marbleWidget(parent),
m_latLonBoxWidget(new LatLonBoxWidget)
{
QPushButton *m_quitButton;
setupUi(this);
horizontalLayout->addWidget(m_latLonBoxWidget);
this->setWindowTitle("Download");
connect(m_marbleWidget,
SIGNAL(visibleLatLonAltBoxChanged(GeoDataLatLonAltBox)),
this,
SLOT(updateCoordinates(GeoDataLatLonAltBox) )
);
m_downloadButton = new QPushButton(tr("Download"));
m_downloadButton->setDefault(true);
m_quitButton = new QPushButton(tr("Cancel"));
m_quitButton->setAutoDefault(false);
buttonBox->addButton(m_downloadButton,QDialogButtonBox::ActionRole);
buttonBox->addButton(m_quitButton,QDialogButtonBox::RejectRole);
connect( m_downloadButton, SIGNAL(clicked(bool)), this, SLOT(downloadFile()) );
connect( m_quitButton, SIGNAL(clicked(bool)), this, SLOT(close()) );
connect( this, SIGNAL(openFile(QString)), annotatePlugin, SLOT(openAnnotationFile(QString)) );
progressBar->hide();
updateCoordinates();
}
DownloadOsmDialog::~DownloadOsmDialog()
{
}
void DownloadOsmDialog::updateCoordinates()
{
m_latLonBoxWidget->setLatLonBox( m_marbleWidget->viewport()->viewLatLonAltBox() );
}
void DownloadOsmDialog::updateCoordinates( const GeoDataLatLonAltBox& boundingBox )
{
m_latLonBoxWidget->setLatLonBox( boundingBox );
}
void DownloadOsmDialog::downloadFile()
{
QString m_west;
QString m_south;
QString m_east;
QString m_north;
QString url;
m_isDownloadSuccess=false;
m_file = new QTemporaryFile(QDir::tempPath()+"/"+"XXXXXXosmdata.osm");
if(!m_file->open())
{
QMessageBox::information(this, tr("ERROR"),
"Unable to create temporary file to download OSM data to.");
this->close();
}
m_downloadButton->setEnabled(false);
m_west=QString::number( m_latLonBoxWidget->latLonBox().west()*RAD2DEG );
m_south=QString::number( m_latLonBoxWidget->latLonBox().south()*RAD2DEG );
m_east=QString::number( m_latLonBoxWidget->latLonBox().east()*RAD2DEG );
m_north=QString::number( m_latLonBoxWidget->latLonBox().north()*RAD2DEG );
url="http://api.openstreetmap.org/api/0.6/map?bbox=";
url+=m_west+",";
url+=m_south+",";
url+=m_east+",";
url+=m_north;
m_reply = m_qnam.get(QNetworkRequest(QUrl(url)));
connect( m_reply, SIGNAL(finished()), this, SLOT(httpFinished()) );
connect( m_reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()) );
progressBar->show();
progressBar->setMinimum(0);
progressBar->setMaximum(0);
}
void DownloadOsmDialog::httpReadyRead()
{
//Reads all the data present and writes it to the file whenever data is
//available
if ( m_file ){
m_file->write(m_reply->readAll());
}
}
void DownloadOsmDialog::httpFinished()
{
QVariant statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
int status = statusCode.toInt();
//in case download fails due to network error or server not available
if ( m_reply->error()==QNetworkReply::NoError ){
m_isDownloadSuccess=true;
}
else {
switch(status)
{
case 400:
QMessageBox::information(this, tr("ERROR"),
"The selected region contains too much data.Please select a smaller region and try again.");
m_downloadButton->setEnabled(true);
m_isDownloadSuccess=false;
break;
case 509:
QMessageBox::information(this, tr("ERROR"),
"The bandwidth limit exceeded.Please try again later.");
m_downloadButton->setEnabled(true);
m_isDownloadSuccess=false;
break;
default:
QMessageBox::information(this, tr("ERROR"),"Sorry, a network error occured.Please check your internet connection"
" or try again later.");
m_downloadButton->setEnabled(true);
m_isDownloadSuccess=false;
break;
}
}
progressBar->hide();
m_file->flush();
m_file->close();
if( m_isDownloadSuccess ) {
emit openFile(m_file->fileName());
}
m_reply->deleteLater();
m_reply=0;
delete m_file;
m_file=0;
if( m_isDownloadSuccess ) {
this->close();
}
}
}
#include "moc_DownloadOsmDialog.cpp"
//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2017 Sutirtha Ghosh <ghsutirtha@gmail.com>
//
#ifndef DOWNLOADOSMDIALOG_H
#define DOWNLOADOSMDIALOG_H
#include <QDialog>
#include <QPushButton>
#include <GeoDataLatLonAltBox.h>
#include <QtNetwork>
#include <QProgressDialog>
#include <QTemporaryFile>
#include "LatLonBoxWidget.h"
#include "ui_DownloadOsmDialog.h"
namespace Marble
{
class MarbleWidget;
class AnnotatePlugin;
class DownloadOsmDialog : public QDialog,private Ui::DownloadOsmDialog
{
Q_OBJECT
public:
explicit DownloadOsmDialog(MarbleWidget *parent = 0,AnnotatePlugin *annotatePlugin = 0);
~DownloadOsmDialog();
Q_SIGNALS:
void openFile(const QString &filename);
private:
void updateCoordinates();
MarbleWidget *m_marbleWidget;
QPushButton *m_downloadButton;
QNetworkAccessManager m_qnam;
QNetworkReply *m_reply;
QTemporaryFile *m_file;
LatLonBoxWidget *m_latLonBoxWidget;
bool m_isDownloadSuccess;
private Q_SLOTS:
void downloadFile();
void updateCoordinates(const GeoDataLatLonAltBox&);
void httpReadyRead();
void httpFinished();
};
}
#endif // DOWNLOADOSMDIALOG_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DownloadOsmDialog</class>
<widget class="QDialog" name="DownloadOsmDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>461</width>
<height>323</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>10</x>
<y>260</y>
<width>441</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::NoButton</set>
</property>
</widget>
<widget class="QProgressBar" name="progressBar">
<property name="geometry">
<rect>
<x>20</x>
<y>230</y>
<width>431</width>
<height>23</height>
</rect>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
<widget class="Line" name="line">
<property name="geometry">
<rect>
<x>0</x>
<y>210</y>
<width>701</width>
<height>16</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>441</width>
<height>201</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout"/>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DownloadOsmDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>DownloadOsmDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment