hivemind 1.0.0
Loading...
Searching...
No Matches
map_manager.cpp
Go to the documentation of this file.
2
3#include <QNetworkAccessManager>
4#include <QNetworkReply>
5#include <QNetworkRequest>
6#include <QtNetwork>
7#include <vector>
8
9namespace MapManagement
10{
11
12 //"GetMap" retrieves map data from geonorges WMS service. It sets up a
13 // network
14 // request to fetch the map image and processes the response to store the
15 // data.
16 void
18 {
19 // Create a QNetworkAccessManager object for making network requests
20 QNetworkAccessManager* manager = new QNetworkAccessManager(nullptr);
21
22 // Set the URL endpoint for the map service
23 QString endpoint =
24 "https://openwms.statkart.no/skwms1/wms.norges_grunnkart";
25 QUrl url(endpoint);
26 QUrlQuery query;
27
28 // Calculate the corner coordinates of the map based on the UTM
29 // coordinate and size
31
32 // Add query parameters
33 query.addQueryItem("service", "WMS");
34 query.addQueryItem("version", "1.3.0");
35 query.addQueryItem("request", "GetMap");
36 query.addQueryItem("layers",
37 "hoyde,Arealtyper,fjellskygge,helning,Vann_og_"
38 "vassdrag,Samferdsel,Bygninger");
39 query.addQueryItem("styles", "default");
40 query.addQueryItem("format", "image/png");
41 query.addQueryItem("crs", "EPSG:25833");
42 // Set the bounding box query parameter using the map area that was
43 // calulated with the CalculateCornerCoordinates function
44 query.addQueryItem("bbox", Instance().m_Area);
45 // Set the width and height query parameters based on the image
46 // resolution
47 query.addQueryItem("width",
48 QString::number(Instance().m_ImageResolution));
49 query.addQueryItem("height",
50 QString::number(Instance().m_ImageResolution));
51 url.setQuery(query);
52 QNetworkRequest request(url);
53
54 // Emit a signal to indicate that an image request is being made
55 emit Instance().RequestImage();
56 QNetworkReply* reply = manager->get(request);
57
58 // Connect a lambda function to the finished signal of the network reply
59 // and check if the reply has no error. If it has no error it read the
60 // response data from the reply.
61 QObject::connect(reply, &QNetworkReply::finished, [=]() {
62 if (reply->error() == QNetworkReply::NoError) {
63 QByteArray data = reply->readAll();
64 Instance().m_Data = data;
65 // Emit a signal to indicate that the image has been received
66 emit Instance().GotImage();
67 }
68 });
69 };
70
71 // CalculateCornerCoordinates calculates the corner coordinate of the map,
72 // ensuring that the origin is centered in the middle and the sides of the
73 // map are equal in length to the specified size. This is added to a QString
74 // variable, which will be used in the HTTP request within the "getMap"
75 // function.
76 void
78 {
79 double minX = coord.Easting - (size / 2);
80 double minY = coord.Northing - (size / 2);
81 double maxX = coord.Easting + (size / 2);
82 double maxY = coord.Northing + (size / 2);
83
84 const QStringList wmsRequestCoordsList{
85 QString::number(minX),
86 QString::number(long(minY)),
87 QString::number(maxX),
88 QString::number(long(maxY)),
89 };
90 Instance().m_Area = wmsRequestCoordsList.join(",");
91 };
92
93} // namespace MapManagement
static void CalculateCornerCoordinates(Core::UTMCoordinate coord, int size)
Calculates the UTM corner coordinates for the specified UTM coordinate and size.
Definition: map_manager.cpp:77
static MapManager & Instance()
Returns the singleton instance of the class.
Definition: map_manager.h:20
static void GetMap(Core::UTMCoordinate coord, int size)
Retrieves the map from Kartverket for the specified UTM coordinate and size.
Definition: map_manager.cpp:17
\ A structure that represents a coordinate in the Universal Transverse Mercator coordinate system
Definition: types.h:46
double Easting
Definition: types.h:54
double Northing
Definition: types.h:54