# `gdal vector grid`

Added in version 3.11.

Create a regular grid from scattered points

## Synopsis

    Usage: gdal vector grid <SUBCOMMAND> [OPTIONS]
    where <SUBCOMMAND> is one of:
      - average:                 Create a regular grid from scattered points using moving average interpolation.
      - average-distance:        Create a regular grid from scattered points using the average distance between the grid node (center of the search ellipse) and all of the data points in the search ellipse.
      - average-distance-points: Create a regular grid from scattered points using the average distance between the data points in the search ellipse.
      - count:                   Create a regular grid from scattered points using the number of points in the search ellipse.
      - invdist:                 Create a regular grid from scattered points using weighted inverse distance interpolation.
      - invdistnn:               Create a regular grid from scattered points using weighted inverse distance interpolation nearest neighbour.
      - linear:                  Create a regular grid from scattered points using linear/barycentric interpolation.
      - maximum:                 Create a regular grid from scattered points using the maximum value in the search ellipse.
      - minimum:                 Create a regular grid from scattered points using the minimum value in the search ellipse.
      - nearest:                 Create a regular grid from scattered points using nearest neighbor interpolation.
      - range:                   Create a regular grid from scattered points using the difference between the minimum and maximum values in the search ellipse.

    Common Options:
      -h, --help              Display help message and exit
      --json-usage            Display usage as JSON document and exit
      --config <KEY>=<VALUE>  Configuration option [may be repeated]

## Description

This program creates a regular grid (raster) from the scattered data read from a vector dataset. Input data will be interpolated to fill grid nodes with values, you can choose from various interpolation methods.

It is possible to set the `GDAL_NUM_THREADS` configuration option to parallelize the processing. The value to specify is the number of worker threads, or `ALL_CPUS` to use all the cores/CPUs of the computer.

Since GDAL 3.12, this algorithm can be part of a gdal pipeline.

## Options common to all algorithms

### Standard options

-f, --of, --format, --output-format <OUTPUT-FORMAT>

  Which output vector format to use. Allowed values may be given by `gdal --formats | grep vector | grep rw | sort`

--co <NAME>=<VALUE>

  Many formats have one or more optional dataset creation options that can be used to control particulars about the file created. For instance, the GeoPackage driver supports creation options to control the version.

  May be repeated.

  The dataset creation options available vary by format driver, and some simple formats have no creation options at all. A list of options supported for a format can be listed with the --formats command line option but the documentation for the format is the definitive source of information on driver creation options. See Vector drivers format specific documentation for legal creation options for each format.

  Note that dataset creation options are different from layer creation options.

--overwrite

  Allow program to overwrite existing target file or dataset. Otherwise, by default, gdal errors out if the target file or dataset already exists.

-l, --layer, --layer-name <LAYER-NAME>

  Indicates the layer(s) from the datasource that will be used for input features. May be specified multiple times

--sql <SQL>|@<filename>

  An SQL statement to be evaluated against the datasource to produce a virtual layer of features to be burned in. The @filename syntax can be used to indicate that the content is in the pointed filename.

--extent <xmin>,<ymin>,<xmax>,<ymax>

  Set georeferenced extents. The values must be expressed in georeferenced units. If not specified, the extent of the output file will be the extent of the vector layers.

--resolution <xres>,<yres>

  Set target resolution. The values must be expressed in georeferenced units. Both must be positive values. Note that --resolution cannot be used with --size.

--size <xsize>,<ysize>

  Set output file size in pixels and lines. Note that --size cannot be used with --resolution.

--ot, --datatype, --output-data-type <OUTPUT-DATA-TYPE>

  Force the output bands to be of the indicated data type. Defaults to `Float64`.

--crs <CRS>

  Override the projection for the output file. If not specified, the projection of the input vector file will be used if available. When using this option, no reprojection of features from the CRS of the input vector to the specified CRS of the output raster, so use only this option to correct an invalid source CRS. The `<CRS>` may be any of the usual GDAL/OGR forms, complete WKT, PROJ.4, EPSG:n or a file containing the WKT.

--bbox <xmin>,<ymin>,<xmax>,<ymax>

  Select only points contained within the specified bounding box.

--zfield <field_name>

  Identifies an attribute field on the features to be used to get a Z value from. This value overrides the Z value read from the feature geometry record (naturally, if you have a Z value in the geometry, otherwise you have no choice and should specify a field name containing a Z value).

