2

I'm trying to create points on the edges of polygons.

So far I have converted the polygons to lines, then exploded the lines, then ran the following code which plots a point in the middle of the lines.

layer = iface.activeLayer()
temp = QgsVectorLayer("Point?crs=epsg:27700", "result", "memory")
temp.startEditing()
attrs = layer.dataProvider().fields().toList()
temp_prov = temp.dataProvider()
temp_prov.addAttributes(attrs)
temp.updateFields()
for elem in layer.getFeatures():
 feat = QgsFeature()
 geom = elem.geometry().interpolate(elem.geometry().length()/2)
 feat.setGeometry(geom)
 feat.setAttributes(elem.attributes())
 temp.addFeatures([feat])
 temp.updateExtents()
temp.commitChanges()
QgsProject.instance().addMapLayer(temp)

I have some polygons which are adjoined, sometimes several squares in a row.

I've noticed that each polygon still has a separate line, even though they're adjoining polygons if that makes sense?

I want to remove the points where two polygons adjoin, and so therefore I just want to remove all instances where there's duplicate points/lines. So not just remove duplicates so there's one left, but remove both instances of duplicates.

Hope that makes sense and someone can help!

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Feb 12, 2020 at 12:02
4
  • "then ran a code" - nice, and which one? Maybe it can be altered to only plot points where no polygons adjoin? Commented Feb 12, 2020 at 12:24
  • layer = iface.activeLayer(); temp = QgsVectorLayer("Point?crs=epsg:27700", "result", "memory"); temp.startEditing(); attrs = layer.dataProvider().fields().toList(); temp_prov = temp.dataProvider(); temp_prov.addAttributes(attrs) temp.updateFields() for elem in layer.getFeatures(): feat = QgsFeature() geom = elem.geometry().interpolate(elem.geometry().length()/2) feat.setGeometry(geom) feat.setAttributes(elem.attributes()) temp.addFeatures([feat]) temp.updateExtents() temp.commitChanges() QgsProject.instance().addMapLayer(temp) Commented Feb 12, 2020 at 14:06
  • Ahh sorry thats a bit of a mess - I've managed to fully get what I want now with a few extra steps! Commented Feb 12, 2020 at 14:07
  • @Liz could you please add your solution as the answer? Commented Feb 12, 2020 at 15:35

2 Answers 2

1

I've worked out a way to do this - although it does not involve deleting all duplicate points (including the 'all but one' normally left by duplicate points.

Instead I have:

  • Created a buffer around the polygon layer and dissolved the result
  • Changed the buffered polygon layer to lines
  • Exploded lines
  • Ran this code:
layer = iface.activeLayer()
temp = QgsVectorLayer("Point?crs=epsg:27700", "result", "memory")
temp.startEditing()
attrs = layer.dataProvider().fields().toList()
temp_prov = temp.dataProvider()
temp_prov.addAttributes(attrs)
temp.updateFields()
for elem in layer.getFeatures():
 feat = QgsFeature()
 geom = elem.geometry().interpolate(elem.geometry().length() / 2) 
 feat.setGeometry(geom)
 feat.setAttributes(elem.attributes())
 temp.addFeatures([feat])
 temp.updateExtents()
temp.commitChanges()
QgsProject.instance().addMapLayer(temp)
  • I have then used 'join by nearest feature' to retain the attributes in my original shapefile layer. Requires a bit of tidying up as first lot of features is from the buffered file, but does what I want it to do!
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Feb 12, 2020 at 15:20
0

You have several way, I use QGis 3.5

  1. In the processing toolbox, go to vector general> delete duplicate geometries. Then select your layer and as output you will have a new "cleaned" layer.

  2. You can also use the Topology checker plugin and then you check for duplicates geometries.

In the first method, QGis will do everything automatically and in the second one, you will do it manually. You have many other method to check the topology and then delete duplicates, like for example check geometries

See below the interface of the topology checker

enter image description here Hope this help,

answered Feb 12, 2020 at 12:42
4
  • OP specificly asks how to delete all duplicate geometries, not only reduce them to one instance. Commented Feb 12, 2020 at 13:07
  • to delete he can use the first method (delete duplicate geometries) Commented Feb 12, 2020 at 13:14
  • 2
    Delete duplicate geometries deletes all but one geometry, but OP want all gone if they are duplicates. Commented Feb 12, 2020 at 13:15
  • Thanks Erik - yes I want all geometries gone, not all but one. I've managed to achieve what I wanted a different way now though! Commented Feb 12, 2020 at 15:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.