I develop a plugin for my project which includes some useful tools. I reviewed some answers in GIS SE about generating skeleton of polygon. I couldn't find any solution which used PyQGIS. HCMGIS plugin has a tool "Skeleton / Medail Axes" but it uses only processing tools.
I want to generate the skeleton of a polygon using pure PyQGIS without using Processing tools. After doing some digging I found
voronoiDiagram method defined in
QgsGeometry. I don't figure it out how to use it correctly.
voronoiDiagram in the following script, but it throws an exception:
AttributeError: 'list' object has no attribute 'disjoint'.
What am I doing wrong? How can I improve/change this code to get skeleton of polygon?
My code is:
# get polygon layer polygonLyr = QgsProject.instance().mapLayersByName("polygons") # create new layer centerlineLyr = QgsVectorLayer("Linestring?crs=EPSG:3857", "Centerlines", "memory") QgsProject.instance().addMapLayer(centerlineLyr) # layer has one polygon polygonGeom = list(polygonLyr.getFeatures()).geometry() # create voronoi lines lineCollection = polygonGeom.voronoiDiagram(edgesOnly=True).asMultiPolyline() #select lines and add them to layer centerlineLyr.startEditing() for line in lineCollection: if not line.disjoint(polygonGeom): f = QgsFeature() f.setGeometry(line) centerlineLyr.addFeature(f) centerlineLyr.commitChanges()