I am working with vector polygons that have derived from hand drawn areas from when mapping was carried out with tracing paper over paper maps. Therefore many of the polygons don't exactly line up to the boundaries on the ground.
Another dataset of vector lines I have is accurate and so I have been manually snapping nodes of the polygons to the nodes of the lines. But this is a lengthy process.
Here is a screenshot…
The green line is the accurate dataset. You can see how the polygon boundaries (orange) are roughly correct. Bottom left I have started to edit the nodes, with the snapping options set up, and this works to manually move the nodes of the polygons to snap to the nodes of the line. (Yes, I could have enabled topographical editing to move both the polygon boundaries together, and I do do that).
Is there a method for snapping multiple nodes of the polygon to the nodes of the lines, or is there perhaps a better method of doing this? Using Python perhaps? I am looking for something akin to the 'Enable tracing' function (which is great) but for moving polygon boundaries.
I am aware that this may in places be a duplicate as there are many similar questions, but I haven't quite found anything that answers my particular situation? In particular I have looked at…
QGIS how to align existing polygon borders
How to snap polygons to lines?
How to snap lines to nearest point?
https://gis.stackexchange.com/search?q=how+to+snap+many+polygons+to+lines
Best Answer
Preamble
I propose a solution using PyQGIS. This is a complex issue and I'm sure that there are (many) specific cases that I didn't consider in my approach (so, take this answer as a starting point rather than a complete solution).
I didn't deeply test the code since I was not sure about the specific issue (see my unanswered comment where I requested some explanations), so please do some tests for validating it: if you are going to use the first attached code, create a backup copy of your layers because the edits are immediately committed to the geometry; instead, if you want to do some test without affecting the original layer, try to use the last attached code (both codes are the same, the unique difference is that the first one commits the edits on the layer itself, while the second one commits them to a new memory layer).
Finally, let's assume to start from this situation, where the red line is the guideline and the two polygons are two separate layers (each one having one feature):
Workflow explanation
The solution requires a line layer as a guideline and a set of polygon layers that need to be snapped. My approach iterates over the nodes of every polygon feature and tries to make a collage with the line geometry by deleting the unnecessary vertices and adding new ones.
This approach is quite simple to understand, but there are some things to consider:
Having said that, my workflow could be summarized in this way:
As you can understand, the main issues are in the last step because it didn't seem too easy enumerating all the possible cases (it would be easier if you provide a sample dataset).
Solution with edits applied to the original layer
You may run the following code as a new script from the Processing Toolbox:
and you will get this result:
If everything works, the edited polygons will perfectly match the guideline and have both the edges and the vertices perfectly aligned to it.
Solution with edits applied to a new layer
This solution differs from the previous one because saves the edits to a new memory layer instead of committing the changes to the original layer (it could be useful for testing):