Exemple #1
0
 def set_ready_only_field(self, read_only=True):
     if self._layers[self.EDITING_LAYER_NAME][LAYER] is not None:
         for field in self.wizard_config[WIZARD_READ_ONLY_FIELDS]:
             # Not validate field that are read only
             QGISUtils.set_read_only_field(
                 self._layers[self.EDITING_LAYER_NAME][LAYER], field,
                 read_only)
Exemple #2
0
    def setUpClass(self):
        print("\nINFO: Setting up copy layer With different Geometries to DB validation...")
        self.qgis_utils = QGISUtils()

        # resore schemas
        restore_schema(SCHEMA_DISTINCT_GEOMS)
        restore_schema(SCHEMA_LADM_COL)
        restore_schema(SCHEMA_LADM_COL_3D)

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

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

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

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

        clean_table(SCHEMA_LADM_COL, BOUNDARY_POINT_TABLE)
        clean_table(SCHEMA_LADM_COL_3D, BOUNDARY_POINT_TABLE)
    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')
Exemple #4
0
 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 setUpClass(self):
     print("\nINFO: Setting up copy CSV points to DB validation...")
     self.qgis_utils = QGISUtils()
     self.db_connection = get_dbconn('test_ladm_col')
     self.db_connection_3d = get_dbconn('test_ladm_col_3d')
     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')
     restore_schema('test_ladm_col_3d')
    def setUpClass(self):

        restore_schema('test_ladm_col_queries')

        self.db_connection = get_dbconn('test_ladm_col_queries')
        result = self.db_connection.test_connection()
        print('test_connection', result)

        if not result[1]:
            print('The test connection is not working')
            return

        self.qgis_utils = QGISUtils()
        self.ladm_data = LADM_DATA(self.qgis_utils)
 def setUpClass(self):
     Processing.initialize()
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     print("\nINFO: Setting up copy CSV points to DB validation...")
     self.qgis_utils = QGISUtils()
     self.db_connection = get_dbconn('test_ladm_col')
     self.db_connection_3d = get_dbconn('test_ladm_col_3d')
     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')
     restore_schema('test_ladm_col_3d')
 def setUpClass(cls):
     print(
         "\nINFO: Setting up copy layer With different Geometries to DB validation..."
     )
     print("INFO: Restoring databases to be used")
     cls.qgis_utils = QGISUtils()
     cls.base_test_path = tempfile.mkdtemp()
     import_qgis_model_baker()
Exemple #9
0
    def setUpClass(cls):
        import_qgis_model_baker()
        cls.db_gpkg_empty = get_copy_gpkg_conn('test_empty_ladm_gpkg')
        cls.db_gpkg_test = get_copy_gpkg_conn('test_export_data_qpkg')
        cls.qgis_utils = QGISUtils()

        result_empty = cls.db_gpkg_empty.test_connection()
        result_test = cls.db_gpkg_test.test_connection()
        cls.assertTrue(result_empty[0],
                       'The test connection is not working for empty db')
        cls.assertTrue(result_test[0],
                       'The test connection is not working for test data db')
Exemple #10
0
    def setUpClass(cls):
        print(
            "\nINFO: Setting up copy layer With different Geometries to DB validation..."
        )

        import_qgis_model_baker()

        # restore schemas
        print("INFO: Restoring databases to be used")
        restore_schema(SCHEMA_DISTINCT_GEOMS)
        restore_schema(SCHEMA_LADM_COL_EMPTY)
        cls.gpkg_path = get_test_copy_path(GPKG_PATH_DISTINCT_GEOMS)

        cls.db_distinct_geoms = get_pg_conn(SCHEMA_DISTINCT_GEOMS)
        cls.db_pg = get_pg_conn(SCHEMA_LADM_COL_EMPTY)

        cls.qgis_utils = QGISUtils()
