def _validate(self, db, db_qr, layer_dict, tolerance, **kwargs):
        self.progress_changed.emit(5)

        building_layer = self._get_layer(layer_dict)
        pre_res, res_obj = self._check_prerrequisite_layer(QCoreApplication.translate("QualityRules", "Building"),
                                                           building_layer)
        if not pre_res:
            return res_obj

        overlapping = GeometryUtils.get_overlapping_polygons(self, building_layer)
        flat_overlapping = [fid for items in overlapping for fid in items]  # Build a flat list of ids
        flat_overlapping = list(set(flat_overlapping))  # unique values

        self.progress_changed.emit(70)

        dict_uuids = {f.id(): f[db.names.T_ILI_TID_F] for f in building_layer.getFeatures(flat_overlapping)}
        error_state = LADMData().get_domain_code_from_value(db_qr, db_qr.names.ERR_ERROR_STATE_D,
                                                            LADMNames.ERR_ERROR_STATE_D_ERROR_V)

        errors = {'geometries': list(), 'data': list()}
        for items in overlapping:
            polygon_id_field = items[0]
            overlapping_id_field = items[1]
            polygon_intersection = GeometryUtils.get_intersection_polygons(building_layer, polygon_id_field, overlapping_id_field)

            if polygon_intersection is not None:
                errors['geometries'].append(polygon_intersection)

                error_data = [  # [obj_uuids, rel_obj_uuids, values, details, state]
                    [dict_uuids.get(polygon_id_field)],
                    [dict_uuids.get(overlapping_id_field)],
                    None,
                    self._errors[self._ERROR_01],
                    error_state]
                errors['data'].append(error_data)

        self._save_errors(db_qr, self._ERROR_01, errors)

        if len(flat_overlapping) > 0:
            res_type = EnumQualityRuleResult.ERRORS
            msg = QCoreApplication.translate("QualityRules", "{} overlapping buildings were found!").format(len(flat_overlapping))
        else:
            res_type = EnumQualityRuleResult.SUCCESS
            msg = QCoreApplication.translate("QualityRules", "There are no overlapping buildings!")

        self.progress_changed.emit(100)

        return QualityRuleExecutionResult(res_type, msg, len(flat_overlapping))
Esempio n. 2
0
    def test_intersection_polygons_tolerance(self):
        print('\nINFO: Validating intersection in polygons (plots)...')

        gpkg_path = get_test_copy_path('db/static/gpkg/geometry_utils.gpkg')
        uri = gpkg_path + '|layername={layername}'.format(
            layername='overlapping_polygons')
        polygons_intersection_layer = QgsVectorLayer(uri,
                                                     'overlapping_polygons',
                                                     'ogr')

        polygon_id = 61
        overlapping_id = 62
        test_overlapping_polygon = 'MultiPolygon (((779846.53495819831732661 1225249.26543459924869239, 779783.95215819834265858 1225214.60283459979109466, 779755.42265819816384465 1225312.01313459943048656, 779751.20725819806102663 1225339.76893460075370967, 779753.92445819883141667 1225441.12693459982983768, 779750.35635819809976965 1225530.46543460036627948, 779822.77425819879863411 1225579.77523459936492145, 779989.38215819804463536 1225693.37413460086099803, 780035.07055819837842137 1225719.08503460022620857, 780048.81795819813851267 1225694.37763460050337017, 780107.8898581980029121 1225665.00753459963016212, 780145.38955819851253182 1225642.4988345995079726, 780156.81765819864813238 1225634.07963460008613765, 780178.43775819859001786 1225625.24393460038118064, 780098.00075819867197424 1225413.46203460055403411, 779976.68275819870177656 1225330.46993460017256439, 779918.56405819824431092 1225292.73203460010699928, 779846.53495819831732661 1225249.26543459924869239)))'
        polygon_intersection = GeometryUtils.get_intersection_polygons(
            polygons_intersection_layer, polygon_id, overlapping_id)
        self.assertEqual(polygon_intersection.asWkt(),
                         test_overlapping_polygon)