4

I wrote a Python init code for a form. It creates folder and subfolder if the value of a field is "Si".

# -*- coding: utf-8 -*-
import os
from qgis.core import *
from qgis.utils import *

from qgis.PyQt.QtWidgets import QWidget

def crea_cartelle(layer, idx, path):
  layer = iface.activeLayer()
  idx = layer.fieldNameIndex('codice_opera') 
  folder = layer.fieldNameIndex('crea_directory')
  path = "\\\\xx.xx.x.xx\\dati_gis\\archivio_opere_tv\\"
  for feat in layer.getFeatures():
    attrs = feat.attributes()
    if attrs[folder]== "Si":
      if not os.path.exists(path + attrs[idx]):

        os.makedirs(path + attrs[idx]+os.sep+ attrs[idx]+"_documenti")
        os.makedirs(path + attrs[idx]+os.sep+ attrs[idx]+"_foto")

Well after creating folders I would like adding a code that inserts the hotlink values (es. path + attrs[idx]+os.sep+ attrs[idx]+"_foto") in fields "link_foto" an "link_document"

I have tried to add this code:

        layer = iface.activeLayer() 
        feats = layer.getFeatures()
        field_index = layer.dataProvider().fieldNameIndex('link_foto')
        layer.startEditing()
        l_foto = "some text"
        layer.changeAttributeValue(feat.id(), field_index, l_foto)

layer.commitChanges()

But It returns an error for layer that doesn't recognize. Any suggestions is welcome.

1 Answer 1

3

The following should hopefully work:

from qgis.utils import iface
import os

def crea_cartelle(dialog, layer, feature):
    layer = iface.activeLayer()
    idx = layer.fieldNameIndex('codice_opera') 
    folder = layer.fieldNameIndex('crea_directory')
    path = "D:/prova/"
    provider = layer.dataProvider()
    updateMap = {}
    fieldIdx = provider.fields().indexFromName( 'link_foto')
    features = provider.getFeatures()
    for feature in features:
        attrs = feature.attributes()
        cod=str(attrs[idx])
        if attrs[folder]== "Si":
            if not os.path.exists(path + cod):
                os.makedirs(path+cod+"/"+cod+"_documenti")
                os.makedirs(path+cod+"/"+cod+"_foto")
            updateMap[feature.id()] = { fieldIdx: path+cod+"/"+cod+'_foto'}
            provider.changeAttributeValues( updateMap )

Then type crea_cartelle as the Function name:

Python init method

I replaced os.sep with just a forward slash as I prefer using these but you can change it back if you want.


Tested on QGIS 2.18.2.

11
  • thanks Joseph. Using your code when the form appears the layer became off (not editing) and it's impossible to insert values. Removing " layer.commitChanges()" it doesn't insert "link_foto" value. Commented Jun 13, 2017 at 12:21
  • @DanielePiccolo - Thanks, yes couldn't test it yesterday but I have now and included a (hopefully) working solution :)
    – Joseph
    Commented Jun 14, 2017 at 10:15
  • But I also need the other code (create folder) too. Commented Jun 14, 2017 at 10:29
  • If I only use it, it doesn't work Commented Jun 14, 2017 at 10:38
  • 1
    Great!!! I have closed and reopened QGIS and now it works fine. Thank you very much. Commented Jun 14, 2017 at 11:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.