Exemple #11
0
    def setUpClass(cls):
        print("\nINFO: Setting up copy CSV points to DB validation...")
        print("INFO: Restoring databases to be used")
        import_qgis_model_baker()
        cls.qgis_utils = QGISUtils()
        restore_schema(SCHEMA_LADM_COL_EMPTY)
        cls.db_pg = get_pg_conn(SCHEMA_LADM_COL_EMPTY)

        cls.names = cls.db_pg.names
        import_asistente_ladm_col()
        cls.ladm_data = LADM_DATA(cls.qgis_utils)
        cls.geometry = GeometryUtils()

        result = cls.db_pg.test_connection()
        cls.assertTrue(result[0],
                       'The test connection is not working for empty db')
        cls.assertIsNotNone(cls.names.OP_BOUNDARY_POINT_T, 'Names is None')
    def download_java_dependency(self, uri):
        self.logger.clear_message_bar()

        if not self._downloading:  # Already downloading report dependency?
            if QGISUtils.is_connected(TEST_SERVER):
                self._downloading = True
                fetcher_task = QgsNetworkContentFetcherTask(QUrl(uri))
                fetcher_task.begun.connect(self.task_begun)
                fetcher_task.progressChanged.connect(
                    self.task_progress_changed)
                fetcher_task.fetched.connect(
                    functools.partial(self.save_java_dependency_file,
                                      fetcher_task))
                fetcher_task.taskCompleted.connect(self.task_completed)
                QgsApplication.taskManager().addTask(fetcher_task)
            else:
                self.logger.warning_msg(
                    __name__,
                    QCoreApplication.translate(
                        "JavaUtils",
                        "There was a problem connecting to Internet."))
                self._downloading = False
