Difference between revisions of "Projects/Marble/Runners/DisplayGeoDataPlacemark"

Jump to: navigation, search
m
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Editing Projects/Marble/PaintingGeoDataLineString}}
+
{{Template:I18n/Language Navigation Bar|Editing Projects/Marble/MarbleCPlusPlus}}
{{TutorialBrowser|
+
 
+
series=Marble C++ Tutorial|{{Template:I18n/Language Navigation Bar|Editing Projects/Marble/PaintingGeoDataLineString}}
+
 
{{TutorialBrowser|
 
{{TutorialBrowser|
  
Line 9: Line 6:
 
name=Search|
 
name=Search|
  
pre=[[Projects/Marble/Runners/PaintingGeoDataLineString|Tutorial 10 - Using the GeoPainter in order to paint GeoDataLineString objects]]|
+
pre=[[Projects/Marble/Runners/ReverseGeocoding|Tutorial 8 - Reverse Geocoding]]|
  
next=[[Projects/Marble/Runners/YetMissing|Tutorial 12 - Yet missing]]|
+
next=[[Projects/Marble/Runners/PaintingGeoDataLineString|Tutorial 10 - Using the GeoPainter in order to paint GeoDataLineString objects]]|
 
}}
 
}}
  
 +
== KML Inspector ==
 +
Marble uses so-called runners to calculate routes, do reverse geocoding, parse files and search for placemarks (cities, addresses, points of interest, ...). This tutorial shows how to use the <tt>MarbleRunnerManager</tt> class to open a .kml (or .gpx, ...) file and display its structure in a tree view.
  
Now,
+
<source lang="cpp-qt">
 +
#include <QtCore/QDebug>
 +
#include <QtCore/QFileInfo>
 +
#include <QtGui/QApplication>
 +
#include <QtGui/QTreeView>
  
The previous tutorial proved how GeoPainter can be used in order to paint shapes and text at different coordinates of the map (through a GeoDataCoordinates parameter) in a [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1MarbleWidget.html MarbleWidget]. Now, we'll show a new way of adding extra content to the globe: by painting GeoDataLineString's using our GeoPainter.  
+
#include <marble/MarbleWidget.h>
 +
#include <marble/MarbleModel.h>
 +
#include <marble/MarbleRunnerManager.h>
 +
#include <marble/GeoDataTreeModel.h>
  
Briefly, [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1GeoDataLineString.html GeoDataLineString] is a tool class which implements LineStrings (also referred to as "polylines"), allowing us to store a contiguous set of line segments. As you will see in the example, it consists of several nodes (GeoDataCoordinates points), which are connected through line segments.
 
 
GeoDataLineString allows LineStrings to be tessellated in order to make them follow the terrain and the curvature of the Earth. The tessellation options allow different ways of visualization:
 
 
* Not tessellated, connects each two nodes directly
 
 
* A tessellated line, each segment is bent such that the LineString follows the curvature of the Earth and its terrain. A tessellated line segment connects each two nodes at the shortest possible distance ("along great circles").
 
 
* A tessellated line which follows latitude circles whenever possible: in this case latitude circles are followed as soon as two subsequent nodes have exactly the same amount of latitude. In all other places the line segments follow great circles.
 
 
 
<source lang="cpp">
 
#include <QtGui/QApplication>
 
#include <marble/MarbleWidget.h>
 
#include <marble/GeoPainter.h>
 
#include <marble/GeoDataLineString.h>
 
 
 
using namespace Marble;
 
using namespace Marble;
  
class MyMarbleWidget : public MarbleWidget
+
int main(int argc, char** argv)
 
{
 
{
public:
+
    QApplication app(argc,argv);
virtual void customPaint(GeoPainter* painter);
+
};
+
  
void MyMarbleWidget::customPaint(GeoPainter* painter) {
+
    QFileInfo inputFile( app.arguments().last() );
GeoDataCoordinates France( 2.2, 48.52, 0.0, GeoDataCoordinates::Degree );
+
    if ( app.arguments().size() < 2 || !inputFile.exists() ) {
GeoDataCoordinates Canada( -77.02, 48.52, 0.0, GeoDataCoordinates::Degree );
+
        qWarning() << "Usage: " << app.arguments().first() << "file.kml";
 +
        return 1;
 +
    }
  
//A line from France to Canada without tessellation
+
    MarbleModel *model = new MarbleModel;
 +
    MarbleRunnerManager* manager = new MarbleRunnerManager( model->pluginManager() );
  
GeoDataLineString shapeNoTessellation( NoTessellation );
+
    GeoDataDocument* document = manager->openFile( inputFile.absoluteFilePath() );
shapeNoTessellation << France << Canada;
+
    if ( document ) {
 +
        GeoDataTreeModel* treeModel = new GeoDataTreeModel;
 +
        treeModel->addDocument( document );
 +
        QTreeView* treeView = new QTreeView;
 +
        treeView->setModel( treeModel );
 +
        treeView->show();
 +
    } else {
 +
        qDebug() << "Unable to open " << inputFile.absoluteFilePath();
 +
    }
  
painter->setPen( oxygenSkyBlue4 );
+
    return app.exec();
painter->drawPolyline( shapeNoTessellation );
+
 
+
//The same line, but with tessellation
+
+
GeoDataLineString shapeTessellate( Tessellate );
+
shapeTessellate << France << Canada;
+
 
+
painter->setPen( oxygenBrickRed4 );
+
painter->drawPolyline( shapeTessellate );
+
 
+
//Now following the latitude circles
+
 
+
GeoDataLineString shapeLatitudeCircle( RespectLatitudeCircle | Tessellate );
+
shapeLatitudeCircle << France << Canada;
+
 
+
painter->setPen( oxygenForestGreen4 );
+
painter->drawPolyline( shapeLatitudeCircle );
+
}
+
 
+
int main(int argc, char** argv) {
+
 
+
QApplication app(argc,argv);
+
 
+
// Create a Marble QWidget without a parent
+
MarbleWidget *mapWidget = new MyMarbleWidget();
+
   
+
// Load the OpenStreetMap map
+
mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
+
+
mapWidget->show();
+
 
+
return app.exec();
+
 
}
 
}
 
</source>
 
</source>
  
Save the code above as <tt>my_marble.cpp</tt> and compile it:
+
Copy and paste the code above into a text editor. Then save it as <tt>my_marble.cpp</tt> and compile it by entering the following command on the command line:
  
 
<source lang="bash">
 
<source lang="bash">
  g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui
+
  g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui -lQtCore
 
</source>
 
</source>
  
If things go fine, execute <tt>./my_marble</tt> and you end up with a globe view similar to this:
+
If things go fine, execute <tt>./my_marble some-file.kml</tt> and you get a tree view of its structure similar to this screenshot (showing the structure of a route calculated with Marble):
  
[[Image:GeoDataLineString_paint.png]]
+
[[Image:Marble-kml-inspector.png]]
  
As you can see, the blue line corresponds to the straight-no-tessellation way of visualization, the red one follows the great circles, and the green one sticks to the latitude circles, since the two endpoints have the same latitude.
+
{{tip|
 +
Here's a little checklist to tackle some problems that might arise when compiling the code above:
  
 
+
* You need Qt and '''Marble development packages''' (or comparable git installations), version 1.3 (Marble library 0.13), shipped post KDE 4.8
name=Painting GeoDataLineString: Using the GeoPainter in order to paint a GeoDataLineString object|
+
* If ''Qt headers'' are not installed in '''/usr/include/qt4''' on your system, change the path in the g++ call above accordingly.
 
+
* Likewise, '''add -I /path/to/marble/headers''' if they're not to be found in /usr/include
pre=[[Projects/Marble/Runners/Parse|Tutorial 9 - Opening .kml, .gpx, ... files]]|
+
}}
 
+
{{note|
next=[[Projects/Marble/Runners/YetMissing|Tutorial 11 - Yet missing]]|
+
If you provide maps in your application please check the '''Terms of Use''' of the map material. The map material that is shipped with Marble is licensed ''in the spirit of Free Software''. This usually means at least that the authors should be credited and that the license is mentioned.
 +
E.g. for ''OpenStreetMap'' the license is [http://creativecommons.org/license/by-sa/2.0 CC-BY-SA]. Other map data shipped with Marble is either public domain or licensed in the spirit of the BSD license.
 
}}
 
}}
 
 
The previous tutorial proved how GeoPainter can be used in order to paint shapes and text at different coordinates of the map (through a GeoDataCoordinates parameter) in a [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1MarbleWidget.html MarbleWidget]. Now, we'll show a new way of adding extra content to the globe: by painting GeoDataLineString's using our GeoPainter.
 
 
Briefly, [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1GeoDataLineString.html GeoDataLineString] is a tool class which implements LineStrings (also referred to as "polylines"), allowing us to store a contiguous set of line segments. As you will see in the example, it consists of several nodes (GeoDataCoordinates points), which are connected through line segments.
 
 
GeoDataLineString allows LineStrings to be tessellated in order to make them follow the terrain and the curvature of the Earth. The tessellation options allow different ways of visualization:
 
 
* Not tessellated, connects each two nodes directly
 
 
* A tessellated line, each segment is bent such that the LineString follows the curvature of the Earth and its terrain. A tessellated line segment connects each two nodes at the shortest possible distance ("along great circles").
 
 
* A tessellated line which follows latitude circles whenever possible: in this case latitude circles are followed as soon as two subsequent nodes have exactly the same amount of latitude. In all other places the line segments follow great circles.
 
 
 
<source lang="cpp">
 
#include <QtGui/QApplication>
 
#include <marble/MarbleWidget.h>
 
#include <marble/GeoPainter.h>
 
#include <marble/GeoDataLineString.h>
 
 
using namespace Marble;
 
 
class MyMarbleWidget : public MarbleWidget
 
{
 
public:
 
virtual void customPaint(GeoPainter* painter);
 
};
 
 
void MyMarbleWidget::customPaint(GeoPainter* painter) {
 
GeoDataCoordinates France( 2.2, 48.52, 0.0, GeoDataCoordinates::Degree );
 
GeoDataCoordinates Canada( -77.02, 48.52, 0.0, GeoDataCoordinates::Degree );
 
 
//A line from France to Canada without tessellation
 
 
GeoDataLineString shapeNoTessellation( NoTessellation );
 
shapeNoTessellation << France << Canada;
 
 
painter->setPen( oxygenSkyBlue4 );
 
painter->drawPolyline( shapeNoTessellation );
 
 
//The same line, but with tessellation
 
 
GeoDataLineString shapeTessellate( Tessellate );
 
shapeTessellate << France << Canada;
 
 
painter->setPen( oxygenBrickRed4 );
 
painter->drawPolyline( shapeTessellate );
 
 
//Now following the latitude circles
 
 
GeoDataLineString shapeLatitudeCircle( RespectLatitudeCircle | Tessellate );
 
shapeLatitudeCircle << France << Canada;
 
 
painter->setPen( oxygenForestGreen4 );
 
painter->drawPolyline( shapeLatitudeCircle );
 
}
 
 
 
int main(int argc, char** argv) {
 
 
QApplication app(argc,argv);
 
 
 
// Create a Marble QWidget without a parent
 
MarbleWidget *mapWidget = new MyMarbleWidget();
 
   
 
// Load the OpenStreetMap map
 
mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
 
 
mapWidget->show();
 
 
 
return app.exec();
 
}
 
</source>
 
 
Save the code above as <tt>my_marble.cpp</tt> and compile it:
 
 
<source lang="bash">
 
g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui
 
</source>
 
 
If things go fine, execute <tt>./my_marble</tt> and you end up with a globe view similar to this:
 
 
[[Image:GeoDataLineString_paint.png]]
 
 
As you can see, the blue line corresponds to the straight-no-tessellation way of visualization, the red one follows the great circles, and the green one sticks to the latitude circles, since the two endpoints have the same latitude.
 

Revision as of 13:56, 24 May 2012


Editing Projects/Marble/MarbleCPlusPlus

Search
Tutorial Series   Marble C++ Tutorial
Previous   Tutorial 8 - Reverse Geocoding
What's Next   Tutorial 10 - Using the GeoPainter in order to paint GeoDataLineString objects
Further Reading   n/a

KML Inspector

Marble uses so-called runners to calculate routes, do reverse geocoding, parse files and search for placemarks (cities, addresses, points of interest, ...). This tutorial shows how to use the MarbleRunnerManager class to open a .kml (or .gpx, ...) file and display its structure in a tree view.

#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtGui/QApplication>
#include <QtGui/QTreeView>
 
#include <marble/MarbleWidget.h>
#include <marble/MarbleModel.h>
#include <marble/MarbleRunnerManager.h>
#include <marble/GeoDataTreeModel.h>
 
using namespace Marble;
 
int main(int argc, char** argv)
{
    QApplication app(argc,argv);
 
    QFileInfo inputFile( app.arguments().last() );
    if ( app.arguments().size() < 2 || !inputFile.exists() ) {
        qWarning() << "Usage: " << app.arguments().first() << "file.kml";
        return 1;
    }
 
    MarbleModel *model = new MarbleModel;
    MarbleRunnerManager* manager = new MarbleRunnerManager( model->pluginManager() );
 
    GeoDataDocument* document = manager->openFile( inputFile.absoluteFilePath() );
    if ( document ) {
        GeoDataTreeModel* treeModel = new GeoDataTreeModel;
        treeModel->addDocument( document );
        QTreeView* treeView = new QTreeView;
        treeView->setModel( treeModel );
        treeView->show();
    } else {
        qDebug() << "Unable to open " << inputFile.absoluteFilePath();
    }
 
    return app.exec();
}

Copy and paste the code above into a text editor. Then save it as my_marble.cpp and compile it by entering the following command on the command line:

 g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui -lQtCore

If things go fine, execute ./my_marble some-file.kml and you get a tree view of its structure similar to this screenshot (showing the structure of a route calculated with Marble):

Marble-kml-inspector.png

Ktip.png
 
Tip
Here's a little checklist to tackle some problems that might arise when compiling the code above:
  • You need Qt and Marble development packages (or comparable git installations), version 1.3 (Marble library 0.13), shipped post KDE 4.8
  • If Qt headers are not installed in /usr/include/qt4 on your system, change the path in the g++ call above accordingly.
  • Likewise, add -I /path/to/marble/headers if they're not to be found in /usr/include
noframe
 
Note
If you provide maps in your application please check the Terms of Use of the map material. The map material that is shipped with Marble is licensed in the spirit of Free Software. This usually means at least that the authors should be credited and that the license is mentioned. E.g. for OpenStreetMap the license is CC-BY-SA. Other map data shipped with Marble is either public domain or licensed in the spirit of the BSD license.

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