QGIS 9 Cropping and splicing

Robert Crowther Jun 2022
Last Modified: Feb 2023

PrevNext

Or, map‐size editing. Because most map data loads provide too much information. And some will not provide enough.

Cropping (clipping a map to a rectangle)

So, you don’t want a whole map tile. Each tile in the VectorMapDistrict data covers about 50 square miles. I, and likely you, only want part of this—a county, a town, even a village. In the words of an image editor, we want to ‘crop’ the map.

Unfortunately, map editing is not like this. Far as I know (there may be a plugin) QGIS has no general ‘crop’ tool. The word ‘crop’ implies cutting to a rectangle. QGIS has only a sophisticated tool called the ‘polygon clip’. From it’s name, you can tell a ‘polygon clip’ can cut to any vector shape. But is much more work than a simple ‘crop’. Worse, a ‘polygon clip’ only works on one layer at a time. Ok, let’s use it to imitate a ‘crop’.

image of qgis_default_clip
A unique QGIS experience-blind cropping

Create Polygon layer

Layer > Create Layer > New Shapefile Layer

It’s new layer, so will need a filename and a place to save the file,

File name

(navigate and make filename)

Geometry Type

Polygon

QGIS will build the layer name from the filename. Click ‘Ok’. The new layer should appear in the ‘layers’ display. I’d move the new layer out of any group, get it to a clear position. Either drag or,

[right click the layer] ‘Move layer out of group’

Important! If the new layer is not visible, make it visible.

Create rectangle (polygon)

Make sure the new layer is selected, not a different layer, then,

Layer > Toggle Editing (there’s also a button for this, with a ‘pencil’ icon)

Zoom in on your chosen area

Edit > Add Rectangle > Add Rectangle from Extent

There’s a general button for this too, ‘Add Polygon Feature’, but that tool builds a polygon from points. The listed command generates a two‐click rectangle.

[Left click to start the rectangle, right click to finish]

NOTE: No other way will finish the rectangle. Not an ESC, or an ENTER, or anything else you try.

You’ll be asked about ID. Ignore and ‘OK’ that.

Use the new rectangle (polygon) layer to clip a different layer

Warning: the clipped layer produces new data. This data must be saved somewhere. It can go to a temporary file, or to a physical file. If you use a physical file i.e. enter a filepath into the dialog, QGIS can handle several formats, such as Shapefiles, GeoPAckage table insertion, etc. But do not use GML format unless you must. I’ve had repeated warnings that GML files can not be edited in QGIS.

Easier to start if the layer you want to clip is selected,

Vector > Geoprocessing Tools > Clip

This gives a dialogue about the layer. Answer like this,

input layer

layer to clip. If that was selected, the field will be set to that layer

overlay layer

for this operation means ‘clip to polygon’ layer. Select the polygon layer

clipped

Use the file browser, ‘Save to file’, set to ‘Shape files’, then type in a name for the new layer

Close the dialogue. The clip will run, the layer will be ‘cropped’. If you used a temporary layer,

[right click] the layer > Save’.

All‐layer cropping

The problem with clipping is that it needs to be run on every layer. For a map with four tiles of 20 layers each, this consumes a huge amount of time. I have looked for solutions. I found no help online, but I have solutions.

Plugins

There is a plugin for this. There a probably several. I’ve not tried because, although a plugin may solve the problem, plugins and support are usually no use for casual users—install, use, performance and maintenance is far too prone to error, which can destroy a casual project. If you are prepared for tragedy, try.

QGIS solution

Involves two QGIS features.

Expressions

As usual, I recommend you don’t read the help. Nice that it’s there but it won’t help. QGIS has a feature I’ve not talked about. Underneath, many QGIS tools reference a computer language. ‘Clip’ probably references a line or two of computer language which says ‘take layer X, strip against the polygons on layer Y, save to a file’. Why do I say this trivial thing? Because the programming interface is available to the user. I talk more in this article on QGIS expressions.

Batch editing

