本章讲述使用qgis c++ Api加载各种类型的矢量地图数据显示。
在QGIS中,图层并不保存数据的实体,而是引用各种类型的数据源,并利用图层样式等属性渲染数据。
QgsVectorLayer
代表矢量图层,首先看一下官方文档The QgsVectorLayer is instantiated by specifying the name of a data provider, such as postgres or wfs, and url defining the specific data set to connect to. The vector layer constructor in turn instantiates a QgsVectorDataProvider subclass corresponding to the provider type, and passes it the url. The data provider connects to the data source.
QgsVectorLayer
需要提供data provider的名称以及文件路径或url,其构造函数如下QgsVectorLayer (const QString &path=QString(), const QString &baseName=QString(), const QString &providerLib="ogr", const QgsVectorLayer::LayerOptions &options=QgsVectorLayer::LayerOptions())
Provider | 说明 |
---|---|
ogr | OGR提供了一组基于标准的接口和函数库,用于处理和操作地理空间数据。 |
delimitedtext | 文本数据按指定的分隔符进行分割 |
gpx | GPX(GPS eXchange Format,GPS交换格式)是一种用于存储坐标数据的 XML 文件格式 |
spatialite | Spatialite是SQLite数据库的空间数据引擎。 |
memory | 数据存在内存中 |
postgres | POSTGRES是一个对象-关系型数据库管理系统(ORDBMS) |
mssql | MSSQL是指微软的SQLServer数据库服务器 |
wfs | WFS(Web Feature Service)是一种允许用户在分布式环境下通过HTTP对空间数据进行增加、删除、修改、查询的GIS数据服务 |
grass | GRASS GIS是一个开源的地理信息系统,可用于处理栅格、拓扑矢量、影像和图表数据。 |
C:\src\OSGeo4W\src\qgis-ltr-dev\qgis\src\core\providers\qgsproviderregistry.cpp(337) : (QgsProviderRegistry::init) [633ms] Loaded 25 providers (OAPIF;WFS;arcgisfeatureserver;arcgismapserver;copc;delimitedtext;ept;gdal;geonode;gpx;hana;mdal;memory;mesh_memory;mssql;ogr;pdal;postgres;postgresraster;spatialite;vectortile;virtual;virtualraster;wcs;wms)
下边分别加载不同data provider的数据
Accesses data using the OGR drivers (https://gdal.org/drivers/vector/index.html). The url is the OGR connection string. A wide variety of data formats can be accessed using this driver, including file based formats used by many GIS systems, database formats, and web services. Some of these formats are also supported by custom data providers listed below.
OGR是用于读写矢量数据的抽象数据模型类库,是GDAL开源项目的一个分支,也采用X/MIT协议发布。
QgsVectorLayer
QgsProject
的成员函数addMapLayer
即可void MainWindow::addShpSlot()
{
QString filename = QStringLiteral("maps/shapefile/protected_areas.shp");
QFileInfo ff(filename);
//创建图层
QgsVectorLayer* vecLayer = new QgsVectorLayer(filename,ff.baseName(),"ogr");
if(!vecLayer->isValid())
{
QMessageBox::critical(this,tr("error"),tr("invalid layer"));
return;
}
QgsProject::instance()->addMapLayer(vecLayer);
zoomToFirstLayer();
}
GPX是一种以xml格式记录的坐标轨迹文件,通常由GPS设备生成
GPX通常包括航点(Waypoints)、路线(Routes)和轨迹(Tracks)三个主要图层。
不同于Shapefile,GPX文件通常包含多个图层,在QGis中会提示用户选择图层
选择之后,添加图层
|layername=tracks
如下示例代码void MainWindow::addGpxSlot()
{
QString filename = QStringLiteral("maps/route.gpx");
QFileInfo ff(filename);
//创建图层
QgsVectorLayer* route_pointsLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=route_points"),"route_points","ogr");
QgsVectorLayer* routesLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=routes"),"routes","ogr");
QgsVectorLayer* tracks_pointsLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=track_points"),"track_points","ogr");
QgsVectorLayer* tracksLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=tracks"),"tracks","ogr");
QgsVectorLayer* waypointsLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=waypoints"),"waypoints","ogr");
QList mapLayers;
mapLayers << route_pointsLayer << routesLayer << tracks_pointsLayer << tracksLayer << waypointsLayer;
QgsProject::instance()->addMapLayers(mapLayers);
zoomToFirstLayer();
}
全称是GeoPackage,是一种开放的、基于标准的、独立于平台的、可移植的、自描述的、用于传输地理空间信息的紧凑数据格式。
它是一个独立于平台的SQLite数据库文件,其中包含矢量要素、不同比例尺的图像和光栅映射的瓦矩阵集、属性(非空间数据)以及扩展机制。
gpkg基于SQLite,一般会存储多个图层,在QGis中会提示用户选择图层
void MainWindow::addGpkgSlot()
{
QString filename = QStringLiteral("maps/points_gpkg.gpkg");
QFileInfo ff(filename);
//创建图层
QgsVectorLayer* points_gpkgLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=points_gpkg"),"points_gpkg","ogr");
QgsVectorLayer* points_smallLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=points_small"),"points_small","ogr");
QList mapLayers;
mapLayers << points_gpkgLayer << points_smallLayer;
QgsProject::instance()->addMapLayers(mapLayers);
zoomToFirstLayer();
}
包含空间信息的数据交换格式,经常用于Web服务,进行数据交换
void MainWindow::addGeoJsonSlot()
{
QString filename = QStringLiteral("maps/grid_4326.geojson");
QFileInfo ff(filename);
QgsVectorLayer* vecLayer = new QgsVectorLayer(filename,ff.baseName(),"ogr");
if(!vecLayer->isValid())
{
QMessageBox::critical(this,tr("error"),tr("invalid layer"));
return;
}
QgsProject::instance()->addMapLayer(vecLayer);
zoomToFirstLayer();
}
地理标记语言(GML)是一种用可扩展标记语言(XML)编写地理信息的方式,以方便地理信息的传输和存储。可以将其视为描述应用程序架构的XML语法,应用程序架构描述特定领域或给定上下文中的概念结构。
void MainWindow::addGmlSlot()
{
QString filename = QStringLiteral("maps/linedensity.gml");
QFileInfo ff(filename);
QgsVectorLayer* vecLayer = new QgsVectorLayer(filename,ff.baseName(),"ogr");
if(!vecLayer->isValid())
{
QMessageBox::critical(this,tr("error"),tr("invalid layer"));
return;
}
QgsProject::instance()->addMapLayer(vecLayer);
zoomToFirstLayer();
}
Keyhole 标记语言 (KML) 是一种基于 XML 的格式,用于存储地理数据和相关内容,是一种官方的开放地理空间联盟 (OGC) 标准。 KML 格式便于在 Internet 上发布并可通过许多免费应用程序(例如 Google Earth 和 ArcGIS Explorer)进行查看,因此常用于与非 GIS 用户共享地理数据。 KML 文件以 .kml 或 .kmz(表示压缩的 KML 文件)为扩展名。
void MainWindow::addKmlSlot()
{
QString filename = QStringLiteral("maps/multilayer.kml");
QFileInfo ff(filename);
//创建图层
QgsVectorLayer* layer1 = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=Layer1"),"Layer1","ogr");
QgsVectorLayer* layer2 = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=Layer2"),"Layer2","ogr");
QgsVectorLayer* layer3 = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=Layer3"),"Layer3","ogr");
QList mapLayers;
mapLayers << layer1 << layer2 << layer3;
QgsProject::instance()->addMapLayers(mapLayers);
zoomToFirstLayer();
}
DXF 是Autodesk公司开发的用于AutoCAD与其它软件之间进行CAD数据交换的CAD数据文件格式
geometrytype=Point
和geometrytype=LineString
void MainWindow::addDxfSlot()
{
QString filename = QStringLiteral("maps/points_lines_3d.dxf");
QFileInfo ff(filename);
//创建图层
QgsVectorLayer* points_gpkgLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=entities|geometrytype=LineString"),"entities1","ogr");
QgsVectorLayer* points_smallLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=entities|geometrytype=Point"),"entities2","ogr");
QList mapLayers;
mapLayers << points_gpkgLayer << points_smallLayer;
QgsProject::instance()->addMapLayers(mapLayers);
zoomToFirstLayer();
}
Coverage 是一种用于存储矢量数据的地理相关数据模型,它包含地理要素的空间(位置)数据和属性(描述性)数据。Coverage 使用一组要素类来表示地理要素。每个要素类存储一组点、线(弧)、面或注记(文本)。Coverage 可以具有拓扑,用于确定要素间的关系。
Coverage 以目录形式存储,而目录中的每个要素类则以一组文件的形式进行存储。
void MainWindow::addCoverageSlot()
{
QString filename = QStringLiteral("maps/CoverageDir/testvector");
QFileInfo ff(filename);
QgsVectorLayer* aaLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=aa"),"aa","ogr");
QgsVectorLayer* ARCLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=ARC"),"ARC","ogr");
QgsVectorLayer* CNTLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=CNT"),"CNT","ogr");
QgsVectorLayer* PALLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("|layername=PAL"),"PAC","ogr");
QList mapLayers;
mapLayers << aaLayer << ARCLayer << CNTLayer << PALLayer;
QgsProject::instance()->addMapLayers(mapLayers);
zoomToFirstLayer();
}
layerName,geometryType
等参数,这里给出ogr支持的所有参数QString QgsOgrProviderMetadata::encodeUri( const QVariantMap &parts ) const
{
const QString vsiPrefix = parts.value( QStringLiteral( "vsiPrefix" ) ).toString();
const QString vsiSuffix = parts.value( QStringLiteral( "vsiSuffix" ) ).toString();
const QString path = parts.value( QStringLiteral( "path" ) ).toString();
const QString layerName = parts.value( QStringLiteral( "layerName" ) ).toString();
const QString layerId = parts.value( QStringLiteral( "layerId" ) ).toString();
const QString subset = parts.value( QStringLiteral( "subset" ) ).toString();
const QString geometryType = parts.value( QStringLiteral( "geometryType" ) ).toString();
const QString authcfg = parts.value( QStringLiteral( "authcfg" ) ).toString();
const QStringList openOptions = parts.value( QStringLiteral( "openOptions" ) ).toStringList();
const QString uniqueGeometryType = parts.value( QStringLiteral( "uniqueGeometryType" ) ).toString();
......
}
Provider reads tracks, routes, and waypoints from a GPX file. The url defines the name of the file, and the type of data to retrieve from it (“track”, “route”, or “waypoint”).
An example url is “/home/user/data/holiday.gpx?type=route”
maps/route.gpx?type=route
,并且只支持route,track和waypointvoid MainWindow::addGpx1Slot()
{
QString filename = QStringLiteral("maps/route.gpx");
QFileInfo ff(filename);
QgsVectorLayer* routesLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("?type=route"),"route","gpx");
QgsVectorLayer* tracksLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("?type=track"),"track","gpx");
QgsVectorLayer* waypointsLayer = new QgsVectorLayer(QString("%1%2").arg(filename).arg("?type=waypoint"),"waypoint","gpx");
QList mapLayers;
mapLayers << routesLayer << tracksLayer << waypointsLayer;
QgsProject::instance()->addMapLayers(mapLayers);
zoomToFirstLayer();
}