def addLayer(self, iface, mdl, layer1=None): if layer1 == None: templist = [] j = 0 # Ask the layer by a input dialog for i in range(0, iface.mapCanvas().layerCount()): donothing = False layer = iface.mapCanvas().layer(i) if isProfilable(layer): for j in range(0, mdl.rowCount()): if str(mdl.item(j, 2).data(Qt.EditRole)) == str(layer.name()): donothing = True else: donothing = True if donothing == False: templist += [[layer, layer.name()]] if len(templist) == 0: QMessageBox.warning(iface.mainWindow(), "Profile tool", "No raster to add") return else: testqt, ok = QInputDialog.getItem( iface.mainWindow(), "Layer selector", "Choose layer", [templist[k][1] for k in range(len(templist))], False, ) if ok: for i in range(0, len(templist)): if templist[i][1] == testqt: layer2 = templist[i][0] else: return else: layer2 = layer1 # Complete the tableview row = mdl.rowCount() mdl.insertRow(row) mdl.setData(mdl.index(row, 0, QModelIndex()), True, Qt.CheckStateRole) mdl.item(row, 0).setFlags(Qt.ItemIsSelectable) lineColour = Qt.red mdl.setData(mdl.index(row, 1, QModelIndex()), QColor(lineColour), Qt.BackgroundRole) mdl.item(row, 1).setFlags(Qt.NoItemFlags) mdl.setData(mdl.index(row, 2, QModelIndex()), layer2.name()) mdl.item(row, 2).setFlags(Qt.NoItemFlags) mdl.setData(mdl.index(row, 3, QModelIndex()), layer2) mdl.item(row, 3).setFlags(Qt.NoItemFlags) mdl.setData(mdl.index(row, 4, QModelIndex()), "") mdl.item(row, 4).setFlags(Qt.NoItemFlags) self.layerAddedOrRemoved.emit()
def addLayer(self, iface, mdl, layer1=None): if layer1 == None: templist = [] j = 0 # Ask the layer by a input dialog for i in range(0, iface.mapCanvas().layerCount()): donothing = False layer = iface.mapCanvas().layer(i) if isProfilable(layer): for j in range(0, mdl.rowCount()): if str(mdl.item(j, 2).data(Qt.EditRole)) == str( layer.name()): donothing = True else: donothing = True if donothing == False: templist += [[layer, layer.name()]] if len(templist) == 0: QMessageBox.warning(iface.mainWindow(), "Profile tool", "No raster to add") return else: testqt, ok = QInputDialog.getItem( iface.mainWindow(), "Layer selector", "Choose layer", [templist[k][1] for k in range(len(templist))], False) if ok: for i in range(0, len(templist)): if templist[i][1] == testqt: layer2 = templist[i][0] else: return else: layer2 = layer1 # Ask the Band by a input dialog #First, if isProfilable, considerate the real band number (instead of band + 1 for raster) if layer2.type() == layer2.PluginLayer and isProfilable(layer2): self.bandoffset = 0 typename = 'parameter' else: self.bandoffset = 1 typename = 'band' if layer2.bandCount() != 1: listband = [] for i in range(0, layer2.bandCount()): listband.append(str(i + self.bandoffset)) testqt, ok = QInputDialog.getItem(iface.mainWindow(), typename + " selector", "Choose the " + typename, listband, False) if ok: choosenBand = int(testqt) - self.bandoffset else: return 2 else: choosenBand = 0 #Complete the tableview row = mdl.rowCount() mdl.insertRow(row) mdl.setData(mdl.index(row, 0, QModelIndex()), True, Qt.CheckStateRole) mdl.item(row, 0).setFlags(Qt.ItemIsSelectable) lineColour = Qt.red if layer2.type( ) == layer2.PluginLayer and layer2.LAYER_TYPE == 'crayfish_viewer': lineColour = Qt.blue mdl.setData(mdl.index(row, 1, QModelIndex()), QColor(lineColour), Qt.BackgroundRole) mdl.item(row, 1).setFlags(Qt.NoItemFlags) mdl.setData(mdl.index(row, 2, QModelIndex()), layer2.name()) mdl.item(row, 2).setFlags(Qt.NoItemFlags) mdl.setData(mdl.index(row, 3, QModelIndex()), choosenBand + self.bandoffset) mdl.item(row, 3).setFlags(Qt.NoItemFlags) mdl.setData(mdl.index(row, 4, QModelIndex()), layer2) mdl.item(row, 4).setFlags(Qt.NoItemFlags) self.layerAddedOrRemoved.emit()
def dataReaderTool(self, iface1,tool1, profile1, pointstoDraw1, fullresolution1): """ Return a dictionnary : {"layer" : layer read, "band" : band read, "l" : array of computed lenght, "z" : array of computed z """ #init self.tool = tool1 #needed to transform point coordinates self.profiles = profile1 #profile with layer and band to compute self.pointstoDraw = pointstoDraw1 #the polyline to compute self.iface = iface1 #QGis interface to show messages in status bar layer = self.profiles["layer"] choosenBand = self.profiles["band"] #Get the values on the lines l = [] z = [] x = [] y = [] lbefore = 0 for i in range(0,len(self.pointstoDraw)-2): # work for each segment of polyline # for each polylines, set points x,y with map crs (%D) and layer crs (%C) pointstoCal1 = self.tool.toLayerCoordinates(self.profiles["layer"] , QgsPoint(self.pointstoDraw[i][0],self.pointstoDraw[i][1])) pointstoCal2 = self.tool.toLayerCoordinates(self.profiles["layer"] , QgsPoint(self.pointstoDraw[i+1][0],self.pointstoDraw[i+1][1])) x1D = float(self.pointstoDraw[i][0]) y1D = float(self.pointstoDraw[i][1]) x2D = float(self.pointstoDraw[i+1][0]) y2D = float(self.pointstoDraw[i+1][1]) x1C = float(pointstoCal1.x()) y1C = float(pointstoCal1.y()) x2C = float(pointstoCal2.x()) y2C = float(pointstoCal2.y()) #lenght between (x1,y1) and (x2,y2) tlC = sqrt (((x2C-x1C)*(x2C-x1C)) + ((y2C-y1C)*(y2C-y1C))) #Set the res of calcul try: res = min(self.profiles["layer"].rasterUnitsPerPixelX(),self.profiles["layer"].rasterUnitsPerPixelY()) * tlC / max(abs(x2C-x1C), abs(y2C-y1C)) # res depend on the angle of ligne with normal except ZeroDivisionError: res = min(self.profiles["layer"].rasterUnitsPerPixelX(),self.profiles["layer"].rasterUnitsPerPixelY()) * 1.2 #enventually use bigger step, wether full res is selected or not steps = 1000 # max graph width in pixels if fullresolution1: steps = int(tlC/res) else: if res != 0 and tlC/res < steps: steps = int(tlC/res) else: steps = 1000 if steps < 1: steps = 1 # calculate dx, dy and dl for one step dxD = (x2D - x1D) / steps dyD = (y2D - y1D) / steps dlD = sqrt ((dxD*dxD) + (dyD*dyD)) dxC = (x2C - x1C) / steps dyC = (y2C - y1C) / steps #dlC = sqrt ((dxC*dxC) + (dyC*dyC)) stepp = steps / 10 if stepp == 0: stepp = 1 progress = "Creating profile: " temp = 0 # reading data if i == 0: debut = 0 else: debut = 1 for n in range(debut, steps+1): l += [dlD * n + lbefore] xC = x1C + dxC * n yC = y1C + dyC * n attr = 0 if layer.type() == layer.PluginLayer and isProfilable(layer): ident = layer.identify(QgsPoint(xC,yC)) try: attr = float(ident[1].values()[choosenBand]) except: pass else: #RASTER LAYERS # this code adapted from valuetool plugin ident = layer.dataProvider().identify(QgsPoint(xC,yC), QgsRaster.IdentifyFormatValue ) #if ident is not None and ident.has_key(choosenBand+1): if ident is not None and (choosenBand in ident.results()): attr = ident.results()[choosenBand] #if attr is None: # attr=float("nan") #print(attr) #if layer.dataProvider().isNoDataValue ( choosenBand+1, attr ): #attr = 0 #print "Null cell value catched as zero!" # For none values, profile height = 0. It's not elegant... z += [attr] x += [xC] y += [yC] temp = n if n % stepp == 0: progress += "|" self.iface.mainWindow().statusBar().showMessage(progress) lbefore = l[len(l)-1] #End of polyline analysis #filling the main data dictionary "profiles" self.profiles["l"] = l self.profiles["z"] = z self.profiles["x"] = x self.profiles["y"] = y self.iface.mainWindow().statusBar().showMessage("") return self.profiles
def addLayer(self , iface, mdl, layer1 = None): if layer1 == None: templist=[] j=0 # Ask the layer by a input dialog for i in range(0, iface.mapCanvas().layerCount()): donothing = False layer = iface.mapCanvas().layer(i) if isProfilable(layer): for j in range(0, mdl.rowCount()): if str(mdl.item(j,2).data(Qt.EditRole)) == str(layer.name()): donothing = True else: donothing = True if donothing == False: templist += [[layer, layer.name()]] if len(templist) == 0: QMessageBox.warning(iface.mainWindow(), "Profile tool", "No raster to add") return else: testqt, ok = QInputDialog.getItem(iface.mainWindow(), "Layer selector", "Choose layer", [templist[k][1] for k in range( len(templist) )], False) if ok: for i in range (0,len(templist)): if templist[i][1] == testqt: layer2 = templist[i][0] else: return else : layer2 = layer1 # Ask the Band by a input dialog #First, if isProfilable, considerate the real band number (instead of band + 1 for raster) if layer2.type() == layer2.PluginLayer and isProfilable(layer2): self.bandoffset = 0 typename = 'parameter' else: self.bandoffset = 1 typename = 'band' if layer2.bandCount() != 1: listband = [] for i in range(0,layer2.bandCount()): listband.append(str(i+self.bandoffset)) testqt, ok = QInputDialog.getItem(iface.mainWindow(), typename + " selector", "Choose the " + typename, listband, False) if ok : choosenBand = int(testqt) - self.bandoffset else: return 2 else: choosenBand = 0 #Complete the tableview row = mdl.rowCount() mdl.insertRow(row) mdl.setData( mdl.index(row, 0, QModelIndex()) ,True, Qt.CheckStateRole) mdl.item(row,0).setFlags(Qt.ItemIsSelectable) lineColour = Qt.red if layer2.type() == layer2.PluginLayer and layer2.LAYER_TYPE == 'crayfish_viewer': lineColour = Qt.blue mdl.setData( mdl.index(row, 1, QModelIndex()) ,QColor(lineColour) , Qt.BackgroundRole) mdl.item(row,1).setFlags(Qt.NoItemFlags) mdl.setData( mdl.index(row, 2, QModelIndex()) ,layer2.name()) mdl.item(row,2).setFlags(Qt.NoItemFlags) mdl.setData( mdl.index(row, 3, QModelIndex()) ,choosenBand + self.bandoffset) mdl.item(row,3).setFlags(Qt.NoItemFlags) mdl.setData( mdl.index(row, 4, QModelIndex()) ,layer2) mdl.item(row,4).setFlags(Qt.NoItemFlags) self.layerAddedOrRemoved.emit()
def dataReaderTool(self, iface1, tool1, profile1, pointstoDraw1, fullresolution1): """ Return a dictionnary : {"layer" : layer read, "band" : band read, "l" : array of computed lenght, "z" : array of computed z """ #init self.tool = tool1 #needed to transform point coordinates self.profiles = profile1 #profile with layer and band to compute self.pointstoDraw = pointstoDraw1 #the polyline to compute self.iface = iface1 #QGis interface to show messages in status bar layer = self.profiles["layer"] choosenBand = self.profiles["band"] #Get the values on the lines l = [] z = [] x = [] y = [] lbefore = 0 for i in range(0, len(self.pointstoDraw) - 2): # work for each segment of polyline # for each polylines, set points x,y with map crs (%D) and layer crs (%C) pointstoCal1 = self.tool.toLayerCoordinates( self.profiles["layer"], QgsPoint(self.pointstoDraw[i][0], self.pointstoDraw[i][1])) pointstoCal2 = self.tool.toLayerCoordinates( self.profiles["layer"], QgsPoint(self.pointstoDraw[i + 1][0], self.pointstoDraw[i + 1][1])) x1D = float(self.pointstoDraw[i][0]) y1D = float(self.pointstoDraw[i][1]) x2D = float(self.pointstoDraw[i + 1][0]) y2D = float(self.pointstoDraw[i + 1][1]) x1C = float(pointstoCal1.x()) y1C = float(pointstoCal1.y()) x2C = float(pointstoCal2.x()) y2C = float(pointstoCal2.y()) #lenght between (x1,y1) and (x2,y2) tlC = sqrt(((x2C - x1C) * (x2C - x1C)) + ((y2C - y1C) * (y2C - y1C))) #Set the res of calcul try: res = min( self.profiles["layer"].rasterUnitsPerPixelX(), self.profiles["layer"].rasterUnitsPerPixelY()) * tlC / max( abs(x2C - x1C), abs(y2C - y1C) ) # res depend on the angle of ligne with normal except ZeroDivisionError: res = min(self.profiles["layer"].rasterUnitsPerPixelX(), self.profiles["layer"].rasterUnitsPerPixelY()) * 1.2 #enventually use bigger step, wether full res is selected or not steps = 1000 # max graph width in pixels if fullresolution1: steps = int(tlC / res) else: if res != 0 and tlC / res < steps: steps = int(tlC / res) else: steps = 1000 if steps < 1: steps = 1 # calculate dx, dy and dl for one step dxD = (x2D - x1D) / steps dyD = (y2D - y1D) / steps dlD = sqrt((dxD * dxD) + (dyD * dyD)) dxC = (x2C - x1C) / steps dyC = (y2C - y1C) / steps #dlC = sqrt ((dxC*dxC) + (dyC*dyC)) stepp = steps / 10 if stepp == 0: stepp = 1 progress = "Creating profile: " temp = 0 # reading data if i == 0: debut = 0 else: debut = 1 for n in range(debut, steps + 1): l += [dlD * n + lbefore] xC = x1C + dxC * n yC = y1C + dyC * n attr = 0 if layer.type() == layer.PluginLayer and isProfilable(layer): ident = layer.identify(QgsPoint(xC, yC)) try: attr = float(ident[1].values()[choosenBand]) except: pass else: #RASTER LAYERS # this code adapted from valuetool plugin ident = layer.dataProvider().identify( QgsPoint(xC, yC), QgsRaster.IdentifyFormatValue) #if ident is not None and ident.has_key(choosenBand+1): if ident is not None and (choosenBand in ident.results()): attr = ident.results()[choosenBand] #if attr is None: # attr=float("nan") #print(attr) #if layer.dataProvider().isNoDataValue ( choosenBand+1, attr ): #attr = 0 #print "Null cell value catched as zero!" # For none values, profile height = 0. It's not elegant... z += [attr] x += [xC] y += [yC] temp = n if n % stepp == 0: progress += "|" self.iface.mainWindow().statusBar().showMessage(progress) lbefore = l[len(l) - 1] #End of polyline analysis #filling the main data dictionary "profiles" self.profiles["l"] = l self.profiles["z"] = z self.profiles["x"] = x self.profiles["y"] = y self.iface.mainWindow().statusBar().showMessage("") return self.profiles