<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span>Hi there,</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span>I am having a problem with gdal.RasterizeLayer() in Python. I look at the documentation and code example in forums and tutorial but I can't find a solution to my problem.</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span>My code is supposed to loops thru a shape files, with countries for example, and create a boolean image of each country but the images always come as all black pixels. It</span><span> should use GDAL and OGR memory drivers but in order to debug the issue,
 I am saving one GEOJSON and one GTIFF file for each country.</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
What is most intriguing is that I can run gdal_rasterize with any of the countries pairs of file and it works as expected. I open it on QGIS and it all looks perfectly georeferenced too. The resolution is coming from another remote sensing image that I am using
 as reference. And again, everything works fine with the gdal_rasterize command, so it should be right.</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
Here is my code:</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span style="font-family: "Courier New",monospace;">        geom_ds = ogr.GetDriverByName('GEOJSON').CreateDataSource('E:\TMP\MEMORY_%s.JSON' % name)</span><span><br>
</span>
<div><span style="font-family: "Courier New",monospace;">        geom_lyr = geom_ds.CreateLayer('MEMORY_LAYER', geom_type=ogr.wkbPolygon, srs=ref_srs)​</span></div>
<div><span style="font-family: "Courier New",monospace;">        </span><span style="font-family: "Courier New",monospace;">geom_feat = ogr.Feature(geom_lyr.GetLayerDefn())​</span></div>
<div><span style="font-family: "Courier New",monospace;">        geom_feat.SetGeometry(geom)​</span></div>
<div><span style="font-family: "Courier New",monospace;">       </span><span style="font-family: "Courier New",monospace;"> geom_lyr.CreateFeature(geom_feat)​</span></div>
<div><span style="font-family: "Courier New",monospace;"><br>
</span></div>
<div><span style="font-family: "Courier New",monospace;">        mem_ds = gdal.GetDriverByName('GTIFF').Create('E:\TMP\MEMORY_%s.TIF' % name, xcount, ycount, 1, gdal.GDT_Byte)​</span></div>
<div><span style="font-family: "Courier New",monospace;">        mem_ds.SetGeoTransform((​</span><span style="font-family: "Courier New",monospace;">xmin, xResolut, 0,​
</span><span style="font-family: "Courier New",monospace;">ymax, 0, yResolut,​</span><span style="font-family: "Courier New",monospace;">))​</span></div>
<div><span style="font-family: "Courier New",monospace;">        mem_ds.SetProjection(ref_ds.GetProjectionRef())​</span></div>
<div><span style="font-family: "Courier New",monospace;">        mem_ds.FlushCache()​</span></div>
<div><span style="font-family: "Courier New",monospace;">   ​</span></div>
<div><span style="font-family: "Courier New",monospace;">        err = gdal.RasterizeLayer(mem_ds, [1], geom_lyr, burn_values=[1])​</span></div>
<div><span style="font-family: "Courier New",monospace;">        if err != 0:​</span></div>
<div><span style="font-family: "Courier New",monospace;">            print(err)​</span></div>
<span></span><br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
Here is the command line:</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<blockquote style="padding: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;" dir="ltr">
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span style="font-family: "Courier New",monospace;">(base) E:\TMP>gdal_rasterize MEMORY_Peru.JSON" "MEMORY_Peru.TIF" -burn 1</span><br>
</div>
</blockquote>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
I am using GDAL 2.3.3 and Python 3.7.3 installed by Anaconda3 (64-bit). The function gdal.UseExceptions() was called but there is nothing been reported.</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<br>
</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
I might be asking too much, but if someone had the same experience and have found a solution and want to share, that would be awesome. Or maybe there is an obvious error on my code that you can point out. I tried several things like flushing and closing and
 re-opening the files,  to make sure that the data is ready for the functions, but nothing really helped.</div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title=""><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title="">One other thing is, once I got it to work, I am thinking that I don't need to prepare the little geometry file if I could call gdal.rasterizelayer() the original shape with a *query* for each country at the time. That should be more efficient,
 I guess.</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title=""><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title="">Thank you for your attention and have a nice day everybody,</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title=""><span title=""><br>
</span></span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title=""><span title="">Ivan</span></span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title=""><span title=""><br>
</span></span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title=""><span title=""><br>
</span></span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span title=""><span title=""><br>
</span></span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><br>
</span></div>
<div style="color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif; font-size: 12pt;">
<span><br>
</span></div>
</body>
</html>