The goal of my python script is to get geographic data from an XML file and transform it into a vector layer. This task is successful but each of my layers have the same name... and I can't find a way to rename these layers with a field of my attribute table.
This is my code:
def espaces(code):
node1 =root.find('EspaceS')
node2 =root.find('PartieS')
node3 =root.find('ServiceS')
node4 =root.find('VolumeS')
node5 =root.find('FrequenceS')
for item in node1:
if get_text(item,'TypeEspace')==code:
layer = QgsVectorLayer('MultiPolygon?&field=PK:integer(3)&field=NOM:string&field=Horaire:string&field=PLAFOND:string&field=PLANCHER:string&field=CLASSE:string&field=FREQ:string&crs=epsg:2154',code,"memory")
feat = QgsFeature(layer.fields())
feat['PK']=get_attrib(item,'pk')
for item2 in node2:
feat['NOM']=item2.find('Espace').attrib['lk'][5:-1]+get_text(item2,'NomPartie')
if item2.find('Espace').attrib['pk']==get_attrib(item,'pk'):
for item3 in node3:
if item3.find('Espace') is not None:
if item3.find('Espace').attrib['pk']==item2.find('Espace').attrib['pk']:
for item5 in node5:
if item5.find('Service').attrib['pk']==item3.attrib['pk']:
feat['FREQ']=get_text(item5,'Frequence')
break
break
for item4 in node4:
if item4.find('Partie').attrib['pk']==item2.attrib['pk']:
feat['Horaire']=get_text(item4,'HorCode')
feat['CLASSE']=get_text(item4,'Classe')
feat['PLAFOND']=get_plafond(item4)
feat['PLANCHER']=get_plancher(item4)
break
GEO=[]
GEO=get_text(item2,'Geometrie').split(chr(10))
PTS=[]
for i in range(0,len(GEO)-1):
PTS.append(Coords(GEO[i]))
feat.setGeometry(QgsGeometry.fromPolygonXY([PTS]))
layer.dataProvider().addFeatures([feat])
layer.name
layer.setOpacity(0.5)
QgsProject.instance().addMapLayer(layer)
#CTL espaces('CTL')
I would like to put the field 'Nom' as the name of each of my layers
Can someone tell me how to do this ?
layer.setName(feat["NOM"])
?