Skip to content
← Back to QGIS from first project to capstone
QGIS · Part 04 of 20Beginner· 8 min readSources

QGIS Tables: Import CSV, Extract Coordinates, Join Data, Reproject and Calculate Fields

Turn a CSV of coordinates into a point layer, pull coordinates back out, join an external table, change the projection, and compute new fields with the Field Calculator.

Data used

Two practice files, in the vector folder of the tutorial data.

places.csv has ten places in and around Bengaluru.

idnamelatitudelongitude
1Kempegowda International Airport13.198977.7068
2Lalbagh Botanical Garden12.950777.5848
3Cubbon Park12.976377.5929

(The other seven rows follow the same pattern.) The coordinates are approximate, so check them against the basemap.

place_attributes.csv has the same ten id values with a category column and a practice_visitors_per_day column. The visitor numbers are made-up practice values. Do not quote them as real data.

1. Import a CSV as points

A CSV is a plain table. It becomes a map layer only when you tell QGIS which columns hold the coordinates.

  1. Layer > Add Layer > Add Delimited Text Layer (Ctrl+Shift+T).
  2. File name: choose places.csv.
  3. File format: CSV (comma separated values).
  4. Record and Fields Options: tick First record has field names, and tick Detect field types.
  5. Geometry Definition: choose Point coordinates.
  6. X field: longitude. Y field: latitude.
  7. Geometry CRS: EPSG:4326 (WGS 84), because the values are in degrees.
  8. Click Add, then Close.
Delimited Text dialog with X and Y fields set
Figure 4.1. Delimited Text dialog with X and Y fields set

Check the data types

Open the attribute table. Numbers should be left-aligned as numbers, not as text. In the dialog's sample data, set the type to Whole number or Decimal number if QGIS guessed Text. A text id will not join to a numeric id in step 3.

Make it permanent

A delimited text layer is only a view of the CSV. Export it so you have a real spatial file.

  1. Right-click the layer > Export > Save Features As.
  2. Format: GeoPackage. File name: places.gpkg. Layer name: places.
  3. CRS: keep EPSG:4326 for now.
  4. Click OK. The new layer loads into the project.

2. Extract coordinates

You can calculate coordinates from geometry and store them in attribute fields.

With the geometry tool

  1. Vector > Geometry Tools > Add Geometry Attributes.
  2. Input layer: places. Calculate using: Layer CRS.
  3. Run. The output layer has xcoord and ycoord fields.

With the Field Calculator

Open the calculator with Ctrl+I. These expressions return the coordinates of a point layer:

$x
$y

$x and $y return values in the layer's CRS. To get latitude and longitude from a layer in a projected CRS, convert first:

x(transform($geometry, @layer_crs, 'EPSG:4326'))
y(transform($geometry, @layer_crs, 'EPSG:4326'))

For a line or polygon layer, use the centroid:

x(centroid($geometry))

Read coordinates by clicking

The Coordinate box in the status bar shows the cursor position in the project CRS. Click the small icon next to it to switch between map coordinates and the extent of the view, and use Identify Features to read the coordinates of a clicked point under Derived.

Attribute table with xcoord and ycoord fields
Figure 4.2. Attribute table with xcoord and ycoord fields

3. Join an external table

A join attaches the columns of a table to a layer by matching a common field.

  1. Load place_attributes.csv through Layer > Add Layer > Add Delimited Text Layer. Choose No geometry (attribute only table).
  2. Double-click the places layer to open Layer Properties > Joins.
  3. Click the green plus.
  4. Join layer: place_attributes. Join field: id. Target field: id.
  5. Tick Custom field name prefix and type attr_ so the new fields are easy to spot.
  6. Click OK twice, then open the attribute table.
Join dialog with fields chosen
Figure 4.3. Join dialog with fields chosen
Attribute table showing the joined columns
Figure 4.4. Attribute table showing the joined columns

