From 08d376813c702251005fb5c0c8754209bdfb4d47 Mon Sep 17 00:00:00 2001 From: AmonRaNet Date: Wed, 5 Jun 2024 01:02:10 +0200 Subject: [PATCH] Fixes for PR 53 --- CMakeLists.txt | 3 + samples/gdal-shapefile/mainwindow.cpp | 10 +- samples/gdal-shapefile/polygon.cpp | 326 +++++++++++++------------- samples/gdal-shapefile/polygon.h | 112 ++++----- 4 files changed, 227 insertions(+), 224 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b00264b..25c1e16 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,9 @@ project(QGeoView LANGUAGES C CXX) option(BUILD_EXAMPLES "Build examples" ON) find_package(GDAL CONFIG) + add_subdirectory(lib) + if (${BUILD_EXAMPLES}) message(STATUS "Enabled building of examples") add_subdirectory(samples/shared) @@ -24,6 +26,7 @@ if (${BUILD_EXAMPLES}) add_subdirectory(samples/mouse-actions) add_subdirectory(samples/camera-actions) add_subdirectory(samples/drag-and-drop) + if(GDAL_FOUND) add_subdirectory(samples/gdal-shapefile) endif() diff --git a/samples/gdal-shapefile/mainwindow.cpp b/samples/gdal-shapefile/mainwindow.cpp index 19f7aef..cf306b6 100644 --- a/samples/gdal-shapefile/mainwindow.cpp +++ b/samples/gdal-shapefile/mainwindow.cpp @@ -18,13 +18,13 @@ #include "mainwindow.h" -#include #include #include +#include +#include "polygon.h" #include #include -#include "polygon.h" #include "cpl_conv.h" #include "ogrsf_frmts.h" @@ -69,14 +69,14 @@ MainWindow::MainWindow() CPLSetConfigOption("PROJ_DATA", PROJ_DATA.c_str()); GDALAllRegister(); // Load GDAL drivers - GDALDataset *poDS = static_cast(GDALOpenEx("countries.shp", GDAL_OF_VECTOR, NULL, NULL, NULL)); // Open vector file + GDALDataset* poDS = static_cast( + 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++) - { + for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++) { OGRLayer* poLayer = poDS->GetLayer(iLayer); OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn(); poLayer->ResetReading(); diff --git a/samples/gdal-shapefile/polygon.cpp b/samples/gdal-shapefile/polygon.cpp index 024b79a..210f61b 100644 --- a/samples/gdal-shapefile/polygon.cpp +++ b/samples/gdal-shapefile/polygon.cpp @@ -1,163 +1,163 @@ -/*************************************************************************** - * 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 "polygon.h" - -#include - -Polygon::Polygon(const PointList& geoPoints, QColor stroke, QColor fill) - : mGeoPoints(geoPoints) - , mColorStroke(stroke) - , mColorFill(fill) -{ -} - -void Polygon::setPoints(const PointList& geoPoints) -{ - mGeoPoints = geoPoints; - - // Geo coordinates need to be converted manually again to projection - onProjection(getMap()); - - // Now we can inform QGV about changes for this - resetBoundary(); - refresh(); -} - -PointList Polygon::getPoints() const -{ - return mGeoPoints; -} - -void Polygon::onProjection(QGVMap* geoMap) -{ - QGVDrawItem::onProjection(geoMap); - mProjPoints.clear(); - for (const QGV::GeoPos& pos : mGeoPoints) - mProjPoints << geoMap->getProjection()->geoToProj(pos); -} - -QPainterPath Polygon::projShape() const -{ - QPainterPath path; - path.addPolygon(mProjPoints); - return path; -} - -void Polygon::projPaint(QPainter* painter) -{ - QPen pen = QPen(QBrush(mColorStroke), 1); - pen.setCosmetic(true); - painter->setPen(pen); - painter->setBrush(QBrush(mColorFill)); - painter->drawPolygon(mProjPoints); -} - -QPointF Polygon::projAnchor() const -{ - return mProjPoints.boundingRect().center(); -} - -QTransform Polygon::projTransform() const -{ - // This method is optional (needed flag is QGV::ItemFlag::Transformed). - // Custom transformation for item. - // In this case we rotate item by 45 degree. - - return QGV::createTransfromAzimuth(projAnchor(), 45); -} - -QString Polygon::projTooltip(const QPointF& projPos) const -{ - // This method is optional (when empty return then no tooltip). - // Text for mouse tool tip. - - auto geo = getMap()->getProjection()->projToGeo(projPos); - - return "Polygon with color " + mColorFill.name() + "\nPosition " + geo.latToString() + " " + geo.lonToString(); -} - -void Polygon::projOnMouseClick(const QPointF& projPos) -{ - // This method is optional (needed flag is QGV::ItemFlag::Clickable). - // Custom reaction to item single mouse click. - // To avoid collision with item selection this code applies only if item selection disabled. - // In this case we change opacity for item. - - if (!isSelectable()) { - if (getOpacity() <= 0.5) - setOpacity(1.0); - else - setOpacity(0.5); - - qInfo() << "single click" << projPos; - } else { - setOpacity(1.0); - } -} - -void Polygon::projOnMouseDoubleClick(const QPointF& projPos) -{ - // This method is optional (needed flag is QGV::ItemFlag::Clickable). - // Custom reaction to item double mouse click. - // In this case we change color for item. - - const QList colors = { Qt::red, Qt::blue, Qt::green, Qt::gray, Qt::cyan, Qt::magenta, Qt::yellow }; - - const auto iter = - std::find_if(colors.begin(), colors.end(), [this](const QColor& color) { return color == mColorFill; }); - mColorFill = colors[(iter - colors.begin() + 1) % colors.size()]; - repaint(); - - setOpacity(1.0); - - qInfo() << "double click" << projPos; -} - -void Polygon::projOnObjectStartMove(const QPointF& projPos) -{ - // This method is optional (needed flag is QGV::ItemFlag::Movable). - // Custom reaction to item move start. - // In this case we only log message. - - qInfo() << "object move started at" << projPos; -} - -void Polygon::projOnObjectMovePos(const QPointF& projPos) -{ - // This method is optional (needed flag is QGV::ItemFlag::Movable). - // Custom reaction to mouse pos change when item move is started. - // In this case actually changing location of object. - - PointList newPoints; - for (const QPointF& pt : mProjPoints) - newPoints << getMap()->getProjection()->projToGeo(pt + projPos); - - setPoints(newPoints); - - qInfo() << "object moved" << mProjPoints; -} - -void Polygon::projOnObjectStopMove(const QPointF& projPos) -{ - // This method is optional (needed flag is QGV::ItemFlag::Movable). - // Custom reaction to item move finished. - // In this case we only log message. - - qInfo() << "object move stopped" << projPos; -} +/*************************************************************************** + * 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 "polygon.h" + +#include + +Polygon::Polygon(const PointList& geoPoints, QColor stroke, QColor fill) + : mGeoPoints(geoPoints) + , mColorStroke(stroke) + , mColorFill(fill) +{ +} + +void Polygon::setPoints(const PointList& geoPoints) +{ + mGeoPoints = geoPoints; + + // Geo coordinates need to be converted manually again to projection + onProjection(getMap()); + + // Now we can inform QGV about changes for this + resetBoundary(); + refresh(); +} + +PointList Polygon::getPoints() const +{ + return mGeoPoints; +} + +void Polygon::onProjection(QGVMap* geoMap) +{ + QGVDrawItem::onProjection(geoMap); + mProjPoints.clear(); + for (const QGV::GeoPos& pos : mGeoPoints) + mProjPoints << geoMap->getProjection()->geoToProj(pos); +} + +QPainterPath Polygon::projShape() const +{ + QPainterPath path; + path.addPolygon(mProjPoints); + return path; +} + +void Polygon::projPaint(QPainter* painter) +{ + QPen pen = QPen(QBrush(mColorStroke), 1); + pen.setCosmetic(true); + painter->setPen(pen); + painter->setBrush(QBrush(mColorFill)); + painter->drawPolygon(mProjPoints); +} + +QPointF Polygon::projAnchor() const +{ + return mProjPoints.boundingRect().center(); +} + +QTransform Polygon::projTransform() const +{ + // This method is optional (needed flag is QGV::ItemFlag::Transformed). + // Custom transformation for item. + // In this case we rotate item by 45 degree. + + return QGV::createTransfromAzimuth(projAnchor(), 45); +} + +QString Polygon::projTooltip(const QPointF& projPos) const +{ + // This method is optional (when empty return then no tooltip). + // Text for mouse tool tip. + + auto geo = getMap()->getProjection()->projToGeo(projPos); + + return "Polygon with color " + mColorFill.name() + "\nPosition " + geo.latToString() + " " + geo.lonToString(); +} + +void Polygon::projOnMouseClick(const QPointF& projPos) +{ + // This method is optional (needed flag is QGV::ItemFlag::Clickable). + // Custom reaction to item single mouse click. + // To avoid collision with item selection this code applies only if item selection disabled. + // In this case we change opacity for item. + + if (!isSelectable()) { + if (getOpacity() <= 0.5) + setOpacity(1.0); + else + setOpacity(0.5); + + qInfo() << "single click" << projPos; + } else { + setOpacity(1.0); + } +} + +void Polygon::projOnMouseDoubleClick(const QPointF& projPos) +{ + // This method is optional (needed flag is QGV::ItemFlag::Clickable). + // Custom reaction to item double mouse click. + // In this case we change color for item. + + const QList colors = { Qt::red, Qt::blue, Qt::green, Qt::gray, Qt::cyan, Qt::magenta, Qt::yellow }; + + const auto iter = + std::find_if(colors.begin(), colors.end(), [this](const QColor& color) { return color == mColorFill; }); + mColorFill = colors[(iter - colors.begin() + 1) % colors.size()]; + repaint(); + + setOpacity(1.0); + + qInfo() << "double click" << projPos; +} + +void Polygon::projOnObjectStartMove(const QPointF& projPos) +{ + // This method is optional (needed flag is QGV::ItemFlag::Movable). + // Custom reaction to item move start. + // In this case we only log message. + + qInfo() << "object move started at" << projPos; +} + +void Polygon::projOnObjectMovePos(const QPointF& projPos) +{ + // This method is optional (needed flag is QGV::ItemFlag::Movable). + // Custom reaction to mouse pos change when item move is started. + // In this case actually changing location of object. + + PointList newPoints; + for (const QPointF& pt : mProjPoints) + newPoints << getMap()->getProjection()->projToGeo(pt + projPos); + + setPoints(newPoints); + + qInfo() << "object moved" << mProjPoints; +} + +void Polygon::projOnObjectStopMove(const QPointF& projPos) +{ + // This method is optional (needed flag is QGV::ItemFlag::Movable). + // Custom reaction to item move finished. + // In this case we only log message. + + qInfo() << "object move stopped" << projPos; +} diff --git a/samples/gdal-shapefile/polygon.h b/samples/gdal-shapefile/polygon.h index 2dc9862..1a6daf7 100644 --- a/samples/gdal-shapefile/polygon.h +++ b/samples/gdal-shapefile/polygon.h @@ -1,56 +1,56 @@ -/*************************************************************************** - * 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 - -#include -#include - -typedef QList PointList; - -class Polygon : public QGVDrawItem -{ - Q_OBJECT - -public: - explicit Polygon(const PointList& geoPoints, QColor stroke, QColor fill); - - void setPoints(const PointList& geoPoints); - PointList getPoints() const; - -private: - void onProjection(QGVMap* geoMap) override; - QPainterPath projShape() const override; - void projPaint(QPainter* painter) override; - QPointF projAnchor() const override; - QTransform projTransform() const override; - QString projTooltip(const QPointF& projPos) const override; - void projOnMouseClick(const QPointF& projPos) override; - void projOnMouseDoubleClick(const QPointF& projPos) override; - void projOnObjectStartMove(const QPointF& projPos) override; - void projOnObjectMovePos(const QPointF& projPos) override; - void projOnObjectStopMove(const QPointF& projPos) override; - -private: - PointList mGeoPoints; - QPolygonF mProjPoints; - QColor mColorStroke; - QColor mColorFill; -}; +/*************************************************************************** + * 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 + +#include +#include + +typedef QList PointList; + +class Polygon : public QGVDrawItem +{ + Q_OBJECT + +public: + explicit Polygon(const PointList& geoPoints, QColor stroke, QColor fill); + + void setPoints(const PointList& geoPoints); + PointList getPoints() const; + +private: + void onProjection(QGVMap* geoMap) override; + QPainterPath projShape() const override; + void projPaint(QPainter* painter) override; + QPointF projAnchor() const override; + QTransform projTransform() const override; + QString projTooltip(const QPointF& projPos) const override; + void projOnMouseClick(const QPointF& projPos) override; + void projOnMouseDoubleClick(const QPointF& projPos) override; + void projOnObjectStartMove(const QPointF& projPos) override; + void projOnObjectMovePos(const QPointF& projPos) override; + void projOnObjectStopMove(const QPointF& projPos) override; + +private: + PointList mGeoPoints; + QPolygonF mProjPoints; + QColor mColorStroke; + QColor mColorFill; +};