Meters Vs Degrees: How To Force Qgis Buffer Tool To Use Metric Units
The Problem: Buffers Created in Degrees Instead of Meters
A common issue when working with geospatial data in QGIS is that distance and area calculations can unexpectedly be returned in geographic degrees instead of projected units like meters. This happens frequently when using the Buffer tool to create buffer zones around vector features.
For example, you may attempt to create a 100 meter buffer around a point, line, or polygon feature to analyze or select additional features within that distance. However, instead of a 100 meter buffer, the output is a buffer zone that covers a 0.008983 degree area. This occurs because the underlying coordinate reference system (CRS) of the data layer or QGIS project is in an unprojected geographic CRS using latitude and longitude values in degrees, instead of a projected CRS with linear units.
Dealing with unwanted degree-based buffers instead of the expected metric distances is frustrating for QGIS users who require metric measurement values for analysis tasks. The buffer outputs cannot be meaningfully measured or converted when in non-linear geographic degrees. Fortunately, QGIS offers full control and transformation capabilities make sure buffers calculations are performed in the desired metric units.
Understanding Coordinate Reference Systems in QGIS
To force metric buffer distances, it is important to first understand the coordinate reference system (CRS) concept in GIS software like QGIS. The CRS defines the translation between locations on the Earth’s surface and numeric coordinate values. It also determines the units and projections used to represent real-world distances and areas.
There are two broad categories of CRS:
- Geographic CRS – Based on a spheroidal model of the Earth, with coordinates defined in latitude and longitude degrees. Distances and areas will be in decimal degrees, not linear units like meters.
- Projected CRS – Projects geographic coordinates onto a 2D planar surface, allowing use of linear units like meters. Provides accurate real-world distances, areas, and shapes for mapping and analysis within the projection zone.
QGIS layers can have an associated CRS defining the coordinate system. When data layers with different CRSs are visualized or analyzed together, QGIS performs “on-the-fly” transformation between CRSs behind the scenes in most cases. This allows seamless mixing of data – but can also lead to unprojected analyses if geographic and projected data are combined without explicit transformation instructions.
Setting the Project CRS to a Metric System
The first step to ensure metric buffer distances is to set an appropriate projected coordinate reference system (CRS) at the QGIS project level. This avoids “on-the-fly” geographic CRS transformations for any analyses within this project.
To define the project CRS:
- In the QGIS browser panel, right-click on the project name and select Properties.
- In the Properties dialog, click on the CRS tab.
- Under the Coordinate reference systems of the world section, expand the Projected CRS > UTM node.
- Select the desired UTM zone for your project area. For example, WGS 84 / UTM zone 15N.
- Click OK to set the selected CRS as the project’s reference system.
The project now has an explicitly defined metric coordinate system. Any geospatial calculations done within this project will be in meters rather than unprojected geographic degrees. This avoids unwanted degree-based outputs for buffer zones or other geometries created with the project’s layers.
Using the On-the-Fly CRS Transformation
In some workflows, setting the underlying project CRS may not be possible or desirable. In these cases, the on-the-fly coordinate transformation tool can be used to define layer-level projections to enable metric distance calculations.
To use on-they-fly transformation:
- In the Layers panel, right-click on the layer to buffer and select Properties.
- In the Properties dialog, switch to the Source tab.
- Expand the Coordinate reference system section.
- Enable the On the fly CRS transformation option.
- In the Filter text box, search for a metric projected CRS appropriate for the layer’s geographic location, such as UTM.
- Select the desired CRS. A globe icon indicates it is a projected metric CRS.
- Click OK to apply the layer CRS transformation.
With on-the-fly transformation enabled, projections to the metric target CRS will now automatically occur whenever this layer is analyzed. Any buffer distances calculated will use the linear units defined in the metric CRS instead of unprojected degrees.
Creating Metric Buffers with Python Code
Using Python scripting in QGIS is a flexible approach to force metric units on buffer operations. Custom Python code can explicitly define the desired metric CRS and output format for geometry calculations.
For example, this script projects input layers to UTM before buffering, and formats the output distances in meters:
layer = qgis.utils.iface.activeLayer()
layer_crs = layer.crs().authid()
utm_crs = 'EPSG:32615'
buffered_layer = processing.run("native:buffer",
{'INPUT': QgsProcessingFeatureSourceDefinition(layer.id(), True, utm_crs, {}),
'DISTANCE': 100,
'SEGMENTS': 5,
'END_CAP_STYLE': 0,
'JOIN_STYLE': 0,
'MITER_LIMIT': 2,
'DISSOLVE': False,
'OUTPUT': 'memory:'})
format_string = '{:.3f} meters'
buffered_layer.dataProvider().capabilities()
buffered_layer.startEditing()
buffered_layer.dataProvider().addAttributes([QgsField('distance', QVariant.Double, 'double', 24, 15, format_string)])
buffered_layer.updateFields()
meters_100 = float(format_string.strip(' meters'))
for feature in buffered_layer.getFeatures():
buffered_geometry = feature.geometry()
feature.setAttribute(feature.fieldNameIndex('distance'),
round(buffered_geometry.length(), 3))
buffered_layer.updateFeature(feature)
buffered_layer.commitChanges()
The key steps are:
- Define desired metric CRS (UTM zone)
- Use this CRS in Buffer tool parameters
- Format output to show metric units
The output buffer layer’s distances will now be calculated and displayed in the defined meters units.
Verifying Buffers are Now in Meters
Once project CRS settings, on-the-fly transformations, or Python scripts are implemented to enable metric buffers, the results should be verified to validate correct distances.
To check that buffers are being calculated in meters:
- Add an identifiable point, line, or polygon feature to visualize (or use existing layer).
- Apply a buffer operation with a defined metric distance eg 100 meters.
- Inspect buffer result geometry to confirm it matches expected size and shape at defined distance.
- Optionally add metro grid or other reference data in meters to visually verify buffer dimensions.
- For additional validation, export the output buffer layer and check coordinate values in GIS or text editor.
Pay attention to buffer width, arcs, and corners during visual inspection, as these aspects will clearly show if an unprojected degree-based buffer was created instead of the expected metric distances.
Automated testing using PyQGIS could also be implemented to validate buffer output geometry against expected metric measurements.
Additional Tips for Managing CRS in QGIS
Utilizing the various CRS capabilities in QGIS is key to getting reliable and expected outputs from geospatial analyses.
Here are some additional tips for handling CRS transformations:
- Always analyze vector data in a projected CRS, don’t use unprojected latitude/longitude degrees.
- Set the project CRS to control analysis coordinate space.
- Use on-the-fly reprojection for individual layers if needed.
- Enable “on-the-fly” transformations with care as they may hide latent unprojected geospatial calculations.
- Label key layers with their native CRS for reference.
- Save analysis result layers with relevant CRS to avoid later confusion.
- Use Python scripts for advanced control of CRS handling and conversions.
Following best practices for coordinate reference system management will help avoid unwanted degree-based measurements and facilitate accurate, metric-based geospatial analysis in QGIS.