Skip to main content
added 20 characters in body; edited tags; edited title
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

Programmatically adding layers with Python script, layers Layers corrupted after savesaving, closeclosing and openopening of QGIS project with PyQGIS

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.

Programmatically adding layers with Python script, layers corrupted after save, close and open of project

Using a python script, I am creating a layer from a CSV file and then writing it out to a gpkg file. I then create a group in the layer tree, load in the gpkg file and add it as a child of the group. This all works fine. 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)

As I say, this works fine, until I save/close/reopen project and then it only shows as the layer title without any geometry. I can add the gpkg file again manually as there is nothing wrong with the produced file. After I have added the layer via the script, I have also tried manually checking that the source is correct, which it is. And even used apply on 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.

Layers corrupted after saving, closing and opening of QGIS project with PyQGIS

Using a Python script, I create a layer from a CSV file and then write it out to a gpkg file.

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 all works fine.

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)

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 can add the gpkg file again manually as there is nothing wrong with the produced file. After I had added the layer via the script, I also tried manually checking that the source was correct, which it was. And even applied 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.

edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Using a python script I, I am creating a layer from a CSV file and then writing it out to a gpkg file. I then create a group in the layer tree, load in the gpkg file and add it as a child of the group. This all works fine. 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)

As I say, this works fine, until I save/close/reopen project and then it only shows as the layer title without any geometry. I can add the gpkg file again manually as there is nothing wrong with the produced file. After I have added the layer via the script, I have also tried manually checking that the source is correct, which it is. And even used apply on 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.

Thanks in advance

Using a python script I am creating a layer from a CSV file and then writing it out to a gpkg file. I then create a group in the layer tree, load in the gpkg file and add it as a child of the group. This all works fine. 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)

As I say, this works fine, until I save/close/reopen project and then it only shows as the layer title without any geometry. I can add the gpkg file again manually as there is nothing wrong with the produced file. After I have added the layer via the script, I have also tried manually checking that the source is correct, which it is. And even used apply on 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.

Thanks in advance

Using a python script, I am creating a layer from a CSV file and then writing it out to a gpkg file. I then create a group in the layer tree, load in the gpkg file and add it as a child of the group. This all works fine. 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)

As I say, this works fine, until I save/close/reopen project and then it only shows as the layer title without any geometry. I can add the gpkg file again manually as there is nothing wrong with the produced file. After I have added the layer via the script, I have also tried manually checking that the source is correct, which it is. And even used apply on 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.

Source Link

Programmatically adding layers with Python script, layers corrupted after save, close and open of project

Using a python script I am creating a layer from a CSV file and then writing it out to a gpkg file. I then create a group in the layer tree, load in the gpkg file and add it as a child of the group. This all works fine. 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)

As I say, this works fine, until I save/close/reopen project and then it only shows as the layer title without any geometry. I can add the gpkg file again manually as there is nothing wrong with the produced file. After I have added the layer via the script, I have also tried manually checking that the source is correct, which it is. And even used apply on 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.

Thanks in advance