Skip to content

Commit

Permalink
Processing - Support no geometry in VectorWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-morvan committed May 17, 2016
1 parent a5ef216 commit 63d23d2
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions python/plugins/processing/tools/vector.py
Expand Up @@ -47,6 +47,7 @@


GEOM_TYPE_MAP = {
QGis.WKBNoGeometry: 'none',
QGis.WKBPoint: 'Point',
QGis.WKBLineString: 'LineString',
QGis.WKBPolygon: 'Polygon',
Expand Down Expand Up @@ -535,6 +536,13 @@ class VectorWriter:
POSTGIS_LAYER_PREFIX = 'postgis:'
SPATIALITE_LAYER_PREFIX = 'spatialite:'

nogeometry_extensions = [
u'csv',
u'dbf',
u'ods',
u'xlsx',
]

def __init__(self, destination, encoding, fields, geometryType,
crs, options=None):
self.destination = destination
Expand Down Expand Up @@ -592,9 +600,10 @@ def _runSQL(sql):
for f in fields)

_runSQL("CREATE TABLE %s.%s (%s)" % (uri.schema(), uri.table().lower(), fieldsdesc))
_runSQL("SELECT AddGeometryColumn('{schema}', '{table}', 'the_geom', {srid}, '{typmod}', 2)".format(
table=uri.table().lower(), schema=uri.schema(), srid=crs.authid().split(":")[-1],
typmod=GEOM_TYPE_MAP[geometryType].upper()))
if geometryType != QGis.WKBNoGeometry:
_runSQL("SELECT AddGeometryColumn('{schema}', '{table}', 'the_geom', {srid}, '{typmod}', 2)".format(
table=uri.table().lower(), schema=uri.schema(), srid=crs.authid().split(":")[-1],
typmod=GEOM_TYPE_MAP[geometryType].upper()))

self.layer = QgsVectorLayer(uri.uri(), uri.table(), "postgres")
self.writer = self.layer.dataProvider()
Expand Down Expand Up @@ -622,9 +631,10 @@ def _runSQL(sql):

_runSQL("DROP TABLE IF EXISTS %s" % uri.table().lower())
_runSQL("CREATE TABLE %s (%s)" % (uri.table().lower(), fieldsdesc))
_runSQL("SELECT AddGeometryColumn('{table}', 'the_geom', {srid}, '{typmod}', 2)".format(
table=uri.table().lower(), srid=crs.authid().split(":")[-1],
typmod=GEOM_TYPE_MAP[geometryType].upper()))
if geometryType != QGis.WKBNoGeometry:
_runSQL("SELECT AddGeometryColumn('{table}', 'the_geom', {srid}, '{typmod}', 2)".format(
table=uri.table().lower(), srid=crs.authid().split(":")[-1],
typmod=GEOM_TYPE_MAP[geometryType].upper()))

self.layer = QgsVectorLayer(uri.uri(), uri.table(), "spatialite")
self.writer = self.layer.dataProvider()
Expand All @@ -636,12 +646,22 @@ def _runSQL(sql):
extension = extension[extension.find('*.') + 2:]
extension = extension[:extension.find(' ')]
OGRCodes[extension] = value
OGRCodes['dbf'] = "DBF file"

extension = self.destination[self.destination.rfind('.') + 1:]

if extension not in OGRCodes:
extension = 'shp'
self.destination = self.destination + '.shp'

if geometryType == QGis.WKBNoGeometry:
if extension == 'shp':
extension = 'dbf'
self.destination = self.destination[:self.destination.rfind('.')] + '.dbf'
if extension not in self.nogeometry_extensions:
raise GeoAlgorithmExecutionException(
"Unsupported format for tables with no geometry")

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

0 comments on commit 63d23d2

Please sign in to comment.