QGeoJson 命名空间
QGeoJson 命名空间包含从和到 GeoJson 文件的地理定位信息导入和导出的函数。 更多信息...
| 头文件 | #include <QGeoJson> |
| qmake | QT += location |
函数
| QJsonDocument | exportGeoJson(const QVariantList &geoData) |
| QVariantList | importGeoJson(const QJsonDocument &geoJson) |
| QString | toString(const QVariantList &geoData) |
详细描述
The methods in the QGeoJson namespace 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.
导入 GeoJSON
The importGeoJson() 方法接受一个 QJsonDocument,从中提取一个 JSON 对象。GeoJSON RFC 预期,有效的 GeoJSON 文档在其根目录下有一个单一的 JSON 对象。此方法不对输入进行任何验证。导入器返回一个包含单个 QVariantMap 的 QVariantList。此映射始终至少有 2 个 (键,值) 对。第一个具有 type 作为键,相应的值是标识 GeoJSON 类型的字符串。此值可以是 GeoJSON 对象类型之一:Point、MultiPoint、LineString、MultiLineString、Polygon、MultiPolygon、GeometryCollection、FeatureCollection。第二个对具有 data 作为键,相应的值可以是 QGeoShape 或列表,具体取决于 GeoJSON 类型。下一节提供了将 GeoJson 对象转换为 Qt Location 中已知对象的详细说明。将 Feature 类型转换为包含在其内部的几何类型,并附加一个额外的 (键,值) 对,其中键是 properties,值是 QVariantMap。因此,可以通过查找 properties 成员将特征映射与相应的几何体区分开来。
数据节点的结构
对于单一种类的几何对象(点、线段和多边形),与data键对应的值是一个QGeoShape
- 当类型为
点时,数据是一个具有点坐标存储在中心属性的QGeoCircle。例如,下面的GeoJSON文档包含一个
点几何形状{ "type" : "Point", "data" : [60.0, 11.0] }它被转换为具有以下结构的QVariantMap
{ type : "Point" data : QGeoCircle({60.000, 11.000}, -1) } - 当类型为
线段时,数据是一个QGeoPath。例如,下面的GeoJSON文档包含一个
线段几何形状{ "type" : "LineString", "coordinates" : [[13.5, 43],[10.73, 59.92]] }它被转换为具有以下结构的QVariantMap
{ type : "LineString", data : QGeoPath([{43.000, 13.500}, {59.920, 10.730}]) } - 当类型为
多边形时,数据是一个支持空洞的QGeoPolygon。例如,下面的GeoJSON文档包含一个
多边形几何形状{ "type" : "Polygon", "coordinates" : [ [[17.13, 51.11], [30.54, 50.42], [26.70, 58.36], [17.13, 51.11]] ], "bbox" : [60, 60, -60, -60] }它被转换为具有以下结构的QVariantMap
{ type : "Polygon" data : QGeoPolygon([{51.110, 17.130}, {50.420,30.540}, {58.360, 26.700}, {51.110, 17.130}]) }
对于同质类型的多部分几何对象(多点、多线段、多边形),与data键对应的值是一个QVariantList。列表中的每个元素是上述列出的类型之一的QVariantMap。列表中的元素都将具有相同的GeoJSON类型
- 当类型为
多点时,数据是点的列表。例如,下面的GeoJSON文档包含一个
多点几何形状{ "type" : "MultiPoint", "coordinates" : [ [11,60], [5.5,60.3], [5.7,58.90] ] }它被转换为具有以下结构的QVariantMap
{ 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) } ] } - 当类型为
多线段时,数据是线段的列表。例如,下面的GeoJSON文档包含一个
多线段几何形状{ "type" : "MultiLineString", "coordinates" : [ [[13.5, 43], [10.73, 59.92]], [[9.15, 45], [-3.15, 58.90]] ] }它被转换为具有以下结构的QVariantMap
- 当类型为
多边形时,数据是多边形的列表。例如,下面的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] ] ] ] }它被转换为具有以下结构的QVariantMap
{ type : "MultiPolygon" data : [ { type : "Polygon" data : QGeoPolygon([{51.110, 17.130}, {50.420,30.540}, {58.360, 26.700}, {51.110, 17.130}]) }, { type : "Polygon" data : QGeoPolygon([{41.330, 19.840}, {49.260,30.450}, {50.100, 17.070}, {41.330, 19.840}]) } ] }
GeometryCollection是其他几何类型的异构组合。在生成的QVariantMap中,data成员的值是一个由各种几何形状的QVariantMaps填充的QVariantList,包括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]]
]
]
}
]
}它被转换为具有以下结构的QVariantMap
{
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)
}
]
}
]
}Feature对象,它由一个以上的前面的几何形状和相关属性组成,其结构类似于上面提到的7种几何类型之一,再加上一个properties成员。此成员的值是一个QVariantMap。区分Feature与包含的几何形状的唯一方法是检查QVariantMap中是否存在properties节点。
例如,下面是一个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]]
]
}
}它被转换为具有以下结构的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对象组成的。FeatureCollection中的data成员的值是一个由Feature类型QVariantMaps填充的QVariantList。
例如,下面是一个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]]
]
}
}
]
}它被转换为具有以下结构的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的导出
导出器接受由导入器返回的QVariantList,并返回一个JSON文档。导出器是导入器的补充,因为它执行相反的操作。
toString函数
toString方法输出,用于调试目的,将结构类似于importGeoJson的QVariantList的内容,以美化的格式转换为QString。
函数文档
QJsonDocument QGeoJson::exportGeoJson(const QVariantList &geoData)
此方法将预期的结构如Importing GeoJSON部分中所述的QVariantList输出为包含转换为GeoJSON的数据的QJsonDocument。
注意:此方法不执行输入验证。
另请参阅:importGeoJson。
QVariantList QGeoJson::importGeoJson(const QJsonDocument &geoJson)
此方法将预期的GeoJSON数据包含的文档导入为如Importing GeoJSON部分中所述的结构化QVariantList。
注意:此方法不执行输入验证。
另请参阅:exportGeoJson。
QString QGeoJson::toString(const QVariantList &geoData)
此方法接受如Importing GeoJSON所述的结构化的QVariantList,并以可读的格式返回包含相同数据的字符串。
© 2024 The Qt Company Ltd. 本文档中包含的贡献属于各自的所有者版权。本提供的文档是根据由自由软件基金会发布的GNU自由文档许可协议版本1.3的条款授权的。Qt及其相应标志是芬兰以及/或世界上其他国家的The Qt Company Ltd.的商标。所有其他商标均为其各自所有者的财产。