<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body dir="auto">
<div dir="ltr">
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;">Howdy! I’m trying to rasterize a shape that is in absolute pixel/line coordinates, but it gets burned wrong if the destination dataset has an srs due to coordinate conversion. There aren’t any docs for that specific function (<a href="https://gdal.org/en/stable/api/python/utilities.html#osgeo.gdal.RasterizeLayer" style="color: rgb(70, 120, 134);">https://gdal.org/en/stable/api/python/utilities.html#osgeo.gdal.RasterizeLayer</a>)
so I dug into the C++/CLI options (<a href="https://gdal.org/en/latest/api/gdal_alg.html#_CPPv432GDALCreateGenImgProjTransformer212GDALDatasetH12GDALDatasetHPPc" style="color: rgb(70, 120, 134);">https://gdal.org/en/latest/api/gdal_alg.html#_CPPv432GDALCreateGenImgProjTransformer212GDALDatasetH12GDALDatasetHPPc</a>)
but couldn’t get them working.<o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;"> </span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;">If my layer’s srs is configured as None, it generates a warning that it’s assuming the destination srs. If I set it to an unconfigured srs (whatever that is), the warning goes away, but I assume it’s using an identity transform
for the layer. I couldn’t find an srs I could apply to the layer that would disable coordinate conversion to the dst raster.<o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;"> </span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;">I know I could get the geotransform and convert it, if it exists, but my target images may not have a reference system and may not have had their gcps applied yet. I’m also hesitant to try and remove it and then reapply after
burning because I’m unsure of what other effects it could have, such as with gcps.<o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;"><br>
</span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;">Also, is there any chance there's a secret way to enable inverse mode on RasterizeLayer? Would be nice to know if there is, I don't have the experience with rasterize to generate an inverse and merge, I just end up inverting my
input, but there's always a chance to get the raster bounds wrong that way.</span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;"> </span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;">Appreciate the help,<o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;"> </span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;">Will<o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;"> </span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;"> </span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;">Sample code snippets, not sure if this supports attachments:<o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt;"> </span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">from osgeo import gdal, ogr, osr</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">gdal.UseExceptions()</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">def make_ogr_feature(spatial_ref, shape):</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> # Cobbled together from hours of stackoverflow and I don’t fully understand it, but it works</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> rast_ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('wrk')</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> rast_mem_lyr = rast_ogr_ds.CreateLayer('poly', srs=spatial_ref)</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> feat.SetGeometryDirectly(ogr.Geometry(wkt=shape))</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> rast_mem_lyr.CreateFeature(feat)</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> return rast_mem_lyr, rast_ogr_ds</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> </span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"># create a blank image, no/blank/identity SRS</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">driver = gdal.GetDriverByName('GTiff')</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">ds = driver.Create('unset.tif', xsize=100, ysize=100, bands=3, eType=gdal.GDT_Byte)</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"># Create a layer with no srs</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">layer, layer_ds = make_ogr_feature(None, 'Polygon ((6 29, 24 22, 3 5, 6 29))')</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"># Burn to image, close</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">gdal.RasterizeLayer(ds, [1,2,3], layer, burn_values=[255,255,255])</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"># New image, set GCPs and SRS</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">ds = driver.Create('set.tif', xsize=100, ysize=100, bands=3, eType=gdal.GDT_Byte)</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">srs = osr.SpatialReference()</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">srs.ImportFromEPSG(4326)</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">ds.SetGCPs([gdal.GCP(30, 30, 0, 0.5, 0.5, '', 'UpperLeft'),</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> gdal.GCP(40, 30, 0, 99.5, 0.5, '', 'UpperRight'),</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> gdal.GCP(40,40,0,99.5,99.5,'','LowerRight'),</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"> gdal.GCP(30,40,0,0.5,99.5, '','LowerLeft')],srs)</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"># try to rasterize, but nothing will render because it assumes dest srs</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">gdal.RasterizeLayer(ds, [1,2,3], layer, burn_values=[255,255,255])</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"># found some docs about transform options</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">gdal.RasterizeLayer(ds, [1,2,3], layer, burn_values=[255,255,255], options=['SRC_METHOD=NO_GEOTRANSFORM'])</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">gdal.RasterizeLayer(ds, [1,2,3], layer, burn_values=[255,255,255], options=['SRC_METHOD=NO_GEOTRANSFORM','DST_METHOD=NO_GEOTRANSFORM'])</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);"># but it doesn't seem to do anything or be validated?</span><span style="color: rgb(33, 33, 33);"><o:p></o:p></span></p>
<p class="MsoNormal" style="font-size: 12pt; -webkit-text-size-adjust: auto; margin: 0in; font-family: Aptos, sans-serif;">
<span style="font-size: 11pt; color: rgb(33, 33, 33);">gdal.RasterizeLayer(ds, [1,2,3], layer, burn_values=[255,255,255], options=[SRC_METHOD=($)#($)(#$'])</span></p>
<div dir="ltr"></div>
</div>
</body>
</html>