Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for reading shapefiles with GDAL and displaying the geometry. #53

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ project(QGeoView LANGUAGES C CXX)

option(BUILD_EXAMPLES "Build examples" ON)

find_package(GDAL CONFIG)
AmonRaNet marked this conversation as resolved.
Show resolved Hide resolved
add_subdirectory(lib)
if (${BUILD_EXAMPLES})
message(STATUS "Enabled building of examples")
Expand All @@ -22,6 +23,9 @@ if (${BUILD_EXAMPLES})
add_subdirectory(samples/moving-objects)
add_subdirectory(samples/mouse-actions)
add_subdirectory(samples/camera-actions)
if(GDAL_FOUND)
add_subdirectory(samples/shapefile)
endif()
else ()
message(STATUS "Disabled building of examples")
endif ()
40 changes: 40 additions & 0 deletions samples/shapefile/CMakeLists.txt
AmonRaNet marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
set(CMAKE_CXX_STANDARD 11)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Set the QT version
find_package(Qt6 COMPONENTS Core QUIET)
if (NOT Qt6_FOUND)
set(QT_VERSION 5 CACHE STRING "Qt version for QGeoView")
else()
set(QT_VERSION 6 CACHE STRING "Qt version for QGeoView")
endif()

find_package(Qt${QT_VERSION} REQUIRED COMPONENTS
Core
Gui
Widgets
Network
)

add_executable(qgeoview-samples-shapefile
main.cpp
mainwindow.h
mainwindow.cpp
polygon.cpp
polygon.h
)

target_link_libraries(qgeoview-samples-shapefile
PRIVATE
Qt${QT_VERSION}::Core
Qt${QT_VERSION}::Network
Qt${QT_VERSION}::Gui
Qt${QT_VERSION}::Widgets
QGeoView
qgeoview-samples-shared
GDAL::GDAL
)
37 changes: 37 additions & 0 deletions samples/shapefile/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2023 Andrey Yaroshenko.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see https://www.gnu.org/licenses.
****************************************************************************/

#include <QApplication>
#include <QCommandLineParser>

#include "mainwindow.h"

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
app.setApplicationName("QGeoView Samples");

QCommandLineParser parser;
parser.addHelpOption();
parser.addVersionOption();
parser.process(app);

MainWindow window;
window.show();
return app.exec();
}
121 changes: 121 additions & 0 deletions samples/shapefile/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2023 Andrey Yaroshenko.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see https://www.gnu.org/licenses.
****************************************************************************/

#include "mainwindow.h"

#include <QTimer>
#include <QApplication>
#include <QDir>

#include <QGeoView/QGVLayerOSM.h>
#include <helpers.h>
#include "polygon.h"

#include "cpl_conv.h"
#include "ogrsf_frmts.h"

QList<QGV::GeoPos> convert(OGRPolygon* poPolygon)
{
OGRPoint ptTemp;
QList<QGV::GeoPos> result;
OGRLinearRing* poExteriorRing = poPolygon->getExteriorRing();
int NumberOfExteriorRingVertices = poExteriorRing->getNumPoints();
for (int k = 0; k < NumberOfExteriorRingVertices; k++) {
poExteriorRing->getPoint(k, &ptTemp);
result << QGV::GeoPos(ptTemp.getY(), ptTemp.getX());
}
return result;
}

MainWindow::MainWindow()
{
setWindowTitle("QGeoView Samples - Shapefile");

mMap = new QGVMap(this);
setCentralWidget(mMap);

Helpers::setupCachedNetworkAccessManager(this);

// Background layer
auto osmLayer = new QGVLayerOSM();
mMap->addItem(osmLayer);

// Please adapt the following two points to your system:
// 1. GDAL data path
QDir gdalData(QApplication::applicationDirPath());
gdalData.cd("gdal");
// 2. PROJ data path
QDir projData(gdalData);

// GDAL configuration (https://gdal.org/user/configoptions.html)
std::string GDAL_DATA = QDir::toNativeSeparators(gdalData.absolutePath()).toStdString();
CPLSetConfigOption("GDAL_DATA", GDAL_DATA.c_str());
std::string PROJ_DATA = QDir::toNativeSeparators(projData.absolutePath()).toStdString();
CPLSetConfigOption("PROJ_DATA", PROJ_DATA.c_str());

GDALAllRegister(); // Load GDAL drivers
GDALDataset *poDS = static_cast<GDALDataset*>(GDALOpenEx("countries.shp", GDAL_OF_VECTOR, NULL, NULL, NULL)); // Open vector file
if (poDS == NULL) {
printf("Open failed.\n");
exit(1);
}

for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++)
{
OGRLayer* poLayer = poDS->GetLayer(iLayer);
OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn();
poLayer->ResetReading();
OGRFeature* poFeature;
while ((poFeature = poLayer->GetNextFeature()) != NULL) {
OGRGeometry* poGeometry = poFeature->GetGeometryRef();
auto type = wkbFlatten(poGeometry->getGeometryType());
if (poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon) {
OGRPolygon* poPolygon = (OGRPolygon*)poGeometry;
if (poPolygon->IsValid()) {
QList<QGV::GeoPos> points = convert(poPolygon);
if (points.count() > 2)
mMap->addItem(new Polygon(points, Qt::red, Qt::blue));
}
} else if (poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon) {
OGRMultiPolygon* poMultiPolygon = (OGRMultiPolygon*)poGeometry;
for (int i = 0; i < poMultiPolygon->getNumGeometries(); i++) {
OGRPolygon* poPolygon = (OGRPolygon*)poMultiPolygon->getGeometryRef(i);
if (poPolygon->IsValid()) {
QList<QGV::GeoPos> points = convert(poPolygon);
if (points.count() > 2)
mMap->addItem(new Polygon(points, Qt::red, Qt::blue));
}
}
} else {
printf("no polygon geometry\n");
}
OGRFeature::DestroyFeature(poFeature);
}
}
GDALClose(poDS);

// Show whole world
QTimer::singleShot(100, this, [this]() {
auto target = mMap->getProjection()->boundaryGeoRect();
mMap->cameraTo(QGVCameraActions(mMap).scaleTo(target));
});
}

MainWindow::~MainWindow()
{
}
35 changes: 35 additions & 0 deletions samples/shapefile/mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2023 Andrey Yaroshenko.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see https://www.gnu.org/licenses.
****************************************************************************/

#pragma once

#include <QMainWindow>

#include <QGeoView/QGVMap.h>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();
~MainWindow();

private:
QGVMap* mMap;
};
Loading
Loading