--zincrease <increase_value>

  Addition to the attribute field on the features to be used to get a Z value from. The addition should be the same unit as the Z value. The result value will be Z value + Z increase value. The default value is 0.

--zmultiply <multiply_value>

  This is multiplication ratio for the Z field. This can be used for a shift from e.g. feet to meters or from elevation to depth. The result value will be (Z value + Z increase value) * Z multiply value. The default value is 1.

### Advanced options

--oo <NAME>=<VALUE>

  Dataset open option (format specific).

  May be repeated.

--if <format>

  Format/driver name to be attempted to open the input file(s). It is generally not necessary to specify it, but it can be used to skip automatic driver detection, when it fails to select the appropriate driver. This option can be repeated several times to specify several candidate drivers. Note that it does not force those drivers to open the dataset. In particular, some drivers have requirements on file extensions.

  May be repeated.

## "invdist" algorithm

Interpolation using an inverse distance to a power.

When `--min-points-per-quadrant` or `--max-points-per-quadrant` is specified, the actual algorithm used is "invdistnn".

### Options

--power <val>

  Weighting power (default 2.0).

--smoothing <val>

  Smoothing parameter (default 0.0).

--radius <val>

  Set first and second radius (mutually exclusive with `--radius1` and `--radius2`. By default, uses the whole point array.

--radius1 <val>

  The first radius (X axis if rotation angle is 0) of the search ellipse. By default, uses the whole point array.

--radius2 <val>

  The second radius (Y axis if rotation angle is 0) of the search ellipse. By default, uses the whole point array.

--angle <val>

  Angle of search ellipse rotation in degrees (counter clockwise, default is 0).

--max-points <val>

  Maximum number of data points to use. Do not search for more points than this number. This may only used if the search ellipse is set (both radii are non-zero). By default, no limitation.

--min-points <val>

  Minimum number of data points to use. If less points in the search ellipse than the specified value are found, the grid node considered empty and will be filled with the nodata value. This may only used if the search ellipse is set (both radii are non-zero). By default, no limitation.

--max-points-per-quadrant <val>

  Maximum number of data points to use per quadrant. By default, no limitation. When specified, the algorithm will only take into account up to max_points_per_quadrant points for each of the right-top, left-top, right-bottom and right-top quadrant relative to the point being interpolated.

--min-points-per-quadrant <val>

  Minimum number of data points to use per quadrant. By default, no limitation. When specified, the algorithm will collect at least min_points_per_quadrant points for each of the right-top, left-top, right-bottom and right-top quadrant relative to the point being interpolated.

--nodata <val>

  Nodata value to fill empty points (default is 0).

## "invdistnn" algorithm

Interpolation using an inverse distance to a power with nearest neighbor searching, ideal when `--max-points` is used.

When `--min-points-per-quadrant` or `--max-points-per-quadrant` is specified, the search will start with the closest point to the point being interpolated from the first quadrant, then the closest point to the point being interpolated from the second quadrant, etc. up to the 4th quadrant, and will continue with the next closest point in the first quadrant, etc. until `--max-points` and/or `--max-points-per-quadrant` thresholds are reached.

### Required option

--radius <val>

  Set search radius (mutually exclusive with `--radius1` and `--radius2`. Required.

### Options

--power <val>

  Weighting power (default 2.0).

--smoothing <val>

  Smoothing parameter (default 0.0).

--max-points <val>

  Maximum number of data points to use. Do not search for more points than this number. This may only used if the search ellipse is set (both radii are non-zero). By default, no limitation.

--min-points <val>

  Minimum number of data points to use. If less points in the search ellipse than the specified value are found, the grid node considered empty and will be filled with the nodata value. This may only used if the search ellipse is set (both radii are non-zero). By default, no limitation.

--max-points-per-quadrant <val>

  Maximum number of data points to use per quadrant. By default, no limitation. When specified, the algorithm will only take into account up to max_points_per_quadrant points for each of the right-top, left-top, right-bottom and right-top quadrant relative to the point being interpolated.

--min-points-per-quadrant <val>

  Minimum number of data points to use per quadrant. By default, no limitation. When specified, the algorithm will collect at least min_points_per_quadrant points for each of the right-top, left-top, right-bottom and right-top quadrant relative to the point being interpolated.

--nodata <val>

  Nodata value to fill empty points (default is 0).

## "average" algorithm

Interpolation using a moving average algorithm.

When `--min-points-per-quadrant` or `--max-points-per-quadrant` is specified, the search will start with the closest point to the point being interpolated from the first quadrant, then the closest point to the point being interpolated from the second quadrant, etc. up to the 4th quadrant, and will continue with the next closest point in the first quadrant, etc. until `--max-points` and/or `--max-points-per-quadrant` thresholds are reached.

### Options

--radius <val>

  Set first and second radius (mutually exclusive with `--radius1` and `--radius2`. By default, uses the whole point array.

--radius1 <val>

  The first radius (X axis if rotation angle is 0) of the search ellipse. By default, uses the whole point array.

--radius2 <val>

  The second radius (Y axis if rotation angle is 0) of the search ellipse. By default, uses the whole point array.

--angle <val>

  Angle of search ellipse rotation in degrees (counter clockwise, default is 0).

--max-points <val>

  Maximum number of data points to use. Do not search for more points than this number. This may only used if the search ellipse is set (both radii are non-zero). By default, no limitation.

--min-points <val>

  Minimum number of data points to use. If less points in the search ellipse than the specified value are found, the grid node considered empty and will be filled with the nodata value. This may only used if the search ellipse is set (both radii are non-zero). By default, no limitation.

--max-points-per-quadrant <val>

  Maximum number of data points to use per quadrant. By default, no limitation. When specified, the algorithm will only take into account up to max_points_per_quadrant points for each of the right-top, left-top, right-bottom and right-top quadrant relative to the point being interpolated.

--min-points-per-quadrant <val>

  Minimum number of data points to use per quadrant. By default, no limitation. When specified, the algorithm will collect at least min_points_per_quadrant points for each of the right-top, left-top, right-bottom and right-top quadrant relative to the point being interpolated.

--nodata <val>

  Nodata value to fill empty points (default is 0).

## "nearest" algorithm

Interpolation using nearest neighbor algorithm.

### Options

--radius <val>

  Set first and second radius (mutually exclusive with `--radius1` and `--radius2`. By default, uses the whole point array.

--radius1 <val>

  The first radius (X axis if rotation angle is 0) of the search ellipse. By default, uses the whole point array.

--radius2 <val>

  The second radius (Y axis if rotation angle is 0) of the search ellipse. By default, uses the whole point array.

--angle <val>

  Angle of search ellipse rotation in degrees (counter clockwise, default is 0).

--nodata <val>

  Nodata value to fill empty points (default is 0).

## "linear" algorithm

Linear interpolation by computing a Delaunay triangulation of the point cloud, finding in which triangle of the triangulation the point is, and by doing linear interpolation from its barycentric coordinates within the triangle. If the point is not in any triangle, depending on the radius, the algorithm will use the value of the nearest point or the nodata value.

### Options

--radius <val>

  In case the point to be interpolated does not fit into a triangle of the Delaunay triangulation, use that maximum distance to search a nearest neighbour, or use nodata otherwise. If unset, the search distance is infinite. If set to 0, nodata value will be always used.

--nodata <val>

  Nodata value to fill empty points (default is 0).

## Data metrics algorithms

Besides the interpolation functionality gdal vector grid can be used to compute some data metrics using the specified window and output grid geometry. These metrics are:

- `minimum`: Minimum value found in grid node search ellipse.

- `maximum`: Maximum value found in grid node search ellipse.

- `range`: A difference between the minimum and maximum values found in grid node search ellipse.

- `count`: A number of data points found in grid node search ellipse.

- `average-distance`: An average distance between the grid node (center of the search ellipse) and all of the data points found in grid node search ellipse.

- `average-distance-points`: An average distance between the data points found in grid node search ellipse. The distance between each pair of points within ellipse is calculated and average of all distances is set as a grid node value.

All the metrics have the same set of options:

--radius <val>

  Set first and second radius (mutually exclusive with `--radius1` and `--radius2`. By default, uses the whole point array.

--radius1 <val>

  The first radius (X axis if rotation angle is 0) of the search ellipse. By default, uses the whole point array.

--radius2 <val>

  The second radius (Y axis if rotation angle is 0) of the search ellipse. By default, uses the whole point array.

--angle <val>

  Angle of search ellipse rotation in degrees (counter clockwise, default is 0).

--min-points <val>

  Minimum number of data points to use. If less points in the search ellipse than the specified value are found, the grid node considered empty and will be filled with the nodata value. This may only used if the search ellipse is set (both radii are non-zero). By default, no limitation.

--max-points-per-quadrant <val>

  Maximum number of data points to use per quadrant. By default, no limitation. When specified, the algorithm will only take into account up to max_points_per_quadrant points for each of the right-top, left-top, right-bottom and right-top quadrant relative to the point being interpolated.

--min-points-per-quadrant <val>

  Minimum number of data points to use per quadrant. By default, no limitation. If that number is not reached, the point being interpolated will be set with the nodata value. When specified, the algorithm will collect at least min_points_per_quadrant points for each of the right-top, left-top, right-bottom and right-top quadrant relative to the point being interpolated.

--nodata <val>

  Nodata value to fill empty points (default is 0).

When `--min-points-per-quadrant` or `--max-points-per-quadrant` is specified, the search will start with the closest point to the point being interpolated from the first quadrant, then the closest point to the point being interpolated from the second quadrant, etc. up to the 4th quadrant, and will continue with the next closest point in the first quadrant, etc. until `--max-points` and/or `--max-points-per-quadrant` thresholds are reached.

## Reading comma separated values

Often you have a text file with a list of comma separated XYZ values to work with (so called CSV file). You can easily use that kind of data source in gdal vector grid. All you need is to create a virtual dataset header (VRT) for your CSV file and use it as an input datasource for gdal vector grid. You can find details on the VRT format on the VRT -- OGR Virtual Format description page.

Here is a small example. Let's say we have a CSV file called dem.csv containing

    Easting,Northing,Elevation
    86943.4,891957,139.13
    87124.3,892075,135.01
    86962.4,892321,182.04
    87077.6,891995,135.01
    ...

For the above data we will create a dem.vrt header with the following content:

    <OGRVRTDataSource>
        <OGRVRTLayer name="dem">
            <SrcDataSource>dem.csv</SrcDataSource>
            <GeometryType>wkbPoint</GeometryType>
            <GeometryField encoding="PointFromColumns" x="Easting" y="Northing" z="Elevation"/>
        </OGRVRTLayer>
    </OGRVRTDataSource>

This description specifies so called 2.5D geometry with three coordinates X, Y and Z. The Z value will be used for interpolation. Now you can use dem.vrt with all OGR programs (start with ogrinfo to test that everything works fine). The datasource will contain a single layer called "dem" filled with point features constructed from values in the CSV file. Using this technique you can handle CSV files with more than three columns, switch columns, etc. OK, now the final step:

    gdal vector grid invdist dem.vrt demv.tif

Or, if we do not wish to use a VRT file:

    gdal vector grid invdist -l dem -oo X_POSSIBLE_NAMES=Easting \
    -oo Y_POSSIBLE_NAMES=Northing --zfield=Elevation dem.csv dem.tif

If your CSV file does not contain column headers then it can be handled in the VRT file in the following way:

    <GeometryField encoding="PointFromColumns" x="field_1" y="field_2" z="field_3"/>

The Comma Separated Value (.csv) description page contains details on CSV format supported by GDAL.

Added in version 3.12.

## GDALG output (on-the-fly / streamed dataset)

This program supports serializing the command line as a JSON file using the `GDALG` output format. The resulting file can then be opened as a raster dataset using the GDALG: GDAL Streamed Algorithm driver, and apply the specified pipeline in a on-the-fly / streamed way.

Note

However this algorithm is not natively streaming compatible. Consequently a temporary dataset will be generated, which may cause significant processing time at opening.

## Examples

### Example 1: Create a raster from a VRT datasource using inverse distance to a power

    gdal vector grid invdist --power=2.0 --smoothing=1.0 --extent=85000,894000,89000,890000 \
        --size=400,400 -l dem dem.vrt dem.tif

This example creates a raster TIFF file from the VRT datasource described in Reading comma separated values section using the inverse distance to a power method. Values to interpolate will be read from Z value of geometry record.

### Example 2: Read values to interpolate from an attribute field

    gdal vector grid invdist --zfield=Elevation --config GDAL_NUM_THREADS ALL_CPUS \
        --power=2.0 --smoothing=1.0 --extent=85000,894000,89000,890000 \
        --size=400,400 -l dem dem.vrt dem.tif

This command does the same thing as the previous one, but reads values to interpolate from the attribute field specified with `--zfield` option instead of geometry record. So in this case X and Y coordinates are being taken from geometry and Z is being taken from the "Elevation" field. The `GDAL_NUM_THREADS` is also set to parallelize the computation.