Skip to content

Commit

Permalink
Merge pull request #30173 from olivierdalang/dbmanager_fix_generic_geom
Browse files Browse the repository at this point in the history
[dbmanager] allow to import GEOMETRY tables
  • Loading branch information
m-kuhn committed Jun 12, 2019
2 parents 4ff8429 + 86f8bdb commit 83c8b5b
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -31,7 +31,8 @@
Qgis,
QgsApplication,
QgsSettings,
QgsMapLayerType
QgsMapLayerType,
QgsWkbTypes
)
from ..db_plugins import createDbPlugin

Expand Down Expand Up @@ -522,8 +523,28 @@ def rasterTablesFactory(self, row, db, schema=None):
def tables(self, schema=None, sys_tables=False):
tables = self.connector.getTables(schema.name if schema else None, sys_tables)
if tables is not None:
tables = [self.tablesFactory(x, self, schema) for x in tables]
return tables
ret = []
for t in tables:
table = self.tablesFactory(t, self, schema)
ret.append(table)

# Similarly to what to browser does, if the geom type is generic geometry,
# we additionnly add three copies of the layer to allow importing
if isinstance(table, VectorTable):
if table.geomType == 'GEOMETRY':
point_table = self.tablesFactory(t, self, schema)
point_table.geomType = 'POINT'
ret.append(point_table)

line_table = self.tablesFactory(t, self, schema)
line_table.geomType = 'LINESTRING'
ret.append(line_table)

poly_table = self.tablesFactory(t, self, schema)
poly_table.geomType = 'POLYGON'
ret.append(poly_table)

return ret

def createTable(self, table, fields, schema=None):
field_defs = [x.definition() for x in fields]
Expand Down Expand Up @@ -692,6 +713,13 @@ def uri(self):
geomCol = self.geomColumn if self.type in [Table.VectorType, Table.RasterType] else ""
uniqueCol = self.getValidQgisUniqueFields(True) if self.isView else None
uri.setDataSource(schema, self.name, geomCol if geomCol else None, None, uniqueCol.name if uniqueCol else "")
uri.setSrid(str(self.srid))
for f in self.fields():
if f.primaryKey:
uri.setKeyColumn(f.name)
break
uri.setWkbType(QgsWkbTypes.parseType(self.geomType))

return uri

def mimeUri(self):
Expand Down

0 comments on commit 83c8b5b

Please sign in to comment.