In QGIS, is there a way to count the number of vertices of polygons and lines in a given layer, and create a separate field containing these values?
I suspect it's a function somewhere in the field calculator but I can't find it.
You can use the excellent 'Vertices Counter' plugin which does exactly this. You can see the step-by-step guide here:
As times go by and version changes, some hints concerning the recent QGIS versions 3.2 and 2.18.22 LTR (by Aug. 7th, 2018). In QGIS 2.18 the Vertices Counter plugin works as usual, but seems not yet been ported to QGIS 3. But fortunally, there is another solution available in field calculator that you expected to find nearly six years ago.
Two relevant functions are available:
MULTIPOINT geometry of the source geoemtries nodes and
num_geometries() counts the geometries in a collection or
so combining these functions and creating a virtual field will accomplish this task:
Look in the vector menu, there is a "extract nodes" tool.
We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.