本章讲述使用qgis c++ Api加载各种类型的矢量地图数据显示。
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是一个开源的地理信息系统,可用于处理栅格、拓扑矢量、影像和图表数据。 |
Accesses data in a delimited text file, for example CSV files generated by spreadsheets. The contents of the file are split into columns based on specified delimiter characters. Each record may be represented spatially either by an X and Y coordinate column, or by a WKT (well known text) formatted columns.
The url defines the filename, the formatting options (how the text in the file is divided into data fields, and which fields contain the X,Y coordinates or WKT text definition. The options are specified as url query items.
file://home/t/gis/maps/QGIS/jilin_maincity.csv?type=csv&maxFields=10000&detectTypes=yes&xField=longitude&yField=latitude&crs=EPSG:4326&spatialIndex=no&subsetIndex=no&watchFile=no
QgsVectorLayer
文档file:
void MainWindow::addCsvSlot()
{
QString filename = QStringLiteral("maps/jilin_maincity.csv");
QFileInfo ff(filename);
QString uri = QString("file:///%1/%2%3").arg(QCoreApplication::applicationDirPath()).arg(filename).arg("?type=csv&xField=longitude&yField=latitude&crs=EPSG:4326");
QgsVectorLayer* vecLayer = new QgsVectorLayer(uri,ff.baseName(),"delimitedtext");
if(!vecLayer->isValid())
{
QMessageBox::critical(this,tr("error"),tr("invalid layer"));
return;
}
QgsProject::instance()->addMapLayer(vecLayer);
zoomToFirstLayer();
}
SpatiaLite是一个基于SQLite轻量级数据库的空间数据引擎。SQLite是一个以单一文件存在的数据库,数据库中的所有信息和功能都保存到一个文件中(后缀名通常为“sqlite”、“db”、“sqlite3”、“db3”和“s3db”等),且不需要系统服务的支持。SpatiaLite可以通过关系型表结构支持OGC简单要素模型,以存储矢量数据,也可以通过SQL查询语句进行简单的空间查询和分析。
Class for storing the component parts of a RDBMS data source URI (e.g.a Postgres data source).
This structure stores the database connection information, including host, database, user name, password, schema, password, and SQL where clause.
QgsDataSourceUri
setDatabase
函数设置数据库setDataSource
函数设置表和列void MainWindow::addSpatiaLiteSlot()
{
QString filename = QStringLiteral("maps/landuse.sqlite");
QgsDataSourceUri uri;
uri.setDatabase(filename);
uri.setDataSource("","landuse","Geometry");
QFileInfo ff(filename);
QgsVectorLayer* vecLayer = new QgsVectorLayer(uri.uri(),ff.baseName(),"spatialite");
if(!vecLayer->isValid())
{
QMessageBox::critical(this,tr("error"),tr("invalid layer"));
return;
}
QgsProject::instance()->addMapLayer(vecLayer);
zoomToFirstLayer();
}
OGC提出了一系列Web服务标准(OWS),包括Web地图服务(Web Map Service,WMS)、Web地图切片服务(Web Map Tile Service,WMTS)、Web要素服务(Web Feature Service,WFS)和Web覆盖服务(Web Coverage Service,WCS)等。许多开源GIS服务器(如GeoServer、MapServer等)提供了上述Web服务标准的具体实现,并且QGIS也提供了访问这些OWS服务的功能。
WFS直接向网络提供矢量要素的数据服务。相对于WMS和WMTS,WFS的特点是直接提供完整的数据信息,没有经过渲染与符号化,因此它更加灵活,常用于点要素的信息传递及多用户编辑等专业领域。由于WFS返回的数据是原始的,完整的要素数据传输到客户端以后才能进行渲染或处理,因此它在一定程度上会加重网络负担和客户端负担。WFS的核心方法是GetFeature方法,用于根据地理范围等参数请求返回矢量要素数据。
url=string (mandatory): HTTP url to a WFS server endpoint. e.g http://foobar/wfs
typename=string (mandatory): WFS typename
srsname=string (recommended): SRS like 'EPSG:XXXX'
QgsVectorLayer
文档QgsDataSourceUri
,代码如下void MainWindow::addWfsSlot()
{
QString uri;
#if 0
uri = QStringLiteral("https://demo.mapserver.org/cgi-bin/wfs?service=WFS&version=2.0.0&request=GetFeature&typename=ms:cities");
#else
QgsDataSourceUri dataSourceUri;
dataSourceUri.setParam("url","https://demo.mapserver.org/cgi-bin/wfs");
dataSourceUri.setParam("typename","ms:cities");
dataSourceUri.setParam("version","2.0");
dataSourceUri.setParam("srsname","EPSG:4326");
uri = dataSourceUri.uri();
#endif
auto layer = new QgsVectorLayer(uri,"mapserver","WFS");
QgsProject::instance()->addMapLayer(layer);
zoomToFirstLayer();
}
The memory data provider is used to construct in memory data, for example scratch data or data generated from spatial operations such as contouring. There is no inherent persistent storage of the data. The data source uri is constructed. The url specifies the geometry type (“point”, “linestring”, “polygon”, “multipoint”,“multilinestring”,“multipolygon”), optionally followed by url parameters as follows:
- crs=definition Defines the coordinate reference system to use for the layer. definition is any string accepted by QgsCoordinateReferenceSystem::createFromString()
- index=yes Specifies that the layer will be constructed with a spatial index
- field=name:type(length,precision) Defines an attribute of the layer. Multiple field parameters can be added to the data provider definition. type is one of “integer”, “double”, “string”.
Point?crs=epsg:4326&field=id:integer&field=name:string(20)&index=yes
void MainWindow::addMemorySlot()
{
QString uri = QStringLiteral("Point?crs=epsg:4326&field=name:string(20)");
QgsVectorLayer *layer = new QgsVectorLayer(uri,"New stratch layer","memory");
QgsProject::instance()->addMapLayer(layer);
if(layer)
{
//开启编辑
layer->startEditing();
//设置Feature的符号
QgsFeatureRenderer * layerRenderer= layer->renderer();
QgsSingleSymbolRenderer *singleRenderer = QgsSingleSymbolRenderer::convertFromRenderer(layerRenderer);
//使用Simple Marker
QVariantMap mp;
mp[QString("name")] = QStringLiteral("circle");
mp[QString("color")] = QStringLiteral("red");
mp[QString("size")] = QStringLiteral("2.0");
QgsMarkerSymbol *newsym = QgsMarkerSymbol::createSimple(mp);
singleRenderer->setSymbol(newsym);
layer->setRenderer(singleRenderer);
//添加Feature
QgsVectorDataProvider * dataProvider = layer->dataProvider();
QgsFields fields;
QgsField fName("test");
fields.append(fName);
QgsFeature f(fields);
f.setGeometry(QgsGeometry::fromPointXY(QgsPointXY(-34.0146,20.3046)));
f.setAttribute("test",QString("string1"));
dataProvider->addFeature(f);
//提交改动
layer->commitChanges();
}
zoomToFirstLayer();
}