Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdeclarativegeojsondata.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 Julian Sherollari <jdotsh@gmail.com>
2// Copyright (C) 2023 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
6
7#include <QtLocation/private/qgeojson_p.h>
8
10
11namespace extractor
12{
14 {
15 QVariant props = item->property("props");
16 return !props.isNull();
17 }
18
19 static QVariant toVariant(QObject *mapItem)
20 {
21 if (QDeclarativeGeoMapItemView *miv = qobject_cast<QDeclarativeGeoMapItemView *>(mapItem)) {
22 return toVariant(miv);
23 } else if (QDeclarativePolylineMapItem *polyline = qobject_cast<QDeclarativePolylineMapItem *>(mapItem)) {
24 return toVariant(polyline);
25 } else if (QDeclarativePolygonMapItem *polygon = qobject_cast<QDeclarativePolygonMapItem *>(mapItem)) {
26 return toVariant(polygon);
27 } else if (QDeclarativeCircleMapItem *circle = qobject_cast<QDeclarativeCircleMapItem *>(mapItem)) {
28 return toVariant(circle); // If GeoJSON Point type is visualized in other ways, handle those types here instead.
29 } else if (QDeclarativeRectangleMapItem *rectangle = qobject_cast<QDeclarativeRectangleMapItem *>(mapItem)) {
30 return toVariant(rectangle); // For the self-drawn rectangles. Will be exported as Polygons
31 }
32 return QVariant();
33 }
34
36 {
37 QVariantMap ls;
38 ls[QStringLiteral("type")] = QStringLiteral("Polygon");
39 ls[QStringLiteral("data")] = QVariant::fromValue(mapPolygon->geoShape());
40 if (hasProperties(mapPolygon))
41 ls[QStringLiteral("properties")] = mapPolygon->property("props").toMap();
42 return ls;
43 }
44
46 {
47 QVariantMap ls;
48 ls[QStringLiteral("type")] = QStringLiteral("LineString");
49 ls[QStringLiteral("data")] = QVariant::fromValue(mapPolyline->geoShape());
50 if (hasProperties(mapPolyline))
51 ls[QStringLiteral("properties")] = mapPolyline->property("props").toMap();
52 return ls;
53 }
54
56 {
57 QVariantMap pt;
58 pt[QStringLiteral("type")] = QStringLiteral("Point");
59 pt[QStringLiteral("data")] = QVariant::fromValue(mapCircle->geoShape());
60 QVariantMap propMap = mapCircle->property("props").toMap();
61 propMap[QStringLiteral("radius")] = mapCircle->radius();
62 pt[QStringLiteral("properties")] = propMap;
63 return pt;
64 }
65
67 {
68 QVariantMap pt;
69 pt[QStringLiteral("type")] = QStringLiteral("Polygon");
70 QGeoRectangle rectanlge = mapRectangle->geoShape();
71 QGeoPolygon poly;
72 poly.addCoordinate(rectanlge.topLeft());
73 poly.addCoordinate(rectanlge.topRight());
74 poly.addCoordinate(rectanlge.bottomRight());
75 poly.addCoordinate(rectanlge.bottomLeft());
76 pt[QStringLiteral("data")] = QVariant::fromValue(poly);
77 if (hasProperties(mapRectangle))
78 pt[QStringLiteral("properties")] = mapRectangle->property("props").toMap();
79 return pt;
80 }
81};
82
83
114
119
129{
130 return m_content;
131}
132
134{
135 m_content = model;
137}
138
139
151{
152 return m_url;
153}
154
161{
162 m_content = QVariantList();
164}
165
174{
176 if (auto *polyline = qobject_cast<QDeclarativePolylineMapItem *>(item))
177 entry = extractor::toVariant(polyline);
178 else if (auto *polygon = qobject_cast<QDeclarativePolygonMapItem *>(item))
180 else if (auto *circle = qobject_cast<QDeclarativeCircleMapItem *>(item))
181 entry = extractor::toVariant(circle);
182 else if (auto *rectangle = qobject_cast<QDeclarativeRectangleMapItem *>(item))
183 entry = extractor::toVariant(rectangle);
184 else
185 return;
186
187 QVariantList geoJson = m_content.toList();
188 if (!geoJson.isEmpty()){
189 QVariantList geoData = (geoJson[0].toMap()[QStringLiteral("type")] == QStringLiteral("FeatureCollection")) ? geoJson[0].toMap()[QStringLiteral("data")].toList() : geoJson;
190 geoData.append(entry);
191 geoJson[0] = QVariantMap{{QStringLiteral("type"), QStringLiteral("FeatureCollection")}, {QStringLiteral("data"), geoData}};
192 }
193 else {
194 geoJson.append(entry);
195 }
196 m_content = geoJson;
198}
199
208{
209 return openUrl(m_url);
210}
211
221{
222 // Reading GeoJSON file
223 QFile loadFile(url.toLocalFile());
224 if (!loadFile.open(QIODevice::ReadOnly)) {
225 qWarning() << "Error while opening the file: " << url;
226 qWarning() << loadFile.errorString();
227 return false;
228 }
229
230 // Load the GeoJSON file using Qt's API
231 QJsonParseError err;
232 QJsonDocument loadDoc(QJsonDocument::fromJson(loadFile.readAll(), &err));
233 if (err.error) {
234 qWarning() << "Parsing while importing the JSON document:\n" << err.errorString();
235 return false;
236 }
237
238 // Import geographic data to a QVariantList
239 m_content = QGeoJson::importGeoJson(loadDoc);
240 if (m_url != url) {
241 m_url = url;
243 }
245 return true;
246}
247
257{
258 if (m_url != url){
259 m_url = url;
261 }
262 return save();
263}
264
274{
275 return dumpGeoJSON(m_content.toList(), m_url);
276}
277
294
301QVariantList QDeclarativeGeoJsonData::toVariant(QDeclarativeGeoMap *map)
302{
304 const QList<QObject*> mapChildren = map->children();
305 for (QObject *child : mapChildren) {
306 QVariant mapItemAsVariant = extractor::toVariant(child);
307 if (mapItemAsVariant.isValid())
308 res.append(mapItemAsVariant);
309 }
310 return res;
311}
312
318bool QDeclarativeGeoJsonData::dumpGeoJSON(const QVariantList &geoJson, const QUrl &url)
319{
321 QFile jsonFile(url.toLocalFile());
322 if (!jsonFile.open(QIODevice::WriteOnly))
323 return false;
324 jsonFile.write(json.toJson());
325 jsonFile.close();
326 return true;
327}
328
335bool QDeclarativeGeoJsonData::writeDebug(const QVariantList &geoJson, const QUrl &url)
336{
337 QString prettyPrint = QGeoJson::toString(geoJson);
338 QFile debugFile(url.toLocalFile());
339 if (!debugFile.open(QIODevice::WriteOnly))
340 return false;
341 debugFile.write(prettyPrint.toUtf8());
342 debugFile.close();
343 return true;
344}
345
352QString QDeclarativeGeoJsonData::toString(const QVariantList &geoJson)
353{
354 return QGeoJson::toString(geoJson);
355}
356
Q_INVOKABLE bool save()
\qmlmethod bool QtLocation::GeoJsonData::save()
Q_INVOKABLE bool saveAs(const QUrl &url)
\qmlmethod bool QtLocation::GeoJsonData::saveAs(Url url)
Q_INVOKABLE bool openUrl(const QUrl &url)
\qmlmethod bool QtLocation::GeoJsonData::openUrl(Url url)
void setModel(const QVariant &model)
Q_INVOKABLE void setModelToMapContents(QDeclarativeGeoMap *map)
\qmlmethod void QtLocation::GeoJsonData::setModelToMapContents(MapView mapItemView)
Q_INVOKABLE void clear()
\qmlmethod void QtLocation::GeoJsonData::clear()
Q_INVOKABLE void addItem(QQuickItem *item)
\qmlmethod bool QtLocation::GeoJsonData::addItem(Item item)
QDeclarativeGeoJsonData(QObject *parent=nullptr)
\qmltype GeoJsonData \instantiates QDeclarativeGeoJsonData \inqmlmodule QtLocation
Q_INVOKABLE bool open()
\qmlmethod bool QtLocation::GeoJsonData::open()
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
\inmodule QtPositioning
Definition qgeopolygon.h:16
Q_INVOKABLE void addCoordinate(const QGeoCoordinate &coordinate)
Appends coordinate to the polygon.
\inmodule QtPositioning
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
\inmodule QtCore\reentrant
QByteArray toJson(JsonFormat format=Indented) const
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
QList< T > toList() const noexcept
Definition qlist.h:723
void append(parameter_type t)
Definition qlist.h:458
\inmodule QtCore
Definition qobject.h:103
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QByteArray toUtf8() const &
Definition qstring.h:634
\inmodule QtCore
Definition qurl.h:94
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
Definition qurl.cpp:3425
\inmodule QtCore
Definition qvariant.h:65
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has userType() \l QMetaType::QVariantList.
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
QMap< QString, QString > map
[6]
QVariantList importGeoJson(const QJsonDocument &geoJson)
This method imports the geoJson document, expected to contain valid GeoJSON data, into a QVariantList...
Definition qgeojson.cpp:935
QJsonDocument exportGeoJson(const QVariantList &geoData)
This method exports the QVariantList geoData, expected to be structured like described in the section...
QString toString(const QVariantList &geoData)
This method accepts the QVariantList geoData, structured as described in \l {Importing GeoJSON},...
Combined button and popup list for selecting options.
static QVariant toVariant(QObject *mapItem)
static bool hasProperties(QQuickItem *item)
QList< QVariant > QVariantList
Definition qjsonarray.h:15
#define qWarning
Definition qlogging.h:166
GLenum GLuint GLsizei const GLenum * props
GLuint res
GLuint entry
#define QStringLiteral(str)
#define emit
QSqlQueryModel * model
[16]
QUrl url("example.com")
[constructor-url-reference]
value toMap().value(key)
[3]
QGraphicsItem * item
QLayoutItem * child
[0]
\inmodule QtCore\reentrant
ParseError error
QString errorString() const
\variable QJsonParseError::error