15.2. Criando Camadas

As camadas podem ser criadas de várias maneiras, incluindo:

  • empty layers from scratch

  • camadas de camadas existentes

  • layers from the clipboard

  • layers as a result of an SQL-like query based on one or many layers (virtual layers)

QGIS also provides tools to import/export from/to different formats.

15.2.1. Criando novas camadas vetoriais

QGIS allows you to create new layers in different formats. It provides tools for creating GeoPackage, Shapefile, SpatiaLite, GPX format and Temporary Scratch layers (aka memory layers). Creation of a new GRASS layer is supported within the GRASS plugin.

15.2.1.1. Criando uma nova camada GeoPackage

To create a new GeoPackage layer, press the newGeoPackageLayer New GeoPackage Layer… button in the Layer ► Create Layer ► menu or from the Data Source Manager toolbar. The New GeoPackage Layer dialog will be displayed as shown in Fig. 15.18.

../../../_images/editNewGeoPackage.png

Fig. 15.18 Creating a New GeoPackage layer dialog

  1. The first step is to indicate the database file location. This can be done by pressing the button to the right of the Database field and select an existing GeoPackage file or create a new one. QGIS will automatically add the right extension to the name you provide.

  2. Give the new layer / table a name (Table name)

  3. Define the Geometry type. If not a geometryless layer, you can specify whether it should Include Z dimension and/or Include M values.

  4. Specify the coordinate reference system using the setProjection button

Para adicionar campos à camada que você está criando:

  1. Enter the Name of the field

  2. Select the data Type. Supported types are Text data, Whole number (both integer and integer64), Decimal number, Date and Date and time, Binary (BLOB) and Boolean.

  3. Depending on the selected data format, enter the Maximum length of values.

  4. Click on the newAttribute Add to Fields List button

  5. Reproduzir as etapas acima para cada campo que você precisa adicionar

  6. Once you are happy with the attributes, click OK. QGIS will add the new layer to the legend, and you can edit it as described in section Digitalizar uma camada existente.

By default, when creating a GeoPackage layer, QGIS generates a Feature id column called fid which acts as the primary key of the layer. The name can be changed. The geometry field, if availabe, is named geometry, and you can choose to Create a spatial index on it. These options can be found under the Advanced Options together with the Layer identifier (short human readable name of the layer) and the Layer description.

Further management of GeoPackage layers can be done with the DB Manager.

15.2.1.2. Criando uma nova camada Shapefile

To create a new ESRI Shapefile format layer, press the newVectorLayer New Shapefile Layer… button in the Layer ► Create Layer ► menu or from the Data Source Manager toolbar. The New Shapefile Layer dialog will be displayed as shown in Fig. 15.19.

  1. Provide a path and file name using the button next to File name. QGIS will automatically add the right extension to the name you provide.

  2. Next, indicate the File encoding of the data

  3. Choose the Geometry type of the layer: No Geometry (resulting in a .DBF format file), point, multipoint, line or polygon

  4. Specify whether the geometry should have additional dimensions: None, Z (+ M values) or M values

  5. Specify the coordinate reference system using the setProjection button

../../../_images/editNewVector.png

Fig. 15.19 Creating a new Shapefile layer dialog

Para adicionar campos à camada que você está criando:

  1. Enter the Name of the field

  2. Select the data Type. Only Decimal number, Whole number, Text data and Date attributes are supported.

  3. Depending on the selected data format, enter the Length and Precision.

  4. Click on the newAttribute Add to Fields List button

  5. Reproduzir as etapas acima para cada campo que você precisa adicionar

  6. Once you are happy with the attributes, click OK. QGIS will add the new layer to the legend, and you can edit it as described in section Digitalizar uma camada existente.

Por padrão, uma primeira coluna inteira id é adicionada, mas pode ser removida.

15.2.1.3. Creating a new SpatiaLite layer

To create a new SpatiaLite layer, press the newSpatiaLiteLayer New SpatiaLite Layer… button in the Layer ► Create Layer ► menu or from the Data Source Manager toolbar. The New SpatiaLite Layer dialog will be displayed as shown in Fig. 15.20.

../../../_images/editNewSpatialite.png

