A function that converts a set of geometry objects into heat map tiles (HMT) based on a specified range and resolution. The return value is a protobuf-encoded binary matrix suitable for heatmap rendering.
Syntax
bytea ST_AsHMT(geometry geometry_set, geometry extent, int4 width, int4 height, int4 value default 1, boolean point_mode default false);
bytea ST_AsHMT(geometry geometry_set, geometry extent, int4 width, int4 height, int4 value);
bytea ST_AsHMT(geometry geometry_set, geometry extent, int4 width, int4 height, int4 value, boolean point_mode);
bytea ST_AsHMT(geometry geometry_set, geometry extent, int4 width, int4 height, float8 value);
bytea ST_AsHMT(geometry geometry_set, geometry extent, int4 width, int4 height, float8 value, boolean point_mode);
bytea ST_AsHMT(geometry geometry_set, geometry extent, int4 width, int4 height, int4 value, cstring config);
bytea ST_AsHMT(geometry geometry_set, geometry extent, int4 width, int4 height, float8 value, cstring config);Choosing a variant:
Count geometries (no
valueargument): use the first variant with defaultvalue=1andpoint_mode=false.Sum an
int4column (e.g., counts, quantities): use theint4 valuevariants. If the sum may exceed 2,147,483,647, usefloat8 valueinstead to avoid overflow.Sum a
float8column (e.g., metrics, ratios): use thefloat8 valuevariants.Advanced aggregation (min, max, avg, or point-mode via JSON): use the
cstring configvariants.
Return values
Returns a protobuf-based binary data matrix (bytea). The proto schema is:
syntax = "proto2";
option optimize_for = LITE_RUNTIME;
message HMT {
required Type type = 1; // data value type
required uint32 rows = 2; // rows of matrix
required uint32 columns = 3; // columns of matrix
required uint32 srid = 4;
required float xmin = 5; // xmin
required float ymin = 6; // ymin
required float xmax = 7; // xmax
required float ymax = 8; // ymax
oneof matrix {
intMatrix intValues = 10;
doubleMatrix doubleValues = 11;
}
message intMatrix {
repeated sint32 values = 12 [packed = true];
}
message doubleMatrix {
repeated double values = 13 [packed = true];
}
enum Type {
INT32 = 0;
DOUBLE = 1;
}
}Matrix layout:
typeis determined by the inputvalueparameter:INT32forint4,DOUBLEforfloat8.rowsandcolumnscorrespond to theheightandwidthparameters.Values are stored row by row, in ascending order on the X axis and descending order on the Y axis, which matches standard image coordinate conventions.
To convert the return value to an array representation, use ST_HMTAsArray.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
geometry_set | geometry | — | The geometry column to aggregate. |
extent | geometry | — | The geographic range. Only the bounding box is used. Compatible with ST_TileEnvelope. |
width | int4 | — | The grid width in pixels. Corresponds to columns in the result matrix. |
height | int4 | — | The grid height in pixels. Corresponds to rows in the result matrix. |
value | int4 or float8 | 1 | The value to sum per grid cell. Use float8 when the sum may exceed the int32 range (–2,147,483,648 to 2,147,483,647). |
point_mode | boolean | false | When true, only the points within the mesh are counted. |
config | cstring | — | A JSON string for advanced aggregation options. See the config options table below. |
Config options
| Option | Type | Default | Valid values |
|---|---|---|---|
type | string | sum | sum, min, max, avg |
point_mod | boolean | false | true, false |
The config option name ispoint_mod(notpoint_mode).
Usage notes
SRID mismatch behavior: If the spatial reference of geometry_set differs from the spatial reference of extent, the function applies the spatial reference of extent to the data. To confirm that both use the same SRID before calling the function, run ST_SRID() on a sample geometry and compare it with the SRID embedded in extent.
int4 overflow: When using int4 value to calculate sums, values are bounded by the int32 range (–2,147,483,648 to 2,147,483,647). For data where the sum may exceed this range, pass a float8 column or expression instead.
Examples
All examples use a test table created with:
CREATE TABLE test_table AS
SELECT i num,
ST_SetSrid(st_makepoint((i-0.5)::numeric, (i-0.5)::numeric), 4326) geom,
i*100::int4 weight,
i*i*i::float8 volume
FROM generate_series(1, 10) i;Count geometries (recommended starting point)
Count the number of geometry objects in each grid cell. This is the simplest use case and requires no value argument.
SELECT ST_AsHMT(geom,
ST_MakeEnvelope(0, 0, 10, 10, 4326), -- extent
1024, -- width, in pixels
800 -- height, in pixels
)
FROM test_table;
---------
\x080010a0061880083284...Sum a column
Sum an int4 column (weight) per grid cell.
SELECT ST_AsHMT(geom,
ST_MakeEnvelope(0, 0, 10, 10, 4326), -- extent
1024, -- width
800, -- height
weight -- value column
)
FROM test_table;
---------
\x080010a0061880...Sum a computed expression
Pass a float8 expression as the value. The result matrix uses the DOUBLE type.
SELECT ST_AsHMT(geom,
ST_MakeEnvelope(0, 0, 10, 10, 4326), -- extent
1024, -- width
800, -- height
weight / volume * 1.2 -- float8 expression
)
FROM test_table;
---------
\x080110a0061880083a85...Use point mode
Count only the points within the mesh.
SELECT ST_AsHMT(geom,
ST_MakeEnvelope(0, 0, 10, 10, 4326), -- extent
1024, -- width, in pixels
800, -- height
1::integer, -- value
true -- point mode
)
FROM test_table;
---------
\x080010a0061880083...Filter with a WHERE clause
Apply a filter before aggregation.
SELECT ST_AsHMT(geom,
ST_MakeEnvelope(0, 0, 10, 10, 4326), -- extent
1024, -- width, in pixels
800 -- height
)
FROM test_table
WHERE num < 5;
---------
\x080010a00618...Average using the config parameter
Use the config JSON string to specify avg aggregation.
SELECT ST_AsHMT(the_geom,
ST_MakeEnvelope(0, 0, 10, 10, 4326), -- extent
256, -- width, in pixels
256, -- height
weight,
'{"type":"avg"}'::cstring)
FROM test_table;
---------
\x080010a00618...Minimum with point mode using the config parameter
Use the config JSON string to combine min aggregation with point mode.
SELECT ST_AsHMT(the_geom,
ST_MakeEnvelope(0, 0, 10, 10, 4326), -- extent
256, -- width, in pixels
256, -- height
weight,
'{"type":"min", "point_mode":true}'::cstring)
FROM test_table;
---------
\x080010a00618...What's next
ST_HMTAsArray — convert an HMT binary value to an array representation