Exemplo n.º 1
0
  def loadtofield(self):
    layersmap=QgsMapLayerRegistry.instance().mapLayers()
    layerslist=[]
    curLayer = self.iface.mapCanvas().currentLayer()
    if (curLayer == None):
      infoString = "No layers selected"
      QMessageBox.information(self.iface.mainWindow(),"Warning",infoString)
      return
    if (curLayer.type() != curLayer.VectorLayer):
      infoString = "Not a vector layer"
      QMessageBox.information(self.iface.mainWindow(),"Warning",infoString)
      return
    fProvider = curLayer.dataProvider()
    myFields = fProvider.fields()
    allFieldsNames= [f.name() for f in myFields]
    myFieldsNames=[]
    for f in myFields:
       if f.typeName() == "String":
          myFieldsNames.append(f.name())
    if len(myFieldsNames) == 0:
       QMessageBox.information(self.iface.mainWindow(),"Warning","No string field names. Exiting")
       return
    elif len(myFieldsNames) == 1:
       attrfieldname = myFieldsNames[0]
    else:
      res = dlgSelField(myFieldsNames)
      if res.exec_():
        attrfieldname=res.selectedAttr()
      else:
        return
    attrindex = allFieldsNames.index(attrfieldname)
    attrfield = myFields[attrindex]
    aloadfile = QFileDialog.getOpenFileName(None, "Open file dialog","","Text (*.txt)")
    fileHandle = open(aloadfile, 'r')
    #QMessageBox.information(self.iface.mainWindow(),"Warning",str(curLayer.isEditable()))

    curLayer.startEditing()
    for f in curLayer.getFeatures():
       astr = unicode(fileHandle.next(),'utf-8')
       f[attrfieldname] = astr.strip()
       curLayer.updateFeature(f)

       #bbul = curLayer.commitChanges()
       #QMessageBox.information(self.iface.mainWindow(),"Warning",str(fid))
       #QMessageBox.information(self.iface.mainWindow(),"Warning",str(bbul))
    curLayer.commitChanges()
    fileHandle.close()
Exemplo n.º 2
0
 def dumpfield(self):
   curLayer = self.iface.mapCanvas().currentLayer()
   if (curLayer == None):
     infoString = "No layers selected"
     QMessageBox.information(self.iface.mainWindow(),"Warning",infoString)
     return
   if (curLayer.type() != curLayer.VectorLayer):
     infoString = "Not a vector layer"
     QMessageBox.information(self.iface.mainWindow(),"Warning",infoString)
     return
   featids=curLayer.selectedFeaturesIds()
   if (len(featids) == 0):
     infoString = "No features selected, using all " + str(curLayer.featureCount()) + " features"
     QMessageBox.information(self.iface.mainWindow(),"Warning",infoString)
     featids = range(curLayer.featureCount())
   fProvider = curLayer.dataProvider()
   myFields = fProvider.fields()
   allFieldsNames= [f.name() for f in myFields]
   myFieldsNames=[]
   for f in myFields:
      if f.typeName() == "String":
         myFieldsNames.append(f.name())
   if len(myFieldsNames) == 0:
      QMessageBox.information(self.iface.mainWindow(),"Warning","No string field names. Exiting")
      return
   elif len(myFieldsNames) == 1:
      attrfield = myFieldsNames[0]
   else:
     res = dlgSelField(myFieldsNames)
     if res.exec_():
       attrfield=res.selectedAttr()
     else:
       return
   attrindex = allFieldsNames.index(attrfield)
   adumpfile = QFileDialog.getSaveFileName(None, "save file dialog", attrfield +'.txt', "Text (*.txt)")
   fileHandle = open(adumpfile, 'w')
   
   for f in curLayer.getFeatures():
      attr=f[attrfield]
      fileHandle.write(attr.encode('utf-8') +'\n')
   fileHandle.close()