Join as a new, permanent layer

A join in Layer Properties exists only inside the project. To keep it, use Processing > Join Attributes by Field Value, or export the joined layer with Save Features As. If you export, the joined columns come with it.

When the join does not work

  • Fields with different types (text against number) never match.
  • Extra spaces or different capitalisation in text keys break matches.
  • A key that is not unique in the table gives you only the first match.
  • Leading zeros lost in a CSV, for example 0123 read as 123, break matches.

4. Projection and transformation

The CRS tells software how coordinate numbers relate to places on the Earth. Three things are easy to mix up.

ActionWhat it doesChanges the data?
Project CRSSets the map canvas CRS. QGIS reprojects layers on the flyNo
Set Layer CRSCorrects the CRS label of a layer when the label is wrong or missingNo
Reproject (export with a new CRS)Converts the coordinates into a new CRSYes, in the new file

Fix a wrong or missing CRS

If a layer sits in the wrong place, QGIS may have the wrong CRS label for it. Right-click the layer > Layer CRS > Set Layer CRS, and pick the one the data was really made in. Use this only when you know the correct CRS. It does not move the data.

Reproject a layer

  1. Vector > Data Management Tools > Reproject Layer.
  2. Input layer: places. Target CRS: EPSG:32643 (WGS 84 / UTM zone 43N).
  3. Run, then save the output as places_utm.gpkg.
  4. Open the attribute table and add $x and $y fields to see that the numbers are now in metres.
Reproject Layer dialog
Figure 4.5. Reproject Layer dialog
Coordinates before (degrees) and after (metres)
Figure 4.6. Coordinates before (degrees) and after (metres)

A geographic CRS such as EPSG:4326 stores coordinates in degrees. A projected CRS such as EPSG:32643 stores them in metres, which suits measuring distance and area. For Bengaluru, UTM zone 43N is a reasonable choice. For all of India, a national projection such as an Albers equal-area CRS is used when you need area comparisons across states.

Datum transformations

Changing between CRSs on different datums can need a datum transformation. QGIS suggests one when you reproject and lists the options with their accuracy. Pick the one that matches your region, and avoid the Ballpark option unless an error of a few metres is acceptable.

5. Field calculator

The Field Calculator creates or updates a field from an expression.

  1. Open the attribute table, then the Field Calculator (Ctrl+I).
  2. Choose Create a new field, and set Output field name, Output field type and Output field length.
  3. Write the expression, check the preview, then click OK.
  4. Turn off editing and save.
Field Calculator with an expression and preview
Figure 4.7. Field Calculator with an expression and preview

Useful expressions

GoalExpression
Combine textconcat("name", ' - ', "attr_category")
Upper caseupper("name")
Area in square metres (polygons)$area
Area in hectares$area / 10000
Length in metres (lines)$length
Classify with a conditionCASE WHEN "attr_practice_visitors_per_day" > 10000 THEN 'high' WHEN "attr_practice_visitors_per_day" > 3000 THEN 'medium' ELSE 'low' END
Distance to a pointdistance($geometry, make_point(77.5929, 12.9763))
Round a numberround("value", 2)

$area and $length use the ellipsoid set under Project > Properties > General, so the figures are ellipsoidal measurements in metres or square metres. Use area($geometry) for planar values in the layer's CRS units. In a geographic CRS the planar value is in square degrees, which is rarely what you want.

Update an existing field

Tick Update existing field and choose it from the list. This is how you fill a column in bulk. Work on a copy if the layer matters, because there is no undo once you save.

Common mistakes

  • Putting latitude in the X field.
  • Using EPSG:4326 for distance or area, which gives degrees.
  • Setting the layer CRS to fix a position problem without knowing the real CRS.
  • Joining a text field to a numeric field.
  • Treating a project join as permanent.
  • Dividing an area in square metres by the wrong factor when converting to hectares or square kilometres.

Key takeaways