Example #1
0
    def progress_dialog(progress=0, title='CQFS Progress', label='Running...'):
        dialog = QProgressDialog()
        dialog.setWindowTitle(title)
        dialog.setLabelText(label)
        dialog.setAutoClose(True)

        bar = QProgressBar(dialog)
        bar.setTextVisible(True)
        bar.setValue(progress)
        bar.setMaximum(100)

        dialog.setBar(bar)
        dialog.setMinimumWidth(300)
        dialog.show()

        if int(progress) == 0:
            bar.setValue(0)
        return dialog, bar
Example #2
0
class RebuildIndex(object):
    """
    Tool class to rebuild the indexation
    """
    def __init__(self, iface):
        """
        Constructor
        :param iface: interface
        """
        self.__iface = iface
        self.icon_path = ':/plugins/VDLTools/icons/rebuild_icon.png'
        self.text = QCoreApplication.translate("VDLTools", "Rebuild Index")
        self.killed = False

    def start(self):
        """
        To start the rebuild
        """
        snap_util = self.__iface.mapCanvas().snappingUtils()
        extent = self.__iface.mapCanvas().extent()
        self.__progressDialog = QProgressDialog()
        self.__progressDialog.setWindowTitle(
            QCoreApplication.translate("VDLTools", "Rebuild Index..."))
        self.__progressDialog.setLabelText(
            QCoreApplication.translate("VDLTools",
                                       "Percentage of indexed layers"))
        progressBar = QProgressBar(self.__progressDialog)
        progressBar.setTextVisible(True)
        cancelButton = QPushButton()
        cancelButton.setText(QCoreApplication.translate("VDLTools", "Cancel"))
        cancelButton.clicked.connect(self.kill)
        self.__progressDialog.setBar(progressBar)
        self.__progressDialog.setCancelButton(cancelButton)
        self.__progressDialog.setMinimumWidth(300)
        self.__progressDialog.show()

        lcs_list = snap_util.layers()
        step = 0
        self.killed = False
        for lc in lcs_list:
            if self.killed:
                break
            locator = snap_util.locatorForLayer(lc.layer)
            if locator.extent() is not None:
                txt = locator.extent().toString()
            else:
                txt = "None"
            print("old extent : " + txt)
            print("new extent : " + extent.toString())
            locator.setExtent(extent)
            if not locator.hasIndex():
                locator.init()
            else:
                locator.rebuildIndex()
            locator.setExtent(None)
            progressBar.setValue(100 * step / len(lcs_list))
            step += 1
        self.__progressDialog.close()

    def kill(self):
        """
        To stop the rebuild at the end of the current working layer
        """
        self.killed = True
Example #3
0
def run(iface,
        lineLayer,
        nodeLayer,
        nodeRadiiExpr="0",
        lineWidthExpr="1",
        iterations=100,
        w_flows=1,
        w_nodes=0.5,
        w_antiTorsion=0.8,
        w_spring=1,
        w_angRes=3.75,
        snapThreshold=0,
        bezierRes=15,
        alpha=4,
        beta=4,
        kShort=0.5,
        kLong=0.05,
        Cp=2.5,
        K=4,
        C=4,
        constraintRectAspect=0.5,
        nodeBuffer=0):
    """
    Runs the algorithm
    :return:
    """
    # Generate a progress bar
    progDialog = QProgressDialog()
    progDialog.setWindowTitle("Progress")
    progDialog.setLabelText("Generating flowmap from layers")
    progBar = QProgressBar(progDialog)
    progBar.setTextVisible(True)
    progBar.setValue(0)
    progDialog.setBar(progBar)
    progDialog.setMinimumWidth(300)
    progBar.setMaximum(iterations)
    progDialog.show()
    # Load the nodes and flows into a data structure
    fm = FlowMap(w_flows=w_flows,
                 w_nodes=w_nodes,
                 w_antiTorsion=w_antiTorsion,
                 w_spring=w_spring,
                 w_angRes=w_angRes,
                 snapThreshold=snapThreshold,
                 bezierRes=bezierRes,
                 alpha=alpha,
                 beta=beta,
                 kShort=kShort,
                 kLong=kLong,
                 Cp=Cp,
                 K=K,
                 C=C,
                 constraintRectAspect=constraintRectAspect,
                 nodeBuffer=nodeBuffer)
    fm.getNodesFromLineLayer(lineLayer)
    if nodeLayer is not None:
        fm.getNodeRadiiFromLayer(nodeLayer, nodeRadiiExpr)
    else:
        fm.nodeRadii = [0 for node in fm.nodes]
    fm.getLineWidthFromLayer(lineLayer, lineWidthExpr)
    fm.calculateMapScale()
    fm.loadFlowLinesFromLayer(lineLayer)
    # Iterate
    j = 0  # used in node collision avoidance algorithm
    progDialog.setLabelText("Iterating")
    for i in range(iterations):
        progBar.setValue(i)
        if progDialog.wasCanceled():
            break
        w = 1 - float(i) / iterations
        # Calculate flowline-against-flowline forces
        flowline_forces, Fs = fm.calculateInterFlowLineForces(
            returnSumOfMagnitudes=True)
        # Calculate node-against-flowline forces
        node_forces = fm.calculateNodeToFlowLineForces()
        # Calculate anti-torsion forces
        antitorsion_forces = fm.calculateAntiTorsionForces()
        # Calculate spring forces
        spring_forces = fm.calculateSpringForces(flowline_forces, Fs)
        # Calculate angular-resolution-of-flowlines-around-nodes forces
        angRes_forces = fm.calculateAngleResForces()
        # Apply forces to arc control points
        resultantForces = []
        for i in range(len(fm.flowlines)):
            rForce = (flowline_forces[i]*fm.w_flows +
                      node_forces[i]*fm.w_nodes +
                      antitorsion_forces[i]*fm.w_antiTorsion +
                      spring_forces[i]*fm.w_spring)*w +\
                     (angRes_forces[i]*fm.w_angRes)*(w - w**2)
            resultantForces.append(rForce)
        fm.applyForces(resultantForces)
        # Apply rectangle constraint
        fm.applyRectangleConstraints()
        # Reduce intersections of flowlines with a common node
        fm.reduceFlowLineIntersections()
        # Move flows off of nodes
        if i > 0.1 * iterations and j <= 0:
            N = fm.getFlowLinesOverlappingNodes()
            j = (iterations - i) / (len(N) + 1) / 2
            n = math.ceil(float(len(N)) / (iterations - i - 1))
            movedFlows = 0  # number of flows which have been moved. Iterate until this equals n
            for flowline in N:
                movedFlows += 1 if fm.moveFlowLineOffNodes(flowline) else 0
                if movedFlows >= n:
                    break
        else:
            j -= 1

    progDialog.setLabelText("Updating Geometry")
    # Update the geometry of the layer
    fm.updateGeometryOnLayer(lineLayer)
    iface.mapCanvas().refresh()
    # close the dialog
    progDialog.close()