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!

4
  • "then ran a code" - nice, and which one? Maybe it can be altered to only plot points where no polygons adjoin?
    – Erik
    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)
    – Liz
    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!
    – Liz
    Feb 12, 2020 at 14:07
  • @Liz could you please add your solution as the answer? 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!
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,

4
  • OP specificly asks how to delete all duplicate geometries, not only reduce them to one instance.
    – Erik
    Feb 12, 2020 at 13:07
  • to delete he can use the first method (delete duplicate geometries) Feb 12, 2020 at 13:14
  • 2
    Delete duplicate geometries deletes all but one geometry, but OP want all gone if they are duplicates.
    – Erik
    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!
    – Liz
    Feb 12, 2020 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.