1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 03:35:53 +01:00

Properly ignore objects with null/empty GeoJSON coordinates

This commit is contained in:
Martin Tůma 2024-03-10 07:46:50 +01:00
parent 8780a40b9f
commit 4417b3d5e2
3 changed files with 55 additions and 56 deletions

View File

@ -182,7 +182,7 @@ GeoJSONParser::Type GeoJSONParser::type(const QJsonObject &json)
} }
bool GeoJSONParser::point(const QJsonObject &object, const Projection &parent, bool GeoJSONParser::point(const QJsonObject &object, const Projection &parent,
const QJsonValue &properties, Waypoint &waypoint) const QJsonValue &properties, QVector<Waypoint> &waypoints)
{ {
if (!object.contains("coordinates")) { if (!object.contains("coordinates")) {
_errorString = "Missing Point coordinates array"; _errorString = "Missing Point coordinates array";
@ -203,11 +203,11 @@ bool GeoJSONParser::point(const QJsonObject &object, const Projection &parent,
return false; return false;
} }
waypoint.setCoordinates(c); Waypoint waypoint(c);
if (coordinates.count() == 3 && coordinates.at(2).isDouble()) if (coordinates.count() == 3 && coordinates.at(2).isDouble())
waypoint.setElevation(coordinates.at(2).toDouble()); waypoint.setElevation(coordinates.at(2).toDouble());
setWaypointProperties(waypoint, properties); setWaypointProperties(waypoint, properties);
waypoints.append(waypoint);
return true; return true;
} }
@ -223,8 +223,6 @@ bool GeoJSONParser::multiPoint(const QJsonObject &object,
if (object["coordinates"].isNull()) if (object["coordinates"].isNull())
return true; return true;
QJsonArray coordinates(object["coordinates"].toArray()); QJsonArray coordinates(object["coordinates"].toArray());
if (coordinates.isEmpty())
return true;
Projection proj; Projection proj;
if (!crs(object, proj)) if (!crs(object, proj))
return false; return false;
@ -234,8 +232,6 @@ bool GeoJSONParser::multiPoint(const QJsonObject &object,
_errorString = "Invalid MultiPoint data"; _errorString = "Invalid MultiPoint data";
return false; return false;
} else { } else {
waypoints.resize(waypoints.size() + 1);
QJsonArray data(coordinates.at(i).toArray()); QJsonArray data(coordinates.at(i).toArray());
Coordinates c(::coordinates(data, proj.isNull() ? parent : proj)); Coordinates c(::coordinates(data, proj.isNull() ? parent : proj));
if (!c.isValid()) { if (!c.isValid()) {
@ -243,11 +239,11 @@ bool GeoJSONParser::multiPoint(const QJsonObject &object,
return false; return false;
} }
waypoints.last().setCoordinates(c); Waypoint waypoint(c);
if (data.count() == 3 && data.at(2).isDouble()) if (data.count() == 3 && data.at(2).isDouble())
waypoints.last().setElevation(data.at(2).toDouble()); waypoint.setElevation(data.at(2).toDouble());
setWaypointProperties(waypoint, properties);
setWaypointProperties(waypoints.last(), properties); waypoints.append(waypoint);
} }
} }
@ -255,7 +251,8 @@ bool GeoJSONParser::multiPoint(const QJsonObject &object,
} }
bool GeoJSONParser::lineString(const QJsonObject &object, bool GeoJSONParser::lineString(const QJsonObject &object,
const Projection &parent, const QJsonValue &properties, TrackData &track) const Projection &parent, const QJsonValue &properties,
QList<TrackData> &tracks)
{ {
if (!object.contains("coordinates")) { if (!object.contains("coordinates")) {
_errorString = "Missing LineString coordinates array"; _errorString = "Missing LineString coordinates array";
@ -269,8 +266,7 @@ bool GeoJSONParser::lineString(const QJsonObject &object,
Projection proj; Projection proj;
if (!crs(object, proj)) if (!crs(object, proj))
return false; return false;
SegmentData sd;
track.append(SegmentData());
for (int i = 0; i < coordinates.size(); i++) { for (int i = 0; i < coordinates.size(); i++) {
if (!coordinates.at(i).isArray()) { if (!coordinates.at(i).isArray()) {
@ -288,16 +284,19 @@ bool GeoJSONParser::lineString(const QJsonObject &object,
Trackpoint t(c); Trackpoint t(c);
if (data.count() == 3 && data.at(2).isDouble()) if (data.count() == 3 && data.at(2).isDouble())
t.setElevation(data.at(2).toDouble()); t.setElevation(data.at(2).toDouble());
track.last().append(t); sd.append(t);
} }
TrackData track(sd);
setTrackProperties(track, properties); setTrackProperties(track, properties);
tracks.append(track);
return true; return true;
} }
bool GeoJSONParser::multiLineString(const QJsonObject &object, bool GeoJSONParser::multiLineString(const QJsonObject &object,
const Projection &parent, const QJsonValue &properties, TrackData &track) const Projection &parent, const QJsonValue &properties,
QList<TrackData> &tracks)
{ {
if (!object.contains("coordinates")) { if (!object.contains("coordinates")) {
_errorString = "Missing MultiLineString coordinates array"; _errorString = "Missing MultiLineString coordinates array";
@ -311,13 +310,14 @@ bool GeoJSONParser::multiLineString(const QJsonObject &object,
Projection proj; Projection proj;
if (!crs(object, proj)) if (!crs(object, proj))
return false; return false;
TrackData track;
for (int i = 0; i < coordinates.size(); i++) { for (int i = 0; i < coordinates.size(); i++) {
if (!coordinates.at(i).isArray()) { if (!coordinates.at(i).isArray()) {
_errorString = "Invalid MultiLineString data"; _errorString = "Invalid MultiLineString data";
return false; return false;
} else { } else {
track.append(SegmentData()); SegmentData sd;
QJsonArray ls(coordinates.at(i).toArray()); QJsonArray ls(coordinates.at(i).toArray());
for (int j = 0; j < ls.size(); j++) { for (int j = 0; j < ls.size(); j++) {
@ -336,18 +336,21 @@ bool GeoJSONParser::multiLineString(const QJsonObject &object,
Trackpoint t(c); Trackpoint t(c);
if (data.count() == 3 && data.at(2).isDouble()) if (data.count() == 3 && data.at(2).isDouble())
t.setElevation(data.at(2).toDouble()); t.setElevation(data.at(2).toDouble());
track.last().append(t); sd.append(t);
} }
track.append(sd);
} }
} }
setTrackProperties(track, properties); setTrackProperties(track, properties);
tracks.append(track);
return true; return true;
} }
bool GeoJSONParser::polygon(const QJsonObject &object, const Projection &parent, bool GeoJSONParser::polygon(const QJsonObject &object, const Projection &parent,
const QJsonValue &properties, Area &area) const QJsonValue &properties, QList<Area> &areas)
{ {
if (!object.contains("coordinates")) { if (!object.contains("coordinates")) {
_errorString = "Missing Polygon coordinates array"; _errorString = "Missing Polygon coordinates array";
@ -390,14 +393,15 @@ bool GeoJSONParser::polygon(const QJsonObject &object, const Projection &parent,
pg.append(data); pg.append(data);
} }
area.append(pg); Area area(pg);
setAreaProperties(area, properties); setAreaProperties(area, properties);
areas.append(area);
return true; return true;
} }
bool GeoJSONParser::multiPolygon(const QJsonObject &object, bool GeoJSONParser::multiPolygon(const QJsonObject &object,
const Projection &parent, const QJsonValue &properties, Area &area) const Projection &parent, const QJsonValue &properties, QList<Area> &areas)
{ {
if (!object.contains("coordinates")) { if (!object.contains("coordinates")) {
_errorString = "Missing MultiPolygon coordinates array"; _errorString = "Missing MultiPolygon coordinates array";
@ -411,6 +415,8 @@ bool GeoJSONParser::multiPolygon(const QJsonObject &object,
Projection proj; Projection proj;
if (!crs(object, proj)) if (!crs(object, proj))
return false; return false;
Area area;
for (int i = 0; i < coordinates.size(); i++) { for (int i = 0; i < coordinates.size(); i++) {
if (!coordinates.at(i).isArray()) { if (!coordinates.at(i).isArray()) {
@ -452,6 +458,7 @@ bool GeoJSONParser::multiPolygon(const QJsonObject &object,
} }
setAreaProperties(area, properties); setAreaProperties(area, properties);
areas.append(area);
return true; return true;
} }
@ -475,9 +482,8 @@ bool GeoJSONParser::geometryCollection(const QJsonObject &object,
switch (type(geometry)) { switch (type(geometry)) {
case Point: case Point:
waypoints.resize(waypoints.size() + 1);
if (!point(geometry, proj.isNull() ? parent : proj, properties, if (!point(geometry, proj.isNull() ? parent : proj, properties,
waypoints.last())) waypoints))
return false; return false;
break; break;
case MultiPoint: case MultiPoint:
@ -486,27 +492,23 @@ bool GeoJSONParser::geometryCollection(const QJsonObject &object,
return false; return false;
break; break;
case LineString: case LineString:
tracks.append(TrackData());
if (!lineString(geometry, proj.isNull() ? parent : proj, if (!lineString(geometry, proj.isNull() ? parent : proj,
properties, tracks.last())) properties, tracks))
return false; return false;
break; break;
case MultiLineString: case MultiLineString:
tracks.append(TrackData());
if (!multiLineString(geometry, proj.isNull() ? parent : proj, if (!multiLineString(geometry, proj.isNull() ? parent : proj,
properties, tracks.last())) properties, tracks))
return false; return false;
break; break;
case Polygon: case Polygon:
areas.append(Area());
if (!polygon(geometry, proj.isNull() ? parent : proj, properties, if (!polygon(geometry, proj.isNull() ? parent : proj, properties,
areas.last())) areas))
return false; return false;
break; break;
case MultiPolygon: case MultiPolygon:
areas.append(Area());
if (!multiPolygon(geometry, proj.isNull() ? parent : proj, if (!multiPolygon(geometry, proj.isNull() ? parent : proj,
properties, areas.last())) properties, areas))
return false; return false;
break; break;
case GeometryCollection: case GeometryCollection:
@ -540,31 +542,26 @@ bool GeoJSONParser::feature(const QJsonObject &object, const Projection &parent,
switch (type(geometry)) { switch (type(geometry)) {
case Point: case Point:
waypoints.resize(waypoints.size() + 1);
return point(geometry, proj.isNull() ? parent : proj, properties, return point(geometry, proj.isNull() ? parent : proj, properties,
waypoints.last()); waypoints);
case MultiPoint: case MultiPoint:
return multiPoint(geometry, proj.isNull() ? parent : proj, return multiPoint(geometry, proj.isNull() ? parent : proj,
properties, waypoints); properties, waypoints);
case LineString: case LineString:
tracks.append(TrackData());
return lineString(geometry, proj.isNull() ? parent : proj, return lineString(geometry, proj.isNull() ? parent : proj,
properties, tracks.last()); properties, tracks);
case MultiLineString: case MultiLineString:
tracks.append(TrackData());
return multiLineString(geometry, proj.isNull() ? parent : proj, return multiLineString(geometry, proj.isNull() ? parent : proj,
properties, tracks.last()); properties, tracks);
case GeometryCollection: case GeometryCollection:
return geometryCollection(geometry, proj.isNull() ? parent : proj, return geometryCollection(geometry, proj.isNull() ? parent : proj,
properties, tracks, areas, waypoints); properties, tracks, areas, waypoints);
case Polygon: case Polygon:
areas.append(Area());
return polygon(geometry, proj.isNull() ? parent : proj, properties, return polygon(geometry, proj.isNull() ? parent : proj, properties,
areas.last()); areas);
case MultiPolygon: case MultiPolygon:
areas.append(Area());
return multiPolygon(geometry, proj.isNull() ? parent : proj, return multiPolygon(geometry, proj.isNull() ? parent : proj,
properties, areas.last()); properties, areas);
default: default:
_errorString = geometry["type"].toString() _errorString = geometry["type"].toString()
+ ": invalid/missing Feature geometry"; + ": invalid/missing Feature geometry";
@ -620,16 +617,13 @@ bool GeoJSONParser::parse(QFile *file, QList<TrackData> &tracks,
switch (type(object)) { switch (type(object)) {
case Point: case Point:
waypoints.resize(waypoints.size() + 1); return point(object, proj, QJsonValue(), waypoints);
return point(object, proj, QJsonValue(), waypoints.last());
case MultiPoint: case MultiPoint:
return multiPoint(object, proj, QJsonValue(), waypoints); return multiPoint(object, proj, QJsonValue(), waypoints);
case LineString: case LineString:
tracks.append(TrackData()); return lineString(object, proj, QJsonValue(), tracks);
return lineString(object, proj, QJsonValue(), tracks.last());
case MultiLineString: case MultiLineString:
tracks.append(TrackData()); return multiLineString(object, proj, QJsonValue(), tracks);
return multiLineString(object, proj, QJsonValue(), tracks.last());
case GeometryCollection: case GeometryCollection:
return geometryCollection(object, proj, QJsonValue(), tracks, areas, return geometryCollection(object, proj, QJsonValue(), tracks, areas,
waypoints); waypoints);
@ -638,11 +632,9 @@ bool GeoJSONParser::parse(QFile *file, QList<TrackData> &tracks,
case FeatureCollection: case FeatureCollection:
return featureCollection(object, proj, tracks, areas, waypoints); return featureCollection(object, proj, tracks, areas, waypoints);
case Polygon: case Polygon:
areas.append(Area()); return polygon(object, proj, QJsonValue(), areas);
return polygon(object, proj, QJsonValue(), areas.last());
case MultiPolygon: case MultiPolygon:
areas.append(Area()); return multiPolygon(object, proj, QJsonValue(), areas);
return multiPolygon(object, proj, QJsonValue(), areas.last());
case Unknown: case Unknown:
if (object["type"].toString().isNull()) if (object["type"].toString().isNull())
_errorString = "Not a GeoJSON file"; _errorString = "Not a GeoJSON file";

View File

@ -33,17 +33,17 @@ private:
Type type(const QJsonObject &json); Type type(const QJsonObject &json);
bool crs(const QJsonObject &object, Projection &proj); bool crs(const QJsonObject &object, Projection &proj);
bool point(const QJsonObject &object, const Projection &parent, bool point(const QJsonObject &object, const Projection &parent,
const QJsonValue &properties, Waypoint &waypoint); const QJsonValue &properties, QVector<Waypoint> &waypoints);
bool multiPoint(const QJsonObject &object, const Projection &parent, bool multiPoint(const QJsonObject &object, const Projection &parent,
const QJsonValue &properties, QVector<Waypoint> &waypoints); const QJsonValue &properties, QVector<Waypoint> &waypoints);
bool lineString(const QJsonObject &coordinates, const Projection &parent, bool lineString(const QJsonObject &coordinates, const Projection &parent,
const QJsonValue &properties, TrackData &track); const QJsonValue &properties, QList<TrackData> &tracks);
bool multiLineString(const QJsonObject &object, const Projection &proj, bool multiLineString(const QJsonObject &object, const Projection &proj,
const QJsonValue &properties, TrackData &track); const QJsonValue &properties, QList<TrackData> &tracks);
bool polygon(const QJsonObject &object, const Projection &parent, bool polygon(const QJsonObject &object, const Projection &parent,
const QJsonValue &properties, Area &area); const QJsonValue &properties, QList<Area> &areas);
bool multiPolygon(const QJsonObject &object, const Projection &proj, bool multiPolygon(const QJsonObject &object, const Projection &proj,
const QJsonValue &properties, Area &area); const QJsonValue &properties, QList<Area> &areas);
bool geometryCollection(const QJsonObject &json, const Projection &parent, bool geometryCollection(const QJsonObject &json, const Projection &parent,
const QJsonValue &properties, QList<TrackData> &tracks, const QJsonValue &properties, QList<TrackData> &tracks,
QList<Area> &areas, QVector<Waypoint> &waypoints); QList<Area> &areas, QVector<Waypoint> &waypoints);

View File

@ -13,6 +13,13 @@ typedef QVector<Trackpoint> SegmentData;
class TrackData : public QList<SegmentData> class TrackData : public QList<SegmentData>
{ {
public: public:
TrackData() {}
TrackData(const SegmentData &segment)
{
reserve(1);
append(segment);
}
const QString &name() const {return _name;} const QString &name() const {return _name;}
const QString &description() const {return _desc;} const QString &description() const {return _desc;}
const QString &comment() const {return _comment;} const QString &comment() const {return _comment;}