class TestGeomsLoad(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        print("\nINFO: Setting up copy Layer With different Geometries to DB validation...")
        self.qgis_utils = QGISUtils()

        # resore schemas
        restore_schema('test_distinct_geoms')
        restore_schema('test_ladm_col')
        restore_schema('test_ladm_col_3d')

        self.db_distinct_geoms = get_dbconn('test_distinct_geoms')
        result = self.db_distinct_geoms.test_connection()
        print('test_connection for: '.format('test_distinct_geoms'), result)
        if not result[1]:
            print('The test connection with  {} db is not working'.format("test_distinct_geoms"))
            return

        self.db_connection = get_dbconn('test_ladm_col')
        result = self.db_connection.test_connection()
        print('test_connection for: '.format('test_ladm_col'), result)
        if not result[1]:
            print('The test connection with  {} db is not working'.format("test_ladm_col"))
            return

        self.db_connection_3d = get_dbconn('test_ladm_col_3d')
        result = self.db_connection_3d.test_connection()
        print('test_connection for: '.format('test_ladm_col_3d'), result)
        if not result[1]:
            print('The test connection with  {} db is not working'.format("test_ladm_col_3d"))
            return

        self.gpkg_path = get_test_copy_path('geopackage/points_z_m_zm.gpkg')


    def test_valid_import_geom_2d_to_db_gpkg(self):
        print('\nINFO: Validating ETL-Model from [ Point, PointZ, PointM, PointZM ] to Point geometries...')

        # Layer in LADM for test
        test_layer = self.qgis_utils.get_layer(self.db_connection, BOUNDARY_POINT_TABLE, load=True)

        # Point To Point
        print("Validating Point to Point")
        uri = self.gpkg_path + '|layername={layername}'.format(layername='points')
        point_layer = QgsVectorLayer(uri, 'points', 'ogr')
        print("Is Valid layer :", point_layer.isValid())
        run_etl_model(point_layer, out_layer=test_layer)
        test_layer.startEditing()
        print("Info: Validating geometry Point...")
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer.type())
        self.assertEqual(QgsWkbTypes.Point, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.Point, test_layer.wkbType())
        self.assertEqual(test_layer.featureCount(), 51)
        clean_table('test_ladm_col', BOUNDARY_POINT_TABLE)
        test_layer.dataProvider().truncate()

        # PointZ To Point
        print("Validating PointZ to Point")
        uri = self.gpkg_path + '|layername={layername}'.format(layername='points_Z')
        point_layer = QgsVectorLayer(uri, 'points_Z', 'ogr')
        print("Is Valid layer :", point_layer.isValid())
        self.assertIn(point_layer.wkbType(), [QgsWkbTypes.PointZ, QgsWkbTypes.Point25D])
        output = run_etl_model(point_layer, out_layer=test_layer)
        print("Info: Validating geometry PointZ...", test_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer.type())
        self.assertEqual(QgsWkbTypes.Point, test_layer.wkbType())
        self.assertEqual(test_layer.featureCount(), 51)
        clean_table('test_ladm_col', BOUNDARY_POINT_TABLE)
        test_layer.dataProvider().truncate()

        # PointM To Point
        print("Validating PointM To Point")
        uri = self.gpkg_path + '|layername={layername}'.format(layername='points_M')
        point_layer = QgsVectorLayer(uri, 'points', 'ogr')
        print("Is Valid layer :", point_layer.isValid())
        run_etl_model(point_layer, out_layer=test_layer)
        print("Info: Validating geometry PointZ...", test_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer.type())
        self.assertEqual(QgsWkbTypes.PointM, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.Point, test_layer.wkbType())
        self.assertEqual(test_layer.featureCount(), 51)
        clean_table('test_ladm_col', BOUNDARY_POINT_TABLE)
        test_layer.dataProvider().truncate()

        # PointZM To Point
        print("Validating PointZM To Point")
        uri = self.gpkg_path + '|layername={layername}'.format(layername='points_ZM')
        point_layer = QgsVectorLayer(uri, 'points', 'ogr')
        print("Is Valid layer :", point_layer.isValid())
        run_etl_model(point_layer, out_layer=test_layer)
        print("Info: Validating geometry PointZ...", test_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer.type())
        self.assertEqual(QgsWkbTypes.PointZM, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.Point, test_layer.wkbType())
        self.assertEqual(test_layer.featureCount(), 51)
        clean_table('test_ladm_col', BOUNDARY_POINT_TABLE)
        test_layer.dataProvider().truncate()


    def test_valid_import_geom_3d_to_db_gpkg(self):
        print('\nINFO: Validating ETL-Model from [ Point, PointZ, PointM, PointZM ] to PointZ geometries...')

        test_layer_3d = self.qgis_utils.get_layer(self.db_connection_3d, BOUNDARY_POINT_TABLE, load=True)

        # Point To PointZ
        print("Validating Point to PointZ")
        uri = self.gpkg_path + '|layername={layername}'.format(layername='points')
        point_layer = QgsVectorLayer(uri, 'points', 'ogr')
        print("Is Valid layer :", point_layer.isValid())
        run_etl_model(point_layer, out_layer=test_layer_3d)
        print("Info: Validating geometry Point...")
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer_3d.type())
        self.assertEqual(QgsWkbTypes.Point, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointZ, test_layer_3d.wkbType())
        self.assertEqual(test_layer_3d.featureCount(), 51)
        test_layer_3d.startEditing()
        clean_table('test_ladm_col_3d', BOUNDARY_POINT_TABLE)
        test_layer_3d.dataProvider().truncate()

        # PointZ To PointZ
        print("Validating PointZ to PointZ")
        uri = self.gpkg_path + '|layername={layername}'.format(layername='points_Z')
        point_layer = QgsVectorLayer(uri, 'points', 'ogr')
        run_etl_model(point_layer, out_layer=test_layer_3d)
        print("Info: Validating geometry PointZ...", test_layer_3d.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer_3d.type())
        self.assertIn(point_layer.wkbType(), [QgsWkbTypes.PointZ, QgsWkbTypes.Point25D])
        self.assertEqual(QgsWkbTypes.PointZ, test_layer_3d.wkbType())
        self.assertEqual(test_layer_3d.featureCount(), 51)
        clean_table('test_ladm_col_3d', BOUNDARY_POINT_TABLE)
        test_layer_3d.dataProvider().truncate()

        # PointM To PointZ
        print("Validating PointM To PointZ")
        uri = self.gpkg_path + '|layername={layername}'.format(layername='points_M')
        point_layer = QgsVectorLayer(uri, 'points', 'ogr')
        run_etl_model(point_layer, out_layer=test_layer_3d)
        print("Info: Validating geometry PointZ...", test_layer_3d.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer_3d.type())
        self.assertEqual(QgsWkbTypes.PointM, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointZ, test_layer_3d.wkbType())
        self.assertEqual(test_layer_3d.featureCount(), 51)
        clean_table('test_ladm_col_3d', BOUNDARY_POINT_TABLE)
        test_layer_3d.dataProvider().truncate()

        # PointZM To PointZ
        print("Validating PointZM To PointZ")
        uri = self.gpkg_path + '|layername={layername}'.format(layername='points_ZM')
        point_layer = QgsVectorLayer(uri, 'points', 'ogr')
        run_etl_model(point_layer, out_layer=test_layer_3d)
        print("Info: Validating geometry PointZ...", test_layer_3d.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer_3d.type())
        self.assertEqual(QgsWkbTypes.PointZM, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointZ, test_layer_3d.wkbType())
        self.assertEqual(test_layer_3d.featureCount(), 51)
        clean_table('test_ladm_col_3d', BOUNDARY_POINT_TABLE)
        test_layer_3d.dataProvider().truncate()

    def test_valid_import_geom_2d_to_db_postgres(self):
        print('\nINFO: Validating ETL-Model from [ Point, PointZ, PointM, PointZM ] to Point (Postgres) geometries...')
        print('\nUSING POSTGRESQL SCHEMA')

        test_layer = self.qgis_utils.get_layer(self.db_connection, BOUNDARY_POINT_TABLE, load=True)

        # Point To Point
        print("Validating Point to Point")
        point_layer = self.qgis_utils.get_layer(self.db_distinct_geoms, 'points', load=True)
        print("Is Valid layer :", point_layer.isValid())
        run_etl_model(point_layer, out_layer=test_layer)
        test_layer.startEditing()
        print("Info: Validating geometry Point...")
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer.type())
        self.assertEqual(QgsWkbTypes.Point, test_layer.wkbType())
        self.assertEqual(test_layer.featureCount(), 51)

        # PointZ To Point
        print("Validating PointZ to Point")
        point_layer = self.qgis_utils.get_layer(self.db_distinct_geoms, 'points_z', load=True)
        print("Is Valid layer :", point_layer.isValid())
        self.assertIn(point_layer.wkbType(), [QgsWkbTypes.PointZ, QgsWkbTypes.Point25D])
        run_etl_model(point_layer, out_layer=test_layer)
        print("Info: Validating geometry PointZ...", test_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer.type())
        self.assertEqual(QgsWkbTypes.Point, test_layer.wkbType())
        self.assertEqual(test_layer.featureCount(), 51)

        # PointM To Point
        print("Validating PointM To Point")
        point_layer = self.qgis_utils.get_layer(self.db_distinct_geoms, 'points_m', load=True)
        print("Is Valid layer :", point_layer.isValid())
        run_etl_model(point_layer, out_layer=test_layer)
        print("Info: Validating geometry PointZ...", test_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer.type())
        self.assertEqual(QgsWkbTypes.PointM, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.Point, test_layer.wkbType())
        self.assertEqual(test_layer.featureCount(), 51)

        # PointZM To Point
        print("Validating PointZM To Point")
        point_layer = self.qgis_utils.get_layer(self.db_distinct_geoms, 'points_zm', load=True)
        print("Is Valid layer :", point_layer.isValid())
        run_etl_model(point_layer, out_layer=test_layer)
        print("Info: Validating geometry PointZ...", test_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer.type())
        self.assertEqual(QgsWkbTypes.PointZM, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.Point, test_layer.wkbType())
        self.assertEqual(test_layer.featureCount(), 51)

    def test_valid_import_geom_3d_to_db_postgres(self):
        print('\nINFO: Validating ETL-Model from [ Point, PointZ, PointM, PointZM ] to PointZ (Postgres) geometries...')

        test_layer_3d = self.qgis_utils.get_layer(self.db_connection_3d, BOUNDARY_POINT_TABLE, load=True)
        test_layer_3d.startEditing()

        # Point To PointZ
        print("Validating Point to PointZ")
        point_layer = self.qgis_utils.get_layer(self.db_distinct_geoms, 'points', load=True)
        run_etl_model(point_layer, out_layer=test_layer_3d)
        print("Info: Validating geometry Point...")
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer_3d.type())
        self.assertEqual(QgsWkbTypes.Point, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointZ, test_layer_3d.wkbType())
        self.assertEqual(test_layer_3d.featureCount(), 51)

        # PointZ To PointZ
        print("Validating PointZ to PointZ")
        point_layer = self.qgis_utils.get_layer(self.db_distinct_geoms, 'points_z', load=True)
        run_etl_model(point_layer, out_layer=test_layer_3d)
        print("Info: Validating geometry PointZ...", test_layer_3d.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer_3d.type())
        self.assertIn(point_layer.wkbType(), [QgsWkbTypes.PointZ, QgsWkbTypes.Point25D])
        self.assertEqual(QgsWkbTypes.PointZ, test_layer_3d.wkbType())
        self.assertEqual(test_layer_3d.featureCount(), 51)

        # PointM To PointZ
        print("Validating PointM To PointZ")
        point_layer = self.qgis_utils.get_layer(self.db_distinct_geoms, 'points_m', load=True)
        run_etl_model(point_layer, out_layer=test_layer_3d)
        print("Info: Validating geometry PointZ...", test_layer_3d.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer_3d.type())
        self.assertEqual(QgsWkbTypes.PointM, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointZ, test_layer_3d.wkbType())
        self.assertEqual(test_layer_3d.featureCount(), 51)

        # PointZM To PointZ
        print("Validating PointZM To PointZ")
        point_layer = self.qgis_utils.get_layer(self.db_distinct_geoms, 'points_zm', load=True)
        run_etl_model(point_layer, out_layer=test_layer_3d)
        print("Info: Validating geometry PointZ...", test_layer_3d.wkbType())
        self.assertEqual(QgsWkbTypes.PointGeometry, test_layer_3d.type())
        self.assertEqual(QgsWkbTypes.PointZM, point_layer.wkbType())
        self.assertEqual(QgsWkbTypes.PointZ, test_layer_3d.wkbType())
        self.assertEqual(test_layer_3d.featureCount(), 51)

    @classmethod
    def tearDownClass(self):
        print('tearDown test_change_geometries_in_load')
