forked from boostorg/geometry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8d8c39
commit 5a9003e
Showing
10 changed files
with
274 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Boost.Geometry | ||
// | ||
// Copyright (c) 2007-2024 Barend Gehrels, Amsterdam, the Netherlands. | ||
// Use, modification and distribution is subject to the Boost Software License, | ||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef READ_COUNTRIES_HPP | ||
#define READ_COUNTRIES_HPP | ||
|
||
#include <fstream> | ||
#include <boost/geometry/geometry.hpp> | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Read an ASCII file containing WKT's of either POLYGON or MULTIPOLYGON | ||
// ---------------------------------------------------------------------------- | ||
template <typename Geometry> | ||
std::vector<Geometry> read_countries(std::string const& filename) | ||
{ | ||
std::vector<Geometry> geometries; | ||
std::ifstream cpp_file(filename.c_str()); | ||
if (!cpp_file.is_open()) | ||
{ | ||
return geometries; | ||
} | ||
while (! cpp_file.eof() ) | ||
{ | ||
std::string line; | ||
std::getline(cpp_file, line); | ||
if (line.empty()) | ||
{ | ||
continue; | ||
} | ||
Geometry geometry; | ||
if (line.substr(0, 4) == "POLY") | ||
{ | ||
using polygon_t = std::decay_t<decltype(*geometry.begin())>; | ||
polygon_t polygon; | ||
boost::geometry::read_wkt(line, polygon); | ||
geometry.push_back(polygon); | ||
} | ||
else | ||
{ | ||
boost::geometry::read_wkt(line, geometry); | ||
} | ||
|
||
geometries.push_back(geometry); | ||
} | ||
return geometries; | ||
} | ||
|
||
// Returns the envelope of a collection of geometries | ||
template <typename Box, typename Countries> | ||
Box calculate_envelope(Countries const& countries) | ||
{ | ||
Box box; | ||
boost::geometry::assign_inverse(box); | ||
|
||
for (auto const& country : countries) | ||
{ | ||
boost::geometry::expand(box, boost::geometry::return_envelope<Box>(country)); | ||
} | ||
return box; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Boost.Geometry | ||
# Example CMakeLists.txt building the Boost.Geometry with Qt example | ||
# | ||
# Copyright (c) 2021-2024 Barend Gehrels, Amsterdam, the Netherlands. | ||
|
||
# Use, modification and distribution is subject to the Boost Software License, | ||
# Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | ||
# http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
cmake_minimum_required(VERSION 3.16) | ||
project(qt_world_mapper LANGUAGES CXX) | ||
|
||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) | ||
|
||
qt_standard_project_setup() | ||
|
||
qt_add_executable(${PROJECT_NAME} | ||
qt_world_mapper.cpp | ||
qt_world_mapper.hpp | ||
) | ||
|
||
set_target_properties(${PROJECT_NAME} PROPERTIES | ||
WIN32_EXECUTABLE TRUE | ||
MACOSX_BUNDLE TRUE | ||
) | ||
|
||
target_link_libraries(${PROJECT_NAME} PRIVATE | ||
Qt6::Core | ||
Qt6::Gui | ||
Qt6::Widgets | ||
) | ||
|
||
target_include_directories(${PROJECT_NAME} PRIVATE | ||
.. | ||
../../../../.. | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# ![Boost.Geometry](../../../doc/other/logo/logo_bkg.png) | ||
|
||
# Qt | ||
|
||
## Introduction | ||
|
||
[Qt](https://www.qt.io/product/framework) is a stable and powerful open source framework for developing native cross-platform GUI applications in C++. | ||
|
||
## Installing Qt | ||
|
||
There are several possibilities. On a Mac it is trivial: | ||
|
||
`brew install qt` | ||
|
||
On other platforms, or if you have already installed Qt, change the makefiles accordingly. | ||
|
||
## Building this example | ||
|
||
Assuming you want to build it with CMake | ||
|
||
``` | ||
cd example/with_external_libs/qt | ||
mkdir my_build_folder | ||
cd my_build_folder | ||
cmake .. -G Ninja | ||
ninja | ||
``` | ||
|
||
## Running this example | ||
|
||
You can pass an Ascii file with WKT polygons as the first command line argument. There are several | ||
packed with Boost.Geometry as examples and as test data. | ||
|
||
For example: `././qt_world_mapper.app/Contents/MacOS/qt_world_mapper ../../../data/world.wkt` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Boost.Geometry | ||
// | ||
// Copyright (c) 2007-2024 Barend Gehrels, Amsterdam, the Netherlands. | ||
// Use, modification and distribution is subject to the Boost Software License, | ||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
// Qt World Mapper Example | ||
|
||
// Qt is a well-known and often used platform independent windows library | ||
|
||
// To build and run this example, on Mac | ||
// - install qt using brew | ||
// If this is not possible or you are using another platform, | ||
// use your own way to install Qt | ||
|
||
#include "qt_world_mapper.hpp" | ||
#include "common/read_countries.hpp" | ||
|
||
#include <QApplication> | ||
#include <QPainter> | ||
#include <QTime> | ||
#include <QTimer> | ||
|
||
#include <boost/geometry/geometries/register/point.hpp> | ||
#include <boost/geometry/geometries/register/ring.hpp> | ||
|
||
// Adapt a QPointF such that it can be handled by Boost.Geometry | ||
BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(QPointF, double, cs::cartesian, x, y, setX, setY) | ||
|
||
// Adapt a QPolygonF as well. | ||
// A QPolygonF has no holes (interiors) so it is similar to a Boost.Geometry ring | ||
BOOST_GEOMETRY_REGISTER_RING(QPolygonF) | ||
|
||
|
||
qt_world_mapper::qt_world_mapper(std::vector<country_type> const& countries, boost::geometry::model::box<point_2d> const& box, QWidget *parent) | ||
: QWidget(parent) | ||
, m_countries(countries) | ||
, m_box(box) | ||
{ | ||
setPalette(QPalette(Qt::blue)); | ||
setAutoFillBackground(true); | ||
} | ||
|
||
void qt_world_mapper::paintEvent(QPaintEvent *) | ||
{ | ||
map_transformer_type transformer(m_box, this->width(), this->height()); | ||
|
||
QPainter painter(this); | ||
painter.setBrush(Qt::green); | ||
painter.setRenderHint(QPainter::Antialiasing); | ||
|
||
for(auto const& country : m_countries) | ||
{ | ||
typedef boost::range_value<country_type>::type polygon_type; | ||
for(auto const& polygon : country) | ||
{ | ||
// This is the essention: | ||
// Directly transform from a multi_polygon (ring-type) to a QPolygonF | ||
QPolygonF qring; | ||
boost::geometry::transform(boost::geometry::exterior_ring(polygon), qring, transformer); | ||
painter.drawPolygon(qring); | ||
} | ||
} | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
const std::string filename = argc > 1 ? argv[1] : "../../../data/world.wkt"; | ||
const auto countries = read_countries<country_type>(filename); | ||
if (countries.empty()) | ||
{ | ||
std::cout << "No countries read" << std::endl; | ||
return 1; | ||
} | ||
|
||
const auto box = calculate_envelope<boost::geometry::model::box<point_2d>>(countries); | ||
|
||
QApplication app(argc, argv); | ||
qt_world_mapper mapper(countries, box); | ||
mapper.setWindowTitle("Boost.Geometry for Qt - Hello World!"); | ||
mapper.setGeometry(100, 100, 1024, 768); | ||
mapper.show(); | ||
return app.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Boost.Geometry | ||
// | ||
// Copyright (c) 2007-2024 Barend Gehrels, Amsterdam, the Netherlands. | ||
// Use, modification and distribution is subject to the Boost Software License, | ||
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
// Qt World Mapper Example | ||
|
||
#ifndef QT_WORLD_MAPPER_H | ||
#define QT_WORLD_MAPPER_H | ||
|
||
#include <QWidget> | ||
|
||
#include <boost/geometry/geometry.hpp> | ||
|
||
#include <boost/geometry/geometries/geometries.hpp> | ||
|
||
using point_2d = boost::geometry::model::d2::point_xy<double>; | ||
using country_type = boost::geometry::model::multi_polygon | ||
< | ||
boost::geometry::model::polygon<point_2d> | ||
>; | ||
|
||
class qt_world_mapper : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
qt_world_mapper(std::vector<country_type> const& countries, boost::geometry::model::box<point_2d> const& box, QWidget *parent = nullptr); | ||
|
||
protected: | ||
void paintEvent(QPaintEvent *event) override; | ||
|
||
private: | ||
using map_transformer_type = boost::geometry::strategy::transform::map_transformer | ||
< | ||
double, 2, 2, | ||
true, true | ||
>; | ||
|
||
std::vector<country_type> m_countries; | ||
boost::geometry::model::box<point_2d> m_box; | ||
}; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.