2

I want to set snap raster for the function rasterize of GDAL, but I can not found. The options parameters are as the following code (C#). What is wrong?

rasterizeOptions = new string[] { "ALL_TOUCHED=TRUE", "ATTRIBUTE=" + fieldName }; //To set all touched pixels into raster pixel
Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion");

This seems to be possible in other tools such as [ArcGIS’s Polygon to Raster] snap raster

yixing
  • 21
  • 3

2 Answers2

0

I would have a try by making an empty copy of the raster to be snapped with gdal_create https://gdal.org/programs/gdal_create.html

gdal_create -if prototype.tif output.tif

and then rasterize the vectors into this empty template file "output.tif".

user30184
  • 65,331
  • 4
  • 65
  • 118
  • many thanks too, I will try to finish the snap function based on your help. This is also a good idea. – yixing Jul 22 '22 at 13:34
0

I'm aware of two ways that raster snapping can be accomplished in QGIS:

  1. From the Raster > Projections > Warp tool. With this tool, the input layer is the layer to be adjusted (snapped). The only options that have to be set are 1) the Output file resolution in target georeferenced units, which is set to the snap raster’s pixel size and 2) the Georeferenced extents of output file to be created” drop-down, where the snap raster is selected.

You'll notice that this builds a GDAL/OGR console call, visible near the bottom of the tool. You can copy/paste this text should you wish to utilize a GDAL operation outside of QGIS.

  1. Use the Raster > Align Raster tool.

These have been previously discussed at: Specifying snap raster in QGIS?

To help people understand the snap raster concept, ESRI has a nice explanation at https://desktop.arcgis.com/en/arcmap/latest/tools/environments/how-snap-raster-environment-works.htm Since links can go stale, here's a screenshot from that web page that provides "a picture is worth a thousand words" view:

enter image description here

Stu Smith
  • 8,252
  • 8
  • 35
  • 84