Exemple #1
0
    def __init__(self, interface, settings, profiles, intersections):

        QDialog.__init__(self, interface.mainWindow())
        self.iface = interface
        #output debug Log Messages: mainly for debugging matplotlib
        self.debug = False
        self.settings = settings
        self.profiles = profiles
        self.intersections = intersections

        # Set up the user interface from Designer.
        self.ui = Ui_VoGISProfilToolPlot()
        self.ui.setupUi(self)

        if self.settings.onlyHektoMode is True:
            self.ui.IDC_frPlot.hide()
            self.ui.IDC_frToolbar.hide()
            self.adjustSize()
            self.ui.IDC_chkHekto.setCheckState(Qt.Checked)
            self.ui.IDC_chkHekto.setEnabled(False)

        #self.filePath = ''
        if QGis.QGIS_VERSION_INT < 10900:
            self.filePath = QSettings().value("vogisprofiltoolmain/savepath",
                                              "").toString()
        else:
            self.filePath = QSettings().value("vogisprofiltoolmain/savepath",
                                              "")

        #nimmt die Locale vom System, nicht von QGIS
        #kein Weg gefunden, um QGIS Locale: QSettings().value("locale/userLocale")
        #zu initialisieren, nur um Dezimaltrenne auszulesen
        #QgsMessageLog.logMessage('QGIS Locale:{0}'.format(QSettings().value("locale/userLocale").toString()), 'VoGis')
        #!!!nl_langinfo not available on Windows!!!
        #http://docs.python.org/2.7/library/locale.html#locale.nl_langinfo
        # ...  This function is not available on all systems ...
        #decimalDelimiter = locale.nl_langinfo(locale.RADIXCHAR)
        decimalDelimiter = locale.localeconv()['decimal_point']
        QgsMessageLog.logMessage('delimiter:{0}'.format(decimalDelimiter),
                                 'VoGis')
        idx = self.ui.IDC_cbDecimalDelimiter.findText(decimalDelimiter,
                                                      Qt.MatchExactly)
        QgsMessageLog.logMessage('idx:{0}'.format(idx), 'VoGis')
        self.ui.IDC_cbDecimalDelimiter.setCurrentIndex(idx)

        plt_extent = PlotExtent()
        for profile in self.profiles:
            plt_extent.union(profile.getExtent())
            if self.debug:
                QgsMessageLog.logMessage(plt_extent.toString(), 'VoGis')

        plt_extent.expand()
        self.orig_plt_xtnt = PlotExtent(plt_extent.xmin, plt_extent.ymin,
                                        plt_extent.xmax, plt_extent.ymax)
        self.plt_widget = self.__createMatplotlibCanvas(plt_extent)
        layout = self.ui.IDC_frPlot.layout()
        #QgsMessageLog.logMessage('layout: {0}'.format(layout), 'VoGis')
        layout.addWidget(self.plt_widget)

        # check matplotlib version solves https://github.com/BergWerkGIS/VoGIS-Profil-Tool/issues/6
        if matplotlib.__version__ < 1.5:
            plt_toolbar = matplotlib.backends.backend_qt4agg.NavigationToolbar2QTAgg(
                self.plt_widget, self.ui.IDC_frPlot)
        else:
            plt_toolbar = matplotlib.backends.backend_qt4agg.NavigationToolbar2QT(
                self.plt_widget, self.ui.IDC_frPlot)

        self.ui.IDC_frToolbar.layout().addWidget(plt_toolbar)

        #adjust actions
        #QgsMessageLog.logMessage('{0}'.format(dir(lstActions[0])), 'VoGis')
        #        for a in plt_toolbar.actions():
        #            QgsMessageLog.logMessage('{0}'.format(a.text()), 'VoGis')
        #        for t in plt_toolbar.toolitems:
        #            QgsMessageLog.logMessage('{0}'.format(t), 'VoGis')
        #lstActions = plt_toolbar.actions()

        #https://hub.qgis.org/wiki/17/Icons_20

        firstaction = None
        for a in plt_toolbar.actions():
            atxt = a.text()
            if atxt == 'Home':
                firstaction = a
                a.setIcon(
                    QIcon(":/plugins/vogisprofiltoolmain/icons/home.png"))
            elif atxt == 'Back':
                a.setIcon(
                    QIcon(":/plugins/vogisprofiltoolmain/icons/zoomlast.png"))
            elif atxt == 'Forward':
                a.setIcon(
                    QIcon(":/plugins/vogisprofiltoolmain/icons/zoomnext.png"))
            elif atxt == 'Pan':
                a.setIcon(QIcon(":/plugins/vogisprofiltoolmain/icons/pan.png"))
            elif atxt == 'Zoom':
                a.setIcon(
                    QIcon(
                        ":/plugins/vogisprofiltoolmain/icons/zoomselect.png"))
            elif atxt == 'Save':
                a.setIcon(
                    QIcon(":/plugins/vogisprofiltoolmain/icons/save.png"))
            else:
                plt_toolbar.removeAction(a)

        #insert 1:1 zoom button
        self.one2one = QPushButton()
        self.one2one.setText('1:1')
        self.one2one.clicked.connect(self.__one2oneClicked)
        #plt_toolbar.addWidget(self.one2one)
        #insert QLineEdit to change the exaggeration
        #catch updating of figure, when exaggeration QLineEdit has been updated from draw_event of figure
        self.draw_event_fired = False
        #catch closing of dialog, when enter key has been used accept exaggeration edit field
        self.exaggeration_edited = False

        #insert edit field for exaggeration
        self.editExaggeration = QLineEdit()
        self.editExaggeration.setFixedWidth(60)
        self.editExaggeration.setMaximumWidth(60)
        plt_toolbar.insertWidget(firstaction, self.editExaggeration)
        self.editExaggeration.editingFinished.connect(
            self.__exaggeration_edited)

        #insert identify button -> deactivate all tools
        #HACK: picked event gets fired before click event
        self.plotpicked = False
        plt_toolbar.insertWidget(firstaction, self.one2one)
        self.identify = QPushButton()
        self.identify.setIcon(
            QIcon(":/plugins/vogisprofiltoolmain/icons/identify.png"))
        self.identify.clicked.connect(self.__identify)
        plt_toolbar.insertWidget(firstaction, self.identify)

        #insert identify label to show name of clicked dhm
        self.dhmLbl = QLabel()
        plt_toolbar.insertWidget(firstaction, self.dhmLbl)

        #insert measure tool
        self.measuring = False
        self.measure_tool = QPushButton()
        self.measure_tool.setIcon(
            QIcon(":/plugins/vogisprofiltoolmain/icons/measure.png"))
        self.measure_tool.clicked.connect(self.__measure)
        plt_toolbar.insertWidget(firstaction, self.measure_tool)

        #show measure results
        self.click1 = None
        self.click2 = None
        self.click1pnt = None
        self.click2pnt = None
        self.measure_lbl = QLabel()
        self.measure_lbl.setText(u'  ')
        plt_toolbar.insertWidget(firstaction, self.measure_lbl)

        #for less thatn 10 colors:
        #alternative method: http://stackoverflow.com/a/14720445
        colors = [
            (1.0, 0.0, 0.0, 1.0),
            (0.0, 1.0, 0.0, 1.0),
            (0.0, 0.0, 1.0, 1.0),
            (1.0, 1.0, 0.5, 1.0),
            (1.0, 0.0, 1.0, 1.0),
            (0.0, 1.0, 1.0, 1.0),
            (0.415686275, 0.807843137, 0.890196078, 1.0),
            (0.121568627, 0.470588235, 0.705882353, 1.0),
            (0.698039216, 0.874509804, 0.541176471, 1.0),
            (0.2, 0.62745098, 0.17254902, 1.0),
            (0.984313725, 0.603921569, 0.6, 1.0),
            (0.890196078, 0.101960784, 0.109803922, 1.0),
            (0.992156863, 0.749019608, 0.435294118, 1.0),
            (1, 0.498039216, 0, 1.0),
            (0.792156863, 0.698039216, 0.839215686, 1.0),
            (0.415686275, 0.239215686, 0.603921569, 1.0),
            (1, 1, 0.521568627, 1.0),
        ]

        max_profile_length = 0
        #idxCol = 0
        for idx, p in enumerate(self.profiles):
            max_profile_length = max(p.getExtent().xmax, max_profile_length)
            #if idxCol > len(colors) - 1:
            #    idxCol = 0
            #x, plt_segments = p.getPlotSegments()
            #QgsMessageLog.logMessage('x: {0}'.format(x), 'VoGis')
            plt_segments = p.getPlotSegments()
            #QgsMessageLog.logMessage('plt_segments: {0}'.format(plt_segments), 'VoGis')
            lineColl = LineCollection(
                plt_segments,
                linewidths=2,
                linestyles='solid',
                #colors=colors[randrange(len(colors))],
                #colors=colors[idxCol],
                colors=colors[:len(settings.mapData.rasters.selectedRasters()
                                   )],
                picker=True,
                label='LBL')
            #lineColl.set_array(x)
            #lineColl.text.set_text('line label')
            self.subplot.add_collection(lineColl)
            #idxCol += 1

        #LINE STYLES: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot
        for idx, intersection in enumerate(self.intersections):
            color_intersection = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
            x_1 = intersection.from_dist
            x_2 = intersection.to_dist
            z_1 = intersection.from_z[self.settings.intersection_dhm_idx]
            z_2 = intersection.to_z[self.settings.intersection_dhm_idx]
            self.subplot.plot((x_1, x_2), (z_1, z_2),
                              color_intersection[idx] + '--',
                              linewidth=4,
                              zorder=1)
            #http://matplotlib.org/users/annotations_guide.html#using-complex-coordinate-with-annotation
            self.subplot.annotate(u'{0:.2f}'.format(z_1), (x_1, z_1),
                                  xytext=(0, 30),
                                  textcoords="offset points",
                                  bbox=dict(boxstyle="round", fc="w"),
                                  arrowprops=dict(arrowstyle="->"),
                                  ha='center',
                                  va='bottom',
                                  rotation=90)
            self.subplot.annotate(u'{0:.2f}'.format(z_2), (x_2, z_2),
                                  xytext=(0, 30),
                                  textcoords="offset points",
                                  bbox=dict(boxstyle="round", fc="w"),
                                  arrowprops=dict(arrowstyle="->"),
                                  ha='center',
                                  va='bottom',
                                  rotation=90)

            #horizontale Ausspiegeling
            self.subplot.plot((x_1, max_profile_length), (z_1, z_1),
                              color_intersection[idx] + ':',
                              linewidth=4,
                              zorder=1)

        #save inital view in history
        plt_toolbar.push_current()
        #select pan tool
        plt_toolbar.pan()
        self.plt_toolbar = plt_toolbar
        #(matplotlib.pyplot).tight_layout(True)
        #plt.tight_layout()
        #self.fig.tight_layout()
        QApplication.restoreOverrideCursor()