class TestCopy(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        print("\nINFO: Setting up copy CSV points to DB validation...")
        self.qgis_utils = QGISUtils()
        self.db_connection = get_dbconn('test_ladm_col')
        self.db_connection_3d = get_dbconn('test_ladm_col_3d')
        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')
        restore_schema('test_ladm_col_3d')

    def test_copy_csv_to_db(self):
        print("\nINFO: Validating copy CSV points to DB...")
        clean_table('test_ladm_col', BOUNDARY_POINT_TABLE)
        self.qgis_utils.disable_automatic_fields(self.db_connection, BOUNDARY_POINT_TABLE)
        self.upload_points_from_csv()
        self.validate_points_in_db()
        clean_table('test_ladm_col', BOUNDARY_POINT_TABLE)

    def upload_points_from_csv(self):
        print("Copying CSV data with no elevation...")
        csv_path = get_test_path('csv/puntos_fixed.csv')
        txt_delimiter = ';'
        cbo_longitude = 'x'
        cbo_latitude = 'y'
        res = self.qgis_utils.copy_csv_to_db(csv_path,
                                    txt_delimiter,
                                    cbo_longitude,
                                    cbo_latitude,
                                    self.db_connection,
                                    DEFAULT_EPSG,
                                    BOUNDARY_POINT_TABLE)
        self.assertEqual(res, True)

    def test_upload_points_from_csv_crs_wgs84(self):
        print("\nINFO: Copying CSV data with EPSG:4326...")
        clean_table('test_ladm_col', BOUNDARY_POINT_TABLE)
        self.qgis_utils.disable_automatic_fields(self.db_connection, BOUNDARY_POINT_TABLE)
        self.upload_points_from_csv_crs_wgs84()
        self.validate_points_in_db_from_wgs84()
        clean_table('test_ladm_col', BOUNDARY_POINT_TABLE)

    def upload_points_from_csv_crs_wgs84(self):
        print("Copying CSV data in WGS84...")
        csv_path = get_test_path('csv/puntos_crs_4326_wgs84.csv')
        txt_delimiter = ';'
        cbo_longitude = 'x'
        cbo_latitude = 'y'
        epsg =  '4326'

        res = self.qgis_utils.copy_csv_to_db(csv_path,
                                    txt_delimiter,
                                    cbo_longitude,
                                    cbo_latitude,
                                    self.db_connection,
                                    epsg,
                                    BOUNDARY_POINT_TABLE)

        self.assertEqual(res, True)

    def validate_points_in_db_from_wgs84(self):
        cur = self.db_connection.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
        print('\nINFO: Validating points')
        query = cur.execute("""SELECT st_x(localizacion_original), st_y(localizacion_original) FROM test_ladm_col.puntolindero;""")
        results = cur.fetchall()
        self.assertEqual(len(results), 3)
        self.assertEqual(results, [[963052.433292674, 1077370.54548811], [963056.711416816, 1077286.71929338], [963056.670399835, 1077210.30465577]])

    def test_copy_csv_with_z_to_db(self):
        print("\nINFO: Validating copy CSV points with Z to DB...")
        clean_table('test_ladm_col_3d', BOUNDARY_POINT_TABLE)
        self.qgis_utils.disable_automatic_fields(self.db_connection_3d, BOUNDARY_POINT_TABLE)
        self.upload_points_from_csv_with_elevation()
        self.validate_points_z_in_db()
        clean_table('test_ladm_col_3d', BOUNDARY_POINT_TABLE)

    def upload_points_from_csv_with_elevation(self):
        print("\nINFO: Copying CSV data with elevation...")
        csv_path = get_test_path('csv/puntos_fixed.csv')
        txt_delimiter = ';'
        cbo_longitude = 'x'
        cbo_latitude = 'y'
        elevation = 'z'
        Processing.initialize()

        res = self.qgis_utils.copy_csv_to_db(csv_path,
                                             txt_delimiter,
                                             cbo_longitude,
                                             cbo_latitude,
                                             self.db_connection_3d,
                                             DEFAULT_EPSG,
                                             BOUNDARY_POINT_TABLE,
                                             elevation)

        self.assertEqual(res, True)

    def validate_points_in_db(self):
        cur = self.db_connection.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
        print('\nINFO: Validating points')
        query = cur.execute("""SELECT * FROM test_ladm_col.puntolindero;""")
        results = cur.fetchall()
        colnames = {desc[0]: cur.description.index(desc) for desc in cur.description}
        # for result in results:
        #     print(result[colnames['t_id']])
        self.assertEqual(len(results), 51)

        row = results[50]
        #self.assertEqual(row[colnames['t_id']], 52)
        self.assertEqual(row[colnames['acuerdo']], 'Acuerdo')
        self.assertEqual(row[colnames['definicion_punto']], 'No_Bien_Definido')
        self.assertEqual(row[colnames['descripcion_punto']], 'Otros')
        self.assertEqual(row[colnames['exactitud_vertical']], 1)
        self.assertEqual(row[colnames['exactitud_horizontal']], 1)
        self.assertEqual(row[colnames['confiabilidad']], None)
        self.assertEqual(row[colnames['nombre_punto']], None)
        self.assertEqual(row[colnames['posicion_interpolacion']], 'Centro_Arco')
        self.assertEqual(row[colnames['monumentacion']], None)
        self.assertEqual(row[colnames['puntotipo']], 'Catastro')
        self.assertEqual(row[colnames['p_espacio_de_nombres']], '-1')
        self.assertEqual(row[colnames['p_local_id']], '-1')
        self.assertEqual(row[colnames['ue_la_unidadespacial']], None)
        self.assertEqual(row[colnames['ue_terreno']], None)
        self.assertEqual(row[colnames['ue_la_espaciojuridicoredservicios']], None)
        self.assertEqual(row[colnames['ue_la_espaciojuridicounidadedificacion']], None)
        self.assertEqual(row[colnames['ue_servidumbrepaso']], None)
        self.assertEqual(row[colnames['ue_unidadconstruccion']], None)
        self.assertEqual(row[colnames['ue_construccion']], None)
        self.assertEqual(row[colnames['comienzo_vida_util_version']], datetime.datetime(2017, 4, 19, 14, 16, 41, 221713))
        self.assertEqual(row[colnames['fin_vida_util_version']], None)
        self.assertEqual(row[colnames['localizacion_original']], '01010000202C0C0000B01E85ABFC642D41F2D24DE20A703041')

    def validate_points_z_in_db(self):
        cur = self.db_connection.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
        print('\nINFO: Validating points Z')
        query = cur.execute("""SELECT * FROM test_ladm_col_3d.puntolindero;""")
        results = cur.fetchall()
        colnames = {desc[0]: cur.description.index(desc) for desc in cur.description}
        self.assertEqual(len(results), 51)

        row = results[50]

        self.assertEqual(row[colnames['acuerdo']], 'Acuerdo')
        self.assertEqual(row[colnames['definicion_punto']], 'No_Bien_Definido')
        self.assertEqual(row[colnames['descripcion_punto']], 'Otros')
        self.assertEqual(row[colnames['exactitud_vertical']], 1)
        self.assertEqual(row[colnames['exactitud_horizontal']], 1)
        self.assertEqual(row[colnames['confiabilidad']], None)
        self.assertEqual(row[colnames['nombre_punto']], None)
        self.assertEqual(row[colnames['posicion_interpolacion']], 'Centro_Arco')
        self.assertEqual(row[colnames['monumentacion']], None)
        self.assertEqual(row[colnames['puntotipo']], 'Catastro')
        self.assertEqual(row[colnames['p_espacio_de_nombres']], '-1')
        self.assertEqual(row[colnames['p_local_id']], '-1')
        self.assertEqual(row[colnames['ue_la_unidadespacial']], None)
        self.assertEqual(row[colnames['ue_terreno']], None)
        self.assertEqual(row[colnames['ue_la_espaciojuridicoredservicios']], None)
        self.assertEqual(row[colnames['ue_la_espaciojuridicounidadedificacion']], None)
        self.assertEqual(row[colnames['ue_servidumbrepaso']], None)
        self.assertEqual(row[colnames['ue_unidadconstruccion']], None)
        self.assertEqual(row[colnames['ue_construccion']], None)
        self.assertEqual(row[colnames['comienzo_vida_util_version']], datetime.datetime(2017, 4, 19, 14, 16, 41, 221713))
        self.assertEqual(row[colnames['fin_vida_util_version']], None)
        self.assertEqual(row[colnames['localizacion_original']], '01010000A02C0C0000B01E85ABFC642D41F2D24DE20A7030418B6CE7FB29529740')

    def test_copy_csv_overlapping_to_db(self):
        clean_table('test_ladm_col', 'puntolindero')
        self.upload_points_from_csv_overlapping()
        self.validate_points_overlapping_in_db()
        clean_table('test_ladm_col', 'puntolindero')

    def upload_points_from_csv_overlapping(self):
        csv_path = get_test_path('csv/puntos_overlapping.csv')
        txt_delimiter = ';'
        cbo_longitude = 'x'
        cbo_latitude = 'y'
        res = self.qgis_utils.copy_csv_to_db(csv_path,
                                    txt_delimiter,
                                    cbo_longitude,
                                    cbo_latitude,
                                    self.db_connection,
                                    DEFAULT_EPSG,
                                    BOUNDARY_POINT_TABLE)

        self.assertEqual(res, False)

    def validate_points_overlapping_in_db(self):
        cur = self.db_connection.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
        print('\nINFO: Validating points')
        query = cur.execute("""SELECT * FROM test_ladm_col.puntolindero;""")
        results = cur.fetchall()
        colnames = {desc[0]: cur.description.index(desc) for desc in cur.description}
        self.assertEqual(len(results), 0)

    @classmethod
    def tearDownClass(self):
        self.db_connection.conn.close()
Exemple #14
0
 def setUpClass(self):
     self.qgis_utils = QGISUtils()
     self.quality = QualityUtils(self.qgis_utils)
     Processing.initialize()
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
class TestLADMData(unittest.TestCase):
    @classmethod
    def setUpClass(self):

        restore_schema('test_ladm_col_queries')

        self.db_connection = get_dbconn('test_ladm_col_queries')
        result = self.db_connection.test_connection()
        print('test_connection', result)

        if not result[1]:
            print('The test connection is not working')
            return

        self.qgis_utils = QGISUtils()
        self.ladm_data = LADM_DATA(self.qgis_utils)

    def test_get_plots_related_to_parcels(self):
        print(
            "\nINFO: Validating get plots related to parcels (Case: t_id)...")

        parcel_ids_tests = [list(), [313], [320, 313, 318]]
        plot_ids_tests = [list(), [791], [778, 791, 809]]

        count = 0
        for parcel_ids_test in parcel_ids_tests:
            plot_ids = self.ladm_data.get_plots_related_to_parcels(
                self.db_connection, parcel_ids_test)
            # We use assertCountEqual to compare if two lists are the same regardless of the order of their elements.
            # https://docs.python.org/3.2/library/unittest.html#unittest.TestCase.assertCountEqual
            self.assertCountEqual(plot_ids, plot_ids_tests[count],
                                  "Failure with data set {}".format(count + 1))
            count += 1

        print(
            "\nINFO: Validating get plots related to parcels (Case: custom field)..."
        )
        plot_custom_field_ids_tests = [
            list(), [49379], [2614.3, 49379, 59108.5]
        ]

        count = 0
        for parcel_ids_test in parcel_ids_tests:
            plot_custom_field_ids = self.ladm_data.get_plots_related_to_parcels(
                self.db_connection,
                parcel_ids_test,
                field_name=PLOT_CALCULATED_AREA_FIELD)
            self.assertCountEqual(plot_custom_field_ids,
                                  plot_custom_field_ids_tests[count],
                                  "Failure with data set {}".format(count + 1))
            count += 1

        print(
            "\nINFO: Validating get plots related to parcels (Case: t_id) with preloaded tables..."
        )

        layers = {
            PLOT_TABLE: {
                'name': PLOT_TABLE,
                'geometry': QgsWkbTypes.PolygonGeometry,
                LAYER: None
            },
            UEBAUNIT_TABLE: {
                'name': UEBAUNIT_TABLE,
                'geometry': None,
                LAYER: None
            }
        }
        self.qgis_utils.get_layers(self.db_connection, layers, load=True)
        self.assertIsNotNone(
            layers,
            'An error occurred while trying to get the layers of interest')

        count = 0
        for parcel_ids_test in parcel_ids_tests:
            plot_ids = self.ladm_data.get_plots_related_to_parcels(
                self.db_connection,
                parcel_ids_test,
                plot_layer=layers[PLOT_TABLE][LAYER],
                uebaunit_table=layers[UEBAUNIT_TABLE][LAYER])
            self.assertCountEqual(plot_ids, plot_ids_tests[count],
                                  "Failure with data set {}".format(count + 1))
            count += 1

    def test_get_parcels_related_to_plots(self):
        print(
            "\nINFO: Validating get parcels related to plots (Case: t_id)...")

        plot_ids_tests = [list(), [791], [778, 791, 809]]
        parcel_ids_tests = [list(), [313], [320, 313, 318]]

        count = 0
        for plot_ids_test in plot_ids_tests:
            parcel_ids = self.ladm_data.get_parcels_related_to_plots(
                self.db_connection, plot_ids_test)
            # We use assertCountEqual to compare if two lists are the same regardless of the order of their elements.
            # https://docs.python.org/3.2/library/unittest.html#unittest.TestCase.assertCountEqual
            self.assertCountEqual(parcel_ids, parcel_ids_tests[count],
                                  "Failure with data set {}".format(count + 1))
            count += 1

        print(
            "\nINFO: Validating get parcels related to plots (Case: custom field)..."
        )
        parcel_custom_field_ids_tests = [
            list(), ['167-3652'], ['167-8620', '167-3652', '167-18982']
        ]

        count = 0
        for plot_ids_test in plot_ids_tests:
            parcel_custom_field_ids = self.ladm_data.get_parcels_related_to_plots(
                self.db_connection, plot_ids_test, field_name=FMI_FIELD)
            self.assertCountEqual(parcel_custom_field_ids,
                                  parcel_custom_field_ids_tests[count],
                                  "Failure with data set {}".format(count + 1))
            count += 1

        print(
            "\nINFO: Validating get parcels related to plots (Case: t_id) with preloaded tables..."
        )

        layers = {
            PARCEL_TABLE: {
                'name': PARCEL_TABLE,
                'geometry': None,
                LAYER: None
            },
            UEBAUNIT_TABLE: {
                'name': UEBAUNIT_TABLE,
                'geometry': None,
                LAYER: None
            }
        }
        self.qgis_utils.get_layers(self.db_connection, layers, load=True)
        self.assertIsNotNone(
            layers,
            'An error occurred while trying to get the layers of interest')

        count = 0
        for plot_ids_test in plot_ids_tests:
            parcel_ids = self.ladm_data.get_parcels_related_to_plots(
                self.db_connection,
                plot_ids_test,
                parcel_table=layers[PARCEL_TABLE][LAYER],
                uebaunit_table=layers[UEBAUNIT_TABLE][LAYER])
            self.assertCountEqual(parcel_ids, parcel_ids_tests[count],
                                  "Failure with data set {}".format(count + 1))
            count += 1

    def test_get_parcel_data_to_compare_changes(self):
        print("\nINFO: Validating get parcels data ...")

        features_test = {
            '253940000000000230097000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230097000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 307
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': NULL,
                'numero_predial': '253940000000000230097000000000',
                'nombre': 'Santa Lucía',
                'tipo': 'NPH',
                't_id': 312
            }],
            '253940000000000230098000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230098000000000',
                'nombre': 'Santa Lucía',
                'tipo': 'NPH',
                't_id': 308
            }],
            '253940000000000230072000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-24545',
                'numero_predial': '253940000000000230072000000000',
                'nombre': 'El Porvenir',
                'tipo': 'NPH',
                't_id': 309
            }],
            '253940000000000230074000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-1527',
                'numero_predial': '253940000000000230074000000000',
                'nombre': 'Tudelita',
                'tipo': 'NPH',
                't_id': 310
            }],
            '253940000000000230054000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-3652',
                'numero_predial': '253940000000000230054000000000',
                'nombre': 'San Pedro',
                'tipo': 'NPH',
                't_id': 313
            }],
            '253940000000000230257000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-24800',
                'numero_predial': '253940000000000230257000000000',
                'nombre': 'Casimiro',
                'tipo': 'NPH',
                't_id': 314
            }],
            '253940000000000230241000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 315
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 316
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 319
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 320
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 326
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'Tudela Juntas',
                'tipo': 'NPH',
                't_id': 327
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 328
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'El Tigre',
                'tipo': 'NPH',
                't_id': 344
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 345
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'Mardoqueo',
                'tipo': 'NPH',
                't_id': 346
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8620',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'Angel',
                'tipo': 'NPH',
                't_id': 347
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 348
            }, {
                'departamento': '25',
                'municipio': '394',
                'zona': '00',
                'fmi': '167-15523',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'Hoya Las Juntas',
                'tipo': 'PropiedadHorizontal.Matriz',
                't_id': 355
            }],
            '253940000000000230235000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-15137',
                'numero_predial': '253940000000000230235000000000',
                'nombre': 'Los Naranjos',
                'tipo': 'NPH',
                't_id': 317
            }],
            '253940000000000230254000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-18982',
                'numero_predial': '253940000000000230254000000000',
                'nombre': 'El Muche',
                'tipo': 'NPH',
                't_id': 318
            }],
            '253940000000000230242000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-15523',
                'numero_predial': '253940000000000230242000000000',
                'nombre': 'El Guamal',
                'tipo': 'NPH',
                't_id': 321
            }],
            '253940000000000230055000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '103002700241600084',
                'numero_predial': '253940000000000230055000000000',
                'nombre': 'El Volador',
                'tipo': 'NPH',
                't_id': 322
            }],
            '253940000000000230056000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230056000000000',
                'nombre': 'El Volador',
                'tipo': 'NPH',
                't_id': 323
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230056000000000',
                'nombre': 'El Almorzadero',
                'tipo': 'NPH',
                't_id': 324
            }],
            '253940000000000230057000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-21463',
                'numero_predial': '253940000000000230057000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 325
            }],
            '253940000000000230234000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-15166',
                'numero_predial': '253940000000000230234000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 329
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-15166',
                'numero_predial': '253940000000000230234000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 349
            }],
            '253940000000000230213000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-9028',
                'numero_predial': '253940000000000230213000000000',
                'nombre': 'El Volador',
                'tipo': 'NPH',
                't_id': 330
            }],
            '253940000000000230068000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-8114',
                'numero_predial': '253940000000000230068000000000',
                'nombre': 'Tudelita',
                'tipo': 'NPH',
                't_id': 331
            }],
            '253940000000000230101000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230101000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 332
            }],
            '253940000000000230100000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230100000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 333
            }],
            '253940000000000230099000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '10302400719A670000',
                'numero_predial': '253940000000000230099000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 334
            }],
            '253940000000000230069000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '102042200446620144',
                'numero_predial': '253940000000000230069000000000',
                'nombre': 'Las Juntas',
                'tipo': 'NPH',
                't_id': 335
            }],
            '253940000000000230070000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '201023900285580000',
                'numero_predial': '253940000000000230070000000000',
                'nombre': 'Bellavista',
                'tipo': 'NPH',
                't_id': 336
            }],
            '253940000000000230082000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230082000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 337
            }],
            '253940000000000230081000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230081000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 338
            }],
            '253940000000000230080000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230080000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 339
            }],
            '253940000000000230079000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230079000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 340
            }],
            '253940000000000230078000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230078000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 341
            }],
            '253940000000000230077000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230077000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 342
            }],
            '253940000000000320022000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000320022000000000',
                'nombre': 'Escuela Alto de Izacar',
                'tipo': 'NPH',
                't_id': 343
            }],
            '253940000000000230076000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230076000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 350
            }],
            NULL: [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': NULL,
                'nombre': 'Camino',
                'tipo': 'NPH',
                't_id': 351
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': NULL,
                'nombre': 'Camino',
                'tipo': 'NPH',
                't_id': 352
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': NULL,
                'nombre': 'Vía Interveredal',
                'tipo': 'NPH',
                't_id': 353
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': NULL,
                'nombre': 'Bajo',
                'tipo': 'NPH',
                't_id': 354
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': NULL,
                'numero_predial': NULL,
                'nombre': 'Apartamento 101',
                'tipo': 'PropiedadHorizontal.UnidadPredial',
                't_id': 356
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': NULL,
                'numero_predial': NULL,
                'nombre': 'Apartamento 202',
                'tipo': 'PropiedadHorizontal.UnidadPredial',
                't_id': 357
            }],
            '253940000000000230073000000000': [{
                'departamento': '25',
                'municipio': '394',
                'zona': '00',
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230073000000000',
                'nombre': 'El Pomarroso',
                'tipo': 'NPH',
                't_id': 311
            }]
        }
        features = self.ladm_data.get_parcel_data_to_compare_changes(
            self.db_connection)
        self.assertCountEqual(features, features_test)

        print("\nINFO: Validating get parcels data using search criterion...")

        features_test = {
            '253940000000000230054000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': '167-3652',
                'numero_predial': '253940000000000230054000000000',
                'nombre': 'San Pedro',
                'tipo': 'NPH',
                't_id': 313
            }]
        }
        search_criterion = {FMI_FIELD: '167-3652'}
        features = self.ladm_data.get_parcel_data_to_compare_changes(
            self.db_connection, search_criterion=search_criterion)
        self.assertCountEqual(features, features_test)

        features_test = {
            '253940000000000230097000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230097000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 307
            }],
            '253940000000000230098000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230098000000000',
                'nombre': 'Santa Lucía',
                'tipo': 'NPH',
                't_id': 308
            }],
            '253940000000000230241000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230241000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 348
            }],
            '253940000000000230056000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230056000000000',
                'nombre': 'El Volador',
                'tipo': 'NPH',
                't_id': 323
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230056000000000',
                'nombre': 'El Almorzadero',
                'tipo': 'NPH',
                't_id': 324
            }],
            '253940000000000230101000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230101000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 332
            }],
            '253940000000000230100000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230100000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 333
            }],
            '253940000000000230082000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230082000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 337
            }],
            '253940000000000230081000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230081000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 338
            }],
            '253940000000000230080000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230080000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 339
            }],
            '253940000000000230079000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230079000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 340
            }],
            '253940000000000230078000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230078000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 341
            }],
            '253940000000000230077000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230077000000000',
                'nombre': 'SIN INFO',
                'tipo': 'NPH',
                't_id': 342
            }],
            '253940000000000320022000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000320022000000000',
                'nombre': 'Escuela Alto de Izacar',
                'tipo': 'NPH',
                't_id': 343
            }],
            '253940000000000230076000000000': [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230076000000000',
                'nombre': 'Santa Lucia',
                'tipo': 'NPH',
                't_id': 350
            }],
            NULL: [{
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': NULL,
                'nombre': 'Camino',
                'tipo': 'NPH',
                't_id': 351
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': NULL,
                'nombre': 'Camino',
                'tipo': 'NPH',
                't_id': 352
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': NULL,
                'nombre': 'Vía Interveredal',
                'tipo': 'NPH',
                't_id': 353
            }, {
                'departamento': NULL,
                'municipio': NULL,
                'zona': NULL,
                'fmi': 'SIN INFO',
                'numero_predial': NULL,
                'nombre': 'Bajo',
                'tipo': 'NPH',
                't_id': 354
            }],
            '253940000000000230073000000000': [{
                'departamento': '25',
                'municipio': '394',
                'zona': '00',
                'fmi': 'SIN INFO',
                'numero_predial': '253940000000000230073000000000',
                'nombre': 'El Pomarroso',
                'tipo': 'NPH',
                't_id': 311
            }]
        }
        search_criterion = {FMI_FIELD: 'SIN INFO'}
        features = self.ladm_data.get_parcel_data_to_compare_changes(
            self.db_connection, search_criterion=search_criterion)
        self.assertCountEqual(features, features_test)

    def tearDownClass():
        print('tearDown test_ladm_data')
 def setUpClass(self):
     self.qgis_utils = QGISUtils()
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')
Exemple #18
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!!!"
                )