Skip to content
Snippets Groups Projects
profilevisualization.py 84.8 KiB
Newer Older
# -*- coding: utf-8 -*-
"""
/***************************************************************************
                              HUB TimeSeriesViewer
                              -------------------
        begin                : 2017-08-04
        git sha              : $Format:%H$
        copyright            : (C) 2017 by HU-Berlin
        email                : benjamin.jakimow@geo.hu-berlin.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
"""
# noinspection PyPep8Naming
import os, sys, pickle, datetime
from collections import OrderedDict
from qgis.gui import *
from qgis.core import *
from qgis.core import QgsExpression
from PyQt5.QtCore import *
from PyQt5.QtXml import *
from PyQt5.QtGui import *

from timeseriesviewer import jp, SETTINGS
from timeseriesviewer.timeseries import *
from timeseriesviewer.utils import SpatialExtent, SpatialPoint, px2geo
from timeseriesviewer.ui.docks import TsvDockWidgetBase, loadUI
from timeseriesviewer.plotstyling import PlotStyle, PlotStyleButton
Benjamin Jakimow's avatar
Benjamin Jakimow committed
from timeseriesviewer.pixelloader import PixelLoader, PixelLoaderTask
from timeseriesviewer.sensorvisualization import SensorListModel
from timeseriesviewer.temporalprofiles import *
Benjamin Jakimow's avatar
Benjamin Jakimow committed
from pyqtgraph import functions as fn
from pyqtgraph import AxisItem


import datetime

from osgeo import gdal, gdal_array
import numpy as np

DEBUG = False

OPENGL_AVAILABLE = False

try:
    import OpenGL
    OPENGL_AVAILABLE = True
    from pyqtgraph.opengl import *
    from OpenGL.GL import *

    class AxisGrid3D(GLGridItem):
        def __init__(self, *args, **kwds):
            super(AxisGrid3D, self).__init__(*args, **kwds)

            self.mXYRange = np.asarray([[0, 1], [0, 1]])
            self.mColor = QColor('grey')

        def setColor(self, color):
            self.mColor = QColor(color)

        def setXRange(self, x0, x1):
            self.mXYRange[0, 0] = x0
            self.mXYRange[0, 1] = x1

        def setYRange(self, y0, y1):
            self.mXYRange[1, 0] = y0
            self.mXYRange[1, 1] = y1

        def paint(self):
            self.setupGLState()

            if self.antialias:
                glEnable(GL_LINE_SMOOTH)
                glEnable(GL_BLEND)
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
                glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)

            glBegin(GL_LINES)

            x0, x1 = self.mXYRange[0, :]
            y0, y1 = self.mXYRange[1, :]
            xs, ys, zs = self.spacing()
            rx = x1-x0
            ry = y1-y0
            xvals = np.arange(x0, x1, rx/10)
            yvals = np.arange(y0, y1, ry/10)
            #todo: nice line breaks
            #yvals = np.arange(-y / 2., y / 2. + ys * 0.001, ys)
            c = fn.glColor(self.mColor)
            glColor4f(*c)
            for x in xvals:
                glVertex3f(x, yvals[0], 0)
                glVertex3f(x, yvals[-1], 0)
            for y in yvals:
                glVertex3f(xvals[0], y, 0)
                glVertex3f(xvals[-1], y, 0)

            glEnd()

    class Axis3D(GLAxisItem):

        def __init__(self, *args, **kwds):
            super(Axis3D, self).__init__(*args, **kwds)

            self.mRanges = np.asarray([[0,1],[0,1],[0,1]])
            self.mColors = [QColor('white'),QColor('white'),QColor('white')]
            self.mVisibility = [True, True, True]
            self.mLabels = ['X','Y','Z']

        def rangeMinima(self):
            return self.mRanges[:,0]

        def rangeMaxima(self):
            return self.mRanges[:,1]

        def _set(self, ax, vMin=None, vMax=None, label=None, color=None, visible=None):
            i = ['x','y','z'].index(ax.lower())
            if vMin is not None:
                self.mRanges[i][0] = vMin
            if vMax is not None:
                self.mRanges[i][1] = vMax
            if color is not None:
                self.mColors[i] = color
            if label is not None:
                self.mLabels[i] = label
            if visible is not None:
                self.mVisibility[i] = visible


        def setX(self, **kwds):
            self._set('x', **kwds)

        def setY(self, **kwds):
            self._set('y', **kwds)

        def setZ(self, **kwds):
            self._set('z', **kwds)

        def paint(self):
            # glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
            # glEnable( GL_BLEND )
            # glEnable( GL_ALPHA_TEST )
            self.setupGLState()

            if self.antialias:
                glEnable(GL_LINE_SMOOTH)
                glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)

            x0, y0, z0 = self.rangeMinima()
            x1, y1, z1 = self.rangeMaxima()
            glLineWidth(2.0)
            glBegin(GL_LINES)
            glColor4f(*fn.glColor(self.mColors[2]))

            glVertex3f(0, 0, z0)
            glVertex3f(0, 0, z1)

            glColor4f(*fn.glColor(self.mColors[1]))
            glVertex3f(0, y0, 0)
            glVertex3f(0, y1, 0)

            glColor4f(*fn.glColor(self.mColors[0]))
            glVertex3f(x0, 0, 0)
            glVertex3f(x1, 0, 0)

            glEnd()
            glLineWidth(1.0)

    class ViewWidget3D(GLViewWidget):

        def __init__(self, parent=None):
            super(ViewWidget3D, self).__init__(parent)
            self.mousePos = QPoint(-1,-1)
            self.setBackgroundColor(QColor('black'))
            self.setMouseTracking(True)


            self.mDataMinRanges = [0, 0, 0]
            self.mDataMaxRanges = [1, 1, 1]
            self.mDataN = 0
            self.glAxes = Axis3D()
            self.glGridItemXY = AxisGrid3D()
            self.glGridItemXZ = AxisGrid3D()
            self.glGridItemYZ = AxisGrid3D()
            self.glGridItemXZ.setVisible(False)
            self.glGridItemYZ.setVisible(False)
Loading
Loading full blame...