Fig. 15.20 Creating a New SpatiaLite layer dialog

  1. The first step is to indicate the database file location. This can be done by pressing the button to the right of the Database field and select an existing SpatiaLite file or create a new one. QGIS will automatically add the right extension to the name you provide.

  2. Provide a name (Layer name) for the new layer

  3. Define the Geometry type. If not a geometryless layer, you can specify whether it should Include Z dimension and/or Include M values.

  4. Specify the coordinate reference system using the setProjection button.

Para adicionar campos à camada que você está criando:

  1. Enter the Name of the field

  2. Select the data Type. Supported types are Text data, Whole number, Decimal number, Date and Date time.

  3. Click on the newAttribute Add to Fields List button

  4. Reproduzir as etapas acima para cada campo que você precisa adicionar

  5. Once you are happy with the attributes, click OK. QGIS will add the new layer to the legend, and you can edit it as described in section Digitalizar uma camada existente.

If desired, you can select checkbox Create an autoincrementing primary key under the guilabel:Advanced Options section. You can also rename the Geometry column (geometry by default).

Further management of SpatiaLite layers can be done with DB Manager.

15.2.1.4. Criando uma nova camada de malha

To create a new Mesh layer, press the newMeshLayer New Mesh Layer… button in the Layer ► Create Layer ► menu or from the Data Source Manager toolbar. The New Mesh Layer dialog will be displayed as shown in Fig. 15.21.

../../../_images/editNewMesh.png

Fig. 15.21 Creating a New Mesh layer dialog

  1. The first step is to indicate the mesh file location. This can be done by pressing the button to the right of the File name field and select an existing mesh file or create a new one.

  2. Provide a name (Layer name), i.e. the name the layer is displayed with in the Layers panel

  3. Select the File format: currently supported mesh file formats are 2DM Mesh File (*.2dm), Selafin File (*.slf) and UGRID (*.nc).

  4. Indicate the Coordinate Reference System to assign to the dataset

  5. The above steps will generate an empty layer that you can afterwards digitize vertices and add dataset groups to. It’s however also possible to initialize the layer with an existing mesh layer, i.e. populate the new layer with vertices or faces from the other. To do so:

    1. Check checkbox Initialize Mesh using

    2. and select either a Mesh from the current project or Mesh from a file. Informations on the selected mesh file are displayed for checkup.

    Note that only the frame of the mesh layer is transferred to the new layer; their datasets are not copied.

15.2.1.5. Criando uma nova camada GPX

Para criar um novo arquivo GPX:

  1. Select Create Layer ► createGPX New GPX Layer… from the Layer menu.

  2. In the dialog, choose where to save the new file, name it and press Save.

  3. Three new layers are added to the Layers Panel:

    • a point layer to digitize locations (waypoints) with fields storing the name, elevation, comment, description, source, url and url name

    • a line layer to digitize sequences of locations that make up a planned route (routes) with fields storing the name, symbol, number, comment, description, source, url, url name

    • and a line layer to track the receiver’s movement over time (tracks) with fields storing the name, symbol, number, comment, description, source, url, url name.

  4. You can now edit any of them as described in section Digitalizar uma camada existente.

15.2.1.6. Creating a new Temporary Scratch Layer

Temporary Scratch Layers are in-memory layers, meaning that they are not saved on disk and will be discarded when QGIS is closed. They can be handy for storing features you temporarily need or as intermediate layers during geoprocessing operations.

To create a new Temporary Scratch layer, choose the createMemory New Temporary Scratch Layer… entry in the Layer ► Create Layer ► menu or in the Data Source Manager toolbar. The New Temporary Scratch Layer dialog will be displayed as shown in Fig. 15.22. Then:

  1. Provide the Layer name

  2. Select the Geometry type. Here you can create a:

    • No geometry type layer, served as simple table,

    • Camada de``Ponto`` ou MultiPonto,

    • LineString/CompoundCurve or MultiLineString/MultiCurve layer,

    • Polygon/CurvePolygon or MultiPolygon/MultiSurface layer.

  3. For geometric types, specify the dimensions of the dataset: check whether it should Include Z dimension and/or Include M values

  4. Specify the coordinate reference system using the setProjection button.

  5. Adicionar campos à camada. Observe que, ao contrário de muitos formatos, as camadas temporárias podem ser criadas sem nenhum campo. Esta etapa é, portanto, opcional.

    1. Enter the Name of the field

    2. Select the data Type: Text, Whole number, Decimal number, Boolean, Date, Time, Date & Time and Binary (BLOB) are supported.

    3. Depending on the selected data format, enter the Length and Precision

    4. Click on the newAttribute Add to Fields List button

    5. Repita os passos acima para cada campo que você precisa adicionar

  6. Once you are happy with the settings, click OK. QGIS will add the new layer to the Layers panel, and you can edit it as described in section Digitalizar uma camada existente.

