RS_AsRaster¶
Introduction: RS_AsRaster converts a vector geometry into a raster dataset by assigning a specified value to all pixels covered by the geometry. Unlike RS_Clip, which extracts a subset of an existing raster while preserving its original values, RS_AsRaster generates a new raster where the geometry is rasterized onto a raster grid. The function supports all geometry types and takes the following parameters:
geom: The geometry to be rasterized.raster: The reference raster to be used for overlaying thegeomon.pixelType: Defines data type of the output raster. This can be one of the following, D (double), F (float), I (integer), S (short), US (unsigned short) or B (byte).allTouched(Since:v1.7.1): For polygons,falseselects pixels whose centroids intersect the polygon;truealso includes cells entered by the polygon's boundary. For lines,falseselects one pixel in each pixel column or row between the two ends of a segment, as GDAL does by default;trueselects every pixel the line passes through. Defaults tofalse. Point and line selection follows the rules below.value: The value to be used for assigning pixels covered by the geometry. Defaults to using1.0if not provided.noDataValue: The no data value of the resultant raster. Every pixel the geometry does not cover (including interior holes) is filled with the resolved no data value, which is also recorded as the output band's no data metadata. The value must be representable inpixelType: an out-of-range or fractional value for an integerpixelType, or a value that is not exactly representable in a floating-pointpixelType(for example0.1in a 32-bit float band), is rejected with an error rather than silently coerced.NaNis supported as a no data value forFandDpixel types; infinite no data values are not supported. WhennoDataValueis not provided, it is inherited from the referenceraster's first band, andRS_AsRastererrors if that band has no no data value. The programmatic API additionally accepts an explicitnullnoDataValue, which produces a raster with no no data value and a0background.useGeometryExtent: Defines the extent of the resultant raster. When set totrue, it corresponds to the extent ofgeom, and when set to false, it corresponds to the extent ofraster. Default value istrueif not set. When set totrueandgeomneither burns nor overlaps any pixel ofraster, for example a polygon that only touches its boundary or a line along its right or bottom edge, there is nothing to crop to andRS_AsRasterreturnsnull. A polygon that overlaps a pixel without covering its centre still returns its cropped, unburned raster.
Pixel selection depends on the geometry type:
- A polygon never selects a pixel it touches only on the pixel's boundary, whichever edge that is. A polygon whose edges lie on grid lines selects only pixels inside it.
- A point selects its containing half-open pixel, regardless of
allTouched. A pixel contains its first edges in pixel order (the left and top edges on a north-up raster) but not its last. A point on an internal edge or corner selects the pixel after it in pixel order; a point on the raster's last edge selects none. - With
allTouched=false, a line selects the pixels that GDAL selects without itsALL_TOUCHEDoption. Each segment selects a line of pixels from the pixel that contains one of its ends to the pixel that contains the other:- An end is in its half-open pixel, like a point. An end on a grid line is in the pixel after it in pixel order, so a segment lying on a grid line selects the pixels after it, and one lying on the raster's last edge selects none.
- When the two pixels are at least as many columns apart as rows, the segment selects one pixel in every column from one to the other, and otherwise one pixel in every row. In each column, or row, that is the pixel nearest to the straight line between the centres of the two pixels. A segment that is not horizontal, vertical or diagonal therefore selects fewer pixels than it passes through.
- Only the two pixels count, not where in them the ends are. A segment can select a pixel it does not pass through, and leave out one it does.
- Where the straight line between the two centres passes exactly between two pixels, the pixel on the side of the end the line of pixels starts from is selected. As in GDAL, a line is taken from its last point to its first, so each segment starts from its later point, and a line and its reverse can select different pixels.
- The ends are not moved onto the raster. A segment with an end outside the raster selects the pixels of the raster that are on the line between the two pixels its ends are in, on the raster's grid continued beyond its edges. A segment with an end more than 2^60 pixels from the raster selects nothing.
- With
allTouched=true, a line selects the pixels that GDAL selects with itsALL_TOUCHEDoption: every pixel its segments pass through, whether or not a segment reaches the pixel's centre, and a pixel that a segment meets only at a single point when that point belongs to the pixel under the half-open rule used for points. A segment lying on a grid line selects the pixels after it in pixel order; one lying on the raster's last edge selects none. Where a segment crosses a pixel corner exactly, the pixel after that corner in pixel order is selected. The first point of a segment selects the pixel containing it, and the last point selects nothing. The first point is the end with the smaller pixelxcoordinate, or, for a segment that runs along a pixel column, the end with the smaller pixelycoordinate. Only the part of a segment inside the raster counts: a segment that meets the raster at a single point selects nothing, because that point is also its last. This is the rule of GDAL 3.11 and later. Earlier GDAL versions order the ends of a sloped segment that stays within one pixel column by row instead, and do not select the pixel containing its first point. GDAL also computes all of this in floating point with small tolerances, so its result can differ by a pixel for a segment that is within 0.01 pixel of horizontal or vertical, or that passes within rounding error of a pixel corner or a grid line.
For LineString geometries, allTouched=true can expand the geometry extent at grid boundaries. With useGeometryExtent=true, this can change both the output dimensions and the selected pixels. With allTouched=false, the extent is the pixels from the one that contains the line's smallest pixel coordinates to the one that contains its largest, which are the pixels the line can select.
A line is rasterized wherever it crosses the raster, including along segments whose two vertices both lie outside the raster. A line that does not intersect the raster selects none of its pixels, and is rejected like any other geometry that does not. With allTouched=false the line of pixels between the ends of a segment can pass up to about a pixel beside the segment, and so through a raster that the segment misses. When a raster is cut into tiles, a line selects the same pixels on every tile it intersects as on the whole raster, and none on a tile it does not intersect, although the whole raster can have a selected pixel there.
Whether a coordinate lies on a grid line is decided in decimal. Each stored coordinate, the raster's upper-left corner and its pixel size are read as the shortest decimal number that converts back to the same double, and the pixel position is computed exactly from those decimals. For example, on a raster with upper-left y = 0.6 and ScaleY = -0.1, the coordinate 0.3 is on the grid line three pixels down, although (0.3 - 0.6) / -0.1 evaluates to 2.9999999999999996 in floating point. A coordinate next to a grid line is not moved onto it: 0.30000000000000004, the next double above 0.3 and also the result of 0.1 + 2 * 0.1, lies past that line, so a line ending there enters the next pixel. Coordinates computed in floating point from the raster's origin and pixel size are therefore on a grid line only if their decimal form is. This rule applies to points, to lines, to polygon outlines under allTouched=true, and to the geometry extent.
The output is on the grid of raster. It has the same pixel size. With useGeometryExtent=false it has the georeference of raster, so a geometry selects the same pixels on the output as on raster, for example when the output is passed to RS_ZonalStats with the same geometry. With useGeometryExtent=true the output starts a whole number of pixels into that grid, and its origin is the grid line it starts on, rounded to a double.
When the upper-left corner and the pixel size of raster are short decimals, such as 0.6 and 0.1, its grid lines are doubles, and the grid lines of a cropped output are exactly the same. For most rasters read from files they are not: with a pixel size of one arc second, 0.0002777777777777778, no grid line is a double. The origin of a cropped output is then the double next to the grid line, less than a unit in the last place away from it, and so are its other grid lines. A coordinate that close to a grid line, such as a pixel corner computed from the raster's origin and pixel size, can select the neighbouring pixel on the cropped output. Use useGeometryExtent=false when the output is to be used as a reference raster for such coordinates.
A raster is stored with its cell-centre coordinates, and the corner it reports afterwards is computed from them in floating point, as (corner + pixel size / 2) - pixel size / 2. That is not always the corner the raster was made with: with a pixel size of 0.1, the corner 0.1 becomes 0.10000000000000002. A raster is stored when it passes from one step of a query to the next, but not between two functions nested in one expression. Pixels are selected on the grid of the stored corner in both cases, so a geometry selects the same pixels wherever in a query the raster was made. On the grid of that example the coordinate 0.3 is inside a pixel row and not on a grid line, because the grid lines are 0.10000000000000002 + k * 0.1. The raster starts at that corner as well, so a point at 0.1 is outside it: RS_AsRaster and RS_SetValues reject the point, and RS_ZonalStats and RS_Clip return null for it unless lenient is false, whether the raster has been stored or not. A corner that does not change when it is stored, as when the corner and the pixel size are whole numbers, is used as it is.
With useGeometryExtent=true the output does not start on a grid line that would change when stored. It starts one pixel earlier, so it can have a first row or column that geom does not reach.
Format:
RS_AsRaster(geom: Geometry, raster: Raster, pixelType: String)
RS_AsRaster(geom: Geometry, raster: Raster, pixelType: String, allTouched: Boolean)
RS_AsRaster(geom: Geometry, raster: Raster, pixelType: String, allTouched: Boolean, value: Double)
RS_AsRaster(geom: Geometry, raster: Raster, pixelType: String, allTouched: Boolean, value: Double, noDataValue: Double)
RS_AsRaster(geom: Geometry, raster: Raster, pixelType: String, allTouched: Boolean, value: Double, noDataValue: Double, useGeometryExtent: Boolean)
Return type: Raster
Since: v1.5.0
Note
The function doesn't support rasters that have any one of the following properties:
ScaleX < 0
SkewX != 0
SkewY != 0
For more information about ScaleX, ScaleY, SkewX, SkewY, please refer to the Affine Transformations section.
SQL Example
SELECT RS_AsRaster(
ST_GeomFromWKT('POLYGON((15 15, 18 20, 15 24, 24 25, 15 15))'),
RS_MakeEmptyRaster(2, 255, 255, 3, 30, 2, -2, 0, 0, 4326),
'D', false, 255.0, 0d
)
Output:
GridCoverage2D["g...
SQL Example
With the noDataValue omitted, the output inherits the reference band's no data value (here 0, set on the empty reference raster) and fills uncovered pixels with it:
SELECT RS_AsRaster(
ST_GeomFromWKT('POLYGON((15 15, 18 20, 15 24, 24 25, 15 15))'),
RS_SetBandNoDataValue(RS_MakeEmptyRaster(2, 255, 255, 3, 30, 2, -2, 0, 0, 4326), 1, 0),
'D'
)
Output:
GridCoverage2D["g...
SQL Example
SELECT RS_AsRaster(
ST_GeomFromWKT('POLYGON((15 15, 18 20, 15 24, 24 25, 15 15))'),
RS_MakeEmptyRaster(2, 255, 255, 3, 215, 2, -2, 0, 0, 0),
'D', true, 255, 0d, false
)
Output:
GridCoverage2D["g...