QGeoJson Class

Header: #include <QGeoJson>
qmake: QT += location
Since: Qt 5.13

This class was introduced in Qt 5.13.

Static Public Members

QJsonDocument exportGeoJson(const QVariantList &geoData)
QVariantList importGeoJson(const QJsonDocument &geoJson)
QString toString(const QVariantList &geoData)

Detailed Description

QGeoJson class can be used to convert between a GeoJSON document (see the Wikipedia page, RFC) and a QVariantList of QVariantMap elements ready to be used as Model in a MapItemView. WARNING! This private class is part of Qt labs, thus not stable API, it is part of the experimental components of QtLocation. Until it is promoted to public API, it may be subject to source and binary-breaking changes.

Importing GeoJSON

The importGeoJson() method accepts a QJsonDocument from which it extracts a single JSON object, since the GeoJSON RFC expects that a valid GeoJSON Document has in its root a single JSON object. This method doesn't perform any validation on the input. The importer returns a QVariantList containing a single QVariantMap. This map has always at least 2 (key, value) pairs. The first one has type as key, and the corresponding value is a string identifying the GeoJSON type. This value can be one of the GeoJSON object types: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, FeatureCollection. The second pair has data as key, and the corresponding value can be either a QGeoShape or a list, depending on the GeoJSON type. The next section provides details about this node. The Feature type is converted into the type of the geometry contained within, with an additional (key, value) pair, where the key is properties and the value is a QVariantMap. Thus, a feature Map is distinguishable from the corresponding geometry, by looking for a properties member.

Structure of the data node

For the single type geometry objects (Point, LineString, and Polygon), the value corresponding to the data key is a QGeoShape:

  • When the type is Point, the data is a QGeoCircle with the point coordinates stored in the center property.

    For example, the following GeoJSON document contains a Point geometry:

    {
      "type" : "Point",
      "data" : [60.0, 11.0]
    }

    it is converted to a QVariantMap with the following structure:

    {
      type : Point
      data : QGeoCircle({60.000, 11.000}, -1)
    }
  • When the type is LineString the data ia a QGeoPath.

    For example, the following GeoJSON document contains a LineString geometry:

    {
        "type" : "LineString",
        "coordinates" : [[13.5, 43],[10.73, 59.92]]
    }

    it is converted to a QVariantMap with the following structure:

    {
      type : LineString,
      data : QGeoPath([{43.000, 13.500}, {59.920, 10.730}])
    }
  • When the type is Polygon, the data is a QGeoPolygon (holes are supported).

    For example, the following GeoJSON document contains a Polygon geometry:

    {
        "type" : "Polygon",
        "coordinates" : [
          [[17.13, 51.11],
           [30.54, 50.42],
           [26.70, 58.36],
           [17.13, 51.11]]
        ],
        "bbox" : [60, 60, -60, -60]
    
    }

    it is converted to a QVariantMap with the following structure:

    {
      type : Polygon
      data : QGeoPolygon([{51.110, 17.130}, {50.420,30.540}, {58.360, 26.700}, {51.110, 17.130}])
    }

For the homogeneously typed multipart geometry objects (MultiPoint, MultiLineString, MultiPolygon) the value corresponding to the data key is a QVariantList. Each element of the list is a QVariantMap of one of the above listed types. The elements in this list will be all of the same GeoJSON type:

  • When the type is MultiPoint, the data is a List of Points.

    For example, the following GeoJSON document contains a MultiPoint geometry:

    {
        "type" : "MultiPoint",
        "coordinates" : [
          [11,60],
          [5.5,60.3],
          [5.7,58.90]
        ]
    }

    it is converted to a QVariantMap with the following structure:

    {
      type : MultiPoint
      data : [
        {
          type : Point
          data : QGeoCircle({60.000, 11.000}, -1)
        },
        {
          type : Point
          data : QGeoCircle({60.300, 5.500}, -1)
        },
        {
          type : Point
          data : QGeoCircle({58.900, 5.700}, -1)
        }
      ]
    }
  • When the type is MultiLineString, the data is a List of LineStrings.

    For example, the following GeoJSON document contains a MultiLineString geometry:

    {
        "type" : "MultiLineString",
        "coordinates" : [
          [[13.5, 43], [10.73, 59.92]],
          [[9.15, 45], [-3.15, 58.90]]
        ]
    }

    it is converted to a QVariantMap with the following structure:

    {
      type : MultiLineString
      data : [
        {
          type : LineString
          data : QGeoPath([{45.000, 9.150}, {58.900, -3.150}])
        },
        {
          type : LineString
          data : QGeoPath([{43.000, 13.500}, {59.920, 10.730}])
        }
      ]
    }
  • When the type is MultiPolygon, the data is a List of Polygons.

    For example, the following GeoJSON document contains a MultiPolygon geometry:

    {
        "type" : "MultiPoint",
        "coordinates" : [
          [11,60],
          [5.5,60.3],
          [5.7,58.90]
        ]
    }

    it is converted to a QVariantMap with the following structure:

    {
      type : MultiPoint
      data : [
        {
          type : Point
          data : QGeoCircle({60.000, 11.000}, -1)
        },
        {
          type : Point
          data : QGeoCircle({60.300, 5.500}, -1)
        },
        {
          type : Point
          data : QGeoCircle({58.900, 5.700}, -1)
        }
      ]
    }

The GeometryCollection is a heterogeneous composition of other geometry types. In the resulting QVariantMap, the value of the data member is a QVariantList populated by QVariantMaps of various geometries, including the GeometryCollection itself.

For example, the following GeometryCollection:

