def calculatePointProfile(self, point, model, library): self.model = model self.library = library statName = self.getPointProfileStatNames()[0] self.removeClosedLayers(model) if point == None: return PlottingTool().clearData(self.dockwidget, model, library) self.profiles = [] #creating the plots of profiles for i in range(0 , model.rowCount()): self.profiles.append( {"layer": model.item(i,3).data(Qt.EditRole) } ) self.profiles[i][statName] = [] self.profiles[i]["l"] = [] layer = self.profiles[i]["layer"] if layer: try: ident = layer.dataProvider().identify(point, QgsRaster.IdentifyFormatValue ) except: ident = None else: ident = None #if ident is not None and ident.has_key(choosenBand+1): if ident is not None: self.profiles[i][statName] = ident.results().values() self.profiles[i]["l"] = ident.results().keys() self.setXAxisSteps() PlottingTool().attachCurves(self.dockwidget, self.profiles, model, library) PlottingTool().reScalePlot(self.dockwidget, self.profiles, model, library) self.setupTableTab(model)
def onClick(self, iface, wdg, mdl, plotlibrary, index): #action when clicking the tableview item = mdl.itemFromIndex(index) name = mdl.item(index.row(), 5).data(Qt.EditRole) if index.column() == 1: #modifying color color = QColorDialog().getColor(item.data(Qt.BackgroundRole)) mdl.setData(mdl.index(item.row(), 1, QModelIndex()), color, Qt.BackgroundRole) PlottingTool().changeColor(wdg, plotlibrary, color, name) elif index.column() == 0: #modifying checkbox isVisible = not (item.data(Qt.CheckStateRole)) mdl.setData(mdl.index(item.row(), 0, QModelIndex()), isVisible, Qt.CheckStateRole) PlottingTool().changeAttachCurve(wdg, plotlibrary, isVisible, name) else: return
def reScalePlot(self, param): # called when a spinbox value changed if type(param) != float: # don't execute it twice, for both valueChanged(int) and valueChanged(str) signals return if self.dockwidget.sbMinVal.value() == self.dockwidget.sbMaxVal.value() == 0: # don't execute it on init return PlottingTool().reScalePlot(self.dockwidget, self.profiles, self.model, self.library, autoMode = False)
def calculateProfil(self, points1, model1, library, vertline = True): self.pointstoDraw = points1 if self.pointstoDraw == None: return try: PlottingTool().clearData(self.dockwidget, self.profiles, library) PlottingTool().reScalePlot(self.wdg.scaleSlider.value(), self.dockwidget, self.profiles, library) except: pass self.profiles = [] if vertline: #Plotting vertical lines at the node of polyline draw PlottingTool().drawVertLine(self.dockwidget, self.pointstoDraw, library) #creating the plots of profiles for i in range(0 , model1.rowCount()): self.profiles.append( {"layer": model1.item(i,4).data(Qt.EditRole).toPyObject() } ) self.profiles[i]["band"] = model1.item(i,3).data(Qt.EditRole).toPyObject() - 1 self.profiles[i] = DataReaderTool().dataReaderTool(self.iface, self.tool, self.profiles[i], self.pointstoDraw, self.dockwidget.checkBox.isChecked()) PlottingTool().attachCurves(self.dockwidget, self.profiles, model1, library) PlottingTool().reScalePlot(self.dockwidget.scaleSlider.value(), self.dockwidget, self.profiles, library) #*********************** TAble tab ************************************************* try: #Reinitializing the table tab self.VLayout = self.dockwidget.scrollAreaWidgetContents.layout() while 1: child = self.VLayout.takeAt(0) if not child: break child.widget().deleteLater() except: self.VLayout = QVBoxLayout(self.dockwidget.scrollAreaWidgetContents) self.VLayout.setContentsMargins(9, -1, -1, -1) #Setup the table tab self.groupBox = [] self.pushButton = [] self.tableView = [] self.verticalLayout = [] for i in range(0 , model1.rowCount()): self.groupBox.append( QGroupBox(self.dockwidget.scrollAreaWidgetContents) ) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.groupBox[i].sizePolicy().hasHeightForWidth()) self.groupBox[i].setSizePolicy(sizePolicy) self.groupBox[i].setMinimumSize(QSize(0, 150)) self.groupBox[i].setMaximumSize(QSize(16777215, 150)) self.groupBox[i].setTitle(QApplication.translate("GroupBox" + str(i), self.profiles[i]["layer"].name(), None, QApplication.UnicodeUTF8)) self.groupBox[i].setObjectName(QString.fromUtf8("groupBox" + str(i))) self.verticalLayout.append( QVBoxLayout(self.groupBox[i]) ) self.verticalLayout[i].setObjectName(QString.fromUtf8("verticalLayout")) #The table self.tableView.append( QTableView(self.groupBox[i]) ) self.tableView[i].setObjectName(QString.fromUtf8("tableView" + str(i))) font = QFont("Arial", 8) column = len(self.profiles[i]["l"]) self.mdl = QStandardItemModel(2, column) for j in range(len(self.profiles[i]["l"])): self.mdl.setData(self.mdl.index(0, j, QModelIndex()) ,QVariant(self.profiles[i]["l"][j])) self.mdl.setData(self.mdl.index(0, j, QModelIndex()) ,font ,Qt.FontRole) self.mdl.setData(self.mdl.index(1, j, QModelIndex()) ,QVariant(self.profiles[i]["z"][j])) self.mdl.setData(self.mdl.index(1, j, QModelIndex()) ,font ,Qt.FontRole) self.tableView[i].verticalHeader().setDefaultSectionSize(18) self.tableView[i].horizontalHeader().setDefaultSectionSize(60) self.tableView[i].setModel(self.mdl) self.verticalLayout[i].addWidget(self.tableView[i]) #the copy to clipboard button self.pushButton.append( QPushButton(self.groupBox[i]) ) sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton[i].sizePolicy().hasHeightForWidth()) self.pushButton[i].setSizePolicy(sizePolicy) self.pushButton[i].setText(QApplication.translate("GroupBox", "Copy to clipboard", None, QApplication.UnicodeUTF8)) self.pushButton[i].setObjectName(QString.fromUtf8( str(i)) ) self.verticalLayout[i].addWidget(self.pushButton[i]) self.VLayout.addWidget(self.groupBox[i]) QObject.connect(self.pushButton[i], SIGNAL("clicked()"), self.copyTable)
def reScalePlot(self,scale): # called when scale slider moved PlottingTool().reScalePlot(self.dockwidget.scaleSlider.value(), self.dockwidget, self.profiles, self.dockwidget.comboBox_2.currentText () )
def changeXAxisStepType(self, newType): if self.xAxisStepType == newType: return else: self.xAxisStepType = newType PlottingTool().resetAxis(self.dockwidget, self.library)
def calculatePolygonProfile(self, geometry, crs, model, library): self.model = model self.library = library self.removeClosedLayers(model) if geometry is None or geometry.isEmpty(): return PlottingTool().clearData(self.dockwidget, model, library) self.profiles = [] #creating the plots of profiles for i in range(0 , model.rowCount()): self.profiles.append( {"layer": model.item(i,3).data(Qt.EditRole) } ) self.profiles[i]["l"] = [] for statistic in self.getPolygonProfileStatNames(): self.profiles[i][statistic] = [] # Get intersection between polygon geometry and raster following ZonalStatistics code rasterDS = gdal.Open(self.profiles[i]["layer"].source(), gdal.GA_ReadOnly) geoTransform = rasterDS.GetGeoTransform() cellXSize = abs(geoTransform[1]) cellYSize = abs(geoTransform[5]) rasterXSize = rasterDS.RasterXSize rasterYSize = rasterDS.RasterYSize rasterBBox = QgsRectangle(geoTransform[0], geoTransform[3] - cellYSize * rasterYSize, geoTransform[0] + cellXSize * rasterXSize, geoTransform[3]) rasterGeom = QgsGeometry.fromRect(rasterBBox) memVectorDriver = ogr.GetDriverByName('Memory') memRasterDriver = gdal.GetDriverByName('MEM') intersectedGeom = rasterGeom.intersection(geometry) ogrGeom = ogr.CreateGeometryFromWkt(intersectedGeom.exportToWkt()) bbox = intersectedGeom.boundingBox() xMin = bbox.xMinimum() xMax = bbox.xMaximum() yMin = bbox.yMinimum() yMax = bbox.yMaximum() (startColumn, startRow) = self.mapToPixel(xMin, yMax, geoTransform) (endColumn, endRow) = self.mapToPixel(xMax, yMin, geoTransform) width = endColumn - startColumn height = endRow - startRow if width == 0 or height == 0: return srcOffset = (startColumn, startRow, width, height) newGeoTransform = ( geoTransform[0] + srcOffset[0] * geoTransform[1], geoTransform[1], 0.0, geoTransform[3] + srcOffset[1] * geoTransform[5], 0.0, geoTransform[5], ) # Create a temporary vector layer in memory memVDS = memVectorDriver.CreateDataSource('out') memLayer = memVDS.CreateLayer('poly', crs, ogr.wkbPolygon) ft = ogr.Feature(memLayer.GetLayerDefn()) ft.SetGeometry(ogrGeom) memLayer.CreateFeature(ft) ft.Destroy() # Rasterize it rasterizedDS = memRasterDriver.Create('', srcOffset[2], srcOffset[3], 1, gdal.GDT_Byte) rasterizedDS.SetGeoTransform(newGeoTransform) gdal.RasterizeLayer(rasterizedDS, [1], memLayer, burn_values=[1]) rasterizedArray = rasterizedDS.ReadAsArray() for bandNumber in range(1, rasterDS.RasterCount+1): rasterBand = rasterDS.GetRasterBand(bandNumber) noData = rasterBand.GetNoDataValue() if noData is None: noData = np.nan scale = rasterBand.GetScale() if scale is None: scale = 1.0 offset = rasterBand.GetOffset() if offset is None: offset = 0.0 srcArray = rasterBand.ReadAsArray(*srcOffset) srcArray = srcArray*scale+offset masked = np.ma.MaskedArray(srcArray, mask=np.logical_or.reduce(( srcArray == noData, np.logical_not(rasterizedArray), np.isnan(srcArray)))) self.profiles[i]["l"].append(bandNumber) self.profiles[i]["count"].append(float(masked.count())) self.profiles[i]["max"].append(float(masked.max())) self.profiles[i]["mean"].append(float(masked.mean())) self.profiles[i]["median"].append(float(np.ma.median(masked))) self.profiles[i]["min"].append(float(masked.min())) self.profiles[i]["range"].append(float(masked.max()) - float(masked.min())) self.profiles[i]["std"].append(float(masked.std())) self.profiles[i]["sum"].append(float(masked.sum())) self.profiles[i]["unique"].append(np.unique(masked.compressed()).size) self.profiles[i]["var"].append(float(masked.var())) memVDS = None rasterizedDS = None rasterDS = None self.setXAxisSteps() PlottingTool().attachCurves(self.dockwidget, self.profiles, model, library) PlottingTool().reScalePlot(self.dockwidget, self.profiles, model, library) self.setupTableTab(model)