Referring to the GDAL_GRID C++ documentation here.

gdal_grid [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/
          [-of format] [-co "NAME=VALUE"]
          [-zfield field_name] [-z_increase increase_value] [-z_multiply multiply_value]
          [-a_srs srs_def] [-spat xmin ymin xmax ymax]
          [-clipsrc <xmin ymin xmax ymax>|WKT|datasource|spat_extent]
          [-clipsrcsql sql_statement] [-clipsrclayer layer]
          [-clipsrcwhere expression]
          [-l layername]* [-where expression] [-sql select_statement]
          [-txe xmin xmax] [-tye ymin ymax] [-outsize xsize ysize]
          [-a algorithm[:parameter1=value1]*] [-q]
          <src_datasource> <dst_filename> 

Any OGR supported readable datasource.

It says that one needs to pass in a GDALdataset as an input.

However, for the example given on the same page, it is passing in a file ( with the *.vrt) as input.

gdal_grid -a invdist:power=2.0:smoothing=1.0 -txe 85000 89000 -tye 894000 890000 -outsize 400 400 -of GTiff -ot Float64 -l dem dem.vrt dem.tiff

Obviously the example is geared towards the command line utility, not the actual API usage.

I want to use the API and not the command line utility for GDAL_GRID, so how can I call the gdal_grid API properly, with .vrt dataset? Is there any such example in C++ or C#?


There are a lot of examples already in python on how to pass in a .vrt file as input, but this is not what I'm looking for. Just to clarify.

  • GDAL_GRID is the utility, the source is available searchcode.com/codesearch/view/18937240 for example. GDAL_GRID is not part of the API to access as a function. If you want to know how to implement it read the source, it's in C++... unless of course you're not writing for the C++ GDAL API and are trying to implement this in a different language like python, C# etc and you don't understand C++, it's not immediately clear what language you're trying to implement this in. – Michael Stimson Dec 17 '18 at 7:10
  • The source reveals the utility sets up a call to gdal_alg.h function GDALGridCreate gdal.org/gdal__alg_8h.html#a1fdef40bcdbc98eff2328b0d093d3a22 to a block of GDALDataType (float* presumably) large enough which is written by either GDALDataset or GDALRasterBand RasterIO which does the actual work, assuming you know or can derive reasonably the rest of the input objects you could call the 'meat' directly. – Michael Stimson Dec 17 '18 at 7:25
  • @MichaelStimson , I want to use gdal_grid in c++, not reimplementing it. Please read the question carefully . I know how to do that in Python, but there is no example in c++. – Graviton Dec 17 '18 at 8:53
  • Use std::system() cplusplus.com/reference/cstdlib/system to call the external program. There is no equivalent in C++ for gdal.Grid(), as stated in the first comment, except for using GDALGridCreate as mentioned in the previous comment. Either shell the executable or create the required objects and call GDALGridCreate which you can read the source of GDAL_GRID (previously linked) as a tutorial/example... those are your only choices. – Michael Stimson Dec 17 '18 at 23:36

As mentioned in the comment, one can just call gdal_grid as command line, and in the place of src_datasource, one can just pass in the full path of the .vrt file, even though in the documentation, it says that src_datasource has to be a GDALdataset.


If you are coming from GDAL.Net ( C# wrapper), here's the correct syntax to use the GDAL_Grid:

   var ds = Gdal.OpenEx(vrtFile, 0, null, null, null);         
   var gridDS =  Gdal.wrapper_GDALGrid(tiffFile, ds, new GDALGridOptions(new string[] {"-of", "gtiff", "-ot", "Float64" }), null, string.Empty);

The important thing to note is that Gdal.Open opens raster file, and Gdal.OpenEx opens vector file ( which is the case here)

  • It's not C# it's C++ which is an entirely different world. The C# (and python) API has some of the more tedious components bundled together as a single call, which can be useful so long as you want it that way, but C++ works on a much lower level and doesn't have a prepackaged (sanitized) form. It's no surprise that this is easier in C# (or python), C++ enforces/allows control over very low level objects leading to faster execution. – Michael Stimson Dec 18 '18 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.