- Qt 6.12
- Qt Location
- QML Types
- GeoJsonData
- Qt 6.12.0 Reference Documentation

# GeoJsonData QML Type

A model to represent, load and save GeoJSON documents. More...

Import Statement: | `import QtLocation 6.12`
Since: | QtLocation 6.7

- List of all members, including inherited members
- GeoJsonData is part of QML Maps Plugin.

## Properties

- model : QVariant `(since 6.7)`
- sourceUrl : url `(since 6.7)`

## Methods

- bool addItem(Item item)
- void clear()
- bool open()
- bool openUrl(Url url)
- bool save()
- bool saveAs(Url url)
- void setModelToMapContents(MapView mapItemView)

## Detailed Description

The GeoJsonData type reads and writes GeoJson formatted documents. GeoJsonData has functions to open and save the model at the URL set to the sourceUrl property. The loaded model is internally represented by QVariant and binds to the model property. Use Delegates to visualize the items in a view.

For information about GeoJSON, visit the GeoJSON website.

## GeoJSON Object

The GeoJSON object is a valid JSON object that represents geometry, a feature, or a collection of geometries or features.

A GeoJSON object must be one of these types:

- `Point`
- `MultiPoint`
- `LineString`
- `MultiLineString`
- `Polygon`
- `MultiPolygon`
- `GeometryCollection`
- `Feature`
- `FeatureCollection`

To set the type, bind the `type` member to a GeoJSON type. The `coordinates` member can be a type of QGeoShape or a list, depending on the GeoJSON type. The `Feature` type has an additional `geometry` and `properties` member.

A list of the geometric types and their equivalent QVariant representations:

- For a `Point` object, `coordinates` pairs with QGeoCircle. For example:

       {
           "type": "Point",
           "coordinates": [11, 60]
       }

  This GeoJSON object has a corresponding QVariantMap representation:

       {
           type: "Point"
           coordinates: QGeoCircle({11.000, 60.000}, -1)
       }

- For a `LineString` object, `coordinates` pairs with QGeoPath. For example:

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

  This GeoJSON object has a corresponding QVariantMap representation:

       {
           type : "LineString"
           data : QGeoPath([{43.000, 13.500}, {59.920, 10.730}])
       }

- For a `Polygon` object, `coordinates` member pairs with QGeoPolygon (holes are possible). The polygon is a linear ring, whose final coordinate is the same as the first coordinate, thereby opening and closing the ring. The `bbox` member is an optional member and is for setting the area's range, useful for concave boundaries. For more information about the accepted polygon coordinates, read about the Polygon type in the GeoJson specification. For example:

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

  This GeoJSON object has a corresponding QVariantMap representation:

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

For the `MultiPoint`, `MultiLineString`, and `MultiPolygon` types, `coordinates` pairs with a QVariantList. The list element is a QVariantMap containing geometry.

- For a `MultiPoint` object, `coordinates` pairs with a list of Point coordinates. For example:

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

  This GeoJSON object has a corresponding QVariantMap representation:

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

- For a `MultiLineString` object, `coordinates` pairs with a list of LineString coordinates. The following GeoJSON object constructs two non-parallel lines:

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

  This GeoJSON object has a corresponding QVariantMap representation:

       {
           type : "MultiLineString"
           coordinates : [
               {
                   type : "LineString"
                   coordinates : QGeoPath([{45.000, 9.150}, {58.900, -3.150}])
               },
               {
                   type : "LineString"
                   coordinates : QGeoPath([{43.000, 13.500}, {59.920, 10.730}])
               }
           ]
       }

- For a `MultiPolygon` type, `coordinates` pairs with a list of Polygon coordinates. The polygon is a linear ring and the `Polygon` type has more information about accepted formats. The following GeoJSON object contains a list of two triangles:

       {
           "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]
               ]]
           ]
       }

  This GeoJSON object has a corresponding QVariantMap representation:

       {
           type : "MultiPolygon"
           coordinates : [
               {
                   type : "Polygon"
                   coordinates : QGeoPolygon([{51.110, 17.130}, {50.420,30.540}, {58.360, 26.740}])
               },
               {
                   type : "Polygon"
                   coordinates : QGeoPolygon([{41.330, 19.840}, {49.260,30.450}, {50.100, 17.070}])
               }
               ]
       }

The `GeometryCollection` type is a composition of other geometry types. The value of the `geometries` member is a QVariantList containing QVariantMaps of various types, including other GeometryCollection types.

For example, the following `GeometryCollection` type contains several other geometries:

     {
         "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]
                         ]
                     ]
                 ]
             }
         ]
     }

This GeoJSON object has a corresponding QVariantMap representation:

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

The `Feature` type contains an additional `geometry` and `properties` member. The only way to distinguish a Feature object from other geometrical objects is to check for the existence of a `properties` node in the QVariantMap object.

For example, the following `Feature` has a geometry and properties members:

     {
         "type": "Feature",
         "id": "Poly",
         "properties": {
             "name": "Poly",
             "text": "This is a Feature with a Polygon",
             "color": "limegreen"
         },
         "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]
                 ]
             ]
         }
     }

This GeoJSON object has a corresponding QVariantMap representation:

     {
       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 `features` member binds to a QVariantList object containing other Feature objects.

For example, the following `FeatureCollection` has several Features and geometries:

     {
         "type" : "FeatureCollection",
         "properties" : {
             "color" : "crimson"
         },
         "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",
                 "color" : "deepskyblue"
             },
             "geometry" : {
                 "type" : "MultiLineString",
                 "coordinates" : [
                     [[13.5, 43], [10.73, 59.92]],
                     [[9.15, 45], [-3.15, 58.90]]
                 ]
             }
         }
         ]
     }

This GeoJSON object has a corresponding QVariantMap representation:

     {
         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" }
             }
         ]
     }

## GeoJson Example

The GeoJson Viewer example demonstrates the use of the GeoJsonData QML type to load and visualize coordinates on a map.

## Property Documentation

### model : QVariant `[since 6.7]`

A QVariant representation of the GeoJSON document. QML delegates can display the contents in views.

This property was introduced in Qt 6.7.

### sourceUrl : url `[since 6.7]`

The URL of a GeoJSON document. Setting this property loads the document and binds the object to the model member.

This property was introduced in Qt 6.7.

## Method Documentation

### bool addItem(Item item)

Adds the item to the model object.

Returns `true` if adding is successful, `false` otherwise.

### void clear()

Deletes all items bound to the model.

### bool open()

Loads the content of the file at sourceUrl.

Returns `true` if opening is successful, `false` otherwise.

### bool openUrl(Url url)

Loads the GeoJson document at url and binds it to the model. The property sourceUrl is set to url if opening the file is successful.

Returns `true` if opening is successful, `false` otherwise.

### bool save()

Saves the model at sourceUrl.

Returns `true` if saving is successful, `false` otherwise.

### bool saveAs(Url url)

Saves the model at url. The sourceUrl property is set to url if successful.

Returns `true` if saving is successful, `false` otherwise.

### void setModelToMapContents(MapView mapItemView)

Adds all map items of mapItemView to the model of the GeoJsonData object. Deletes previously stored map items from the model.

Returns `true` if setting is successful, `false` otherwise.

See also addItem.