Skip to content

Commit

Permalink
[processing] allow definition of vector fields with plain names and t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
volaya committed Aug 14, 2014
1 parent 9b8f2ba commit dab9638
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
3 changes: 2 additions & 1 deletion python/plugins/processing/core/GeoAlgorithm.py
Expand Up @@ -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.
Expand Down
45 changes: 30 additions & 15 deletions python/plugins/processing/tools/vector.py
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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])
Expand All @@ -410,7 +425,7 @@ def addFeature(self, feature):
self.writer.addFeatures([feature])
else:
self.writer.addFeature(feature)

import csv
import codecs
import cStringIO
Expand Down Expand Up @@ -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.