Moving round QGIS, you may have noticed the ‘Batch Editing’ options. What these do is run tools on multiple layers (or maybe multiple tools against one layer etc.). That’s what we are after, running a simple rectangular clip against many layers.

Multiple layer clip

Let’s state the aim,

Take a layer with a polygon feature, clip all other layers against it, then output those layers, with a suitable name, to one of temporary files/Shapefiles/QGIS GeoPackage

GeoPackage would here be nice, keep all clipped data in one place.

This starts well,

Processing > Toolbox [Type in ‘clip’] > Clip > Run as Batch Process

Returns a dialogue with “Input Layer”, “Overlay Layer” and “Clipped” headers. Each line in the table is one operation of the ‘Batch’ process.

Set the Input Layers. This is cool,

[Click Button on empty row] > ‘Select from Open Layers’ > Select All > Ok

Or select layers you wish. I’d exclude the clip layer—clipped against itself, it returns itself.

Set the Overlay column,

[Click Button on top empty row] > ‘Use Layer Extent’ > [select your clip layer]

This only fills the top box with the clip layer. So now, on that column,

Autofill > Fill Down

Now all the boxes are full, referencing the clip layer.

Set the Clipped column. This is where it gets nasty. All these layer outputs need to go to different files. This is where we leverage the QGIS trick answer, expressions,

Autofill > Calculate By Expression

Another dialogue. Looking mysterious and empty. You can ignore most of it. Enter this in the box on the left, adapt to your purpose. This for Shapefiles,

'/targetPath/' || @INPUT || '.shp'

and this for GeoPackage insertion,

'ogr:dbname="/targetPath/targetFileName.gpkg"' || ' table="' || @INPUT ||'"geometry()'

When you press ‘Ok’ this will fill every box in the column with the results of the expression. Which hopefully modifies the filenames or table‐names to suit. If not what you want, alter the expression. Or adjust the entries by hand.

One last thing. There’s a small checkbox to the left. This lets you reload the results of the operation into QGIS i.e. all the new clipped layers will be loaded. I usually find I want this checked.

Right, press ‘Run’. Scary commandline‐like progress information will appear. Hopefully nothing coloured red. If there is nothing coloured red then, wherever you asked, files will be spewed into you computer. Or new tables pumped into a GeoPackage.

Discussion of clipping

A simple ‘crop’ in QGIS is tedious and unnecessarily fine‐grained. The only obvious way forward is to use the batch interface, but that requires expressions. I’ve found no help for QGIS expressions. Speaking of SQLite (which we were not) they publish their SQL form in a handful of web‐pages. Exclusive.

Hint: a good idea to store the clip rectangle layer someplace. QGIS may loose information somewhere along your process. It’s good to know you can rebuild that layer. Or add extra layers… for example if you change your mind and decide your final map would be more informative or pretty if it includes woodland.

Splicing tiles

Data from different tiles should position correctly next to each other. If that is not happening, there is an inconsistent setting in the coordinate systems the layers are assuming (‘CRS’). In that case, there is no alternative, you must check the CRS details on layers and in the project until you find the rogue assumption.

Next job is to merge the layers. Like clipping, this is a tedious operation, but at least you can multiple select layers to be merged. Select the layers, then,

Vector > Data Management Tools > Merge Vector Layers

This will generate a temporary layer. Then,

[right click the generated temp layer] > Make Permanent > add a filename

If you want to preserve the layer in the current project, you will also need to rename the temporary clip layer.

Clipping to map detail

Rather than a rectangular map, you may want to clip to an irregular region. For example, to make a map that is the shape of a town. To do this… I give a warning about layer duplication later. Better to create a new layer, then trace the shape you need. See the next section.

End of cropping and splicing

If you made it this far, you have a good looking map, which is now of the area you are interested in. Let’s look at some custom editing

Refs

I dislike video tutorials, but this one on how to clip in QGIS is the only good tutorial I found,

https://www.youtube.com/watch?v=1KqWDXYuYMs