{
    "type" : "GeometryCollection",
    "geometries" : [
        {
            "type" : "MultiPoint",
            "coordinates" : [
              [11,60], [5.5,60.3], [5.7,58.90]
            ]
        },
        {
            "type" : "MultiLineString",
            "coordinates" : [
              [[13.5, 43], [10.73, 59.92]],
              [[9.15, 45], [-3.15, 58.90]]
            ]
        },
        {
            "type" : "MultiPolygon",
            "coordinates" : [
                [
                  [[17.13, 51.11],
                   [30.54, 50.42],
                   [26.74, 58.36],
                   [17.13, 51.11]]
                ],
                [
                  [[19.84, 41.33],
                   [30.45, 49.26],
                   [17.07, 50.10],
                   [19.84, 41.33]]
                ]
            ]
        }
    ]
}

it is converted to a QVariantMap with the following structure:

{
  type : GeometryCollection
  data : [
    {
      type : MultiPolygon
      data : [
        {
          type : Polygon
          data : QGeoPolygon([{41.330, 19.840}, {49.260, 30.450}, {50.100, 17.070}, {41.330, 19.840}])
        }
        {
          type : Polygon
          data : QGeoPolygon([{51.110, 17.130}, {50.420, 30.540}, {58.360, 26.740}, {51.110, 17.130}])
        }
      ]
    }
    {
      type : MultiLineString
      data : [
        {
          type : LineString
          data : QGeoPath([{45.000, 9.150}, {58.900, -3.150}])
        }
        {
          type : LineString
          data : QGeoPath([{43.000, 13.500}, {59.920, 10.730}])
        }
      ]
    }
    {
      type : MultiPoint
      data : [
        {
          type : Point
          data : QGeoCircle({58.900, 5.700}, -1)
        },
        {
          type : Point
          data : QGeoCircle({60.300, 5.500}, -1)
        },
        {
          type : Point
          data : QGeoCircle({60.000, 11.000}, -1)
        }
      ]
    }
  ]
}

The Feature object, which consists of one of the previous geometries together with related attributes, is structured like one of the 7 above mentioned geometry types, plus a properties member. The value of this member is a QVariantMap. The only way to distinguish a Feature from the included geometry is to check if a properties node is present in the QVariantMap.

For example, the following Feature:

{
    "type" : "Feature",
    "id" : "Poly",
    "properties" : {
        "text" : "This is a Feature with a Polygon"
    },
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [
            [[17.13, 51.11],
             [30.54, 50.42],
             [26.70, 58.36],
             [17.13, 51.11]],
            [[23.46, 54.36],
             [20.52, 51.91],
             [28.25, 51.50],
             [26.80, 54.36],
             [23.46, 54.36]]
        ]
    }
}

it is converted to a QVariantMap with the following structure:

{
  type : Polygon
  data : QGeoPolygon([{51.110, 17.130}, {50.420,30.540}, {58.360, 26.700}, {51.110, 17.130}])
  properties : {text : This is a Feature with a Polygon}
}

The FeatureCollection is a composition of Feature objects. The value of the data member in a FeatureCollection is a QVariantList populated by Feature type QVariantMaps.

For example, the following FeatureCollection:

{
    "type" : "FeatureCollection",
    "features" : [
        {
            "type" : "Feature",
            "id" : "Poly",
            "properties" : {
                "text" : "This is a Feature with a Polygon"
            },
            "geometry" : {
               "type" : "Polygon",
               "coordinates" : [
                  [[17.13, 51.11],
                   [30.54, 50.42],
                   [26.70, 58.36],
                   [17.13, 51.11]],
                  [[23.46, 54.36],
                   [20.52, 51.91],
                   [28.25, 51.50],
                   [26.80, 54.36],
                   [23.46, 54.36]]
               ]
           }
       },
       {
           "type" : "Feature",
           "id" : "MultiLine",
           "properties" : {
               "text" : "This is a Feature with a MultiLineString"
           },
           "geometry" : {
              "type" : "MultiLineString",
              "coordinates" : [
                [[13.5, 43], [10.73, 59.92]],
                [[9.15, 45], [-3.15, 58.90]]
              ]
           }
       }
    ]
}

it is converted to a QVariantMap with the following structure:

{
  type : FeatureCollection
  data : [
    {
      type : MultiLineString
      data : [
        {
          type : LineString
          data : QGeoPath([{45.000, 9.150}, {58.900, -3.150}])
        }
        {
          type : LineString
          data : QGeoPath([{43.000, 13.500}, {59.920, 10.730}])
        }
      ]
      properties : {text : This is a Feature with a MultiLineString}
    },
    {
      type : Polygon
      data : QGeoPolygon({51.110, 17.130}, {50.420, 30.540}, {58.360, 26.700}, {51.110, 17.130})
      properties : {text : This is a Feature with a Polygon}
    }
  ]
}

Exporting GeoJSON

The exporter accepts the QVariantList returned by the importer, and returns a JSON document. The exporter is complementary to the importer because it executes the inverse action.

The toString function

The toString outputs, for debugging purposes, the content of a QVariantList structured like importGeoJson does, to a QString using a prettyfied format.

Member Function Documentation

[static] QJsonDocument QGeoJson::exportGeoJson(const QVariantList &geoData)

This method exports the QVariantList geoData, expected to be structured like described in the section Importing GeoJSON, to a QJsonDocument containing the data converted to GeoJSON.

Note: This method performs no validation on the input.

See also importGeoJson.

[static] QVariantList QGeoJson::importGeoJson(const QJsonDocument &geoJson)

This method imports the geoJson document, expected to contain valid GeoJSON data, into a QVariantList structured like described in the section Importing GeoJSON.

Note: This method performs no validation on the input.

See also exportGeoJson.

[static] QString QGeoJson::toString(const QVariantList &geoData)

This method accepts the QVariantList geoData, structured as described in Importing GeoJSON, and returns a string containing the same data in a readable form.

© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.