TransWikia.com

Delaunay triangulation returning empty geometry in PyQGIS

Geographic Information Systems Asked by Nardjis H on May 31, 2021

I would like to compute the Delaunay Triangulation for polylines. But sometimes it works and sometimes it does not, I tried with hand drawn lines.

lines=QgsProject.instance().mapLayersByName("lines")[0]
for line in lines.getFeatures():
    print(line.geometry().delaunayTriangulation(0.01, False).asMultiPolygon())

I tried a cross product collinearity test but I did not have a product equal to 0 and I still have empty geometry
enter image description here

from PyQt5 import *
from math import *
def modulo(a,b):
    if b!=0:
        return a-(int(a/b)*b)
    else:
        return -1
lines=QgsProject.instance().mapLayersByName("lines")[0]

for line in lines.getFeatures():
     n = len(list(line.geometry().vertices()))
     delaunay = line.geometry().delaunayTriangulation(0.01, False)
     i = str(line.id())
     if n <= 2:
         print("Line ID: " + i + " - not enough vertices for Delaunay triangulation")
     elif delaunay.isEmpty():
         print("Line ID: " + i + " - possible collinear vertices")
     else:
         print(delaunay.asGeometryCollection())

m=len(line.geometry().asPolyline())
pts=line.geometry().asPolyline()
for i in range(0,m):
    i1=modulo((i+1),m)
    i2=modulo((i+2),m)
    x1=pts[i1][0]-pts[i][0]
    y1=pts[i1][1]-pts[i][1]
    x2=pts[i2][0]-pts[i1][0]
    y2=pts[i2][1]-pts[i1][1]
    print(x1*y2-y1*x2)#if collinear then vector product=0
    #print(x1*x2+y1*y2)#scalar product

One Answer

If a line has only two vertices or all vertices are collinear, delaunayTriangulation returns an empty geometry.

I don't know how to control collinearity, but you can check how many vertices a line has.

lines=QgsProject.instance().mapLayersByName("lines")[0]

for line in lines.getFeatures():
    n = len(list(line.geometry().vertices()))
    delaunay = line.geometry().delaunayTriangulation(0.01, False)
    i = str(line.id())
    if n <= 2:
        print("Line ID: " + i + " - not enough vertices for Delaunay triangulation")
    elif delaunay.isEmpty():
        print("Line ID: " + i + " - possible collinear vertices")
    else:
        print(delaunay.asGeometryCollection())

For example, delaunayTriangulation returns empty/null geometry for "Line 1" and "Line 2" in the image below. Because "Line 1" has only two vertices, and the vertices of "Line 2" are collinear.

enter image description here

Answered by Kadir Şahbaz on May 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP