QGIS – Extracting xy Coordinates in D°M’S”

In this tutorial we will calculate the coordinates for features in a point file with the coordinate reference system (CRS) EPSG:4326.

EPSG:4326 is a specific coordinate reference system (CRS) commonly known as WGS 84 (World Geodetic System 1984). This CRS is widely used for representing geographic data on the Earth's surface.

Firstly, let's generate the point file for this demonstration. We'll utilize the widely adopted vector shapefile format to establish this new layer.

Create a Shapefile layer QGIS

To create a shapefile point file in QGIS with specific fields, follow these steps:

  • Open a new QGIS project
  • Click on the "Layer" menu
  • Select "Create Layer" and then choose "New Shapefile Layer..."
Figure 1. Create a new shapefile.

Set Up the New Shapefile:

In the "Create New Shapefile" dialog:

  • Click on the "Browse" button to select the location where you want to save the new shapefile
  • Name the new file cities
  • Choose the "Point" geometry type
  • Set the CRS to EPSG:4326

Define Fields:

  • For the field name Type "city" as the "Name" column
  • Choose "Text" as the "Type"
  • Set the "Length" to 25
  • Click the “Add fields to list” button
  • For the next field Type "x" as the "Name" column
  • Choose "Decimal (double)" as the "Type"
  • Accept the default ”Length” and “Precision” settings
  • Click the “Add fields to list” button
  • For the next field Type "y" as the "Name" column
  • Choose "Decimal (double)" as the "Type"
  • Accept the default ”Length” and “Precision” settings
  • Click the “Add fields to list” button
  • For the next field Type "coordinate" as the "Name" column
  • Choose "Text" as the "Type"
  • Set the "Length" to 75
  • Click the “Add fields to list” button
  • Click OK
Figure 2. Configuring the Shapefile

A new layer called cities will be added to your project.

We will use a Quick Map Services (QMS) layer to digitize the positions of the cities we want to capture. The QMS plugin should be activated before any layers can be added to the project.

Install QMS Plugin:

To add a layer from Quick Map Services (QMS) in QGIS, you'll need to follow these steps. Please note that QGIS and its plugins may receive updates, so the steps might vary slightly based on the version you're using.

  • Open the “Plugins” menu
  • Select "Manage and Install Plugins..."
  • In the Plugin Manager, search for "QuickMapServices" and install it
  • Make sure the plugin is activated
  • Go to the "Web" menu and select "QuickMapServices."

You'll see a list of available map services categorized by region and provider.

  • Choose Google Road as the service to add
Figure 3. QMS Google Roads.
  • Move the new QMS layer below the cities layer in the layers panel

Layer Properties

Next, we will customize the layer properties to make the appearance and labelling match our preferences.

  • In the “Layers Panel” right click the “cities” layer
  • Select “Properties”
  • Select the “Symbology” tab
  • For the “Marker” select the “diamond red” icon
  • Select the “Labels” tab
  • Select “Single Labels” from the drop down options
  • Set the “Value” as “city”
  • Select the “Buffer” option
  • Check the “Draw text buffer” box
  • Select the “Attributes Form” tab
  • Under the “Fields” category
  • Select “x”
  • Under the default value option type in ‘$x’
  • Check the option to “Apply default value on update”
  • Select “y”
  • Under the default value option type in ‘$y’
  • Check the option to “Apply default value on update”
  • Click “OK”

Capturing new features

Next, we will capture the positions of 4 major cities from around the world.

Start editing

  • From the “Layer” menu select “Toggle Editing”

The “Digitizing  Toolbar” will become active.

Figure 4. The Digitizing Toolbar
  • Click the “Add point feature” button
  • Click on the map to digitize the position of New York

You may need to zoom in on New York to accurately locate its position.

  • In the dialog that pops up input the id as 1 and city as “New York”

You will notice that the x and y value have been captured automatically.

Figure 5. Digitizing new features.
  • Click “OK”

A new feature will be added to the map.  Capture 3 more cities in the same way.

  • On the Digitizing Toolbar click on the “Toggle Editing” button
  • Click “Save edits”

Calculate the Coordinates in D°M’S”

The coordinate values for the x and y fields have been returned in decimal degrees.  These values can be formatted to Degrees Minutes Seconds.  We will use the “coordinates” field to do this using the “Field Calculator” and an equation.

  • In the “Layers Panel” right click on the “cities” layer
  • Select “Open Attribute table”
Figure 6. Attribute table.
  • Click on the “Open field calculator” button
  • Check the “Update existing field” option
  • Select the “coordinate” field from the field drop down menu

We now need to write an expression to return the DMS format for our x and y values.

  • Click inside the “Expression window”
  • Type in the following code

floor (abs($y))

|| '° ' || floor(((abs($y)) - floor (abs($y))) * 60)

||'\' '

|| substr( (tostring((((abs($y)) - floor (abs($y))) * 60) - floor(((abs($y)) - floor (abs($y))) * 60)) * 60),1,5)

|| '"'

|| (CASE WHEN $y < 0 THEN ' S' ELSE ' N' END)

|| ' : '

|| floor (abs($x))

|| '° ' || floor(((abs($x)) - floor (abs($x))) * 60)

||'\' '

|| substr( (tostring((((abs($x)) - floor (abs($x))) * 60) - floor(((abs($x)) - floor (abs($x))) * 60)) * 60),1,5)

|| '"'

|| (CASE WHEN $x < 0 THEN ' W' ELSE ' E' END)

Figure 7. The field calculator.
  • Click “OK”

The “coordinate” field will be populated with the coordinates in DMS.

Using the “Lat Lon Tools” plugin.

The “LatLon” plugin will accomplish the same task without the need to type in multiple lines of code.

  • Open the “Plugins” menu
  • Select "Manage and Install Plugins..."
  • In the Plugin Manager, search for "Lat Lon Tools" and install it
  • Make sure the plugin is activated
  • From the “Plugins Menu” select “Lat Lon Tools > Conversions > Point Layer to Fields”
Figure 8. Calculating DMS using Lat Lon Tools.
  • Set “cities” as the input vector layer
  • Set the “Output coordinate format” to “Coordinate in 1 field”
  • Change the name of the field from “y” to “latlon”
  • Set the “Select Decimal or DMS degrees” option to “DMS”
  • Select the option to “Add space between DMS numbers ”
  • Click “Run”

This will create a temporary layer with the new field “latlon” and coordinate values in DMS.

Leave a Reply

Your email address will not be published. Required fields are marked *