Book Image

QGIS 2 Cookbook

By : Alex Mandel, Víctor Olaya Ferrero, Anita Graser, Alexander Bruy
Book Image

QGIS 2 Cookbook

By: Alex Mandel, Víctor Olaya Ferrero, Anita Graser, Alexander Bruy

Overview of this book

QGIS is a user-friendly, cross-platform desktop geographic information system used to make maps and analyze spatial data. QGIS allows users to understand, question, interpret, and visualize spatial data in many ways that reveal relationships, patterns, and trends in the form of maps. This book is a collection of simple to advanced techniques that are needed in everyday geospatial work, and shows how to accomplish them with QGIS. You will begin by understanding the different types of data management techniques, as well as how data exploration works. You will then learn how to perform classic vector and raster analysis with QGIS, apart from creating time-based visualizations. Finally, you will learn how to create interactive and visually appealing maps with custom cartography. By the end of this book, you will have all the necessary knowledge to handle spatial data management, exploration, and visualization tasks in QGIS.
Table of Contents (19 chapters)
QGIS 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Batch format conversion


The Save as dialog can be used to convert the format of a single layer. When several layers have to be converted, it is a better idea to use some automation. This recipe shows you how to easily convert an arbitrary number of layers.

Getting ready

No previous preparation is needed. Batch conversion is not performed based on open layers but performed directly on files, so there is no need to open layers in QGIS before converting them.

How to do it…

  1. Open the Processing Toolbox menu by selecting Toolbox in the Processing menu. The Processing Toolbox menu is the main element of the QGIS Processing framework, and it is used to call its algorithms:

  2. In the filter box of the Processing Toolbox menu, type save to filter the list of available algorithms. Locate the Save selected features algorithm, right-click on it, and select Execute as batch process. The batch processing interface will be displayed, as shown in the following screenshot:

  3. In the upper cell in the Input layer column, click on the ... button and select Select from filesystem. A file selector dialog will appear. Select the content of the batch_conversion folder in the dataset. It should have a total of three files. Click on OK on the file selection dialog. The batch processing interface should now have all these selected files, one in each row in the parameters table.

  4. In the Output layer column, click on the button in the first row. A dialog for saving the file will be opened. Select a file path in your filesystem where you want to save the output files and type converted.geojson as the output filename. Click on OK and a new dialog like the one shown in the following screenshot will appear:

  5. Select Fill with parameter values in the first field and Input layer in the second one. Click on OK. All the rows in the table will now have an output value, which was created using the entered filename as a prefix, followed by the name of the input layer.

  6. To avoid layers being loaded after they are created, set the first cell in the Load into QGIS column to No. Then, double-click on the column header to automatically copy this value to all the rows below.

  7. With the table already complete, you can launch the batch conversion process by clicking on Run. The GeoJSON files will be created in the specified paths.

How it works…

The conversion is performed by an algorithm from the QGIS Processing framework. Processing algorithms can be run either as individual algorithms or, in this case, in a batch process.

Outputs of Processing algorithms can be created in all formats supported by QGIS. The format is selected using the corresponding extension in the filename and, unlike in the case of saving a single layer, does not have to be selected in a field or list. Using geojson as the extension for your output files, you tell processing that you want to generate a file in this format.

Although the algorithm saves only the selected features of the layer, if there is no selection, it will use all the layer features. This is the default behavior of all algorithms in processing. As there is no selection in the layers that you have converted, all of their features will have been used.

When converting files this way, the additional options from the Save as dialog are not available, and the default configuration values are used.

There's more…

You can also convert vector layers with another more complex algorithm from the Processing Toolbox menu, which allows you to enter the configuration parameters used by the underlying OGR library that takes care of the process. It's called Export vector. Find it in the toolbox, right-click on it, and select Execute as batch process:

In this case, the output format is not controlled by the extension of the output filename as it happens with other processing algorithms according to what has been already explained.