articles

# How can you read a WKT in QGIS and use it as a layer of geographic information

## Learn how to read a Well Known Text (WKT) format in QGIS and use it as a layer of geographic information. Discover how the WKT format is used in QGIS and spatial databases, and explore the process of importing and exporting spatial data based on this standard

1. How can you read a WKT in QGIS

In this article we are going to give some details about what is the Well Known Text format ( WKT ) for the definition of geometries, its relationship with Geographic Information Systems and spatial databases.

We will also show how import and export spatial data based on this standard and how to read a WKT in QGIS, to use as layers of geographic information.

## What is the WKT format?

The WKT or Well Known Text format is a coding format specifically designed for characterizing and storing spatial geometric objects in vector format.

It is a very expanded format within the world of geotechnologies since it is a standard defined by the OGC and therefore it has been adopted by a large number of Geographic Information Systems and is accepted by other geospatial tools and libraries.

### Geometries supported in the WKT standard

Using WKT we can define different types of geometries how:

#### Basic geometry typologies:

• Points ( POINT )
• Lines ( LINESTRING )
• Polygons ( POLYGON )

#### Multiparty geometry typologies:

• Multipoints ( MULTIPOINT )
• Multilines ( MULTILINESTRING )
• Multipolygons ( MULTIPOLYGON )

#### Other types of geometries:

• Geometry collections: elements of different geometric typology.
• Three-dimensional points ( X, Y, Z )
• Four-dimensional points ( X, Y, Z, M )
• Polyhedral surfaces
• TIN ( Triangulated Irregular Network )

### Examples of definition of geometries in WKT

To propose some examples, we are going to show the appearance of some geometries “ translated ” into this format WKT.

The structure of a WKT geometry of type point is as follows:

 1 `POINT(10 20)`

A three-dimensional point, meanwhile, would be made up of three coordinates. For example, to display a lift value on the Z axis:

 1 `POINT(10 5 3)`

A multipoint would consist of:

 1 `MULTIPOINT((10 20), (30 50))`

The geometry of a line in WKT format would have the following appearance:

 1 `LINESTRING (10 35, 5 15, 20 30)`

A polygon would be defined as follows, so that the starting point and the end point are coincident :

 1 `POLYGON(10 10, 15 15, 10 30, 5 10, 10 10)`

Finally, a multipoligone would be defined as follows:

 1234 `MULTIPOLYGON(``(10 10, 15 15, 10 30, 5 10, 10 10), ``(25 5, 20 15, 30 30, 25 5)``)`

Basically, as we see, the structure translates into a parameter for defining the type of geometry followed by the coordinates of the necessary points that define it in the space in parentheses.

Coordinates are defined by pairs of X and Y separated by spaces, and each pair of coordinates separated by commas.

In the case of elements with multipart geometry, each of the parts in parentheses is separated by a comma, encompassed within a higher order parenthesis that logically must be respected at the beginning and end.

## Load CSV in WKT format in QGIS

For load data in WKT format in QGIS From a text file containing a field with such geographic information, we should go to the Layer menu > Data source manager > Delimited text.

In the panel, as if it were a CSV, we must specify the file reading parameters: field delimiters, encoding, field types, coordinate system…

### How can you read a WKT in QGIS

However, in the definition of geometries we must specify that it is a WKT, as well as the type of geometry if it is homogeneous throughout the file. In the sample data viewer we can see the appearance of our data after import.

### How can you read a WKT in QGIS

Once the definition of these parameters is finished, our data should be displayed on the canvas of QGIS and appear as a ( layer not as a ) table in the side table of contents. In this case we have loaded a series of lines ( LINESTRING ) that represent the bike lanes of the city of Barcelona.

## Export data in WKT format from QGIS

If we want to obtain the geometric definition of the elements of a layer based on its geometry, we can export any layer in CSV format and specify the desired parameters for saving.

For export our layer keeping the field of geometry in WKT format We should treat it like a CSV. We will right-click on the > Export > layer Save objects as.

We must specify the file save path and its name, the coordinate reference system, the fields to export and, in the section “ Layer options ”, define geometry as “ AS_WKT ” and the type of separator between fields.

### How can you read a WKT in QGIS

#### How can you read a WKT in QGIS and use it as a layer of geographic information

By doing this action, we are basically adding a new one column called “ WKT ” on our exported layer that contains the geometry in said format, that is, in the POINT ( X Y ) style.

Alternatively, if we wanted to calculate that column without having to export the data to get the geometries in WKT, we could create a new field and calculate that value in it.

The field, obviously, must be of type text and have a sufficient length to house the definition of all geometries. If we assign an extension of the string too short we would get invalid geometries.

The more complex the geometries ( the greater the number of vertices ) the longer their definition will occupy in WKT.

### How can you read a WKT in QGIS

For the geometry calculation in WKT in this new field, from the field calculator we must enter the following formula from QGIS:

 1 `campoWKT = geom_to_wkt(\$geometry)`

which basically returns the geometry in WKT based on the \$ geometry parameter of each element of the layer. In this way, each geometric element is assigned its respective point in WKT with its coordinate pair.