def testCreateEmptyLayer(self): # cleanup (it seems overwrite option doesn't clean the sdo_geom_metadata table) self.execSQLCommand('DROP TABLE "QGIS"."EMPTY_LAYER"', ignore_errors=True) self.execSQLCommand("DELETE FROM user_sdo_geom_metadata where TABLE_NAME='EMPTY_LAYER'", ignore_errors=True) uri = self.dbconn + "srid=4326 type=POINT table=\"EMPTY_LAYER\" (GEOM)" exporter = QgsVectorLayerExporter(uri=uri, provider='oracle', fields=QgsFields(), geometryType=QgsWkbTypes.Point, crs=QgsCoordinateReferenceSystem(4326), overwrite=True) self.assertEqual(exporter.errorCount(), 0) self.assertEqual(exporter.errorCode(), 0) # check IF there is an empty table (will throw error if the EMPTY_LAYER table does not exist) self.execSQLCommand('SELECT count(*) FROM "QGIS"."EMPTY_LAYER"') # check that metadata table has been correctly populated query = QSqlQuery(self.conn) self.assertTrue(query.exec_("SELECT column_name, srid FROM user_sdo_geom_metadata WHERE table_name = 'EMPTY_LAYER'")) self.assertTrue(query.next()) self.assertEqual(query.value(0), "GEOM") self.assertEqual(query.value(1), 4326) query.finish() vl = QgsVectorLayer( self.dbconn + ' sslmode=disable table="QGIS"."EMPTY_LAYER" sql=', 'test', 'oracle') self.assertTrue(vl.isValid())
def publishLayerToCatalogUsingPostgis(connection, catalog, layer, fields): pk = "id" geom = "geom" providerName = "postgres" uri = QgsDataSourceURI() uri.setConnection(connection.host, str(connection.port), connection.dbname, connection.user, connection.passwd) uri.setDataSource(connection.schema, layer.name(), geom, "", pk) options = {} options['overwrite'] = True if singleGeom: options['forceSinglePartGeometryType'] = True exporter = QgsVectorLayerExporter(uri.uri(), providerName, fields, layer.geometryType(), layer.crs(), True, options) for feature in layer.getFeatures(): exporter.addFeature(feature) exporter.flushBuffer() if exporter.errorCount(): raise Exception(exporter.errorMessage()) zipfile = getCompatibleSldAsZip(layer) catalog.publish_vector_layer_from_postgis(connection.host, str(connection.port), connection.dbname, connection.schema, layer.name(), connection.user, connection.passwd, layer.crs().authid(), layer.name(), zipfile, layer.name())
def testCreateEmptyLayer(self): # cleanup (it seems overwrite option doesn't clean the sdo_geom_metadata table) self.execSQLCommand('DROP TABLE "QGIS"."EMPTY_LAYER"', ignore_errors=True) self.execSQLCommand("DELETE FROM user_sdo_geom_metadata where TABLE_NAME='EMPTY_LAYER'", ignore_errors=True) uri = self.dbconn + "srid=4326 type=POINT table=\"EMPTY_LAYER\" (GEOM)" exporter = QgsVectorLayerExporter(uri=uri, provider='oracle', fields=QgsFields(), geometryType=QgsWkbTypes.Point, crs=QgsCoordinateReferenceSystem(4326), overwrite=True) self.assertEqual(exporter.errorCount(), 0) self.assertEqual(exporter.errorCode(), 0) # check IF there is an empty table (will throw error if the EMPTY_LAYER table does not exist) self.execSQLCommand('SELECT count(*) FROM "QGIS"."EMPTY_LAYER"') # check that metadata table has been correctly populated query = QSqlQuery(self.conn) self.assertTrue(query.exec_("SELECT column_name, srid FROM user_sdo_geom_metadata WHERE table_name = 'EMPTY_LAYER'")) self.assertTrue(query.next()) self.assertEqual(query.value(0), "GEOM") self.assertEqual(query.value(1), 4326) query.finish() # no feature, so we cannot guess the geometry type, so the layer is not valid # but srid is set for provider in case you want to add a feature even if the layer is invalid! # layer sourceCrs is empty because the layer is not considered spatial (not know geometry type) vl = QgsVectorLayer(self.dbconn + ' sslmode=disable table="QGIS"."EMPTY_LAYER" (GEOM) sql=', 'test', 'oracle') self.assertFalse(vl.isValid()) self.assertEqual(vl.dataProvider().sourceCrs().authid(), "EPSG:4326") # so we set the geometry type vl = QgsVectorLayer(self.dbconn + ' sslmode=disable type=POINT table="QGIS"."EMPTY_LAYER" (GEOM) sql=', 'test', 'oracle') self.assertTrue(vl.isValid()) self.assertEqual(vl.sourceCrs().authid(), "EPSG:4326") f = QgsFeature(vl.fields()) f.setGeometry(QgsGeometry.fromWkt('POINT (43.5 1.42)')) vl.dataProvider().addFeatures([f]) query = QSqlQuery(self.conn) self.assertTrue(query.exec_('SELECT "l"."GEOM"."SDO_SRID" from "QGIS"."EMPTY_LAYER" "l"')) self.assertTrue(query.next()) self.assertEqual(query.value(0), 4326) query.finish() # now we can autodetect geom type and srid vl = QgsVectorLayer(self.dbconn + ' sslmode=disable table="QGIS"."EMPTY_LAYER" (GEOM) sql=', 'test', 'oracle') self.assertTrue(vl.isValid()) self.assertEqual(vl.sourceCrs().authid(), "EPSG:4326")
def testCreateAspatialLayer(self): """ Test creation of a non-spatial layer """ # cleanup (it seems overwrite option doesn't clean the sdo_geom_metadata table) self.execSQLCommand('DROP TABLE "QGIS"."ASPATIAL_LAYER"', ignore_errors=True) fields = QgsFields() fields.append(QgsField("INTEGER_T", QVariant.Int)) uri = self.dbconn + "table=\"ASPATIAL_LAYER\"" exporter = QgsVectorLayerExporter(uri=uri, provider='oracle', fields=fields, geometryType=QgsWkbTypes.NoGeometry, crs=QgsCoordinateReferenceSystem(), overwrite=True) self.assertEqual(exporter.errorCount(), 0) self.assertEqual(exporter.errorCode(), 0) self.execSQLCommand('SELECT count(*) FROM "QGIS"."ASPATIAL_LAYER"') vl = QgsVectorLayer(self.dbconn + ' sslmode=disable table="QGIS"."ASPATIAL_LAYER" sql=', 'test', 'oracle') self.assertTrue(vl.isValid()) self.assertEqual(vl.fields().names(), ["INTEGER_T"])