Skip to content

Commit dab9638

Browse files
committed
[processing] allow definition of vector fields with plain names and types
1 parent 9b8f2ba commit dab9638

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

python/plugins/processing/core/GeoAlgorithm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def help(self):
109109
"""Returns the help with the description of this algorithm.
110110
It returns a tuple boolean, string. IF the boolean value is true, it means that
111111
the string contains the actual description. If false, it is an url or path to a file
112-
where the description is stored.
112+
where the description is stored. In both cases, the string or the content of the file
113+
have to be HTML, ready to be set into the help display component.
113114
114115
Returns None if there is no help file available.
115116

python/plugins/processing/tools/vector.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -343,23 +343,38 @@ def checkMinDistance(point, index, distance, points):
343343

344344
return True
345345

346-
347-
348346
from PyQt4.QtCore import *
349347
from qgis.core import *
350348

349+
GEOM_TYPE_MAP = {
350+
QGis.WKBPoint: 'Point',
351+
QGis.WKBLineString: 'LineString',
352+
QGis.WKBPolygon: 'Polygon',
353+
QGis.WKBMultiPoint: 'MultiPoint',
354+
QGis.WKBMultiLineString: 'MultiLineString',
355+
QGis.WKBMultiPolygon: 'MultiPolygon',
356+
}
357+
358+
TYPE_MAP = {
359+
str : QVariant.String,
360+
float: QVariant.Double,
361+
int: QVariant.Int
362+
}
363+
364+
def _fieldName(self, f):
365+
if isinstance(f, basestring):
366+
return f
367+
return f.name()
368+
369+
def _toQgsField(self, f):
370+
if isinstance(f, QgsField):
371+
return f
372+
return QgsField(f[0], TYPE_MAP.get(f[1], QVariant.String))
373+
351374
class VectorWriter:
352375

353376
MEMORY_LAYER_PREFIX = 'memory:'
354377

355-
TYPE_MAP = {
356-
QGis.WKBPoint: 'Point',
357-
QGis.WKBLineString: 'LineString',
358-
QGis.WKBPolygon: 'Polygon',
359-
QGis.WKBMultiPoint: 'MultiPoint',
360-
QGis.WKBMultiLineString: 'MultiLineString',
361-
QGis.WKBMultiPolygon: 'MultiPolygon',
362-
}
363378

364379
def __init__(self, fileName, encoding, fields, geometryType,
365380
crs, options=None):
@@ -375,10 +390,10 @@ def __init__(self, fileName, encoding, fields, geometryType,
375390
if self.fileName.startswith(self.MEMORY_LAYER_PREFIX):
376391
self.isMemory = True
377392

378-
uri = self.TYPE_MAP[geometryType]
393+
uri = self.GEOM_TYPE_MAP[geometryType]
379394
if crs.isValid():
380395
uri += '?crs=' + crs.authid() + '&'
381-
fieldsdesc = ['field=' + str(f.name()) for f in fields]
396+
fieldsdesc = ['field=' + _fieldName(f) for f in fields]
382397

383398
fieldsstring = '&'.join(fieldsdesc)
384399
uri += fieldsstring
@@ -400,7 +415,7 @@ def __init__(self, fileName, encoding, fields, geometryType,
400415

401416
qgsfields = QgsFields()
402417
for field in fields:
403-
qgsfields.append(field)
418+
qgsfields.append(_toQgsField(field))
404419

405420
self.writer = QgsVectorFileWriter(self.fileName, encoding,
406421
qgsfields, geometryType, crs, OGRCodes[extension])
@@ -410,7 +425,7 @@ def addFeature(self, feature):
410425
self.writer.addFeatures([feature])
411426
else:
412427
self.writer.addFeature(feature)
413-
428+
414429
import csv
415430
import codecs
416431
import cStringIO
@@ -465,4 +480,4 @@ def writerow(self, row):
465480

466481
def writerows(self, rows):
467482
for row in rows:
468-
self.writerow(row)
483+
self.writerow(row)

0 commit comments

Comments
 (0)