def testGetFieldNames(self): originalNames = ['ID', 'NAME'] field0 = self._mockField(originalNames[0]) field1 = self._mockField(originalNames[1]) fields = [field0, field1] layer = Mock() layer.dataProvider().fields.return_value = fields fieldNames = utils.fieldNames(layer) assert fieldNames == originalNames
def getXyColumns(self, layer): dlg = XyFieldsDialog(self.XY_COMBO_MSG, utils.fieldNames(layer), layer.name()) xyOK = False while not xyOK: dlg.show() if dlg.exec_() ==0: break else: # user hit OK, check valid x and y column index? idx = dlg.getXindex() idy = dlg.getYindex() if idx >= 0 and idy >= 0 and not (idx == idy): self.layerInfo[layer] = XyTable(layer, self.canvas) self.layerInfo[layer].setXYColumnIndex(idx, idy) xyOK = True else: QMessageBox.warning(self.iface.mainWindow(), self.MSG_BOX_TITLE, \ ("Please select the columns to use for x and y \n (cannot be the same column)"), \ QMessageBox.Ok, QMessageBox.Ok) return xyOK
def excelSave(self): if self.layer == None: if self.iface.activeLayer(): self.currentLayerChanged(self.iface.activeLayer()) else: QMessageBox.warning(self.iface.mainWindow(), "No active layer", "Please make an vector layer active before saving it to excel file.") return fieldNames = utils.fieldNames(self.layer) dlg = FieldChooserDialog(fieldNames) names = [] while len(names) == 0: dlg.show() if dlg.exec_() == 0: return names = dlg.getSelectedFields() if len(names) == 0: QMessageBox.warning(self.iface.mainWindow(), "No fields selected", "Please select at least one field.") dirPath = self.settings.value("/xytools/excelSavePath", ".", type=str) (filename, filter) = QFileDialog.getSaveFileNameAndFilter(self.iface.mainWindow(), "Please save excel file as...", dirPath, "Excel files (*.xls)", "Filter list for selecting files from a dialog box") fn, fileExtension = path.splitext(unicode(filename)) if len(fn) == 0: # user choose cancel return self.settings.setValue("/xytools/excelSavePath", QFileInfo(filename).absolutePath()) if fileExtension != '.xls': filename = filename + '.xls' try: from providers import excel except: QMessageBox.warning(self.iface.mainWindow(), "Unable to load Python module", "There is a problem with loading a python module which is needed to read/write Excel files. Please see documentation/help how to install python xlw and xlrd libraries.") return xlw = excel.Writer(filename) self.layer = self.iface.activeLayer() selection = None if self.layer.selectedFeatureCount() > 0: if QMessageBox.question(self.iface.mainWindow(), self.MSG_BOX_TITLE, ("You have a selection in this layer. Only export this selection?\n" "Click Yes to export selection only, click No to export all rows."), QMessageBox.No, QMessageBox.Yes) == QMessageBox.Yes: selection = self.layer.selectedFeaturesIds() feature = QgsFeature(); xlw.writeAttributeRow(0, names) rowNr = 1 if QGis.QGIS_VERSION_INT < 10900: prov = self.layer.dataProvider() prov.select(prov.attributeIndexes()) while prov.nextFeature(feature): # attribute values, either for all or only for selection if selection == None or feature.id() in selection: values = feature.attributeMap().values() rowValues = [] for field in names: rowValues.append(values[field]) xlw.writeAttributeRow(rowNr, values) rowNr += 1 else: prov = self.layer.getFeatures() while prov.nextFeature(feature): # attribute values, either for all or only for selection if selection == None or feature.id() in selection: values = [] for field in names: values.append(feature.attribute(field)) xlw.writeAttributeRow(rowNr, values) rowNr += 1 xlw.saveFile() QMessageBox.information(self.iface.mainWindow(), "Success", "Successfully saved as xls file")