Guest User

Untitled

a guest
Jan 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. from PyQt4.QtCore import *
  2. from osgeo import gdal, ogr
  3. from osgeo.gdalconst import *
  4. import numpy as np
  5.  
  6.  
  7. iface = qgis.utils.iface
  8.  
  9. layers = iface.legendInterface().layers()
  10. raster = gdal.Open("F:\MASTER GEOM\MasterGeomatique_data\PLEIADES\PLEIADES_20130415_COLNAT.TIF")
  11. print(raster)
  12.  
  13. transform = raster.GetGeoTransform()
  14. xOrigin = transform[0]
  15. yOrigin = transform[3]
  16. pixelWidth = transform[1]
  17. pixelHeight = transform[5]
  18. print xOrigin, yOrigin
  19.  
  20.  
  21. shp = ogr.Open("F:essai.shp")
  22. lyr = shp.GetLayer()
  23. feat = lyr.GetNextFeature()
  24. geom = feat.GetGeometryRef()
  25.  
  26. if (geom.GetGeometryName() == 'POLYGON'):
  27. ring = geom.GetGeometryRef(0)
  28. numpoints = ring.GetPointCount()
  29. pointsX = []; pointsY = []
  30. for p in range(numpoints):
  31. lon, lat, z = ring.GetPoint(p)
  32. pointsX.append(lon)
  33. pointsY.append(lat)
  34. #print (pointsX)
  35. xmin = min(pointsX)
  36. xmax = max(pointsX)
  37. ymin = min(pointsY)
  38. ymax = max(pointsY)
  39. print xmin, ymin
  40. print xmax, ymax
  41. # Specify offset and rows and columns to read
  42. xoff = int((xmin - xOrigin)/pixelWidth)
  43. yoff = int((yOrigin - ymax)/pixelWidth)
  44. xcount = int((xmax - xmin)/pixelWidth)+1
  45. ycount = int((ymax - ymin)/pixelWidth)+1
  46. print xoff, yoff
  47. print xcount, ycount
  48.  
  49. bandList = []
  50. # retrieve pixel on region of interest
  51. for i in range(raster.RasterCount):
  52. band = raster.GetRasterBand(i+1)
  53. data = band.ReadAsArray(xoff, yoff, xcount, ycount)
  54. bandList.append(data)
  55.  
  56. # Apply transparency using QgsRasterTransparency.TransparentThreeValuePixel()
  57. active_layer = raster
  58. raster_transparency = active_layer.renderer().rasterTransparency()
  59. ltr = QgsRasterTransparency.TransparentThreeValuePixel()
  60. tr_list = []
  61. ltr.min = np.min(bandList)
  62. ltr.max = np.max(bandList)
  63. ltr.percentTransparent = 100
  64. tr_list.append(ltr)
  65. active_layer.renderer().rasterTransparency().setTransparentThreeValuePixelList(tr_list)
  66. active_layer.triggerRepaint()
  67.  
  68. # Refresh canvas
  69. QgsMapLayerRegistry.instance().addMapLayer(raster)
  70.  
  71. # Message d'erreur : AttributeError: 'Dataset' object has no attribute 'renderer'
Add Comment
Please, Sign In to add comment