1

I'm trying to write a Python script (QGis 3.4.9) to create a group of scratch-layers (polygon, line and point). When it is inside the function the layer is created as it should, but when the function is done the geometry (and anything else it seems) is gone. The layer is there but no geometry. I'm clearly missing something?

def scratch_group():
 isthere = False
 sgroupname = "Scratch_group"
 root = QgsProject.instance().layerTreeRoot()
 for child in root.children():
 if isinstance(child, QgsLayerTreeGroup):
 if child.name().upper() == sgroupname.upper():
 isthere = True
 print("- group: " + child.name() + " already there")
if not isthere:
 root = QgsProject.instance().layerTreeRoot()
 sgroup = root.addGroup(sgroupname)
 stiep = "point"
 slayerp= QgsVectorLayer(stiep, "Scratch_point", "memory")
 sgroup.insertChildNode(0, QgsLayerTreeLayer(slayerp))
 slayerp.startEditing()
QMessageBox.information(iface.mainWindow(),"Check","Geometry",QMessageBox.Ok)
return()
#---main--- 
scratch_group() 
QMessageBox.information(iface.mainWindow(),"Check","If Geometry is gone",QMessageBox.Ok)

EDIT: In the meantime i stumbled on Adding layer to group in layers panel using PyQGIS? You have to create a clone of the layer put it in place and remove the original. That partially solved it:

def create_memlayer_ingroup(stiep, sname, sgroup):
 vlayer = QgsVectorLayer(stiep, sname,"memory")
 QgsProject.instance().addMapLayer(vlayer)
 root = QgsProject.instance().layerTreeRoot()
 ilayer = root.findLayer(vlayer.id())
 clone = ilayer.clone()
 sgroup.insertChildNode(0, clone)
 print(ilayer)
 root.removeChildNode(ilayer)
def create_sgroup(gname):
 rt = QgsProject.instance().layerTreeRoot()
 return(rt.addGroup(gname))
memgroup = create_sgroup("Scratch_group2")
create_memlayer_ingroup("Polygon", "Scratch_Polygon", memgroup)
create_memlayer_ingroup("Linestring", "Scratch_line", memgroup)
create_memlayer_ingroup("Point", "Scratch_point", memgroup)

This works when the group is not placed at the top of the tree. When it is at top (if no other layer is opened), the second and third memlayer is opened twice within the group. So not perfect yet...

asked Aug 12, 2019 at 9:37

2 Answers 2

2

Finally I found the answer in Add layer to a QGIS group using Python from Germán Carrillo (many thanks). Thankfully you don't have to clone etc your layers which results in strange behaviour.

def create_slayer(tiep, lname, sgroup):
 memlay = QgsVectorLayer(tiep,lname, "memory")
 QgsProject.instance().addMapLayer(memlay, False) #False was the key
 sgroup.addLayer(memlay) 
def create_sgroup(gpos,gname):
 isthere = False #check if group already exists
 rt = QgsProject.instance().layerTreeRoot()
 for child in rt.children():
 if isinstance(child, QgsLayerTreeGroup): 
 if child.name().upper() == gname.upper(): 
 isthere = True
 if not isthere: #if group not exists, create one
 memgroup = rt.insertGroup(gpos, gname)
 create_slayer("Point","Scratch_point",memgroup)
 create_slayer("Linestring","Scratch_line",memgroup)
 create_slayer("Polygon","Scratch_polygon",memgroup)
#---Main-----
create_sgroup(0,"Scratch_group")
answered Aug 13, 2019 at 14:46
0

There's a workaround to add a layer in a group. You need to add to the layer tree and then move it in your chosen group.

answered Aug 13, 2019 at 6:23
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.