Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
QGIS 2 Cookbook

You're reading from   QGIS 2 Cookbook Become a QGIS power user and master QGIS data management, visualization, and spatial analysis techniques

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher
ISBN-13 9781783984961
Length 390 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (3):
Arrow left icon
Anita Graser Anita Graser
Author Profile Icon Anita Graser
Anita Graser
Víctor Olaya Ferrero Víctor Olaya Ferrero
Author Profile Icon Víctor Olaya Ferrero
Víctor Olaya Ferrero
Alex Mandel Alex Mandel
Author Profile Icon Alex Mandel
Alex Mandel
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Data Input and Output FREE CHAPTER 2. Data Management 3. Common Data Preprocessing Steps 4. Data Exploration 5. Classic Vector Analysis 6. Network Analysis 7. Raster Analysis I 8. Raster Analysis II 9. QGIS and the Web 10. Cartography Tips 11. Extending QGIS 12. Up and Coming Index

Importing data from text files

Data can be imported from text files, providing some additional about how the geometry information is stored in the text. This recipe shows you how to create a new points layer, based on a text file.

How to do it…

  1. Select the Add delimited text layer menu entry from the Layer menu. You will see a dialog like the following one:
    How to do it…
  2. In the upper field, enter the path to the elev_lid792_randpts.csv file in the sample dataset. That file contains a points layer as text.
  3. Once you enter the file path or select it in the file browser that can be opened by clicking on the Browse button, the fields in the lower part of the dialog will be filled, as shown in the following screenshot:
    How to do it…

    Note

    We are using a CSV file that has values separated by commas, so you must select the CSV option in the Format field.

    The X field and Y field drop-down lists will be populated with the fields that are available, which are described in the first line of the text file. Select X for X field and Y for Y field. Now, QGIS knows how to create the geometries and has enough information to create a new layer from the text file.

  4. Enter a name for the layer in the Layer name field and click on OK. The layer will be added to the QGIS project, as shown in the following screenshot:
    How to do it…
  5. No information about the CRS is contained in the text file or entered in the parameters dialog, so it must be added manually. In this case, the CRS used is EPSG:3358. To set this as the CRS of the layer, right-click on the layer name and select Set layer CRS:
    How to do it…
  6. In the CRS selection dialog, select the EPSG:3358 CRS and click on OK. The layer now has the correct CRS.

How it works…

Data is read from the text file and processed to create geometries. All the fields in the table (all data in a row in the text file) are also added, including the ones used to create the geometries, as you will see by right-clicking on the layer and selecting Open attribute table, as shown in the following screenshot:

How it works…

Along with the CSV file, this file may contains a CSVT file, which describes the types of the fields. This is used by QGIS to set the appropriate type for the attributes table of the layer. If the CSVT file is missing, as in our example's case, QGIS will try to figure out the type based on the values for each field.

There's more…

Layers created from text files are not restricted to point files. Any geometry can be created from the text data. However, if it is not a point, instead of selecting two columns, you must place all the geometry information in a single one and enter a text representation of the geometry. QGIS uses the Well-Known Text (WKT) format, which is a text markup language for vector geometries, to describe geometries as strings. Here is an example of a very simple CSV file with line features and two attributes:

geom,id,elevation
LINESTRING(0 1, 0 2, 1 3),1,50
LINESTRING(0 -1, 0 -2, 1 -3),2,60
LINESTRING(0 1, 0 3, 5 4),3,70

See also

You have been reading a chapter from
QGIS 2 Cookbook
Published in: Apr 2016
Publisher:
ISBN-13: 9781783984961
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image