GeoJsonData QML Type

用于表示、加载和保存 GeoJSON 文档的模型。更多

Import Statement: import QtLocation 6.9
Since: QtLocation 6.7

属性

方法

详细说明

GeoJsonData 类型可读写 GeoJson 格式的文档。GeoJsonData 具有在sourceUrl 属性设置的 URL 上打开和保存模型的功能。加载的模型在内部由QVariant 表示,并绑定到model 属性。使用委托可视化视图中的项目。

有关 GeoJSON 的信息,请访问GeoJSON网站。

GeoJSON 对象

GeoJSON 对象是一个有效的 JSON 对象,用于表示几何图形、特征或几何图形或特征的集合。

GeoJSON 对象必须是上述类型之一:

  • Point
  • MultiPoint
  • LineString
  • MultiLineString
  • Polygon
  • MultiPolygon
  • GeometryCollection
  • Feature
  • FeatureCollection

要设置类型,请将type 成员绑定到 GeoJSON 类型。coordinates 成员可以是QGeoShape 类型或列表,具体取决于 GeoJSON 类型。Feature 类型有一个额外的geometryproperties 成员。

几何类型及其等效QVariant 表示的列表:

  • 对于Point 对象,coordinatesQGeoCircle 配对。例如
    {
        "type": "Point",
        "coordinates": [11, 60]
    }

    此 GeoJSON 对象有一个相应的QVariantMap 表示法:

    {
        type: "Point"
        coordinates: QGeoCircle({11.000, 60.000}, -1)
    }
  • 对于LineString 对象,coordinatesQGeoPath 配对。例如
    {
        "type" : "LineString",
        "coordinates" : [
        [13.5, 43],
        [10.73, 59.92]
        ]
    }

    此 GeoJSON 对象有一个相应的QVariantMap 表示:

    {
        type : "LineString"
        data : QGeoPath([{43.000, 13.500}, {59.920, 10.730}])
    }
  • 对于Polygon 对象,coordinates 成员与QGeoPolygon 成对(可能有洞)。多边形是一个线性环,其最终坐标与第一个坐标相同,因此可以打开和关闭环。bbox 成员是可选成员,用于设置区域范围,对凹形边界很有用。有关可接受的多边形坐标的更多信息,请阅读 GeoJson 规范中的多边形类型。例如
    {
        "type": "Polygon",
        "coordinates": [
            [
                [17.13, 51.11],
                [30.54, 50.42],
                [26.70, 58.36],
                [17.13, 51.11]
            ]
        ],
        "bbox": [50, -50, 10, -10]
    }

    此 GeoJSON 对象有一个相应的QVariantMap 表示法:

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

对于MultiPointMultiLineStringMultiPolygon 类型,coordinatesQVariantList 配对。列表元素是包含几何图形的QVariantMap

  • 对于MultiPoint 对象,coordinates 与点坐标列表配对。例如
    {
        "type": "MultiPoint",
        "coordinates": [
            [11, 60],
            [5.5, 60.3],
            [5.7, 58.90]
        ]
    }

    这个 GeoJSON 对象有一个相应的QVariantMap 表示:

    {
        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)
            }
            ]
    }
  • 对于MultiLineString 对象,coordinates 与 LineString 坐标列表成对。下面的 GeoJSON 对象构建了两条不平行的直线:
    {
        "type" : "MultiLineString",
        "coordinates" : [
        [[13.5, 43], [10.73, 59.92]],
        [[9.15, 45], [-3.15, 58.90]]
        ]
    }

    该 GeoJSON 对象有相应的QVariantMap 表示:

    {
        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}])
            }
        ]
    }
  • 对于MultiPolygon 类型,coordinates 与多边形坐标列表成对。多边形是一个线性环Polygon 类型有更多关于可接受格式的信息。下面的 GeoJSON 对象包含两个三角形的列表:
    {
        "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]
            ]]
        ]
    }

    该 GeoJSON 对象有一个相应的QVariantMap 表示:

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

GeometryCollection 类型是其他几何类型的组合。geometries 成员的值是一个QVariantList ,其中包含各种类型的 QVariantMaps,包括其他 GeometryCollection 类型。

例如,以下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]
                    ]
                ]
            ]
        }
    ]
}

这个 GeoJSON 对象有一个相应的QVariantMap 表示:

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

Feature 类型包含一个额外的geometryproperties 成员。区分特征对象和其他几何对象的唯一方法是检查QVariantMap 对象中是否存在properties 节点。

例如,下面的Feature 有一个几何图形和属性成员:

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

这个 GeoJSON 对象有一个相应的QVariantMap 表示:

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

FeatureCollection 是一个 Feature 对象的组合。features 成员绑定到包含其他地物对象的QVariantList 对象。

例如,下面的FeatureCollection 有多个地物和几何图形:

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

该 GeoJSON 对象有一个相应的QVariantMap 表示法:

{
    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 示例

GeoJson Viewer示例演示了如何使用 GeoJsonData QML 类型在地图上加载坐标并使其可视化。

属性文档

model : QVariant [since 6.7]

GeoJSON 文档的QVariant 表示。QML 委托可在视图中显示其内容。

该属性在 Qt 6.7 中引入。


sourceUrl : url [since 6.7]

GeoJSON 文档的 URL。设置该属性可加载文档并将对象绑定到model 成员。

此属性在 Qt 6.7 中引入。


方法文档

bool addItem(Item item)

item 添加到model 对象。

如果添加成功,则返回true ,否则返回false


void clear()

删除绑定到model 的所有项目。


bool open()

加载sourceUrl 中的文件内容。

如果打开成功,则返回true ,否则返回false


bool openUrl(Url url)

加载url 中的 GeoJson 文档并将其绑定到model 。如果打开文件成功,属性sourceUrl 将被设置为url

如果打开成功,则返回true ,否则返回false


bool save()

sourceUrl 保存模型。

如果保存成功,则返回true ,否则返回false


bool saveAs(Url url)

model 保存在url 。如果保存成功,sourceUrl 属性将设置为url

如果保存成功,则返回true ,否则返回false


void setModelToMapContents(MapView mapItemView)

mapItemView 的所有地图项添加到GeoJsonData 对象的model 中。删除model 中先前存储的地图项。

如果设置成功,则返回true ,否则返回false

另请参阅 addItem


© 2025 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.