|
|
|
@ -1,11 +1,15 @@
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include "map/crs.h"
|
|
|
|
|
#include "geojsonparser.h"
|
|
|
|
|
|
|
|
|
|
#define MARKER_SIZE_MEDIUM 12
|
|
|
|
|
#define MARKER_SIZE_SMALL 8
|
|
|
|
|
#define MARKER_SIZE_LARGE 16
|
|
|
|
|
|
|
|
|
|
#define PROJ(object, parent) \
|
|
|
|
|
((object).isNull() ? (parent) : (object))
|
|
|
|
|
|
|
|
|
|
static int markerSize(const QString &str)
|
|
|
|
|
{
|
|
|
|
|
if (str == "small")
|
|
|
|
@ -109,7 +113,7 @@ static bool isWS(char c)
|
|
|
|
|
return (c == 0x20 || c == 0x09 || c == 0x0A || c == 0x0D) ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isJSONObject(QFile *file)
|
|
|
|
|
static bool possiblyJSONObject(QFile *file)
|
|
|
|
|
{
|
|
|
|
|
char c;
|
|
|
|
|
|
|
|
|
@ -125,6 +129,44 @@ static bool isJSONObject(QFile *file)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::a2c(const QJsonArray &data, const Projection &proj,
|
|
|
|
|
Coordinates &c)
|
|
|
|
|
{
|
|
|
|
|
c = (data.count() >= 2 && data.at(0).isDouble() && data.at(1).isDouble())
|
|
|
|
|
? proj.xy2ll(PointD(data.at(0).toDouble(), data.at(1).toDouble()))
|
|
|
|
|
: Coordinates();
|
|
|
|
|
|
|
|
|
|
if (c.isValid())
|
|
|
|
|
return true;
|
|
|
|
|
else {
|
|
|
|
|
QJsonDocument doc(QJsonDocument::fromVariant(data.toVariantList()));
|
|
|
|
|
_errorString = QString("%1: invalid coordinates")
|
|
|
|
|
.arg(QString::fromUtf8(doc.toJson(QJsonDocument::Compact)));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::crs(const QJsonObject &object, Projection &proj)
|
|
|
|
|
{
|
|
|
|
|
if (!object.contains("crs"))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
QJsonObject crsObj(object["crs"].toObject());
|
|
|
|
|
if (crsObj["type"].toString() != "name" || !crsObj.contains("properties")) {
|
|
|
|
|
_errorString = "Invalid crs object";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
QString str(crsObj["properties"].toObject()["name"].toString());
|
|
|
|
|
proj = CRS::projection(str);
|
|
|
|
|
|
|
|
|
|
if (proj.isValid())
|
|
|
|
|
return true;
|
|
|
|
|
else {
|
|
|
|
|
_errorString = QString("%1: unknown CRS").arg(str);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeoJSONParser::Type GeoJSONParser::type(const QJsonObject &json)
|
|
|
|
|
{
|
|
|
|
|
QString str(json["type"].toString());
|
|
|
|
@ -151,206 +193,327 @@ GeoJSONParser::Type GeoJSONParser::type(const QJsonObject &json)
|
|
|
|
|
return Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::point(const QJsonArray &coordinates, Waypoint &waypoint,
|
|
|
|
|
const QJsonValue &properties)
|
|
|
|
|
bool GeoJSONParser::point(const QJsonObject &object, const Projection &parent,
|
|
|
|
|
const QJsonValue &properties, QVector<Waypoint> &waypoints)
|
|
|
|
|
{
|
|
|
|
|
if (coordinates.count() < 2 || !coordinates.at(0).isDouble()
|
|
|
|
|
|| !coordinates.at(1).isDouble()) {
|
|
|
|
|
_errorString = "Invalid Point Coordinates";
|
|
|
|
|
if (!object.contains("coordinates")) {
|
|
|
|
|
_errorString = "Missing Point coordinates array";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (object["coordinates"].isNull())
|
|
|
|
|
return true;
|
|
|
|
|
QJsonArray coordinates(object["coordinates"].toArray());
|
|
|
|
|
if (coordinates.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
setWaypointProperties(waypoint, properties);
|
|
|
|
|
Coordinates c;
|
|
|
|
|
if (!a2c(coordinates, PROJ(proj, parent), c))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
waypoint.setCoordinates(Coordinates(coordinates.at(0).toDouble(),
|
|
|
|
|
coordinates.at(1).toDouble()));
|
|
|
|
|
Waypoint waypoint(c);
|
|
|
|
|
if (coordinates.count() == 3 && coordinates.at(2).isDouble())
|
|
|
|
|
waypoint.setElevation(coordinates.at(2).toDouble());
|
|
|
|
|
setWaypointProperties(waypoint, properties);
|
|
|
|
|
waypoints.append(waypoint);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::multiPoint(const QJsonArray &coordinates,
|
|
|
|
|
QVector<Waypoint> &waypoints, const QJsonValue &properties)
|
|
|
|
|
bool GeoJSONParser::multiPoint(const QJsonObject &object,
|
|
|
|
|
const Projection &parent, const QJsonValue &properties,
|
|
|
|
|
QVector<Waypoint> &waypoints)
|
|
|
|
|
{
|
|
|
|
|
if (!object.contains("coordinates")) {
|
|
|
|
|
_errorString = "Missing MultiPoint coordinates array";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (object["coordinates"].isNull())
|
|
|
|
|
return true;
|
|
|
|
|
QJsonArray coordinates(object["coordinates"].toArray());
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
Coordinates c;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < coordinates.size(); i++) {
|
|
|
|
|
if (!coordinates.at(i).isArray()) {
|
|
|
|
|
_errorString = "Invalid MultiPoint coordinates";
|
|
|
|
|
_errorString = "Invalid MultiPoint data";
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
waypoints.resize(waypoints.size() + 1);
|
|
|
|
|
if (!point(coordinates.at(i).toArray(), waypoints.last(), properties))
|
|
|
|
|
QJsonArray data(coordinates.at(i).toArray());
|
|
|
|
|
if (!a2c(data, PROJ(proj, parent), c))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Waypoint waypoint(c);
|
|
|
|
|
if (data.count() == 3 && data.at(2).isDouble())
|
|
|
|
|
waypoint.setElevation(data.at(2).toDouble());
|
|
|
|
|
setWaypointProperties(waypoint, properties);
|
|
|
|
|
waypoints.append(waypoint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::lineString(const QJsonArray &coordinates,
|
|
|
|
|
SegmentData &segment)
|
|
|
|
|
bool GeoJSONParser::lineString(const QJsonObject &object,
|
|
|
|
|
const Projection &parent, const QJsonValue &properties,
|
|
|
|
|
QList<TrackData> &tracks)
|
|
|
|
|
{
|
|
|
|
|
if (!object.contains("coordinates")) {
|
|
|
|
|
_errorString = "Missing LineString coordinates array";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (object["coordinates"].isNull())
|
|
|
|
|
return true;
|
|
|
|
|
QJsonArray coordinates(object["coordinates"].toArray());
|
|
|
|
|
if (coordinates.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
SegmentData segment;
|
|
|
|
|
Coordinates c;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < coordinates.size(); i++) {
|
|
|
|
|
QJsonArray point(coordinates.at(i).toArray());
|
|
|
|
|
if (point.count() < 2 || !point.at(0).isDouble()
|
|
|
|
|
|| !point.at(1).isDouble()) {
|
|
|
|
|
_errorString = "Invalid LineString coordinates";
|
|
|
|
|
if (!coordinates.at(i).isArray()) {
|
|
|
|
|
_errorString = "Invalid LineString data";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trackpoint t(Coordinates(point.at(0).toDouble(),
|
|
|
|
|
point.at(1).toDouble()));
|
|
|
|
|
if (point.count() == 3 && point.at(2).isDouble())
|
|
|
|
|
t.setElevation(point.at(2).toDouble());
|
|
|
|
|
QJsonArray data(coordinates.at(i).toArray());
|
|
|
|
|
if (!a2c(data, PROJ(proj, parent), c))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Trackpoint t(c);
|
|
|
|
|
if (data.count() == 3 && data.at(2).isDouble())
|
|
|
|
|
t.setElevation(data.at(2).toDouble());
|
|
|
|
|
segment.append(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::lineString(const QJsonArray &coordinates, TrackData &track,
|
|
|
|
|
const QJsonValue &properties)
|
|
|
|
|
{
|
|
|
|
|
TrackData track(segment);
|
|
|
|
|
setTrackProperties(track, properties);
|
|
|
|
|
|
|
|
|
|
track.append(SegmentData());
|
|
|
|
|
|
|
|
|
|
lineString(coordinates, track.last());
|
|
|
|
|
tracks.append(track);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::multiLineString(const QJsonArray &coordinates,
|
|
|
|
|
TrackData &track, const QJsonValue &properties)
|
|
|
|
|
bool GeoJSONParser::multiLineString(const QJsonObject &object,
|
|
|
|
|
const Projection &parent, const QJsonValue &properties,
|
|
|
|
|
QList<TrackData> &tracks)
|
|
|
|
|
{
|
|
|
|
|
setTrackProperties(track, properties);
|
|
|
|
|
if (!object.contains("coordinates")) {
|
|
|
|
|
_errorString = "Missing MultiLineString coordinates array";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (object["coordinates"].isNull())
|
|
|
|
|
return true;
|
|
|
|
|
QJsonArray coordinates(object["coordinates"].toArray());
|
|
|
|
|
if (coordinates.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
TrackData track;
|
|
|
|
|
Coordinates c;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < coordinates.size(); i++) {
|
|
|
|
|
if (!coordinates.at(i).isArray()) {
|
|
|
|
|
_errorString = "Invalid MultiLineString coordinates";
|
|
|
|
|
_errorString = "Invalid MultiLineString data";
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
track.append(SegmentData());
|
|
|
|
|
if (!lineString(coordinates.at(i).toArray(), track.last()))
|
|
|
|
|
return false;
|
|
|
|
|
SegmentData sd;
|
|
|
|
|
|
|
|
|
|
QJsonArray ls(coordinates.at(i).toArray());
|
|
|
|
|
for (int j = 0; j < ls.size(); j++) {
|
|
|
|
|
if (!ls.at(j).isArray()) {
|
|
|
|
|
_errorString = "Invalid MultiLineString LineString data";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonArray data(ls.at(j).toArray());
|
|
|
|
|
if (!a2c(data, PROJ(proj, parent), c))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Trackpoint t(c);
|
|
|
|
|
if (data.count() == 3 && data.at(2).isDouble())
|
|
|
|
|
t.setElevation(data.at(2).toDouble());
|
|
|
|
|
sd.append(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
track.append(sd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTrackProperties(track, properties);
|
|
|
|
|
tracks.append(track);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::polygon(const QJsonArray &coordinates, ::Polygon &pg)
|
|
|
|
|
bool GeoJSONParser::polygon(const QJsonObject &object, const Projection &parent,
|
|
|
|
|
const QJsonValue &properties, QList<Area> &areas)
|
|
|
|
|
{
|
|
|
|
|
if (!object.contains("coordinates")) {
|
|
|
|
|
_errorString = "Missing Polygon coordinates array";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (object["coordinates"].isNull())
|
|
|
|
|
return true;
|
|
|
|
|
QJsonArray coordinates(object["coordinates"].toArray());
|
|
|
|
|
if (coordinates.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
::Polygon poly;
|
|
|
|
|
Coordinates c;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < coordinates.size(); i++) {
|
|
|
|
|
if (!coordinates.at(i).isArray()) {
|
|
|
|
|
_errorString = "Invalid Polygon linear ring";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QJsonArray lr(coordinates.at(i).toArray());
|
|
|
|
|
QJsonArray lr(coordinates.at(i).toArray());
|
|
|
|
|
QVector<Coordinates> data;
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < lr.size(); j++) {
|
|
|
|
|
QJsonArray point(lr.at(j).toArray());
|
|
|
|
|
if (point.count() < 2 || !point.at(0).isDouble()
|
|
|
|
|
|| !point.at(1).isDouble()) {
|
|
|
|
|
_errorString = "Invalid Polygon linear ring coordinates";
|
|
|
|
|
if (!lr.at(j).isArray()) {
|
|
|
|
|
_errorString = "Invalid Polygon linear ring data";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
data.append(Coordinates(point.at(0).toDouble(),
|
|
|
|
|
point.at(1).toDouble()));
|
|
|
|
|
|
|
|
|
|
QJsonArray point(lr.at(j).toArray());
|
|
|
|
|
if (!a2c(point, PROJ(proj, parent), c))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
data.append(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pg.append(data);
|
|
|
|
|
poly.append(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Area area(poly);
|
|
|
|
|
setAreaProperties(area, properties);
|
|
|
|
|
areas.append(area);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::polygon(const QJsonArray &coordinates, Area &area,
|
|
|
|
|
const QJsonValue &properties)
|
|
|
|
|
bool GeoJSONParser::multiPolygon(const QJsonObject &object,
|
|
|
|
|
const Projection &parent, const QJsonValue &properties, QList<Area> &areas)
|
|
|
|
|
{
|
|
|
|
|
setAreaProperties(area, properties);
|
|
|
|
|
|
|
|
|
|
::Polygon p;
|
|
|
|
|
if (!polygon(coordinates, p))
|
|
|
|
|
if (!object.contains("coordinates")) {
|
|
|
|
|
_errorString = "Missing MultiPolygon coordinates array";
|
|
|
|
|
return false;
|
|
|
|
|
area.append(p);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::multiPolygon(const QJsonArray &coordinates,
|
|
|
|
|
Area &area, const QJsonValue &properties)
|
|
|
|
|
{
|
|
|
|
|
setAreaProperties(area, properties);
|
|
|
|
|
}
|
|
|
|
|
if (object["coordinates"].isNull())
|
|
|
|
|
return true;
|
|
|
|
|
QJsonArray coordinates(object["coordinates"].toArray());
|
|
|
|
|
if (coordinates.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
Area area;
|
|
|
|
|
Coordinates c;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < coordinates.size(); i++) {
|
|
|
|
|
if (!coordinates.at(i).isArray()) {
|
|
|
|
|
_errorString = "Invalid MultiPolygon coordinates";
|
|
|
|
|
_errorString = "Invalid MultiPolygon data";
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
::Polygon p;
|
|
|
|
|
if (!polygon(coordinates.at(i).toArray(), p))
|
|
|
|
|
return false;
|
|
|
|
|
area.append(p);
|
|
|
|
|
::Polygon poly;
|
|
|
|
|
|
|
|
|
|
QJsonArray polygon(coordinates.at(i).toArray());
|
|
|
|
|
for (int j = 0; j < polygon.size(); j++) {
|
|
|
|
|
if (!polygon.at(j).isArray()) {
|
|
|
|
|
_errorString = "Invalid MultiPolygon linear ring";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonArray lr(polygon.at(j).toArray());
|
|
|
|
|
QVector<Coordinates> data;
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < lr.size(); k++) {
|
|
|
|
|
if (!lr.at(k).isArray()) {
|
|
|
|
|
_errorString = "Invalid MultiPolygon linear ring data";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonArray point(lr.at(k).toArray());
|
|
|
|
|
if (!a2c(point, PROJ(proj, parent), c))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
data.append(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
poly.append(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
area.append(poly);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setAreaProperties(area, properties);
|
|
|
|
|
areas.append(area);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::geometryCollection(const QJsonObject &json,
|
|
|
|
|
QList<TrackData> &tracks, QList<Area> &areas,
|
|
|
|
|
QVector<Waypoint> &waypoints, const QJsonValue &properties)
|
|
|
|
|
bool GeoJSONParser::geometryCollection(const QJsonObject &object,
|
|
|
|
|
const Projection &parent, const QJsonValue &properties,
|
|
|
|
|
QList<TrackData> &tracks, QList<Area> &areas, QVector<Waypoint> &waypoints)
|
|
|
|
|
{
|
|
|
|
|
if (!json.contains("geometries") || !json["geometries"].isArray()) {
|
|
|
|
|
if (!object.contains("geometries") || !object["geometries"].isArray()) {
|
|
|
|
|
_errorString = "Invalid/missing GeometryCollection geometries array";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonArray geometries(json["geometries"].toArray());
|
|
|
|
|
QJsonArray geometries(object["geometries"].toArray());
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < geometries.size(); i++) {
|
|
|
|
|
QJsonObject geometry(geometries.at(i).toObject());
|
|
|
|
|
|
|
|
|
|
switch (type(geometry)) {
|
|
|
|
|
case Point:
|
|
|
|
|
waypoints.resize(waypoints.size() + 1);
|
|
|
|
|
if (!point(geometry["coordinates"].toArray(), waypoints.last(),
|
|
|
|
|
properties))
|
|
|
|
|
if (!point(geometry, PROJ(proj, parent), properties,
|
|
|
|
|
waypoints))
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case MultiPoint:
|
|
|
|
|
if (!multiPoint(geometry["coordinates"].toArray(), waypoints,
|
|
|
|
|
properties))
|
|
|
|
|
if (!multiPoint(geometry, PROJ(proj, parent), properties,
|
|
|
|
|
waypoints))
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case LineString:
|
|
|
|
|
tracks.append(TrackData());
|
|
|
|
|
if (!lineString(geometry["coordinates"].toArray(),
|
|
|
|
|
tracks.last(), properties))
|
|
|
|
|
if (!lineString(geometry, PROJ(proj, parent), properties,
|
|
|
|
|
tracks))
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case MultiLineString:
|
|
|
|
|
tracks.append(TrackData());
|
|
|
|
|
if (!multiLineString(geometry["coordinates"].toArray(),
|
|
|
|
|
tracks.last(), properties))
|
|
|
|
|
if (!multiLineString(geometry, PROJ(proj, parent), properties,
|
|
|
|
|
tracks))
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case Polygon:
|
|
|
|
|
areas.append(Area());
|
|
|
|
|
if (!polygon(geometry["coordinates"].toArray(), areas.last(),
|
|
|
|
|
properties))
|
|
|
|
|
if (!polygon(geometry, PROJ(proj, parent), properties, areas))
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case MultiPolygon:
|
|
|
|
|
areas.append(Area());
|
|
|
|
|
if (!multiPolygon(geometry["coordinates"].toArray(),
|
|
|
|
|
areas.last(), properties))
|
|
|
|
|
if (!multiPolygon(geometry, PROJ(proj, parent), properties,
|
|
|
|
|
areas))
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case GeometryCollection:
|
|
|
|
|
if (!geometryCollection(geometry, tracks, areas, waypoints,
|
|
|
|
|
properties))
|
|
|
|
|
if (!geometryCollection(geometry, PROJ(proj, parent),
|
|
|
|
|
properties, tracks, areas, waypoints))
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
@ -363,38 +526,38 @@ bool GeoJSONParser::geometryCollection(const QJsonObject &json,
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::feature(const QJsonObject &json, QList<TrackData> &tracks,
|
|
|
|
|
QList<Area> &areas, QVector<Waypoint> &waypoints)
|
|
|
|
|
bool GeoJSONParser::feature(const QJsonObject &object, const Projection &parent,
|
|
|
|
|
QList<TrackData> &tracks, QList<Area> &areas, QVector<Waypoint> &waypoints)
|
|
|
|
|
{
|
|
|
|
|
QJsonValue properties(json["properties"]);
|
|
|
|
|
QJsonObject geometry(json["geometry"].toObject());
|
|
|
|
|
if (!object.contains("geometry") || !object["geometry"].isObject()) {
|
|
|
|
|
_errorString = "Invalid/missing Feature geometry object";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonValue properties(object["properties"]);
|
|
|
|
|
QJsonObject geometry(object["geometry"].toObject());
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
switch (type(geometry)) {
|
|
|
|
|
case Point:
|
|
|
|
|
waypoints.resize(waypoints.size() + 1);
|
|
|
|
|
return point(geometry["coordinates"].toArray(), waypoints.last(),
|
|
|
|
|
properties);
|
|
|
|
|
return point(geometry, PROJ(proj, parent), properties, waypoints);
|
|
|
|
|
case MultiPoint:
|
|
|
|
|
return multiPoint(geometry["coordinates"].toArray(), waypoints,
|
|
|
|
|
properties);
|
|
|
|
|
return multiPoint(geometry, PROJ(proj, parent), properties,
|
|
|
|
|
waypoints);
|
|
|
|
|
case LineString:
|
|
|
|
|
tracks.append(TrackData());
|
|
|
|
|
return lineString(geometry["coordinates"].toArray(), tracks.last(),
|
|
|
|
|
properties);
|
|
|
|
|
return lineString(geometry, PROJ(proj, parent), properties, tracks);
|
|
|
|
|
case MultiLineString:
|
|
|
|
|
tracks.append(TrackData());
|
|
|
|
|
return multiLineString(geometry["coordinates"].toArray(),
|
|
|
|
|
tracks.last(), properties);
|
|
|
|
|
return multiLineString(geometry, PROJ(proj, parent), properties,
|
|
|
|
|
tracks);
|
|
|
|
|
case GeometryCollection:
|
|
|
|
|
return geometryCollection(geometry, tracks, areas, waypoints);
|
|
|
|
|
return geometryCollection(geometry, PROJ(proj, parent), properties,
|
|
|
|
|
tracks, areas, waypoints);
|
|
|
|
|
case Polygon:
|
|
|
|
|
areas.append(Area());
|
|
|
|
|
return polygon(geometry["coordinates"].toArray(), areas.last(),
|
|
|
|
|
properties);
|
|
|
|
|
return polygon(geometry, PROJ(proj, parent), properties, areas);
|
|
|
|
|
case MultiPolygon:
|
|
|
|
|
areas.append(Area());
|
|
|
|
|
return multiPolygon(geometry["coordinates"].toArray(), areas.last(),
|
|
|
|
|
properties);
|
|
|
|
|
return multiPolygon(geometry, PROJ(proj, parent), properties, areas);
|
|
|
|
|
default:
|
|
|
|
|
_errorString = geometry["type"].toString()
|
|
|
|
|
+ ": invalid/missing Feature geometry";
|
|
|
|
@ -402,18 +565,23 @@ bool GeoJSONParser::feature(const QJsonObject &json, QList<TrackData> &tracks,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeoJSONParser::featureCollection(const QJsonObject &json,
|
|
|
|
|
QList<TrackData> &tracks, QList<Area> &areas,
|
|
|
|
|
bool GeoJSONParser::featureCollection(const QJsonObject &object,
|
|
|
|
|
const Projection &parent, QList<TrackData> &tracks, QList<Area> &areas,
|
|
|
|
|
QVector<Waypoint> &waypoints)
|
|
|
|
|
{
|
|
|
|
|
if (!json.contains("features") || !json["features"].isArray()) {
|
|
|
|
|
if (!object.contains("features") || !object["features"].isArray()) {
|
|
|
|
|
_errorString = "Invalid/missing FeatureCollection features array";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonArray features(json["features"].toArray());
|
|
|
|
|
QJsonArray features(object["features"].toArray());
|
|
|
|
|
Projection proj;
|
|
|
|
|
if (!crs(object, proj))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < features.size(); i++)
|
|
|
|
|
if (!feature(features.at(i).toObject(), tracks, areas, waypoints))
|
|
|
|
|
if (!feature(features.at(i).toObject(), PROJ(proj, parent), tracks,
|
|
|
|
|
areas, waypoints))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
@ -425,7 +593,7 @@ bool GeoJSONParser::parse(QFile *file, QList<TrackData> &tracks,
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(routes);
|
|
|
|
|
|
|
|
|
|
if (!isJSONObject(file)) {
|
|
|
|
|
if (!possiblyJSONObject(file)) {
|
|
|
|
|
_errorString = "Not a GeoJSON file";
|
|
|
|
|
return false;
|
|
|
|
|
} else
|
|
|
|
@ -435,42 +603,39 @@ bool GeoJSONParser::parse(QFile *file, QList<TrackData> &tracks,
|
|
|
|
|
QJsonDocument doc(QJsonDocument::fromJson(file->readAll(), &error));
|
|
|
|
|
|
|
|
|
|
if (doc.isNull()) {
|
|
|
|
|
_errorString = "JSON parse error: " + error.errorString() + " ["
|
|
|
|
|
+ QString::number(error.offset) + "]";
|
|
|
|
|
_errorString = QString("JSON parse error on offset %1: %2")
|
|
|
|
|
.arg(QString::number(error.offset), error.errorString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonObject json(doc.object());
|
|
|
|
|
QJsonObject object(doc.object());
|
|
|
|
|
Projection proj(GCS::WGS84());
|
|
|
|
|
|
|
|
|
|
switch (type(json)) {
|
|
|
|
|
switch (type(object)) {
|
|
|
|
|
case Point:
|
|
|
|
|
waypoints.resize(waypoints.size() + 1);
|
|
|
|
|
return point(json["coordinates"].toArray(), waypoints.last());
|
|
|
|
|
return point(object, proj, QJsonValue(), waypoints);
|
|
|
|
|
case MultiPoint:
|
|
|
|
|
return multiPoint(json["coordinates"].toArray(), waypoints);
|
|
|
|
|
return multiPoint(object, proj, QJsonValue(), waypoints);
|
|
|
|
|
case LineString:
|
|
|
|
|
tracks.append(TrackData());
|
|
|
|
|
return lineString(json["coordinates"].toArray(), tracks.last());
|
|
|
|
|
return lineString(object, proj, QJsonValue(), tracks);
|
|
|
|
|
case MultiLineString:
|
|
|
|
|
tracks.append(TrackData());
|
|
|
|
|
return multiLineString(json["coordinates"].toArray(), tracks.last());
|
|
|
|
|
return multiLineString(object, proj, QJsonValue(), tracks);
|
|
|
|
|
case GeometryCollection:
|
|
|
|
|
return geometryCollection(json, tracks, areas, waypoints);
|
|
|
|
|
return geometryCollection(object, proj, QJsonValue(), tracks, areas,
|
|
|
|
|
waypoints);
|
|
|
|
|
case Feature:
|
|
|
|
|
return feature(json, tracks, areas, waypoints);
|
|
|
|
|
return feature(object, proj, tracks, areas, waypoints);
|
|
|
|
|
case FeatureCollection:
|
|
|
|
|
return featureCollection(json, tracks, areas, waypoints);
|
|
|
|
|
return featureCollection(object, proj, tracks, areas, waypoints);
|
|
|
|
|
case Polygon:
|
|
|
|
|
areas.append(Area());
|
|
|
|
|
return polygon(json["coordinates"].toArray(), areas.last());
|
|
|
|
|
return polygon(object, proj, QJsonValue(), areas);
|
|
|
|
|
case MultiPolygon:
|
|
|
|
|
areas.append(Area());
|
|
|
|
|
return multiPolygon(json["coordinates"].toArray(), areas.last());
|
|
|
|
|
return multiPolygon(object, proj, QJsonValue(), areas);
|
|
|
|
|
case Unknown:
|
|
|
|
|
if (json["type"].toString().isNull())
|
|
|
|
|
if (object["type"].toString().isNull())
|
|
|
|
|
_errorString = "Not a GeoJSON file";
|
|
|
|
|
else
|
|
|
|
|
_errorString = json["type"].toString()
|
|
|
|
|
_errorString = object["type"].toString()
|
|
|
|
|
+ ": unknown GeoJSON object";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|