4

I have this code to read data from raster layer QGIS in var u1 I put the value from read raster layer but x and y put it manually by QgsPointXY(454755, 3910940) now I want the x,y value by mouse click to raster layer.

  layer = self.iface.activeLayer()
  val, res = layer.dataProvider().sample(QgsPointXY(454755, 3910940), 1)
  u1=val

  self.dlg.textBrowser.clear()
  if self.dlg.radioButton.clicked:
      ch=1
  elif self.dlg.radioButton_2.clicked:
       ch=2
    
  Q=float(self.dlg.lineEdit.text())
  H=float(self.dlg.lineEdit_3.text())
       
  def equation1(list2):
      ly=float(list2[2]*math.pow(x,list2[3]))
      lz=float(list2[0]*math.pow(x,list2[1]))
      C=(Q/(math.pi*u1*ly*lz))*math.exp(-0.5*(math.pow((H/lz),2)))      
      return(self.dlg.textBrowser.append(format(C)))

  if (ch==1):
      for x in arange(0,10,0.1):
       if (0 < x <= 0.2 ):list1=[90.673,0.93198,18.333,1.8096];equation1(list1);
       elif  (0.2 < x <= 0.4 ):list1=[98.483,0.98332,18.333,1.8096];equation1(list1)
       elif  (0.4 < x < 5 ):list1=[109.3,1.0971,18.333,1.8096];equation1(list1)
       elif  (x >= 5):lz=5000;ly=5000;C=Q/(math.pi*u1*ly*lz)*math.exp(-0.5*(pow((H/lz),2)))
 
  elif (ch==2):
     for x in arange(0,10,0.1): 
      if (0 < x <= 0.2):list1=[61.141,0.91465,12.5,1.857];equation1(list1)
      elif  (0.2 < x <= 0.4 ):list1=[61.141,0.91465,12.5,1.857];equation1(list1)
      elif  (0.4 < x < 5):list1=[61.141,0.91465,12.5,1.857];equation1(list1)  
      elif  (x >= 5):lz=5000;ly=5000;C=Q/(math.pi*u1*ly*lz)*math.exp(-0.5*(pow((H/lz),2)))    
  else:
   self.dlg.textBrowser.append("Sorry you must choose Select Stability Classes of Pasquill ")*
 

1 Answer 1

4

You can adapt following PyQGIS code in your plugin (see corresponding comment for guidance).

from qgis.gui import QgsMapToolEmitPoint

def display_point( pointTool ): # function before run method as display_point(self, point, button)
    x = pointTool[0]
    y = pointTool[1]
    
    print(x, y)

    raster = iface.activeLayer()

    provider = raster.dataProvider()
    extent = raster.extent()
    xSize = raster.rasterUnitsPerPixelX()
    ySize = raster.rasterUnitsPerPixelY()

    xmin, ymin, xmax, ymax = extent.toRectF().getCoords()
    row = int((ymax - y)/ySize)
    col = int((x - xmin)/xSize)
    
    print(row, col)
    
    value = provider.identify(QgsPointXY(x, y), 
                              QgsRaster.IdentifyFormatValue).results()[1]
    
    print(value)

    
# a reference to our map canvas 
canvas = iface.mapCanvas() # in __init__ as self.canvas = self.iface.mapCanvas()

# this QGIS tool emits as QgsPoint after each click on the map canvas
pointTool = QgsMapToolEmitPoint(canvas) # in __init__ as self.pointTool = QgsMapToolEmitPoint(self.canvas)

pointTool.canvasClicked.connect( display_point ) # in initGui as self.pointTool.canvasClicked.connect(self.display_point)

canvas.setMapTool( pointTool ) # in run method as self.canvas.setMapTool(self.pointTool)

I tried above code in QGIS Python Console, as it can be observed in following image, and it worked as expected (corroborated with QGIS Value Tool plugin).

enter image description here

1
  • thanks , its helpful thank you Jun 6, 2021 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.