Marble/DisplayGeoDataGroundOverlay

From KDE TechBase
Revision as of 20:27, 8 December 2015 by Andrey (talk | contribs)
Search
Tutorial Series   Marble C++ Tutorial
Previous   Tutorial 15 - Painting GeoDataLineString: Using the GeoPainter in order to paint a GeoDataLineString object
What's Next   n/a
Further Reading   n/a

In this tutorial, we'll show you how to display a ground overlay with its associated image over the Earth's surface in Marble.

GeoDataGroundOverlay is the class used in Marble to implement the features of the GroundOverlay KML element. More precisely, an instance references an icon that gets displayed on top of a certain area on the globe and a LatLonBox (GeoDataLatLonBox in Marble) describing its exact surface.

As in the tutorial for displaying placemarks, we will be using a GeoDataDocument as the the containter for our overlay. The document is afterwards exposed to Marble by having it added to the GeoDataTreeModel.

The implementation is actually pretty simple and you can have a look at it here:

#include <QDebug>
#include <QFileInfo>
#include <QApplication>
#include <QImage>

#include <marble/MarbleWidget.h>
#include <marble/GeoDataDocument.h>
#include <marble/GeoDataGroundOverlay.h>
#include <marble/GeoDataTreeModel.h>
#include <marble/MarbleModel.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.png";
        return 1;
    }

    // Create a Marble QWidget without a parent
    MarbleWidget *mapWidget = new MarbleWidget();

    // Load the Satellite map
    mapWidget->setMapThemeId( "earth/bluemarble/bluemarble.dgml" );

    // Create a bounding box from the given corner points
    GeoDataLatLonBox box( 55, 48, 14.5, 6, GeoDataCoordinates::Degree );
    box.setRotation( 0, GeoDataCoordinates::Degree );

    // Create an overlay and assign the image to render and its bounding box to it
    GeoDataGroundOverlay *overlay = new GeoDataGroundOverlay;
    overlay->setLatLonBox( box );
    overlay->setIcon( QImage( inputFile.absoluteFilePath() ) );

    // Create a document as a container for the overlay
    GeoDataDocument *document = new GeoDataDocument();
    document->append( overlay );

    // Add the document to MarbleWidget's tree model
    mapWidget->model()->treeModel()->addDocument( document );

    mapWidget->show();

    return app.exec();
}


Save the above code in a render.cpp file and make sure to create in the same folder a file name CMakeLists.txt looking as below:

CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
SET (TARGET render)
PROJECT (${TARGET})

FIND_PACKAGE (Qt4 4.6.0 REQUIRED QtCore QtGui)
FIND_PACKAGE (Marble REQUIRED)
INCLUDE (${QT_USE_FILE})
INCLUDE_DIRECTORIES (${MARBLE_INCLUDE_DIR})
SET (LIBS ${LIBS} ${MARBLE_LIBRARIES} ${QT_LIBRARIES})

ADD_EXECUTABLE (${TARGET} render.cpp)
TARGET_LINK_LIBRARIES (${TARGET} ${LIBS})


Assuming your Marble library is installed to ~/marble/export, you can run the following commands in the terminal to compile and run it:

cmake -DCMAKE_MODULE_PATH=~/marble/export/share/apps/cmake/modules \
      -DCMAKE_INCLUDE_PATH=~/marble/export/include \
      -DCMAKE_LIBRARY_PATH=~/marble/export/lib \
      .
make
./render your_image_file.png


If everything goes fine, you should be able to get a globe where your chosen image covers the approximate territory of Germany:

The latest source code of this example can be found at main.cpp and CMakeLists.txt.