Skip to content
Permalink
Browse files

[processing] allow definition of vector fields with plain names and t…

…ypes
  • Loading branch information
volaya committed Aug 3, 2014
1 parent 9b8f2ba commit dab96382e65c015817d5620816ab203b2cdf02a9
Showing with 32 additions and 16 deletions.
  1. +2 −1 python/plugins/processing/core/GeoAlgorithm.py
  2. +30 −15 python/plugins/processing/tools/vector.py
@@ -109,7 +109,8 @@ def help(self):
"""Returns the help with the description of this algorithm.
It returns a tuple boolean, string. IF the boolean value is true, it means that
the string contains the actual description. If false, it is an url or path to a file
where the description is stored.
where the description is stored. In both cases, the string or the content of the file
have to be HTML, ready to be set into the help display component.
Returns None if there is no help file available.
@@ -343,23 +343,38 @@ def checkMinDistance(point, index, distance, points):

return True



from PyQt4.QtCore import *
from qgis.core import *

GEOM_TYPE_MAP = {
QGis.WKBPoint: 'Point',
QGis.WKBLineString: 'LineString',
QGis.WKBPolygon: 'Polygon',
QGis.WKBMultiPoint: 'MultiPoint',
QGis.WKBMultiLineString: 'MultiLineString',
QGis.WKBMultiPolygon: 'MultiPolygon',
}

TYPE_MAP = {
str : QVariant.String,
float: QVariant.Double,
int: QVariant.Int
}

def _fieldName(self, f):
if isinstance(f, basestring):
return f
return f.name()

def _toQgsField(self, f):
if isinstance(f, QgsField):
return f
return QgsField(f[0], TYPE_MAP.get(f[1], QVariant.String))

class VectorWriter:

MEMORY_LAYER_PREFIX = 'memory:'

TYPE_MAP = {
QGis.WKBPoint: 'Point',
QGis.WKBLineString: 'LineString',
QGis.WKBPolygon: 'Polygon',
QGis.WKBMultiPoint: 'MultiPoint',
QGis.WKBMultiLineString: 'MultiLineString',
QGis.WKBMultiPolygon: 'MultiPolygon',
}

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

uri = self.TYPE_MAP[geometryType]
uri = self.GEOM_TYPE_MAP[geometryType]
if crs.isValid():
uri += '?crs=' + crs.authid() + '&'
fieldsdesc = ['field=' + str(f.name()) for f in fields]
fieldsdesc = ['field=' + _fieldName(f) for f in fields]

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

qgsfields = QgsFields()
for field in fields:
qgsfields.append(field)
qgsfields.append(_toQgsField(field))

self.writer = QgsVectorFileWriter(self.fileName, encoding,
qgsfields, geometryType, crs, OGRCodes[extension])
@@ -410,7 +425,7 @@ def addFeature(self, feature):
self.writer.addFeatures([feature])
else:
self.writer.addFeature(feature)

import csv
import codecs
import cStringIO
@@ -465,4 +480,4 @@ def writerow(self, row):

def writerows(self, rows):
for row in rows:
self.writerow(row)
self.writerow(row)

0 comments on commit dab9638

Please sign in to comment.
You can’t perform that action at this time.