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

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • plugins
  • render
  • postalcode
PostalCodeModel.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 Valery Kharitonov <kharvd@gmail.com>
9 //
10 
11 // Self
12 #include "PostalCodeModel.h"
13 
14 // Plugin
15 #include "PostalCodeItem.h"
16 
17 // Marble
18 #include "MarbleGlobal.h"
19 #include "MarbleModel.h"
20 #include "GeoDataCoordinates.h"
21 #include "GeoDataLatLonAltBox.h"
22 #include "MarbleDebug.h"
23 #include "Planet.h"
24 
25 // Qt
26 #include <QString>
27 #include <QUrl>
28 #include <QScriptEngine>
29 #include <QScriptValue>
30 #include <QScriptValueIterator>
31 
32 #if QT_VERSION >= 0x050000
33  #include <QUrlQuery>
34 #endif
35 
36 using namespace Marble;
37 
38 PostalCodeModel::PostalCodeModel( const MarbleModel *marbleModel, QObject *parent )
39  : AbstractDataPluginModel( "postalCode", marbleModel, parent )
40 {
41 }
42 
43 PostalCodeModel::~PostalCodeModel() {
44 }
45 
46 void PostalCodeModel::getAdditionalItems( const GeoDataLatLonAltBox& box,
47  qint32 number )
48 {
49  if( marbleModel()->planetId() != "earth" ) {
50  return;
51  }
52 
53  double const lat = box.center().latitude( GeoDataCoordinates::Degree );
54  double const lon = box.center().longitude( GeoDataCoordinates::Degree );
55  double const radius = qMin<double>( 30.0, box.height() * marbleModel()->planet()->radius() * METER2KM );
56 
57  QUrl geonamesUrl( "http://ws.geonames.org/findNearbyPostalCodesJSON" );
58 #if QT_VERSION < 0x050000
59  geonamesUrl.addQueryItem( "lat", QString::number( lat ) );
60  geonamesUrl.addQueryItem( "lng", QString::number( lon ) );
61  geonamesUrl.addQueryItem( "radius", QString::number( radius ) );
62  geonamesUrl.addQueryItem( "maxRows", QString::number( number ) );
63  geonamesUrl.addQueryItem( "username", "marble" );
64 #else
65  QUrlQuery urlQuery;
66  urlQuery.addQueryItem( "lat", QString::number( lat ) );
67  urlQuery.addQueryItem( "lng", QString::number( lon ) );
68  urlQuery.addQueryItem( "radius", QString::number( radius ) );
69  urlQuery.addQueryItem( "maxRows", QString::number( number ) );
70  urlQuery.addQueryItem( "username", "marble" );
71  geonamesUrl.setQuery( urlQuery );
72 #endif
73 
74  downloadDescriptionFile( QUrl( geonamesUrl ) );
75 }
76 
77 void PostalCodeModel::parseFile( const QByteArray& file )
78 {
79  QScriptValue data;
80  QScriptEngine engine;
81 
82  // Qt requires parentheses around json code
83  data = engine.evaluate( '(' + QString::fromUtf8( file ) + ')' );
84 
85  // Parse if any result exists
86  if ( data.property( "postalCodes" ).isArray() ) {
87  QScriptValueIterator iterator( data.property( "postalCodes" ) );
88 
89  // Add items to the list
90  QList<AbstractDataPluginItem*> items;
91  while ( iterator.hasNext() ) {
92  iterator.next();
93 
94  QString const placeName = iterator.value().property( "placeName" ).toString();
95  QString const adminName1 = iterator.value().property( "adminName1" ).toString();
96  QString const adminName2 = iterator.value().property( "adminName2" ).toString();
97  QString const adminName3 = iterator.value().property( "adminName3" ).toString();
98  QString const postalCode = iterator.value().property( "postalCode" ).toString();
99  QString const countryCode = iterator.value().property( "countryCode" ).toString();
100  double const longitude = iterator.value().property( "lng" ).toNumber();
101  double const latitude = iterator.value().property( "lat" ).toNumber();
102 
103  QString const id = "postalCode_" + countryCode + postalCode;
104 
105  if ( !id.isEmpty() ) {
106  QString tooltip;
107 
108  if ( !placeName.isEmpty() ) {
109  tooltip += placeName + ' ';
110  }
111 
112  addLine( &tooltip, postalCode );
113  addLine( &tooltip, countryCode );
114  addLine( &tooltip, adminName1 );
115  addLine( &tooltip, adminName2 );
116  addLine( &tooltip, adminName3 );
117  tooltip = tooltip.trimmed();
118 
119  if( !itemExists( id ) ) {
120  // If it does not exist, create it
121  GeoDataCoordinates coordinates( longitude, latitude, 0.0, GeoDataCoordinates::Degree );
122 
123  PostalCodeItem *item = new PostalCodeItem( this );
124  item->setId( id );
125  item->setCoordinate( coordinates );
126  item->setTarget( "earth" );
127  item->setToolTip( tooltip );
128  item->setText( postalCode );
129 
130  items << item;
131  }
132  }
133  }
134  addItemsToList( items );
135  }
136 }
137 
138 void PostalCodeModel::addLine(QString *string, const QString &line)
139 {
140  Q_ASSERT( string );
141  if ( !line.isEmpty() ) {
142  *string += line + '\n';
143  }
144 }
145 
146 #include "PostalCodeModel.moc"
PostalCodeItem.h
GeoDataCoordinates.h
Marble::AbstractDataPluginModel::items
QList< AbstractDataPluginItem * > items(const ViewportParams *viewport, qint32 number=10)
Get the items on the viewport Returns the currently downloaded images in the viewport.
Definition: AbstractDataPluginModel.cpp:274
Marble::GeoDataLatLonBox::height
qreal height(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
Get the height of the latitude interval.
Definition: GeoDataLatLonBox.cpp:255
Marble::AbstractDataPluginItem::setTarget
void setTarget(const QString &target)
Definition: AbstractDataPluginItem.cpp:66
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
QByteArray
Marble::AbstractDataPluginModel::itemExists
bool itemExists(const QString &id) const
Testing the existence of the item id in the list.
Definition: AbstractDataPluginModel.cpp:563
MarbleModel.h
This file contains the headers for MarbleModel.
Marble::AbstractDataPluginItem::setId
void setId(const QString &id)
Definition: AbstractDataPluginItem.cpp:86
QScriptValue
Marble::PostalCodeModel::~PostalCodeModel
~PostalCodeModel()
Definition: PostalCodeModel.cpp:43
QScriptEngine::evaluate
QScriptValue evaluate(const QString &program, const QString &fileName, int lineNumber)
Marble::AbstractDataPluginModel
An abstract data model (not based on QAbstractModel) for a AbstractDataPlugin.
Definition: AbstractDataPluginModel.h:45
QScriptValueIterator
Marble::AbstractDataPluginModel::marbleModel
const MarbleModel * marbleModel() const
Definition: AbstractDataPluginModel.cpp:269
Marble::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:751
Planet.h
Marble::MarbleModel::planet
const Planet * planet() const
Returns the planet object for the current map.
Definition: MarbleModel.cpp:644
MarbleDebug.h
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
Marble::PostalCodeModel::parseFile
void parseFile(const QByteArray &file)
Parses the file which getAdditionalItems downloads and prepares the data for usage.
Definition: PostalCodeModel.cpp:77
QScriptEngine
Marble::AbstractDataPluginModel::downloadDescriptionFile
void downloadDescriptionFile(const QUrl &url)
Download the description file from the url.
Definition: AbstractDataPluginModel.cpp:392
Marble::PostalCodeItem
Definition: PostalCodeItem.h:21
QString::number
QString number(int n, int base)
QString::fromUtf8
QString fromUtf8(const char *str, int size)
QObject
Marble::radius
static qreal radius(qreal zoom)
Definition: thumbnailer.cpp:99
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
QString
QList< AbstractDataPluginItem * >
MarbleGlobal.h
QScriptValue::property
QScriptValue property(const QString &name, const ResolveFlags &mode) const
Marble::METER2KM
const qreal METER2KM
Definition: MarbleGlobal.h:224
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
QUrl
Marble::GeoDataLatLonAltBox::center
virtual GeoDataCoordinates center() const
returns the center of this box
Definition: GeoDataLatLonAltBox.cpp:151
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:97
Marble::Planet::radius
qreal radius() const
the radius of the planet, in metres
Definition: Planet.cpp:251
GeoDataLatLonAltBox.h
PostalCodeModel.h
Marble::BillboardGraphicsItem::setCoordinate
void setCoordinate(const GeoDataCoordinates &coordinates)
Definition: BillboardGraphicsItem.cpp:98
Marble::AbstractDataPluginItem::setToolTip
void setToolTip(const QString &toolTip)
Set the tool tip for the item.
Definition: AbstractDataPluginItem.cpp:76
Marble::PostalCodeItem::setText
void setText(const QString &text)
Definition: PostalCodeItem.cpp:52
QScriptValue::isArray
bool isArray() const
Marble::GeoDataLatLonAltBox
A class that defines a 3D bounding box for geographic data.
Definition: GeoDataLatLonAltBox.h:49
Marble::AbstractDataPluginModel::addItemsToList
void addItemsToList(const QList< AbstractDataPluginItem * > &items)
Adds the items to the list of initialized items.
Definition: AbstractDataPluginModel.cpp:408
Marble::PostalCodeModel::getAdditionalItems
void getAdditionalItems(const GeoDataLatLonAltBox &box, qint32 number=10)
Generates the download url for the description file from the web service depending on the box surroun...
Definition: PostalCodeModel.cpp:46
Marble::PostalCodeModel::PostalCodeModel
PostalCodeModel(const MarbleModel *marbleModel, QObject *parent=0)
Definition: PostalCodeModel.cpp:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:41 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
  • 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