../../../_images/editNewTemporaryLayer.png

Fig. 15.22 Creating a new Temporary Scratch layer dialog

You can also create prepopulated temporary scratch layers using e.g. the clipboard (see Creating new layers from the clipboard) or as a result of a Processing algorithm.

Dica

Armazene permanentemente a camada de memória no disco

To avoid data loss when closing a project with temporary scratch layers, you can save these layers to any vector format supported by QGIS:

  • clicking the memória indicadora indicator icon next to the layer;

  • selecting the Make permanent entry in the layer contextual menu;

  • using the Export ► entry from the contextual menu or the Layer ► Save As… menu.

Each of these commands opens the Save Vector Layer as dialog described in the Criando novas camadas a partir de uma camada existente section and the saved file replaces the temporary one in the Layers panel.

15.2.2. Criando novas camadas a partir de uma camada existente

Both raster and vector layers can be saved in a different format and/or reprojected to a different coordinate reference system (CRS) using the Layer ► Save As… menu or right-clicking on the layer in the Layers panel and selecting:

  • Export ► Save As… for raster layers

  • Export ► Save Features As… or Export ► Save Selected Features As… for vector layers.

  • Drag and drop the layer from the layer tree to the PostGIS entry in the Browser Panel. Note that you must have a PostGIS connection in the Browser Panel.

15.2.2.1. Parâmetros comuns

The Save Layer as… dialog shows several parameters to change the behavior when saving the layer. Among the common parameters for raster and vector are:

  • File name: the location of the file on the disk. It can refer to the output layer or to a container that stores the layer (for example database-like formats such as GeoPackage, SpatiaLite or Open Document Spreadsheets).

  • CRS: can be changed to reproject the data

  • Extent: restricts the extent of the input that is to be exported using the extent_selector widget

  • Add saved file to map: to add the new layer to the canvas

No entanto, alguns parâmetros são específicos para formatos raster e vetorial:

15.2.2.2. Parâmetros específicos do raster

Dependendo do formato de exportação, algumas destas opções podem não estar disponíveis:

  • Output mode (it can be raw data or rendered image)

  • Format: exports to any raster format GDAL can write to, such as GeoTiff, GeoPackage, MBTiles, Geospatial PDF, SAGA GIS Binary Grid, Intergraph Raster, ESRI .hdr Labelled…

  • Resolution

  • Create Options: use advanced options (file compression, block sizes, colorimetry…) when generating files, either from the predefined create profiles related to the output format or by setting each parameter.

  • Pyramids creation

  • VRT Tiles in case you opted to checkbox Create VRT

  • No data values

../../../_images/saveasraster.png

Fig. 15.23 Salvando como uma nova camada matricial

15.2.2.3. Parâmetros específicos do vetor

Dependendo do formato de exportação, algumas destas opções podem estar disponíveis:

  • Format: exports to any vector format GDAL can write to, such as GeoPackage, GML, ESRI Shapefile, AutoCAD DXF, ESRI FileGDB, Mapinfo TAB or MIF, SpatiaLite, CSV, KML, ODS, …

  • Layer name: available when the File name refers to a container-like format, this entry represents the output layer.

  • Encoding

  • Save only selected features

  • Select fields to export and their export options: provides means to export fields with custom names and form widget settings:

    • Check rows under the Name column to choose fields to keep in the output layer, or press Select All or Deselect All buttons

    • Toggle the Use aliases for exported name checkbox to populate the Export name column with corresponding field aliases or reset to the original field name. Double-clicking a cell will also edit the name.

    • Depending on whether attribute form custom widgets are in use, you can Replace all selected raw field values by displayed values. E.g. if a value map widget is applied to a field, the output layer will contain the description values instead of the original values. The replacement can also be done on a field by field basis, in the Replace with displayed values column.

  • Persist layer metadata: ensures that any layer metadata present in the source layer will be copied and stored:

    • in the newly created layer, if the output is of GeoPackage format

    • as a .qmd file along with the output layer, for other formats. Note that file-based formats supporting more than one dataset (e.g. SpatiaLite, DXF,…) may have unintended behavior.

  • Symbology export: can be used mainly for DXF export and for all file formats who manage OGR feature styles (see note below) as DXF, KML, tab file formats:

    • No symbology: default style of the application that reads the data

    • Feature symbology: save style with OGR Feature Styles (see note below)

    • Symbol Layer symbology: save with OGR Feature Styles (see note below) but export the same geometry multiple times if there are multiple symbology symbol layers used

    • Um valor de Escala pode ser aplicado às opções mais recentes