Exemple #2
0
    def __init__(self, interface, settings, profiles):

        QDialog.__init__(self, interface.mainWindow())
        self.iface = interface
        #output debug Log Messages: mainly for debugging matplotlib
        self.debug = False
        self.settings = settings
        self.profiles = profiles
        # Set up the user interface from Designer.
        self.ui = Ui_VoGISProfilToolPlot()
        self.ui.setupUi(self)

        if self.settings.onlyHektoMode is True:
            self.ui.IDC_frPlot.hide()
            self.ui.IDC_frToolbar.hide()
            self.adjustSize()
            self.ui.IDC_chkHekto.setCheckState(Qt.Checked)
            self.ui.IDC_chkHekto.setEnabled(False)

        #self.filePath = ''
        if QGis.QGIS_VERSION_INT < 10900:
            self.filePath = QSettings().value("vogisprofiltoolmain/savepath",
                                              "").toString()
        else:
            self.filePath = QSettings().value("vogisprofiltoolmain/savepath",
                                              "")

        #nimmt die Locale vom System, nicht von QGIS
        #kein Weg gefunden, um QGIS Locale: QSettings().value("locale/userLocale")
        #zu initialisieren, nur um Dezimaltrenne auszulesen
        #QgsMessageLog.logMessage('QGIS Locale:{0}'.format(QSettings().value("locale/userLocale").toString()), 'VoGis')
        #!!!nl_langinfo not available on Windows!!!
        #http://docs.python.org/2.7/library/locale.html#locale.nl_langinfo
        # ...  This function is not available on all systems ...
        #decimalDelimiter = locale.nl_langinfo(locale.RADIXCHAR)
        decimalDelimiter = locale.localeconv()['decimal_point']
        QgsMessageLog.logMessage('delimiter:{0}'.format(decimalDelimiter),
                                 'VoGis')
        idx = self.ui.IDC_cbDecimalDelimiter.findText(decimalDelimiter,
                                                      Qt.MatchExactly)
        QgsMessageLog.logMessage('idx:{0}'.format(idx), 'VoGis')
        self.ui.IDC_cbDecimalDelimiter.setCurrentIndex(idx)

        pltExt = PlotExtent()
        for p in self.profiles:
            pltExt.union(p.getExtent())
            #QgsMessageLog.logMessage(pltExt.toString(), 'VoGis')

        pltExt.expand()
        self.origPltExt = PlotExtent(pltExt.xmin, pltExt.ymin, pltExt.xmax,
                                     pltExt.ymax)
        self.pltWidget = self.__createMatplotlibCanvas(pltExt)
        layout = self.ui.IDC_frPlot.layout()
        #QgsMessageLog.logMessage('layout: {0}'.format(layout), 'VoGis')
        layout.addWidget(self.pltWidget)
        pltToolbar = matplotlib.backends.backend_qt4agg.NavigationToolbar2QTAgg(
            self.pltWidget, self.ui.IDC_frPlot)
        self.ui.IDC_frToolbar.layout().addWidget(pltToolbar)

        #adjust actions
        #QgsMessageLog.logMessage('{0}'.format(dir(lstActions[0])), 'VoGis')
        #        for a in pltToolbar.actions():
        #            QgsMessageLog.logMessage('{0}'.format(a.text()), 'VoGis')
        #        for t in pltToolbar.toolitems:
        #            QgsMessageLog.logMessage('{0}'.format(t), 'VoGis')
        #lstActions = pltToolbar.actions()
        firstaction = None
        for a in pltToolbar.actions():
            atxt = a.text()
            if atxt == 'Home':
                firstaction = a
                a.setIcon(
                    QIcon(":/plugins/vogisprofiltoolmain/icons/home.png"))
            elif atxt == 'Back':
                a.setIcon(
                    QIcon(":/plugins/vogisprofiltoolmain/icons/zoomlast.png"))
            elif atxt == 'Forward':
                a.setIcon(
                    QIcon(":/plugins/vogisprofiltoolmain/icons/zoomnext.png"))
            elif atxt == 'Pan':
                a.setIcon(QIcon(":/plugins/vogisprofiltoolmain/icons/pan.png"))
            elif atxt == 'Zoom':
                a.setIcon(
                    QIcon(
                        ":/plugins/vogisprofiltoolmain/icons/zoomselect.png"))
            elif atxt == 'Save':
                a.setIcon(
                    QIcon(":/plugins/vogisprofiltoolmain/icons/save.png"))
            else:
                pltToolbar.removeAction(a)

        #insert 1:1 zoom button
        self.one2one = QPushButton()
        self.one2one.setText('1:1')
        self.one2one.clicked.connect(self.__one2oneClicked)
        #pltToolbar.addWidget(self.one2one)
        #insert QLineEdit to change the exaggeration
        #catch updating of figure, when exaggeration QLineEdit has been updated from draw_event of figure
        self.drawEventFired = False
        #catch closing of dialog, when enter key has been used accept exaggeration edit field
        self.exaggerationEdited = False
        self.editExaggeration = QLineEdit()
        self.editExaggeration.setFixedWidth(60)
        self.editExaggeration.setMaximumWidth(60)
        pltToolbar.insertWidget(firstaction, self.editExaggeration)
        self.editExaggeration.editingFinished.connect(
            self.__exaggerationEdited)
        #insert identify button -> deactivate all tools
        #HACK: picked event gets fired before click event
        self.plotpicked = False
        pltToolbar.insertWidget(firstaction, self.one2one)
        self.identify = QPushButton()
        self.identify.setIcon(
            QIcon(":/plugins/vogisprofiltoolmain/icons/identify.png"))
        self.identify.clicked.connect(self.__identify)
        pltToolbar.insertWidget(firstaction, self.identify)
        #insert identify label to show name of clicked dhm
        self.dhmLbl = QLabel()
        pltToolbar.insertWidget(firstaction, self.dhmLbl)
        #measure in figure
        self.click1 = None
        self.click2 = None
        self.click1pnt = None
        self.click2pnt = None
        self.measureLbl = QLabel()
        self.measureLbl.setText(u'  ')
        pltToolbar.insertWidget(firstaction, self.measureLbl)

        #for less thatn 10 colors:
        #alternative method: http://stackoverflow.com/a/14720445
        colors = [
            (1.0, 0.0, 0.0, 1.0),
            (0.0, 1.0, 0.0, 1.0),
            (0.0, 0.0, 1.0, 1.0),
            (1.0, 1.0, 0.5, 1.0),
            (1.0, 0.0, 1.0, 1.0),
            (0.0, 1.0, 1.0, 1.0),
            (0.415686275, 0.807843137, 0.890196078, 1.0),
            (0.121568627, 0.470588235, 0.705882353, 1.0),
            (0.698039216, 0.874509804, 0.541176471, 1.0),
            (0.2, 0.62745098, 0.17254902, 1.0),
            (0.984313725, 0.603921569, 0.6, 1.0),
            (0.890196078, 0.101960784, 0.109803922, 1.0),
            (0.992156863, 0.749019608, 0.435294118, 1.0),
            (1, 0.498039216, 0, 1.0),
            (0.792156863, 0.698039216, 0.839215686, 1.0),
            (0.415686275, 0.239215686, 0.603921569, 1.0),
            (1, 1, 0.521568627, 1.0),
        ]

        #idxCol = 0
        for idx, p in enumerate(self.profiles):
            #if idxCol > len(colors) - 1:
            #    idxCol = 0
            #x, pltSegs = p.getPlotSegments()
            #QgsMessageLog.logMessage('x: {0}'.format(x), 'VoGis')
            pltSegs = p.getPlotSegments()
            #QgsMessageLog.logMessage('pltSegs: {0}'.format(pltSegs), 'VoGis')
            lineColl = LineCollection(
                pltSegs,
                linewidths=2,
                linestyles='solid',
                #colors=colors[randrange(len(colors))],
                #colors=colors[idxCol],
                colors=colors[:len(settings.mapData.rasters.selectedRasters()
                                   )],
                picker=True,
                label='LBL')
            #lineColl.set_array(x)
            #lineColl.text.set_text('line label')
            self.subplot.add_collection(lineColl)
            #idxCol += 1
        #save inital view in history
        pltToolbar.push_current()
        #select pan tool
        pltToolbar.pan()
        self.pltToolbar = pltToolbar
        #(matplotlib.pyplot).tight_layout(True)
        #plt.tight_layout()
        #self.fig.tight_layout()
        QApplication.restoreOverrideCursor()