Skip to content
Snippets Groups Projects
main.py 46.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Benjamin Jakimow's avatar
    Benjamin Jakimow committed
    
    
            if level in [Qgis.Critical, Qgis.Warning]:
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
    
    
                self.ui.messageBar.pushMessage(tag, message, level=level)
                print(r'{}({}): {}'.format(tag, level, message))
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
    
    
                    if len(self.spatialTemporalVis.MVC) == 0:
    
                        self.spatialTemporalVis.createMapView()
    
                        #self.spatialTemporalVis.createMapView()
    
                    extent = self.mTimeSeries.maxSpatialExtent()
    
                    self.spatialTemporalVis.setSpatialExtent(extent)
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
    
    
        def saveTimeSeriesDefinition(self):
    
            s = settings()
    
            defFile = s.value('FILE_TS_DEFINITION')
            if defFile is not None:
                defFile = os.path.dirname(defFile)
    
    
            filters = "CSV (*.csv *.txt);;" + \
                      "All files (*.*)"
            path, filter = QFileDialog.getSaveFileName(caption='Save Time Series definition', filter=filters, directory=defFile)
    
            path = self.mTimeSeries.saveToFile(path)
    
                s.setValue('FILE_TS_DEFINITION', path)
    
        def loadTimeSeriesStack(self):
    
    
            from eotimeseriesviewer.stackedbandinput import StackedBandInputDialog
    
    
            d = StackedBandInputDialog(parent=self.ui)
            if d.exec_() == QDialog.Accepted:
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
                writtenFiles = d.saveImages()
    
                self.addTimeSeriesImages(writtenFiles)
    
    
        def loadExampleTimeSeries(self, n:int=None, loadAsync=True):
    
            """
            Loads an example time series
            :param n: int, max. number of images to load. Useful for developer test-cases
            """
    
            exampleDataDir = os.path.dirname(example.__file__)
            rasterFiles = list(file_search(exampleDataDir, '*.tif', recursive=True))
            vectorFiles = list(file_search(exampleDataDir, re.compile(r'.*\.(gpkg|shp)$'), recursive=True))
    
                n = len(rasterFiles)
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
            # ensure valid inputs for n
    
            n = min(n, len(rasterFiles))
    
            self.addTimeSeriesImages(rasterFiles[0:n], loadAsync=loadAsync)
    
    
            if len(vectorFiles) > 0:
    
    
                # make polygons transparent
    
    
                self.addVectorData(vectorFiles)
    
    
                for lyr in QgsProject.instance().mapLayers().values():
                    if isinstance(lyr, QgsVectorLayer) and lyr.source() in vectorFiles:
                        renderer = lyr.renderer()
                        if lyr.geometryType() == QgsWkbTypes.PolygonGeometry and isinstance(renderer, QgsSingleSymbolRenderer):
                            renderer = renderer.clone()
                            symbol = renderer.symbol()
                            if isinstance(symbol, QgsFillSymbol):
                                symbol.setOpacity(0.25)
                            lyr.setRenderer(renderer)
                        s = ""
    
    
        def timeSeries(self)->TimeSeries:
            """
            Returns the TimeSeries instance.
            :return: TimeSeries
            """
            return self.mTimeSeries
    
    unknown's avatar
    unknown committed
    
        # noinspection PyMethodMayBeStatic
        def tr(self, message):
            """Get the translation for a string using Qt translation API.
    
            We implement this ourselves since we do not inherit QObject.
    
            :param message: String for translation.
            :type message: str, QString
    
            :returns: Translated version of message.
            :rtype: QString
            """
            # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
    
    unknown's avatar
    unknown committed
    
    
        def unload(self):
    
            """Removes the plugin menu item and icon """
            self.iface.removeToolBarIcon(self.action)
    
    unknown's avatar
    unknown committed
    
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
        def show(self):
            self.ui.show()
    
    
    unknown's avatar
    unknown committed
        def clearLayoutWidgets(self, L):
            if L is not None:
                while L.count():
                    w = L.takeAt(0)
    
                    if w.widget():
                        w.widget().deleteLater()
    
    unknown's avatar
    unknown committed
                    #if w is not None:
                    #    w.widget().deleteLater()
    
    unknown's avatar
    unknown committed
    
    
        def addVectorData(self, files=None)->list:
            """
            Adds vector data
            :param files: vector layer sources
            :return: [list-of-QgsVectorLayers]
            """
            vectorLayers = []
    
    unknown's avatar
    unknown committed
            if files is None:
    
                s = settings()
    
                defDir = s.value('DIR_FILESEARCH')
    
                filters = QgsProviderRegistry.instance().fileVectorFilters()
                files, filter = QFileDialog.getOpenFileNames(directory=defDir, filter=filters)
    
                if len(files) > 0 and os.path.exists(files[0]):
                    dn = os.path.dirname(files[0])
                    s.setValue('DIR_FILESEARCH', dn)
    
            if files:
    
                from eotimeseriesviewer.mapvisualization import MapView
    
                from .externals.qps.layerproperties import subLayers
    
                for f in files:
                    vectorLayers.extend(subLayers(QgsVectorLayer(f)))
    
    
                if len(vectorLayers) > 0:
                    QgsProject.instance().addMapLayers(vectorLayers)
    
                    for mapView in self.mapViews():
                        assert isinstance(mapView, MapView)
    
                        for l in vectorLayers:
                            mapView.addLayer(l)
    
    
                        break # add to first mapview only
    
        def addTimeSeriesImages(self, files: list, loadAsync=True):
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
            """
            Adds images to the time series
            :param files:
            """
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
                defDir = s.value('dir_datasources')
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
    
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
                filters = QgsProviderRegistry.instance().fileRasterFilters()
    
                files, filter = QFileDialog.getOpenFileNames(directory=defDir, filter=filters)
    
    
                if len(files) > 0 and os.path.exists(files[0]):
                    dn = os.path.dirname(files[0])
    
    Benjamin Jakimow's avatar
    Benjamin Jakimow committed
                    s.setValue('dir_datasources', dn)
    
    unknown's avatar
    unknown committed
            if files:
    
                progressDialog = self._createProgressDialog()
                progressDialog.setRange(0, len(files))
    
                progressDialog.setLabelText('Start loading {} images....'.format(len(files)))
    
    
                    self.mTimeSeries.addSourcesAsync(files, progressDialog=progressDialog)
    
                    self.mTimeSeries.addSources(files, progressDialog=progressDialog)
    
                #QCoreApplication.processEvents()
    
                #self.mTimeSeries.addSources(files)
    
    unknown's avatar
    unknown committed
    
    
        def clearTimeSeries(self):
    
    unknown's avatar
    unknown committed
    
    
            self.mTimeSeries.beginResetModel()
    
    
    class SaveAllMapsDialog(QDialog, loadUI('saveallmapsdialog.ui')):
    
    
        def __init__(self, parent=None):
    
            super(SaveAllMapsDialog, self).__init__(parent)
            self.setupUi(self)
            self.setWindowTitle('Save Maps')
            assert isinstance(self.fileWidget, QgsFileWidget)
            assert isinstance(self.cbFileType, QComboBox)
    
            self.fileWidget.setStorageMode(QgsFileWidget.GetDirectory)
    
    
            formats = [('Portable Network Graphics (*.png)', 'PNG'),
                       ('Joint Photographic Experts Group (*.jpg)', 'JPG'),
                       ('Windows Bitmap (*.bmp)', 'BMP'),
                       ('Portable Bitmap (*.pbm)', 'PBM'),
                       ('Portable Graymap (*.pgm)', 'PGM'),
                       ('Portable Pixmap (*.ppm)', 'PPM'),
                       ('X11 Bitmap (*.xbm)', 'XBM'),
                       ('X11 Pixmap (*.xpm)', 'XPM'),
                       ]
    
    
    
    
            for t in formats:
                self.cbFileType.addItem(t[0], userData=t[1])
    
            self.fileWidget.fileChanged.connect(self.validate)
    
            self.buttonBox.button(QDialogButtonBox.Save).clicked.connect(lambda : self.setResult(QDialog.Accepted))
            self.buttonBox.button(QDialogButtonBox.Cancel).clicked.connect(lambda : self.setResult(QDialog.Rejected))
            self.validate()
    
        def validate(self, *args):
    
            b = os.path.isdir(self.directory())
            self.buttonBox.button(QDialogButtonBox.Save).setEnabled(b)
    
    
        def setDirectory(self, path:str):
            assert os.path.isdir(path)
            self.fileWidget.setFilePath(path)
    
    
        def directory(self)->str:
            """
            Returns the selected directory
            :return: str
            """
            return self.fileWidget.filePath()
    
        def fileType(self)->str:
            """
            Returns the selected file type
            :return:
            """
            return self.cbFileType.currentData(Qt.UserRole)
    
    
    
    
    def disconnect_signal(signal):
        while True:
            try:
                signal.disconnect()
            except TypeError:
                break