def test_load_layer_from_uri(self): """Test we can load a layer with different parameters.""" # Without provider path = standard_data_path( 'gisv4', 'aggregation', 'small_grid.geojson') layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') layer, purpose = load_layer(path, 'foo') self.assertEqual(layer.name(), 'foo') # With internal URI internal_uri = full_layer_uri(layer) self.assertTrue( internal_uri.endswith('small_grid.geojson|qgis_provider=ogr'), internal_uri ) layer, purpose = load_layer(full_layer_uri(layer)) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') # path plus extra layer parameter path = standard_data_path( 'gisv4', 'aggregation', 'small_grid.geojson') path += '|layerid=0' layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') # CSV path = standard_data_path( 'gisv4', 'impacts', 'exposure_summary_table.csv') layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'exposure_summary_table') self.assertEqual(purpose, 'undefined') self.assertEqual(len(layer.fields()), 4) # QLR # This QLR contains a file based reference layer to # small_grid.geojson, and it should work the same way # as if we load small_grid.geojson # In practice we could put the non file-based layer (PostGIS/WFS) # and load it from QLR path = standard_data_path( 'gisv4', 'aggregation', 'small_grid.qlr') layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation')
def ordered_expected_layers(self): """Get an ordered list of layers according to users input. From top to bottom in the legend: [ ('FromCanvas', layer name, full layer URI, QML), ('FromAnalysis', layer purpose, layer group, None), ... ] The full layer URI is coming from our helper. :return: An ordered list of layers following a structure. :rtype: list """ registry = QgsProject.instance() layers = [] count = self.list_layers_in_map_report.count() for i in range(count): layer = self.list_layers_in_map_report.item(i) origin = layer.data(LAYER_ORIGIN_ROLE) if origin == FROM_ANALYSIS['key']: key = layer.data(LAYER_PURPOSE_KEY_OR_ID_ROLE) parent = layer.data(LAYER_PARENT_ANALYSIS_ROLE) layers.append((FROM_ANALYSIS['key'], key, parent, None)) else: layer_id = layer.data(LAYER_PURPOSE_KEY_OR_ID_ROLE) layer = registry.mapLayer(layer_id) style_document = QDomDocument() layer.exportNamedStyle(style_document) layers.append( (FROM_CANVAS['key'], layer.name(), full_layer_uri(layer), style_document.toString())) return layers
def test_load_layer_from_uri(self): """Test we can load a layer with different parameters.""" # Without provider path = standard_data_path( 'gisv4', 'aggregation', 'small_grid.geojson') layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') layer, purpose = load_layer(path, 'foo') self.assertEqual(layer.name(), 'foo') # With internal URI internal_uri = full_layer_uri(layer) self.assertTrue( internal_uri.endswith('small_grid.geojson|qgis_provider=ogr'), internal_uri ) layer, purpose = load_layer(full_layer_uri(layer)) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') # path plus extra layer parameter path = standard_data_path( 'gisv4', 'aggregation', 'small_grid.geojson') path += '|layerid=0' layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') # CSV path = standard_data_path( 'gisv4', 'impacts', 'exposure_summary_table.csv') layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'exposure_summary_table') self.assertEqual(purpose, 'undefined') self.assertEqual(len(layer.fields()), 4)
def test_load_layer_from_uri(self): """Test we can load a layer with different parameters.""" # Without provider path = standard_data_path('gisv4', 'aggregation', 'small_grid.geojson') layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') layer, purpose = load_layer(path, 'foo') self.assertEqual(layer.name(), 'foo') # With internal URI internal_uri = full_layer_uri(layer) self.assertTrue( internal_uri.endswith('small_grid.geojson|qgis_provider=ogr'), internal_uri) layer, purpose = load_layer(full_layer_uri(layer)) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') # path plus extra layer parameter path = standard_data_path('gisv4', 'aggregation', 'small_grid.geojson') path += '|layerid=0' layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'small_grid') self.assertEqual(purpose, 'aggregation') # CSV path = standard_data_path('gisv4', 'impacts', 'exposure_summary_table.csv') layer, purpose = load_layer(path) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'exposure_summary_table') self.assertEqual(purpose, 'undefined') self.assertEqual(len(layer.fields()), 4)
def test_load_layer_from_uri_with_postgis(self): """Test we can load a layer with different parameters in POSTGIS.""" uri = ('dbname=\'stdm\' ' 'host=localhost ' 'port=5433 ' 'user=\'etienne\' ' 'sslmode=disable ' 'key=\'id\' ' 'srid=4326 ' 'type=MultiPolygon ' 'table="public"."buildings" (geom) sql=') layer, purpose = load_layer(uri) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'buildings') self.assertEqual(purpose, 'undefined') expected = '"public"."buildings" (geom) sql=|qgis_provider=postgres' internal_uri = full_layer_uri(layer) self.assertTrue(internal_uri.endswith(expected), internal_uri)
def test_load_layer_from_uri_with_postgis(self): """Test we can load a layer with different parameters in POSTGIS.""" uri = ( 'dbname=\'stdm\' ' 'host=localhost ' 'port=5433 ' 'user=\'etienne\' ' 'sslmode=disable ' 'key=\'id\' ' 'srid=4326 ' 'type=MultiPolygon ' 'table="public"."buildings" (geom) sql=' ) layer, purpose = load_layer(uri) self.assertTrue(layer.isValid()) self.assertEqual(layer.name(), 'buildings') self.assertEqual(purpose, 'undefined') expected = '"public"."buildings" (geom) sql=|qgis_provider=postgres' internal_uri = full_layer_uri(layer) self.assertTrue( internal_uri.endswith(expected), internal_uri)