mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
[processing] allow definition of vector fields with plain names and types
This commit is contained in:
parent
9b8f2ba912
commit
dab96382e6
@ -109,7 +109,8 @@ class GeoAlgorithm:
|
||||
"""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 @@ class VectorWriter:
|
||||
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 @@ class VectorWriter:
|
||||
|
||||
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 @@ class VectorWriter:
|
||||
self.writer.addFeatures([feature])
|
||||
else:
|
||||
self.writer.addFeature(feature)
|
||||
|
||||
|
||||
import csv
|
||||
import codecs
|
||||
import cStringIO
|
||||
@ -465,4 +480,4 @@ class UnicodeWriter:
|
||||
|
||||
def writerows(self, rows):
|
||||
for row in rows:
|
||||
self.writerow(row)
|
||||
self.writerow(row)
|
||||
|
Loading…
x
Reference in New Issue
Block a user