def __init__(self, fileName, encoding, fields): """ @param fields a list of QgsField """ TableWriter.__init__(self, fileName, encoding, [f.name() for f in fields] ) if len(fields) != 0: with open(self.fileName[:-4]+'.csvt', 'wb') as csvtFile: writer = UnicodeWriter(csvtFile, encoding=self.encoding) writer.writerow([f.typeName() for f in fields])
def __init__(self, fileName, encoding, fields): """ @param fields a list of QgsField """ TableWriter.__init__(self, fileName, encoding, [f.name() for f in fields]) if len(fields) != 0: with open(self.fileName[:-4] + '.csvt', 'wb') as csvtFile: writer = UnicodeWriter(csvtFile, encoding=self.encoding) writer.writerow([f.typeName() for f in fields])
def getTableWriter(self, fields): """Returns a suitable writer to which records can be added as a result of the algorithm. Use this to transparently handle output values instead of creating your own method. @param fields a list of QgsField @return writer instance of the table writer class """ if self.encoding is None: settings = QSettings() self.encoding = settings.value('/Processing/encoding', 'System') return TableWriter(self.value, self.encoding, fields)
geometry = feature.GetGeometryRef() x = geometry.GetX() y = geometry.GetY() if Transform_vector_to_raster_CRS: pnt = coordTransform.TransformPoint(x, y, 0) x = pnt[0] y = pnt[1] (rX, rY) = raster.mapToPixel(x, y, geoTransform) if rX > rasterXSize or rY > rasterYSize: feature = layer.GetNextFeature() continue value = data[rY, rX] col.append(value) feature = layer.GetNextFeature() rasterBand = None columns.append(col) raster = None vector.Destroy() writer = TableWriter(Output_table, 'utf-8', []) row = [] for i in xrange(len(columns[0])): for col in columns: row.append(col[i]) writer.addRecord(row) row[:] = []
from qgis.core import * from PyQt4.QtCore import * from processing.core.TableWriter import TableWriter from collections import defaultdict layer = processing.getObject(Input) inputFields = layer.pendingFields() fieldIdxs = [] fields = Fields.split(',') for f in fields: idx = inputFields.indexFromName(f) if idx == -1: raise GeoAlgorithmExecutionException('Field not found:' + f) fieldIdxs.append(idx) writer = TableWriter(Output, None, fields + ['FREQ']) counts = {} feats = processing.features(layer) nFeats = len(feats) counts = defaultdict(int) for i, feat in enumerate(feats): progress.setPercentage(int(100 * i / nFeats)) attrs = feat.attributes() clazz = tuple([attrs[idx] for idx in fieldIdxs]) counts[clazz] += 1 for c in counts: writer.addRecord(list(c) + [counts[c]])
fields.append(QgsField(createUniqueFieldName('MIN_P', fields), QVariant.Double)) outFeat = QgsFeature() features = processing.features(polygon_layer) n = len(features) # get all unique raster values values = [] for stats in frequency_analysis.itervalues(): freq = stats['freq'] values = values + [ v for (v, f) in freq.iteritems()] values = sorted(list(set(values))) print values layer_writer = VectorWriter(Frequency_analysis_layer, None, fields, provider.geometryType(), polygon_layer.crs()) table_writer = TableWriter(Frequency_analysis_table, None, [id_field, 'majority'] + ['value: %d' % v for v in values]) for i, feat in enumerate(features): progress.setPercentage(int(100 * i / n)) fid = feat.id() stats = frequency_analysis[fid] count = stats['count'] freq = stats['freq'] if count == 0: majority = 0 minority = 0 majority_p = 0 minority_p = 0 else: minority, majority = freq_min_max(freq)
QGis, QgsFeature, QgsField) from processing.tools import vector from processing.core.TableWriter import TableWriter from processing.core.VectorWriter import VectorWriter from processing.core.GeoAlgorithmExecutionException import * vlayer = processing.getObject(layer) vprovider = vlayer.dataProvider() fieldindex = vlayer.fieldNameIndex(fieldname) fields = vprovider.fields() fields.append(QgsField('NUM_FIELD', QVariant.Int)) layer_writer = VectorWriter(Equivalent_numerical_field_layer, None, fields, vprovider.geometryType(), vlayer.crs()) table_writer = TableWriter(Equivalent_numerical_field_table, None, [fieldname, 'num']) outFeat = QgsFeature() inGeom = QgsGeometry() nElement = 0 classes = {} features = vector.features(vlayer) nFeat = len(features) for feature in features: progress.setPercentage(int(100 * nElement / nFeat)) nElement += 1 inGeom = feature.geometry() outFeat.setGeometry(inGeom) atMap = feature.attributes() clazz = atMap[fieldindex] if clazz not in classes:
from qgis.core import * from PyQt4.QtCore import * from processing.core.TableWriter import TableWriter from collections import defaultdict layer = processing.getObject(input) inputFields = layer.pendingFields() fieldIdxs = [] fields = fields.split(',') for f in fields: idx = inputFields.indexFromName(f) if idx == -1: raise GeoAlgorithmExecutionException('Field not found:' + f) fieldIdxs.append(idx) writer = TableWriter(output, None, fields + ['FREQ']) counts = {} feats = processing.features(layer) nFeats = len(feats) counts = defaultdict(int) for i, feat in enumerate(feats): progress.setPercentage(int(100 * i / nFeats)) attrs = feat.attributes() clazz = tuple([attrs[idx] for idx in fieldIdxs]) print clazz counts[clazz] += 1 for c in counts: writer.addRecord(list(c) + [counts[c]])