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()
layer.selectAll()
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]
pages.startEditing()
pagesProvider = pages.dataProvider()
pagesProvider.addAttributes(attributes)
curs = 0
numpages = geom.length()/(width)
step = 1.0/numpages
stepnudge = (1.0-overlap) * step
print(3)
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))
currpoly.translate(0,-height/2)
azimuth = startpoint.asPoint().azimuth(endpoint.asPoint())
currpoly.translate(x_start, y_start)
currpoly.asPolygon()
page = currpoly
curs = curs + stepnudge
feat = QgsFeature()
feat.setAttributes([r])
feat.setGeometry(page)
pageFeatures.append(feat)
r = r + 1
pagesProvider.addFeatures(pageFeatures)
pages.commitChanges()
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()