5.2 加载矢量图层(delimitedtext,spatialite,wfs,memory)

文章目录

前言

本章讲述使用qgis c++ Api加载各种类型的矢量地图数据显示。

加载矢量(vector)图层

  • data provider可以指定的值包括:
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是一个开源的地理信息系统,可用于处理栅格、拓扑矢量、影像和图表数据。

delimitedtext

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.

  • XLS/XLSX文件(Excel电子表格)、CSV(逗号分隔)文件、TXT文件(Tab分隔、空格分隔)等类型的电子表格与文本格式的文件可以导入到QGis中
  • 注意 : QgsVectorLayer构造函数的path(uri)参数中,除指定文件路径外,还需要指定xField,yField(经纬度所在的列),crs(坐标系)等等,如下示例

    uri如下
    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
  • 更多的uri设置,见QgsVectorLayer文档

QGis导入CSV

  • 注意 : 这里使用Delimited Text

代码导入

  • 注意 : path参数前缀添加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<QgsVectorLayer*>();
    }


SpatiaLite data provider (spatialite)

SpatiaLite是一个基于SQLite轻量级数据库的空间数据引擎。SQLite是一个以单一文件存在的数据库,数据库中的所有信息和功能都保存到一个文件中(后缀名通常为"sqlite"、"db"、"sqlite3"、"db3"和"s3db"等),且不需要系统服务的支持。SpatiaLite可以通过关系型表结构支持OGC简单要素模型,以存储矢量数据,也可以通过SQL查询语句进行简单的空间查询和分析。

  • QgsVectorLayer构造函数中path参数,可以使用QgsDataSourceUri类构造

QgsDataSourceUri

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.

QGis导入spatialite

  • 首先新建连接

  • 点击Connect按钮,可以看到数据库中的数据
  • 选择一条数据添加至图层

代码导入

  • 构造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<QgsVectorLayer*>();
    }


Web服务

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 (web feature service) data provider (wfs)

WFS直接向网络提供矢量要素的数据服务。相对于WMS和WMTS,WFS的特点是直接提供完整的数据信息,没有经过渲染与符号化,因此它更加灵活,常用于点要素的信息传递及多用户编辑等专业领域。由于WFS返回的数据是原始的,完整的要素数据传输到客户端以后才能进行渲染或处理,因此它在一定程度上会加重网络负担和客户端负担。WFS的核心方法是GetFeature方法,用于根据地理范围等参数请求返回矢量要素数据。

QGis添加图层
  • 新建一个Server Connections,填写Name和URL
  • 连接并添加图层

代码添加
  • 第一种方法直接使用字符串构造uri,其中必须的参数如下

    • 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<QgsVectorLayer*>();
    }


Memory data providerType (memory)

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".
  • memory类型的data provider数据存储在内存中
  • 数据没有持久存储
  • 示例urlPoint?crs=epsg:4326&field=id:integer&field=name:string(20)&index=yes

QGis新建memory图层

  • 新建Temporary Scratch Layer,Geometry Type为Point类型,添加一个Field
  • 开启图层编辑
  • 添加Point Feature

代码创建memory图层

  • 首先构造uri,类型为Point

  • 注意 : 添加memory图层之后

    1. 需要设置符号才能显示在地图上
    2. 添加feature之后才能显示在地图上
  • feature的添加以及feature符号的设置,后边的章节会讲

    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<QgsVectorLayer*>();
    

    }

  • 示例ll_qgis_realtime_uavs使用了memory图层

其他

  • 其他的由于没有测试环境,所以暂不介绍
相关推荐
雷动软件工作室8 天前
8.12 矢量图层面要素单一符号使用七(随机标记填充)
qgis二次开发
雷动软件工作室15 天前
8.12 矢量图层面要素单一符号使用二(仅渲染中心点)
qgis二次开发
雷动软件工作室8 个月前
8.2 矢量图层点要素单一符号使用一
qt·gis·qgis二次开发