Using a pythonPython script, I am creatingcreate a layer from a CSV file and then writingwrite it out to a gpkg file. I
I then create a group in the layer tree, load it in the gpkg file and add it as a child of the group. This This all works fine. If
If I then save my project, close the project and then open the project again, then the gpkg layer in the group just appears as a title, there is no geometry associated with the layer any more.
So basically it is this in the script
uri = f"file:///{csv_path}?delimiter=,&wktField=geometry&crs=EPSG:4326"
wkt_layer = QgsVectorLayer(uri, layer_name, 'delimitedtext')
# Save to GeoPackage
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "GPKG"
options.layerName = layer_name
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile
error = QgsVectorFileWriter.writeAsVectorFormatV3(wkt_layer, gpkg_path, QgsCoordinateTransformContext(), options)
group = root.insertGroup(0, name)
wkt_layer = QgsVectorLayer(wkt_layer_path, f"{csv['name']} Route", "ogr")
group.addLayer(wkt_layer)
uri = f"file:///{csv_path}?delimiter=,&wktField=geometry&crs=EPSG:4326"
wkt_layer = QgsVectorLayer(uri, layer_name, 'delimitedtext')
# Save to GeoPackage
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "GPKG"
options.layerName = layer_name
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile
error = QgsVectorFileWriter.writeAsVectorFormatV3(wkt_layer, gpkg_path, QgsCoordinateTransformContext(), options)
group = root.insertGroup(0, name)
wkt_layer = QgsVectorLayer(wkt_layer_path, f"{csv['name']} Route", "ogr")
group.addLayer(wkt_layer)
As I say, this works fine, until I save/close/reopen the project and then it only shows as the layer title without any geometry. I
I can add the gpkg file again manually as there is nothing wrong with the produced file. After After I havehad added the layer via the script, I have also tried manually checking that the source iswas correct, which it iswas. And even used apply onapplied to the properties. But this doesn't help fix the issue.
I feel like I am missing a step in telling QGIS about this new layer I have added to it.