7#include <QtCore/QJsonValue>
8#include <QtCore/QVariantMap>
9#include <QtLocation/private/qgeojson_p.h>
10#include <QtPositioning/QGeoCoordinate>
11#include <QtPositioning/QGeoPath>
12#include <QtPositioning/QGeoPolygon>
23 constexpr const char *
_errorPrefix = QT_TR_NOOP(
"GeoJson file load failed. %1");
32 if (value.canConvert<QGeoPolygon>() || value.canConvert<QGeoPath>() || value.canConvert<QGeoShape>()) {
33 (void) shapes.append(value);
36 if (value.typeId() == QMetaType::QVariantList) {
37 const QVariantList children = value.toList();
38 for (
const QVariant &child : children) {
41 }
else if (value.typeId() == QMetaType::QVariantMap) {
42 const QVariantMap map = value.toMap();
43 for (
auto it = map.cbegin(); it != map.cend(); ++it) {
52 for (
const QVariant &value : values) {
65 QString(QT_TRANSLATE_NOOP(
"GeoJson",
"File not found: %1")).arg(filePath));
66 return QJsonDocument();
69 if (!file.open(QIODevice::ReadOnly)) {
71 QString(QT_TRANSLATE_NOOP(
"GeoJson",
"Unable to open file: %1 error: %2"))
72 .arg(filePath, file.errorString()));
73 return QJsonDocument();
76 QJsonDocument jsonDoc;
77 const QByteArray bytes = file.readAll();
87 using ShapeType = ShapeFileHelper::ShapeType;
91 return ShapeType::Error;
94 const QVariantList imported = QGeoJson::importGeoJson(jsonDoc);
96 if (shapes.isEmpty()) {
98 QT_TRANSLATE_NOOP(
"GeoJson",
"No shapes found in GeoJson file."));
99 return ShapeType::Error;
102 for (
const QVariant &shapeVar : shapes) {
103 if (shapeVar.canConvert<QGeoPolygon>()) {
104 return ShapeType::Polygon;
106 if (shapeVar.canConvert<QGeoPath>()) {
107 return ShapeType::Polyline;
109 if (shapeVar.canConvert<QGeoShape>()) {
110 const QGeoShape shape = shapeVar.value<QGeoShape>();
111 if (shape.type() == QGeoShape::PolygonType) {
112 return ShapeType::Polygon;
114 if (shape.type() == QGeoShape::PathType) {
115 return ShapeType::Polyline;
121 QT_TRANSLATE_NOOP(
"GeoJson",
"No supported type found in GeoJson file."));
122 return ShapeType::Error;
135 const QVariantList imported = QGeoJson::importGeoJson(jsonDoc);
137 if (shapes.isEmpty()) {
139 QT_TRANSLATE_NOOP(
"GeoJson",
"No polygon data found in GeoJson file."));
143 for (
const QVariant &shapeVar : shapes) {
144 if (shapeVar.canConvert<QGeoPolygon>()) {
145 const QGeoPolygon poly = shapeVar.value<QGeoPolygon>();
146 vertices = poly.perimeter();
149 if (shapeVar.canConvert<QGeoShape>()) {
150 const QGeoShape shape = shapeVar.value<QGeoShape>();
151 if (shape.type() == QGeoShape::PolygonType) {
152 const QGeoPolygon poly(shape);
153 vertices = poly.perimeter();
154 if (!vertices.isEmpty()) {
162 QT_TRANSLATE_NOOP(
"GeoJson",
"No polygon found in GeoJson file."));
176 const QVariantList imported = QGeoJson::importGeoJson(jsonDoc);
178 if (shapes.isEmpty()) {
180 QT_TRANSLATE_NOOP(
"GeoJson",
"No polyline data found in GeoJson file."));
184 for (
const QVariant &shapeVar : shapes) {
185 if (shapeVar.canConvert<QGeoPath>()) {
186 const QGeoPath path = shapeVar.value<QGeoPath>();
187 coords = path.path();
190 if (shapeVar.canConvert<QGeoShape>()) {
191 const QGeoShape shape = shapeVar.value<QGeoShape>();
192 if (shape.type() == QGeoShape::PathType) {
193 const QGeoPath path(shape);
194 coords = path.path();
195 if (!coords.isEmpty()) {
203 QT_TRANSLATE_NOOP(
"GeoJson",
"No polyline found in GeoJson file."));
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QVariantList _extractShapeValues(const QVariantList &values)
constexpr const char * _errorPrefix
ShapeFileHelper::ShapeType determineShapeType(const QString &filePath, QString &errorString)
bool loadPolylineFromFile(const QString &filePath, QList< QGeoCoordinate > &coords, QString &errorString)
bool loadGeoJsonCoordinate(const QJsonValue &jsonValue, bool altitudeRequired, QGeoCoordinate &coordinate, QString &errorString)
returned error string if load failure
void saveGeoJsonCoordinate(const QGeoCoordinate &coordinate, bool writeAltitude, QJsonValue &jsonValue)
json value to save to
QJsonDocument _loadFile(const QString &filePath, QString &errorString)
bool loadPolygonFromFile(const QString &filePath, QList< QGeoCoordinate > &vertices, QString &errorString)
constexpr int _maxRecursionDepth
void _extractShapeValuesRecursive(const QVariant &value, QVariantList &shapes, int depth=0)
void saveGeoCoordinate(const QGeoCoordinate &coordinate, bool writeAltitude, QJsonValue &jsonValue, bool geoJsonFormat=false)
bool loadGeoCoordinate(const QJsonValue &jsonValue, bool altitudeRequired, QGeoCoordinate &coordinate, QString &errorString, bool geoJsonFormat=false)
if true, use [lon, lat], [lat, lon] otherwise
bool isJsonFile(const QByteArray &bytes, QJsonDocument &jsonDoc, QString &errorString)
Determines whether an in-memory byte buffer contains parseable JSON content.