How can I enable topological editing (create shared nodes on adjacent polygons or intersecting lines) and also activate the option to prevent polygon overlap (that when a new polygon is drawn, its portions that overlap on pre existing polygons is automatically removed) in QGIS version 3? In prior versions these setting were found in Settings->Snapping Options menu.
QGIS 3 Topological Editing – How to Enable Topological Editing in QGIS 3
editingpolygonqgissnappingtopology
Related Solutions
The tolerance of 0.00002 map units is obviously wrong. I have set my snapping tolerance to 10 pixels, which means that a point is snapped to an existing point if it comes nearer than 10 pixels.
With the small tolerance you set you avoid snapping in most cases, unless you are working in a scale of 50.000:1 (not 1:50000!).
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:
- while the polygon vertices are automatically ordered clockwise when digitizing (i.e. there is only one possible direction), a line layer can be digitized in two different directions according to which side you start from (the final direction is obviously unique, but you don't know if it follows the same order of the polygon vertices);
- as a consequence of the previous point, it is very easy creating features that are geometrically wrong and it basically depends on how you are able to consider all the different situations that may occur (this is the part where my solution needs to be improved).
Having said that, my workflow could be summarized in this way:
- read the current line feature geometry as a sequence of coordinates (they are returned in a list);
- read the current polygon feature geometry as a sequence of coordinates (they are returned in a list);
- from the current polygon feature, find the closest vertex to both line start and line end;
- edit the list from point 2 by assigning the coordinates within the list from point 1 in the proper position.
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:
##input_guideline=vector line
##input_polygons=multiple vector
from qgis.core import *
from qgis.utils import iface
polygons = input_polygons.split(';')
line = QgsVectorLayer(input_guideline, input_guideline, 'ogr')
for linefeat in line.getFeatures():
line_geom = linefeat.geometry()
geom_to_add = line_geom.asPolyline()
(first_line_point, end_line_point) = (line_geom.interpolate(0), line_geom.interpolate(-1))
for polygon in polygons:
poly = QgsVectorLayer(polygon, polygon, 'ogr')
for feat in poly.getFeatures():
attrs = feat.attributes()
geom = feat.geometry()
coords = geom.asPolygon()[0]
first_vertex=geom.closestVertex(first_line_point.asPoint())[0]
last_vertex=geom.closestVertex(end_line_point.asPoint())[0]
first_indices = [i for i, x in enumerate(coords) if x == first_vertex]
first_index = first_indices[0]
second_index = coords.index(last_vertex)
if first_index < second_index:
coords[first_index:second_index] = geom_to_add
new_geom = QgsGeometry.fromPolygon([coords])
if not new_geom.isGeosValid():
first_index = first_indices[-1]
if first_index < second_index:
coords = geom.asPolygon()[0]
coords[second_index:first_index] = (geom_to_add)
new_geom = QgsGeometry.fromPolygon([coords])
else:
coords = geom.asPolygon()[0]
coords[second_index:first_index] = reversed(geom_to_add)
new_geom = QgsGeometry.fromPolygon([coords])
else:
coords[first_index:second_index] = reversed(geom_to_add)
new_geom = QgsGeometry.fromPolygon([coords])
if not new_geom.isGeosValid():
first_index = first_indices[-1]
coords = geom.asPolygon()[0]
coords[second_index:first_index] = reversed(geom_to_add)
new_geom = QgsGeometry.fromPolygon([coords])
poly.dataProvider().changeGeometryValues({feat.id(): new_geom})
iface.mapCanvas().refreshAllLayers()
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):
##input_guideline=vector line
##input_polygons=multiple vector
from qgis.core import *
polygons = input_polygons.split(';')
line = QgsVectorLayer(input_guideline, input_guideline, 'ogr')
for linefeat in line.getFeatures():
line_geom = linefeat.geometry()
geom_to_add = line_geom.asPolyline()
(first_line_point, end_line_point) = (line_geom.interpolate(0), line_geom.interpolate(-1))
for polygon in polygons:
poly = QgsVectorLayer(polygon, polygon, 'ogr')
# Create the output layer
crs = poly.crs().toWkt()
outLayer = QgsVectorLayer('Polygon?crs='+ crs, 'snapped' , 'memory')
prov = outLayer.dataProvider()
fields = poly.pendingFields() # Fields from the input layer
prov.addAttributes(fields) # Add input layer fields to the outLayer
outLayer.updateFields()
for feat in poly.getFeatures():
attrs = feat.attributes()
geom = feat.geometry()
coords = geom.asPolygon()[0]
first_vertex=geom.closestVertex(first_line_point.asPoint())[0]
last_vertex=geom.closestVertex(end_line_point.asPoint())[0]
first_indices = [i for i, x in enumerate(coords) if x == first_vertex]
first_index = first_indices[0]
second_index = coords.index(last_vertex)
if first_index < second_index:
coords[first_index:second_index] = geom_to_add
new_geom = QgsGeometry.fromPolygon([coords])
if not new_geom.isGeosValid():
first_index = first_indices[-1]
if first_index < second_index:
coords = geom.asPolygon()[0]
coords[second_index:first_index] = (geom_to_add)
new_geom = QgsGeometry.fromPolygon([coords])
else:
coords = geom.asPolygon()[0]
coords[second_index:first_index] = reversed(geom_to_add)
new_geom = QgsGeometry.fromPolygon([coords])
else:
coords[first_index:second_index] = reversed(geom_to_add)
new_geom = QgsGeometry.fromPolygon([coords])
if not new_geom.isGeosValid():
first_index = first_indices[-1]
coords = geom.asPolygon()[0]
coords[second_index:first_index] = reversed(geom_to_add)
new_geom = QgsGeometry.fromPolygon([coords])
outGeom = QgsFeature()
outGeom.setAttributes(attrs)
outGeom.setGeometry(new_geom)
prov.addFeatures([outGeom])
# Add the layer to the Layers Panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)
Best Answer
In QGIS 3.4 you may need to enable Snapping toolbar by right-click the main menu bar and select Snapping toolbar, and you will find this menu:
The last three icons are Enable Topological Editing, Enable Snapping on Intersection and Enable Tracing, respectively.
Also, in QGIS 3.4 there is a new feature that you can enable Topological Check during digitizing using Layer properties -> Digitizing -> Topological check:
You can refer to the changelog of QGIS 3.4 and scroll down to digitizing new features for more information:
QGIS Changelog for version: 3.4 LTR