<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:DengXian;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:DengXian;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
pre
{mso-style-priority:99;
mso-style-link:"HTML \9884\8BBE\683C\5F0F \5B57\7B26";
margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.HTML
{mso-style-name:"HTML \9884\8BBE\683C\5F0F \5B57\7B26";
mso-style-priority:99;
mso-style-link:"HTML \9884\8BBE\683C\5F0F";
font-family:"Courier New";}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
span.EmailStyle20
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle21
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi, All<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I try to visualize the numpy.array matrix ‘m’ as a raster layer from Qgis. Because I have to show the matrix dynamically, I do not want to save the raster to *.tif file.<o:p></o:p></p>
<p class="MsoNormal">Is there any way to do this. I can create raster dataset using gdal library, but do not know how to create a raster layer to show on Qgis window. The code is as follows. Thank you so much!<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="background:white"><b><span style="font-size:12.0pt;font-family:SimSun;color:navy">def
</span></b><span style="font-size:12.0pt;font-family:SimSun;color:black">show_matrix_as_raster(</span><span style="font-size:12.0pt;font-family:SimSun;color:#94558D">self</span><span style="font-size:12.0pt;font-family:SimSun;color:black">, m, lng_min, lat_min,
pixelWidth, pixelHeight):<o:p></o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:12.0pt;font-family:SimSun;color:black"> cols = m.shape[</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">1</span><span style="font-size:12.0pt;font-family:SimSun;color:black">]<br>
rows = m.shape[</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">0</span><span style="font-size:12.0pt;font-family:SimSun;color:black">]<br>
<br>
driver = gdal.GetDriverByName(</span><b><span style="font-size:12.0pt;font-family:SimSun;color:green">'MEM'</span></b><span style="font-size:12.0pt;font-family:SimSun;color:black">)<br>
</span><b><span style="font-size:12.0pt;font-family:SimSun;color:green"> </span>
</b><span style="font-size:12.0pt;font-family:SimSun;color:black">outRaster = driver.Create('', cols, rows,
</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">1</span><span style="font-size:12.0pt;font-family:SimSun;color:black">, gdal.GDT_Byte) # create dataset in memory<br>
outRaster.SetGeoTransform((lng_min, pixelWidth, </span><span style="font-size:12.0pt;font-family:SimSun;color:blue">0</span><span style="font-size:12.0pt;font-family:SimSun;color:black">, lat_min,
</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">0</span><span style="font-size:12.0pt;font-family:SimSun;color:black">, pixelHeight))<br>
outband = outRaster.GetRasterBand(</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">1</span><span style="font-size:12.0pt;font-family:SimSun;color:black">)<br>
outband.WriteArray(m)<br>
outRasterSRS = osr.SpatialReference()<br>
outRasterSRS.ImportFromEPSG(</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">4326</span><span style="font-size:12.0pt;font-family:SimSun;color:black">)<br>
outRaster.SetProjection(outRasterSRS.ExportToWkt())<br>
outband.FlushCache()<br>
<br>
</span><span style="font-size:12.0pt;font-family:SimSun;color:#94558D">layer </span>
<span style="font-size:12.0pt;font-family:SimSun;color:black">= QgsRasterLayer( file_name_???, layer_name) # how to create QgsRsterLayer from
<span lang="ZH-CN">‘</span>outRaster<span lang="ZH-CN">’</span>, rather than file?<br>
QgsMapLayerRegistry.instance().addMapLayer( </span><span style="font-size:12.0pt;font-family:SimSun;color:#94558D">layer</span><span style="font-size:12.0pt;font-family:SimSun;color:black"> )<br>
<br>
</span><i><span style="font-size:12.0pt;font-family:SimSun;color:gray">###### render<br>
##<br>
</span></i><span style="font-size:12.0pt;font-family:SimSun;color:black">myGrayRenderer = QgsSingleBandGrayRenderer(layer.dataProvider(),
</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">1</span><span style="font-size:12.0pt;font-family:SimSun;color:black">)<br>
layer.setRenderer(myGrayRenderer)<br>
renderer = layer.renderer()<br>
uses_band = renderer.usesBands()<br>
<br>
myType = renderer.dataType(uses_band[</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">0</span><span style="font-size:12.0pt;font-family:SimSun;color:black">])<br>
myEnhancement = QgsContrastEnhancement(myType)<br>
contrast_enhancement = QgsContrastEnhancement.StretchToMinimumMaximum<br>
myEnhancement.setContrastEnhancementAlgorithm(contrast_enhancement, </span><span style="font-size:12.0pt;font-family:SimSun;color:navy">True</span><span style="font-size:12.0pt;font-family:SimSun;color:black">)<br>
<br>
myEnhancement.setMinimumValue(m.min())<br>
myEnhancement.setMaximumValue(m.max())<br>
<br>
layer.renderer().setContrastEnhancement(myEnhancement)<br>
layer.renderer().setOpacity(</span><span style="font-size:12.0pt;font-family:SimSun;color:blue">0.5</span><span style="font-size:12.0pt;font-family:SimSun;color:black">)<br>
layer.triggerRepaint()<br>
<br>
</span><b><span style="font-size:12.0pt;font-family:SimSun;color:navy">if </span>
</b><span style="font-size:12.0pt;font-family:SimSun;color:black">layer.isValid()
</span><b><span style="font-size:12.0pt;font-family:SimSun;color:navy">is </span>
</b><span style="font-size:12.0pt;font-family:SimSun;color:navy">False</span><span style="font-size:12.0pt;font-family:SimSun;color:black">:<br>
</span><b><span style="font-size:12.0pt;font-family:SimSun;color:navy">print
</span></b><b><span style="font-size:12.0pt;font-family:SimSun;color:green">"Unable to show the raster layer."</span></b><span style="font-size:12.0pt;font-family:SimSun;color:black"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best regards!<o:p></o:p></p>
<p class="MsoNormal"><span style="color:#1F497D">Liye,</span><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>