Nota

OGR Feature Styles are a way to store style directly in the data as a hidden attribute. Only some formats can handle this kind of information. KML, DXF and TAB file formats are such formats. For advanced details, you can read the OGR Feature Styles specification document.

  • Geometry: you can configure the geometry capabilities of the output layer

    • geometry type: keeps the original geometry of the features when set to Automatic, otherwise removes or overrides it with any type. You can add an empty geometry column to an attribute table and remove the geometry column of a spatial layer.

    • Force multi-type: forces creation of multi-geometry features in the layer.

    • Include z-dimension to geometries.

Dica

Overriding layer geometry type makes it possible to do things like save a geometryless table (e.g. .csv file) into a shapefile WITH any type of geometry (point, line, polygon), so that geometries can then be manually added to rows with the addPart Add Part tool.

  • Datasource Options, Layer Options or Custom Options which allow you to configure advanced parameters depending on the output format. Some are described in Explorando Formatos de Dados e Campos but for full details, see the GDAL driver documentation. Each file format has its own custom parameters, e.g. for the GeoJSON format have a look at the GDAL GeoJSON documentation.

../../../_images/saveasvector.png

Fig. 15.24 Salvando como uma nova camada vetorial

When saving a vector layer into an existing file, depending on the capabilities of the output format (Geopackage, SpatiaLite, FileGDB…), the user can decide whether to:

  • overwrite the whole file

  • overwrite only the target layer (the layer name is configurable)

  • append features to the existing target layer

  • append features, add new fields if there are any.

For formats like ESRI Shapefile, MapInfo .tab, feature append is also available.

15.2.3. Criando novos arquivos DXF

Besides the Save As… dialog which provides options to export a single layer to another format, including *.DXF, QGIS provides another tool to export multiple layers as a single DXF layer. It’s accessible in the Project ► Import/Export ► Export Project to DXF… menu.

In the DXF Export dialog:

  1. Fornecer o arquivo de destino.

  2. Choose the symbology mode and scale (see the OGR Feature Styles note), if applicable.

  3. Select the data Encoding.

  4. Select the CRS to apply: the selected layers will be reprojected to the given CRS.

  5. Select the layers to include in the DXF files either by checking them in the table widget or automatically picking them from an existing map theme. The Select All and Deselect All buttons can help to quickly set the data to export.

    For each layer, you can choose whether to export all the features in a single DXF layer or rely on a field whose values are used to split the features into layers in the DXF output.

Opcionalmente, você também pode escolher por:

  • checkbox Use the layer title as name if set instead of the layer name itself;

  • checkbox Export features intersecting the current map extent;

  • unchecked Force 2d output (eg. to support polyline width);

  • checkbox Export label as MTEXT elements or TEXT elements.

../../../_images/export_dxf.png

Fig. 15.25 Exporting a project to DXF dialog

15.2.4. Creating new layers from the clipboard

As feições que estão na área de transferência podem ser colados em uma nova camada. Para fazer isso, selecione algumas feições, copie-as para a área de transferência e cole-as em uma nova camada usando Editar ► Colar Feições como ► e escolhendo:

A new layer, filled with selected features and their attributes is created (and added to map canvas).

Nota

Creating layers from the clipboard is possible with features selected and copied within QGIS as well as features from another application, as long as their geometries are defined using well-known text (WKT).

15.2.5. Criando camadas virtuais

A virtual layer is a special kind of vector layer. It allows you to define a layer as the result of an SQL query involving any number of other vector layers that QGIS is able to open. Virtual layers do not carry data by themselves and can be seen as views.

Para criar uma camada virtual, abra a caixa de diálogo de criação de camada virtual:

  • choosing the addVirtualLayer Add/Edit Virtual Layer entry in the Layer ► Add Layer ► menu;

  • enabling the addVirtualLayer Add Virtual Layer tab in the Data Source Manager dialog;

  • or using the DB Manager dialog tree.

