I have built a plugin which does essentially this equal sized polygons along line Main parts of the code are from user lejedi76

I have used this plugin for months now and it worked as expected. Turning from QGIS 3.4.1 to 3.4.1 I get the following error:

Null geometry cannot be converted to a point

I know what the error means, but I do not know why it even shows up. I also could not find the critical changes in the QGIS version that caused this problem. It seems I am overlooking something very simple, but what? The code provided works without any problems in the QGIS 3.4.1 Python console:

width = 142.365
height = 79.842
overlap = 0.2
gridPath = r'C://qgistesting/testgrid.shp'

#to test take a line-layer with one feature only
layer = iface.activeLayer()
for feature in layer.selectedFeatures():
    geom = feature.geometry()

    pages = QgsVectorLayer("Polygon?crs=epsg:"+str(25833), layer.name(), "memory")

    fid = QgsField("fid", QVariant.Int, "int")
    attributes = [fid]
    pagesProvider = pages.dataProvider()
    curs = 0
    numpages = geom.length()/(width)
    step = 1.0/numpages
    stepnudge = (1.0-overlap) * step
    pageFeatures = []
    r = 1
    while curs <= 1:
        startpoint =  geom.interpolate(curs*geom.length())
        endpoint = geom.interpolate((curs+step)*geom.length())
        ##### Null geometry cannot be converted to a point ######
        x_start = startpoint.asPoint().x()
        y_start = startpoint.asPoint().y()
        x_end = endpoint.asPoint().x()
        y_end = endpoint.asPoint().y()
        currline = QgsGeometry().fromWkt('LINESTRING({} {}, {} {})'.format(x_start, y_start, x_end, y_end))
        currpoly = QgsGeometry().fromWkt(
            'POLYGON((0 0, 0 {height},{width} {height}, {width} 0, 0 0))'.format(height=height, width=width))
        azimuth = startpoint.asPoint().azimuth(endpoint.asPoint())

        currpoly.translate(x_start, y_start)
        page = currpoly
        curs = curs + stepnudge
        feat = QgsFeature()
        r = r + 1


    QgsVectorFileWriter.writeAsVectorFormat(pages, gridPath, "utf-8", pages.crs(), "ESRI Shapefile")
    layer_loaded = iface.addVectorLayer(gridPath, "Grid", "ogr")

And here comes the lazy answer for my problem after the problem was identified by ndawson (lazy answer kills the last polygon, there will be a better answer)

while curs <= 1:
    curs = curs + stepnudge
    if (curs+step) <=1:
        startpoint =  geom.interpolate(curs*geom.length())
        endpoint = geom.interpolate((curs+step)*geom.length())
        x_start = startpoint.asPoint().x()
        y_start = startpoint.asPoint().y()
        x_end = endpoint.asPoint().x()
        y_end = endpoint.asPoint().y()

If x_start = startpoint.asPoint().x() is triggering the null geometry warning, then it implies that startPoint is itself a null geometry. The only way this is possible is if QgsGeometry.interpolate returns a null geometry.

From the interpolate documentation:

"If the original geometry is a point type, a null geometry will be returned.... If the input is a NULL geometry, the output will also be a NULL geometry.". It also seems that calling interpolate with too large a distance for the geometry returns a null geometry.

So there's three options:

  1. One (or more) of the input features has a null geometry
  2. One (or more) of the input features has a point geometry
  3. (curs+step) is > 1

The behavior of asPoint() did change in 3.4 -- previously if the geometry could not be converted to a point (e.g., it was null), then a point with coordinates 0,0 was returned instead. This silently lead to many errors or unexpected results, so the explicit exceptions are now thrown instead.

|improve this answer|||||
  • Thx for the quick answer! The winner is --> Number 3;-) I always check the changelog, but could not find what happened after 3.4.1. I postet the lazy answer for my problem below the question. – AndreasK Jan 15 '19 at 7:52
  • No problem -- it's good to see that the exception is working as intended here, and helping identify and flag tricky issues like this. – ndawson Jan 15 '19 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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