1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 13:41:16 +01:00
GPXSee/src/data/geojsonparser.cpp

453 lines
12 KiB
C++
Raw Normal View History

2019-01-25 22:18:21 +01:00
#include <QJsonDocument>
#include <QJsonArray>
#include "geojsonparser.h"
#define MARKER_SIZE_MEDIUM 12
#define MARKER_SIZE_SMALL 8
#define MARKER_SIZE_LARGE 16
static int markerSize(const QString &str)
{
if (str == "small")
return MARKER_SIZE_SMALL;
else if (str == "medium")
return MARKER_SIZE_MEDIUM;
else if (str == "large")
return MARKER_SIZE_LARGE;
else
return -1;
}
static void setAreaProperties(Area &area, const QJsonValue &properties)
{
QColor strokeColor(0x55, 0x55, 0x55);
2022-09-23 21:36:02 +02:00
QColor fillColor(0x55, 0x55, 0x55, 0x99);
double strokeWidth = 2;
2022-09-23 21:36:02 +02:00
if (properties.isObject()) {
QJsonObject o(properties.toObject());
if (o["name"].isString())
area.setName(o["name"].toString());
if (o["title"].isString())
area.setName(o["title"].toString());
if (o["description"].isString())
area.setDescription(o["description"].toString());
if (o["stroke"].isString())
strokeColor = QColor(o["stroke"].toString());
if (o["stroke-opacity"].isDouble())
strokeColor.setAlphaF(o["stroke-opacity"].toDouble());
if (o["stroke-width"].isDouble())
strokeWidth = o["stroke-width"].toDouble();
if (o["fill"].isString())
fillColor = QColor(o["fill"].toString());
if (o["fill-opacity"].isDouble())
fillColor.setAlphaF(o["fill-opacity"].toDouble());
else
fillColor.setAlphaF(0.6);
}
area.setStyle(PolygonStyle(fillColor, strokeColor, strokeWidth));
}
static void setTrackProperties(TrackData &track, const QJsonValue &properties)
{
QColor color(0x55, 0x55, 0x55);
double width = 2;
2022-09-23 21:36:02 +02:00
if (properties.isObject()) {
QJsonObject o(properties.toObject());
if (o["name"].isString())
track.setName(o["name"].toString());
if (o["title"].isString())
track.setName(o["title"].toString());
if (o["description"].isString())
track.setDescription(o["description"].toString());
if (o["stroke"].isString())
color = QColor(o["stroke"].toString());
if (o["stroke-opacity"].isDouble())
color.setAlphaF(o["stroke-opacity"].toDouble());
if (o["stroke-width"].isDouble())
width = o["stroke-width"].toDouble();
}
track.setStyle(LineStyle(color, width));
}
static void setWaypointProperties(Waypoint &waypoint,
const QJsonValue &properties)
{
QColor color(0x7e, 0x7e, 0x7e);
int size = MARKER_SIZE_MEDIUM;
2022-09-23 21:36:02 +02:00
if (!properties.isObject()) {
QJsonObject o(properties.toObject());
if (o["name"].isString())
waypoint.setName(o["name"].toString());
if (o["title"].isString())
waypoint.setName(o["title"].toString());
if (o["description"].isString())
waypoint.setDescription(o["description"].toString());
if (o["marker-color"].isString())
color = QColor(o["marker-color"].toString());
if (o["marker-symbol"].isString())
waypoint.setSymbol(o["marker-symbol"].toString());
if (o["marker-size"].isString())
size = markerSize(o["marker-size"].toString());
}
waypoint.setStyle(PointStyle(color, size));
}
2019-01-25 22:18:21 +01:00
2019-01-31 01:46:53 +01:00
GeoJSONParser::Type GeoJSONParser::type(const QJsonObject &json)
2019-01-25 22:18:21 +01:00
{
QString str(json["type"].toString());
if (str == "Point")
return Point;
else if (str == "MultiPoint")
return MultiPoint;
else if (str == "LineString")
return LineString;
else if (str == "MultiLineString")
return MultiLineString;
else if (str == "Polygon")
return Polygon;
else if (str == "MultiPolygon")
return MultiPolygon;
else if (str == "GeometryCollection")
return GeometryCollection;
else if (str == "Feature")
return Feature;
else if (str == "FeatureCollection")
return FeatureCollection;
else
return Unknown;
}
bool GeoJSONParser::point(const QJsonArray &coordinates, Waypoint &waypoint,
const QJsonValue &properties)
2019-01-25 22:18:21 +01:00
{
if (coordinates.count() < 2 || !coordinates.at(0).isDouble()
|| !coordinates.at(1).isDouble()) {
_errorString = "Invalid Point Coordinates";
return false;
}
setWaypointProperties(waypoint, properties);
2019-01-25 22:18:21 +01:00
waypoint.setCoordinates(Coordinates(coordinates.at(0).toDouble(),
coordinates.at(1).toDouble()));
if (coordinates.count() == 3 && coordinates.at(2).isDouble())
waypoint.setElevation(coordinates.at(2).toDouble());
return true;
}
bool GeoJSONParser::multiPoint(const QJsonArray &coordinates,
QVector<Waypoint> &waypoints, const QJsonValue &properties)
2019-01-25 22:18:21 +01:00
{
for (int i = 0; i < coordinates.size(); i++) {
if (!coordinates.at(i).isArray()) {
2019-01-31 01:46:53 +01:00
_errorString = "Invalid MultiPoint coordinates";
2019-01-25 22:18:21 +01:00
return false;
} else {
waypoints.resize(waypoints.size() + 1);
if (!point(coordinates.at(i).toArray(), waypoints.last(), properties))
return false;
}
}
return true;
}
2019-02-11 23:28:08 +01:00
bool GeoJSONParser::lineString(const QJsonArray &coordinates,
SegmentData &segment)
2019-01-25 22:18:21 +01:00
{
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()) {
2019-01-31 01:46:53 +01:00
_errorString = "Invalid LineString coordinates";
2019-01-25 22:18:21 +01:00
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());
2019-02-11 23:28:08 +01:00
segment.append(t);
2019-01-25 22:18:21 +01:00
}
return true;
}
2019-02-11 23:28:08 +01:00
bool GeoJSONParser::lineString(const QJsonArray &coordinates, TrackData &track,
const QJsonValue &properties)
2019-02-11 23:28:08 +01:00
{
setTrackProperties(track, properties);
2019-02-11 23:28:08 +01:00
track.append(SegmentData());
lineString(coordinates, track.last());
return true;
}
2019-01-25 22:18:21 +01:00
bool GeoJSONParser::multiLineString(const QJsonArray &coordinates,
TrackData &track, const QJsonValue &properties)
2019-01-25 22:18:21 +01:00
{
setTrackProperties(track, properties);
2019-02-11 23:28:08 +01:00
2019-01-25 22:18:21 +01:00
for (int i = 0; i < coordinates.size(); i++) {
if (!coordinates.at(i).isArray()) {
2019-01-31 01:46:53 +01:00
_errorString = "Invalid MultiLineString coordinates";
2019-01-25 22:18:21 +01:00
return false;
} else {
2019-02-11 23:28:08 +01:00
track.append(SegmentData());
if (!lineString(coordinates.at(i).toArray(), track.last()))
2019-01-25 22:18:21 +01:00
return false;
}
}
return true;
}
2019-01-31 01:46:53 +01:00
bool GeoJSONParser::polygon(const QJsonArray &coordinates, ::Polygon &pg)
{
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());
2021-04-10 15:27:40 +02:00
QVector<Coordinates> data;
2019-01-31 01:46:53 +01:00
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";
return false;
}
data.append(Coordinates(point.at(0).toDouble(),
point.at(1).toDouble()));
}
2021-04-10 15:27:40 +02:00
pg.append(data);
2019-01-31 01:46:53 +01:00
}
return true;
}
bool GeoJSONParser::polygon(const QJsonArray &coordinates, Area &area,
const QJsonValue &properties)
2019-01-31 01:46:53 +01:00
{
setAreaProperties(area, properties);
2019-01-31 01:46:53 +01:00
::Polygon p;
if (!polygon(coordinates, p))
return false;
area.append(p);
return true;
2019-01-31 01:46:53 +01:00
}
bool GeoJSONParser::multiPolygon(const QJsonArray &coordinates,
Area &area, const QJsonValue &properties)
2019-01-31 01:46:53 +01:00
{
setAreaProperties(area, properties);
2019-01-31 01:46:53 +01:00
for (int i = 0; i < coordinates.size(); i++) {
if (!coordinates.at(i).isArray()) {
_errorString = "Invalid MultiPolygon coordinates";
return false;
} else {
::Polygon p;
if (!polygon(coordinates.at(i).toArray(), p))
2019-01-31 01:46:53 +01:00
return false;
area.append(p);
2019-01-31 01:46:53 +01:00
}
}
return true;
}
bool GeoJSONParser::geometryCollection(const QJsonObject &json,
QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints, const QJsonValue &properties)
2019-01-25 22:18:21 +01:00
{
if (!json.contains("geometries") || !json["geometries"].isArray()) {
_errorString = "Invalid/missing GeometryCollection geometries array";
return false;
}
QJsonArray geometries(json["geometries"].toArray());
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))
return false;
break;
case MultiPoint:
if (!multiPoint(geometry["coordinates"].toArray(), waypoints,
properties))
return false;
break;
case LineString:
tracks.append(TrackData());
if (!lineString(geometry["coordinates"].toArray(),
tracks.last(), properties))
return false;
break;
case MultiLineString:
2019-02-11 23:28:08 +01:00
tracks.append(TrackData());
if (!multiLineString(geometry["coordinates"].toArray(),
tracks.last(), properties))
2019-01-25 22:18:21 +01:00
return false;
break;
case Polygon:
2019-01-31 01:46:53 +01:00
areas.append(Area());
if (!polygon(geometry["coordinates"].toArray(), areas.last(),
properties))
return false;
break;
2019-01-25 22:18:21 +01:00
case MultiPolygon:
2019-01-31 01:46:53 +01:00
areas.append(Area());
if (!multiPolygon(geometry["coordinates"].toArray(),
areas.last(), properties))
return false;
2019-01-25 22:18:21 +01:00
break;
case GeometryCollection:
2019-01-31 01:46:53 +01:00
if (!geometryCollection(geometry, tracks, areas, waypoints,
2019-01-25 22:18:21 +01:00
properties))
return false;
break;
default:
_errorString = geometry["type"].toString()
+ ": invalid/missing geometry type";
return false;
}
}
return true;
}
bool GeoJSONParser::feature(const QJsonObject &json, QList<TrackData> &tracks,
2019-01-31 01:46:53 +01:00
QList<Area> &areas, QVector<Waypoint> &waypoints)
2019-01-25 22:18:21 +01:00
{
QJsonValue properties(json["properties"]);
2019-01-25 22:18:21 +01:00
QJsonObject geometry(json["geometry"].toObject());
switch (type(geometry)) {
case Point:
waypoints.resize(waypoints.size() + 1);
return point(geometry["coordinates"].toArray(), waypoints.last(),
properties);
case MultiPoint:
return multiPoint(geometry["coordinates"].toArray(), waypoints,
properties);
case LineString:
tracks.append(TrackData());
return lineString(geometry["coordinates"].toArray(), tracks.last(),
properties);
case MultiLineString:
2019-02-11 23:28:08 +01:00
tracks.append(TrackData());
return multiLineString(geometry["coordinates"].toArray(),
tracks.last(), properties);
2019-01-25 22:18:21 +01:00
case GeometryCollection:
2019-01-31 01:46:53 +01:00
return geometryCollection(geometry, tracks, areas, waypoints);
2019-01-25 22:18:21 +01:00
case Polygon:
2019-01-31 01:46:53 +01:00
areas.append(Area());
return polygon(geometry["coordinates"].toArray(), areas.last(),
properties);
2019-01-25 22:18:21 +01:00
case MultiPolygon:
2019-01-31 01:46:53 +01:00
areas.append(Area());
return multiPolygon(geometry["coordinates"].toArray(), areas.last(),
properties);
2019-01-25 22:18:21 +01:00
default:
_errorString = geometry["type"].toString()
+ ": invalid/missing Feature geometry";
return false;
}
}
bool GeoJSONParser::featureCollection(const QJsonObject &json,
2019-01-31 01:46:53 +01:00
QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints)
2019-01-25 22:18:21 +01:00
{
if (!json.contains("features") || !json["features"].isArray()) {
_errorString = "Invalid/missing FeatureCollection features array";
return false;
}
QJsonArray features(json["features"].toArray());
for (int i = 0; i < features.size(); i++)
2019-01-31 01:46:53 +01:00
if (!feature(features.at(i).toObject(), tracks, areas, waypoints))
2019-01-25 22:18:21 +01:00
return false;
return true;
}
bool GeoJSONParser::parse(QFile *file, QList<TrackData> &tracks,
2019-01-31 01:46:53 +01:00
QList<RouteData> &routes, QList<Area> &areas, QVector<Waypoint> &waypoints)
2019-01-25 22:18:21 +01:00
{
Q_UNUSED(routes);
QJsonParseError error;
QJsonDocument doc(QJsonDocument::fromJson(file->readAll(), &error));
if (doc.isNull()) {
_errorString = "JSON parse error: " + error.errorString() + " ["
+ QString::number(error.offset) + "]";
return false;
}
QJsonObject json(doc.object());
switch (type(json)) {
case Point:
waypoints.resize(waypoints.size() + 1);
return point(json["coordinates"].toArray(), waypoints.last());
case MultiPoint:
return multiPoint(json["coordinates"].toArray(), waypoints);
case LineString:
tracks.append(TrackData());
return lineString(json["coordinates"].toArray(), tracks.last());
case MultiLineString:
2019-02-11 23:28:08 +01:00
tracks.append(TrackData());
return multiLineString(json["coordinates"].toArray(), tracks.last());
2019-01-25 22:18:21 +01:00
case GeometryCollection:
2019-01-31 01:46:53 +01:00
return geometryCollection(json, tracks, areas, waypoints);
2019-01-25 22:18:21 +01:00
case Feature:
2019-01-31 01:46:53 +01:00
return feature(json, tracks, areas, waypoints);
2019-01-25 22:18:21 +01:00
case FeatureCollection:
2019-01-31 01:46:53 +01:00
return featureCollection(json, tracks, areas, waypoints);
2019-01-25 22:18:21 +01:00
case Polygon:
2019-01-31 01:46:53 +01:00
areas.append(Area());
return polygon(json["coordinates"].toArray(), areas.last());
2019-01-25 22:18:21 +01:00
case MultiPolygon:
2019-01-31 01:46:53 +01:00
areas.append(Area());
return multiPolygon(json["coordinates"].toArray(), areas.last());
2019-01-25 22:18:21 +01:00
case Unknown:
if (json["type"].toString().isNull())
_errorString = "Not a GeoJSON file";
else
_errorString = json["type"].toString()
+ ": unknown GeoJSON object";
return false;
}
return true;
}