The dialog allows you to specify a Layer name and an SQL Query. The query can use the name (or id) of loaded vector layers as tables, as well as their field names as columns.

For example, if you have a layer called airports, you can create a new virtual layer called public_airports with an SQL query like:

SELECT *
FROM airports
WHERE USE = "Civilian/Public"

The SQL query will be executed, regardless of the underlying provider of the airports layer, even if this provider does not directly support SQL queries.

../../../_images/create_virtual_layers.png

Fig. 15.26 Criar caixa de diálogo de camadas virtuais

Uniões e consultas complexas também podem ser criadas, por exemplo, para unir informações de aeroportos e países:

SELECT airports.*, country.population
FROM airports
JOIN country
ON airports.country = country.name

Nota

It’s also possible to create virtual layers using the SQL window of Plugin do gerenciador de banco de dados.

15.2.5.1. Embedding layers for use in queries

Besides the vector layers available in the map canvas, the user can add layers to the Embedded layers list, which can be used in queries without the need to have them showing in the map canvas or Layers panel.

To embed a layer, click Add and provide the Local name, Provider, Encoding and the path to the Source.

The Import button allows adding layers in the map canvas into the Embedded layers list. Those layers can then be removed from the Layers panel without breaking existent queries.

15.2.5.2. Supported query language

The underlying engine uses SQLite and SpatiaLite to operate.

It means you can use all of the SQL your local installation of SQLite understands.

Functions from SQLite and spatial functions from SpatiaLite can also be used in a virtual layer query. For instance, creating a point layer out of an attribute-only layer can be done with a query similar to:

SELECT id, MakePoint(x, y, 4326) as geometry
FROM coordinates

Functions of QGIS expressions can also be used in a virtual layer query.

To refer the geometry column of a layer, use the name geometry.

Contrary to a pure SQL query, all the fields of a virtual layer query must be named. Don’t forget to use the as keyword to name your columns if they are the result of a computation or a function call.

15.2.5.3. Performance issues

With default parameters, the virtual layer engine will try its best to detect the type of the different columns of the query, including the type of the geometry column if one is present.

This is done by introspecting the query when possible or by fetching the first row of the query (LIMIT 1) as a last resort. Fetching the first row of the result just to create the layer may be undesirable for performance reasons.

The creation dialog parameters:

  • Unique identifier column: specifies a field of the query that represents unique integer values that QGIS can use as row identifiers. By default, an autoincrementing integer value is used. Defining a unique identifier column speeds up the selection of rows by id.

  • No geometry: forces the virtual layer to ignore any geometry field. The resulting layer is an attribute-only layer.

  • Geometry Column: specifies the name of the geometry column.

  • Geometry Type: specifies the type of the geometry.

  • Geometry CRS: specifies the coordinate reference system of the virtual layer.

15.2.5.4. Comentários especiais

The virtual layer engine tries to determine the type of each column of the query. If it fails, the first row of the query is fetched to determine column types.

The type of a particular column can be specified directly in the query by using some special comments.

The syntax is the following: /*:type*/. It has to be placed just after the name of a column. type can be either int for integers, real for floating point numbers or text.

Por exemplo:

SELECT id+1 as nid /*:int*/
FROM table

The type and coordinate reference system of the geometry column can also be set thanks to special comments with the following syntax /*:gtype:srid*/ where gtype is the geometry type (point, linestring, polygon, multipoint, multilinestring or multipolygon) and srid an integer representing the EPSG code of a coordinate reference system.

15.2.5.5. Uso de índices

Ao solicitar uma camada por meio de uma camada virtual, os índices da camada de origem serão utilizados das seguintes formas:

  • if an = predicate is used on the primary key column of the layer, the underlying data provider will be asked for a particular id (FilterFid)

  • for any other predicates (>, <=, !=, etc.) or on a column without a primary key, a request built from an expression will be used to request the underlying vector data provider. It means indexes may be used on database providers if they exist.

A specific syntax exists to handle spatial predicates in requests and triggers the use of a spatial index: a hidden column named _search_frame_ exists for each virtual layer. This column can be compared for equality to a bounding box. Example:

SELECT *
FROM vtab
WHERE _search_frame_=BuildMbr(-2.10,49.38,-1.3,49.99,4326)

Spatial binary predicates like ST_Intersects are sped up significantly when used in conjunction with this spatial index syntax.