Esempio n. 2
0
class TestGetLayers(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.qgis_utils = QGISUtils()
        self.db_connection = get_dbconn('test_ladm_col')
        result = self.db_connection.test_connection()
        print('test_connection', result)
        if not result[1]:
            print('The test connection is not working')
            return
        restore_schema('test_ladm_col')

    def test_get_layer(self):
        print("\nINFO: Validating get_layer() method...")
        RELATED_TABLES = {
            'puntolindero': [
                "col_acuerdotipo", "col_defpuntotipo",
                "col_descripcionpuntotipo", "col_interpolaciontipo",
                "col_monumentaciontipo", "la_puntotipo", "puntolindero"
            ],
            'terreno': [
                "la_contenidoniveltipo", "la_dimensiontipo",
                "la_estructuratipo", "la_nivel", "la_registrotipo",
                "la_relacionsuperficietipo", "terreno"
            ]
        }

        self.qgis_utils.cache_layers_and_relations(
            self.db_connection,
            ladm_col_db=True)  # Gather information from the database
        QgsProject.instance().clear()

        print("\nINFO: Validating get_layer() on empty project...")
        # This test loads puntolindero and terreno tables, checks layers in layer tree after this
        # and finishes with a comparison between loaded layers and expected layers.
        for layer in [BOUNDARY_POINT_TABLE, PLOT_TABLE]:
            loaded_table = self.qgis_utils.get_layer(self.db_connection,
                                                     layer,
                                                     load=True)
            self.assertEquals(loaded_table.name(), layer)
            loaded_layers_tree_names = [
                layer.name()
                for layer in self.qgis_utils.get_ladm_layers_from_layer_tree(
                    self.db_connection)
            ]
            for layer_related in RELATED_TABLES[layer]:
                print("Check if {} exists in loaded layers {}".format(
                    layer_related, loaded_layers_tree_names))
                self.assertIn(layer_related, loaded_layers_tree_names)
            QgsProject.instance().clear()

        print(
            "\nINFO: Validating get_layer() when the project contains some of the related tables..."
        )

        print("First for {} layer".format(BOUNDARY_POINT_TABLE))

        for pre_load in ["col_acuerdotipo",
                         "col_monumentaciontipo"]:  # preload some layers
            self.qgis_utils.get_layer(self.db_connection, pre_load, load=True)

        self.qgis_utils.get_layer(self.db_connection,
                                  BOUNDARY_POINT_TABLE,
                                  load=True)

        # check number if element in Layer Tree and needed element are the same.
        loaded_layers_tree_names = len(RELATED_TABLES[BOUNDARY_POINT_TABLE])
        layer_tree_elements = len([
            layer.name() for layer in
            self.qgis_utils.get_ladm_layers_from_layer_tree(self.db_connection)
        ])
        self.assertEqual(
            loaded_layers_tree_names, layer_tree_elements,
            "Number of loaded layers when loading PuntoLindero is not what we expect..."
        )

        # Load again preloaded layer to check not duplicate layers in load
        for pre_load in ["col_acuerdotipo", "col_monumentaciontipo"]:
            self.qgis_utils.get_layer(self.db_connection, pre_load, load=True)
        layer_tree_elements = len([
            layer.name() for layer in
            self.qgis_utils.get_ladm_layers_from_layer_tree(self.db_connection)
        ])
        self.assertEqual(loaded_layers_tree_names, layer_tree_elements,
                         "Duplicate layers found... This is an error!!!")
        QgsProject.instance().clear()

        print("Then for {} layer".format(PLOT_TABLE))
        for pre_load in ["la_nivel",
                         "la_relacionsuperficietipo"]:  # preload some layers
            self.qgis_utils.get_layer(self.db_connection, pre_load, load=True)

        self.qgis_utils.get_layer(self.db_connection,
                                  PLOT_TABLE,
                                  geometry_type=QgsWkbTypes.PolygonGeometry,
                                  load=True)

        # check number if element in Layer Tree and needed element are the same.
        loaded_layers_tree_names = len(RELATED_TABLES[PLOT_TABLE])
        layer_tree_elements = len([
            layer.name() for layer in
            self.qgis_utils.get_ladm_layers_from_layer_tree(self.db_connection)
        ])
        self.assertEqual(
            loaded_layers_tree_names, layer_tree_elements,
            "Number of loaded layers when loading Terreno is not what we expect..."
        )

        # Check duplicate layers...
        for pre_load in ["la_nivel", "la_relacionsuperficietipo"]:
            self.qgis_utils.get_layer(self.db_connection, pre_load, load=True)
        layer_tree_elements = len([
            layer.name() for layer in
            self.qgis_utils.get_ladm_layers_from_layer_tree(self.db_connection)
        ])
        self.assertEqual(loaded_layers_tree_names, layer_tree_elements,
                         "Duplicate layers found... This is an error!!!")
        QgsProject.instance().clear()

        print("\nINFO: Validating when loaded layers have the same name...")
        # Load terreno without geometry parameter load point and polygon layer with different geometries (10 layers)
        self.qgis_utils.get_layer(self.db_connection, PLOT_TABLE, load=True)
        toc_layers = [
            l for l in self.qgis_utils.get_ladm_layers_from_layer_tree(
                self.db_connection)
        ]
        toc_names = [l.name() for l in toc_layers]
        same_name_layers = [
            layer for layer in toc_layers if toc_names.count(layer.name()) > 1
        ]
        for layer_1, layer_2 in itertools.combinations(same_name_layers, 2):
            if layer_1.name() == layer_2.name():
                print("Testing {} ({}) against {} ({})".format(
                    layer_1.name(), layer_1.geometryType(), layer_2.name(),
                    layer_2.geometryType()))
                self.assertNotEqual(
                    layer_1.geometryType(), layer_2.geometryType(),
                    "Function get_layer loads layers with same name and geometry... This is an error!!!"
                )