A Smaller, Lighter Dataset: Clip Layers in QGIS (Shallow Thoughts)

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Mon, 27 Nov 2023

A Smaller, Lighter Dataset: Clip Layers in QGIS

[QGIS screenshot showing a manufactured polygon to clip a river layer]

The dataset I used for mapping fire perimeters is huge: not surprising if it's all historic fires for the US. Classifying it in QGIS gave a warning, and operations were very slow. Here's how to clip a big dataset in QGIS to restrict it to a smaller geographic area.

First, generate another layer to be the clip boundary: Layer → Create Layer → New Shapefile Layer Use Geometry type: Polygon. Click on the output file button, navigate to where you want to save the clip boundary, choose a filename (which should include the extension, like foo.shp), and click OK. That will make the new layer's name appear in the layer list.

Right-click on the new layer and choose Toggle Editing (or use the Toggle Editing (with a pencil icon [QGIS Pencil icon] ) button in the toolbar).

Then click the toolbox button for Add Polygon Feature: it's the one that looks like [QGIS Add Polygon icon] a green peanut hovering over a smaller gold square with a white star inside it. Now you can click inside the map window to define your polygon. When you're finished, right-click; a prompt appears asking for an id for this feature attribute. Assuming it's the first polygon for the layer, 1 is the right answer. Finally, you can click the Save Layer Edits button (icon is a floppy disk) which for me was two icons left of the green peanut. Then click Toggle Editing (just left of Save Layer Edits) to end editing.

Now you have a layer, which will block everything underneath it, since its style defaults to opaque. If you want to fix that, right click on it in the layer list, choose Properties, make sure you're in the Symbology tab, find Layer Rendering way down at the bottom of the dialog and expand it, then slide the Opacity slider to something like 50% opacity so you can see what's underneath. (I wish I could make this the default for all new layers.) There's another Opacity slider higher up in the dialog next to the color chooser, which also works; I'm not sure what the differences is between them.

Now you can use the new layer to clip the extent of the original fire boundaries layer. Go to Vector → Geoprocessing Tools → Clip... and set Input layer to the data layer you want to clip, and Overlay layer to the polygon layer you just drew. If you want to save the clipped layer so you can use it again, click on the ... icon to the right of the text field under Clipped, choose Save to File..., navigate to where you want to save it (warning: it may not default to the directory of the current project), and choose a filename. I strongly recommend choosing a filename to save to; if you let it use a temporary file, it's difficult to access the layer later, like if you later decide you want to clip other layers using that polygon.

Then click Run. You'll get a new layer, named Clipped unless you gave it a filename to save to.

How to Fix Invalid Geometry

Except, in the case of the fire perimeters, not quite, because clipping popped up a dialog complaining that there are features with invalid geometry in that layer. To fix that, first call up Processing -> Toolbox, scroll down to Vector geometry and expand it, then scroll down again to run Fix geometries. As with the clipping layer, you can choose a filename to save the fixed layer permanently, or make a temporary layer (which will be named "Fixed geometries").

Once the layer is fixed, you should be good to go with Vector → Geoprocessing Tools → Clip.

What About Raster Layers?

You can clip raster layers too, in a similar way. Raster → Extraction → Clip Raster by Extent... worked best for me, and you can use the current view as an extent. There's also Raster → Extraction → Clip Raster by Mask Layer..., but I found that a raster (I was clipping a Digital Elevation Map) clipped to an arbitrary shape didn't work very well. Either way, in the dialog that pops up, set Input layer, and either Mask layer or extent; then scroll down to the bottom and changed the text field under Clipped (mask) from [Save to temporary file] to the filename you want to use for the new clipped layer.

Tags: , , ,
[ 11:43 Nov 27, 2023    More mapping | permalink to this entry | ]

Comments via Disqus:

blog comments powered by Disqus