def update_centroidlinks(self, geom):
            from qgis.core import QgsGeometry, QGis, QgsPoint
            feature_name = self.feature.attributes()[0]
            for section_field_name in self.session.section_types.values():
                try:
                    object = None
                    if self.layer == self.session.model_layers.layer_by_name(
                            section_field_name):
                        section = getattr(self.session.project,
                                          section_field_name)
                        object = section.value[feature_name]
                        if section_field_name != "subcatchments":
                            if hasattr(object, "vertices"):
                                del object.vertices[:]
                                for v in geom.asPolyline():
                                    coord = Coordinate()
                                    coord.x = v.x()
                                    coord.y = v.y()
                                    object.vertices.append(coord)
                    if object is not None and hasattr(object, "centroid"):
                        new_c_g = geom.centroid()
                        new_c = new_c_g.asPoint()
                        object.centroid.x = str(new_c.x)
                        object.centroid.y = str(new_c.y)
                        for fc in self.subcentroids.getFeatures():
                            if fc["sub_modelid"] == feature_name:
                                # from qgis.core import QgsMap
                                change_map = {fc.id(): new_c_g}
                                self.subcentroids.dataProvider(
                                ).changeGeometryValues(change_map)
                                break
                        for fl in self.sublinks.dataProvider().getFeatures():
                            if fl["inlet"] == self.feature["c_modelid"]:
                                if fl.geometry().wkbType(
                                ) == QGis.WKBLineString:
                                    line = fl.geometry().asPolyline()
                                    new_l_g = QgsGeometry.fromPolyline(
                                        [new_c, line[-1]])
                                    change_map = {fl.id(): new_l_g}
                                    self.sublinks.dataProvider(
                                    ).changeGeometryValues(change_map)
                                break

                    break
                except Exception as ex1:
                    print("Searching for object to edit vertex of: " +
                          str(ex1) + '\n' + str(traceback.print_exc()))
Esempio n. 2
0
    def check_coords(self):
        # check source LL coordinates
        new_src_LL = Coordinate()
        val, val_is_good = ParseData.floatTryParse(self.ui.txtLL_src_x.text())
        if val_is_good:
            new_src_LL.x = val
        else:
            return False
        val, val_is_good = ParseData.floatTryParse(self.ui.txtLL_src_y.text())
        if val_is_good:
            new_src_LL.y = val
        else:
            return False

        # check source UR coordinates
        new_src_UR = Coordinate()
        val, val_is_good = ParseData.floatTryParse(self.ui.txtUR_src_x.text())
        if val_is_good:
            new_src_UR.x = val
        else:
            return False
        val, val_is_good = ParseData.floatTryParse(self.ui.txtUR_src_y.text())
        if val_is_good:
            new_src_UR.y = val
        else:
            return False

        # check destination LL coordinates
        new_dst_LL = Coordinate()
        val, val_is_good = ParseData.floatTryParse(self.ui.txtLL_dst_x.text())
        if val_is_good:
            new_dst_LL.x = val
        else:
            return False
        val, val_is_good = ParseData.floatTryParse(self.ui.txtLL_dst_y.text())
        if val_is_good:
            new_dst_LL.y = val
        else:
            return False

        # check destination UR coordinates
        new_dst_UR = Coordinate()
        val, val_is_good = ParseData.floatTryParse(self.ui.txtUR_dst_x.text())
        if val_is_good:
            new_dst_UR.x = val
        else:
            return False
        val, val_is_good = ParseData.floatTryParse(self.ui.txtUR_dst_y.text())
        if val_is_good:
            new_dst_UR.y = val
        else:
            return False

        self.pt_src_ll.x = new_src_LL.x
        self.pt_src_ll.y = new_src_LL.y
        self.pt_src_ur.x = new_src_UR.x
        self.pt_src_ur.y = new_src_UR.y

        if not self.pt_dst_ll:
            self.pt_dst_ll = type(self.pt_src_ll)()
        if not self.pt_dst_ur:
            self.pt_dst_ur = type(self.pt_src_ur)()
        self.pt_dst_ll.x = new_dst_LL.x
        self.pt_dst_ll.y = new_dst_LL.y
        self.pt_dst_ur.x = new_dst_UR.x
        self.pt_dst_ur.y = new_dst_UR.y

        return True