QGISAI 地图制作与分析平台 | AI 驱动
帮助目录

表达式函数

参考更新于 2026-12-02约 1 分钟读完

先看结论

语法与 QGIS 一致:字段写 "字段名",字符串用单引号,变量以 @ 开头。

适用写标注表达式、按表达式选择、字段计算器

表达式用在哪里

表达式用于标注、分类 / 分级渲染、按表达式选择、字段计算器与数据驱动样式。语法与 QGIS 一致:字段名用双引号,字符串用单引号,变量以 @ 开头。共 142 个函数,下面可以按函数名或说明搜索,点函数名展开看签名和示例。

写表达式的小提示

  • 拼接文本用 ||,例如 "name" || '(' || "pop" || ')'。
  • 条件判断用 CASE WHEN … THEN … ELSE … END 或 if(条件, 是, 否)。
  • 几何相关的函数(如 $area、$length)按 WGS84 椭球计算,单位是平方米和米。

表达式用于标注、分类/分级渲染、按表达式选择、字段计算器与数据驱动样式。语法与 QGIS 一致:字段写 "字段名",字符串用单引号,变量以 @ 开头(如 @map_scale)。

字符串(25)

签名说明
array_to_string(array[, delimiter = ','[, empty_value = '']])用分隔符连接数组元素
array_to_string(array('a','b'), '-') → 'a-b'
ascii(string)首字符的 Unicode 码点
ascii('A') → 65
char(code)Unicode 码点对应的字符
char(65) → 'A'
concat(a, b, ...)连接所有参数为一个字符串,NULL 当作空串
concat('a', 1, NULL) → 'a1'
format('%1 %2', a, b, ...)用参数替换模板中的 %1、%2 …
format('%1 是 %2', 'QGIS', '好用的') → 'QGIS 是 好用的'
format_number(number[, places])按千分位分组并保留 places 位小数
format_number(10000.123, 2) → '10,000.12'
left(string, length)取左侧 length 个字符
left('Hello', 2) → 'He'
length(string | geometry)字符串的字符数;若参数为几何(如 $geometry)则返回其长度(米,线为长度、面为周长)
length('hello') → 5
lower(string)转为小写
lower('ABC') → 'abc'
lpad(string, width, fill)左侧用 fill 补齐到 width 个字符;超长则截断
lpad('Hello', 10, 'x') → 'xxxxxHello'
ltrim(string[, characters])去除左侧空白(或指定字符集合中的字符)
ltrim('xxab', 'x') → 'ab'
regexp_match(string, regex)正则首次匹配的位置(从 1 计),不匹配返回 0
regexp_match('QGIS ROCKS', 'rock') → 0
regexp_replace(string, regex, after)按正则替换所有匹配;after 中可用 \1 引用分组
regexp_replace('QGIS SHOULD ROCK', '\\sSHOULD\\s', ' DOES ') → 'QGIS DOES ROCK'
regexp_substr(string, regex)返回正则匹配到的部分(有捕获组时返回第一个分组),不匹配返回空串
regexp_substr('abc123', '(\\d+)') → '123'
replace(string, before, after)替换所有出现的子串
replace('QGIS SHOULD ROCK', 'SHOULD', 'DOES') → 'QGIS DOES ROCK'
right(string, length)取右侧 length 个字符
right('Hello', 2) → 'lo'
rpad(string, width, fill)右侧用 fill 补齐到 width 个字符;超长则截断
rpad('Hello', 10, 'x') → 'Helloxxxxx'
rtrim(string[, characters])去除右侧空白(或指定字符集合中的字符)
rtrim('abxx', 'x') → 'ab'
string_to_array(string[, delimiter = ','[, empty_value = '']])按分隔符拆分字符串为数组
string_to_array('1,2,3') → array(1,2,3)
strpos(haystack, needle)子串首次出现的位置(从 1 计),未找到返回 0
strpos('HELLO WORLD', 'WORLD') → 7
substr(string, start[, length])截取子串:start 从 1 计,负数从尾部数;length 为负表示尾部去掉的字符数
substr('HELLO WORLD', 3, 5) → 'LLO W'
title(string)每个单词首字母大写、其余小写
title('hello WORLD') → 'Hello World'
trim(string)去除首尾空白
trim(' a ') → 'a'
upper(string)转为大写
upper('abc') → 'ABC'
wordwrap(string, length[, delimiter])按最大长度自动换行(length 为负表示最小行长),delimiter 默认空格
wordwrap('UNIVERSITY OF QGIS', 13) → 'UNIVERSITY OF\nQGIS'

数学(29)

签名说明
abs(value)绝对值
abs(-2) → 2
acos(value)反余弦
asin(value)反正弦
atan(value)反正切
atan2(dy, dx)二参数反正切
atan2(1, 1) → 0.785…
ceil(value)向上取整
ceil(1.2) → 2
clamp(minimum, value, maximum)把 value 限制在 [minimum, maximum] 内
clamp(1, 5, 3) → 3
cos(value)余弦(弧度)
degrees(value)弧度转角度
degrees(pi()) → 180
exp(value)e 的 value 次幂
exp(1) → 2.718…
floor(value)向下取整
floor(-1.5) → -2
ln(value)自然对数
ln(1) → 0
log(base, value)以 base 为底的对数
log(2, 8) → 3
log10(value)以 10 为底的对数
log10(100) → 2
max(a, b, ...)最大值(忽略 NULL)
max(2, 10.2, 5.5) → 10.2
min(a, b, ...)最小值(忽略 NULL)
min(2, 10.2, 5.5) → 2
mod(a, b)取模(a 除以 b 的余数);b 为 0 返回 NULL
mod(9, 4) → 1
pi()圆周率 π
pi() → 3.14159…
pow(base, exponent)幂
pow(2, 3) → 8
power(base, exponent)幂(同 pow)
power(2, 3) → 8
radians(value)角度转弧度
radians(180) → 3.14159…
rand(min, max)[min, max] 区间的随机整数
rand(1, 10)
randf([min = 0[, max = 1]])[min, max) 区间的随机浮点数
randf(0, 100)
round(value[, places])四舍五入到 places 位小数(默认 0)
round(1234.567, 2) → 1234.57
scale_exp(value, domain_min, domain_max, range_min, range_max, exponent)指数映射:与 scale_linear 类似,但用 exponent 控制曲线
scale_exp(5, 0, 10, 0, 100, 2) → 25
scale_linear(value, domain_min, domain_max, range_min, range_max)线性映射:把 [domain_min, domain_max] 内的 value 映射到 [range_min, range_max],越界取端点
scale_linear(5, 0, 10, 0, 100) → 50
sin(value)正弦(弧度)
sqrt(value)平方根
sqrt(9) → 3
tan(value)正切(弧度)

转换(6)

签名说明
to_bool(value)转为布尔:数字非 0 为真;字符串 'true'/'1'/'yes' 为真(不区分大小写),其余为假
to_bool('yes') → true
to_date(value[, format])转为日期;format 为 Qt 风格格式串(如 'dd/MM/yyyy'),缺省按 ISO 解析
to_date('2024-03-05') → 2024-03-05
to_datetime(value[, format])转为日期时间;format 为 Qt 风格格式串(如 'yyyy-MM-dd HH:mm:ss')
to_datetime('2024-03-05 12:30:00')
to_int(value)转为整数(截断小数);无法转换返回 NULL
to_int('123.7') → 123
to_real(value)转为实数;无法转换返回 NULL
to_real('123.45') → 123.45
to_string(value)转为字符串
to_string(123) → '123'

条件(4)

签名说明
coalesce(a, b, ...)返回第一个非 NULL 的参数(惰性求值)
coalesce(NULL, 2) → 2
if(condition, result_when_true, result_when_false)条件为真返回第二个参数,否则返回第三个参数(惰性求值)
if("pop" > 1000, '大', '小')
nullif(a, b)a 等于 b 时返回 NULL,否则返回 a
nullif('', '') → NULL
try(expression[, alternative])表达式出错时返回 alternative(默认 NULL),否则返回表达式结果
try(to_int('a'), 0) → 0

日期时间(14)

签名说明
age(datetime_a, datetime_b)两个日期的差(a - b),以天为单位的数值(可为小数)
age('2024-03-05', '2024-03-01') → 4
datetime_from_epoch(milliseconds)由 epoch 毫秒数构造日期时间
datetime_from_epoch(0)
day(datetime)日(1~31)
day('2024-03-05') → 5
day_of_week(datetime)星期几:0 为周日,1 为周一 … 6 为周六
day_of_week('2024-03-05') → 2
epoch(datetime)自 1970-01-01T00:00:00Z 起的毫秒数
epoch('1970-01-02T00:00:00Z') → 86400000
format_date(datetime, format)按 Qt 风格格式串格式化日期:yyyy 年、MM 月、dd 日、HH 时、mm 分、ss 秒、MMM/MMMM 英文月名、ddd/dddd 英文星期
format_date('2024-03-05', 'yyyy年MM月dd日') → '2024年03月05日'
hour(datetime)小时(0~23)
make_date(year, month, day)由年、月、日构造日期
make_date(2024, 3, 5) → 2024-03-05
make_datetime(year, month, day, hour, minute, second)由年、月、日、时、分、秒构造日期时间
make_datetime(2024, 3, 5, 12, 30, 0)
minute(datetime)分钟(0~59)
month(datetime)月份(1~12)
month('2024-03-05') → 3
now()当前日期时间
now()
second(datetime)秒(0~59)
year(datetime)年份
year('2024-03-05') → 2024

几何(31)

签名说明
area(geometry)面积(平方米,大地测量);非面几何为 0
area($geometry)
bounds(geometry)外接矩形,返回面
bounds_height(geometry)外接矩形高度(度)
bounds_width(geometry)外接矩形宽度(度)
buffer(geometry, distance[, segments])按距离(米)生成缓冲区面;segments 为每四分之一圆的分段数(默认 8)
buffer($geometry, 100)
centroid(geometry)几何的质心(面积加权),返回点
centroid($geometry)
contains(geometry_a, geometry_b)geometry_a 是否包含 geometry_b
contains($geometry, geom_from_wkt('POINT (116.4 39.9)'))
convex_hull(geometry)凸包,返回面
crosses(geometry_a, geometry_b)两几何是否交叉
crosses($geometry, geom_from_wkt('POINT (116.4 39.9)'))
disjoint(geometry_a, geometry_b)两几何是否不相交
disjoint($geometry, geom_from_wkt('POINT (116.4 39.9)'))
distance(geometry_a, geometry_b)两几何间最短距离(米,大地测量)
distance($geometry, geom_from_wkt('POINT (116.4 39.9)'))
end_point(geometry)最后一个顶点,返回点
geom_from_wkt(text)由 WKT 文本构造几何
geom_from_wkt('POINT (116.4 39.9)')
geom_to_wkt(geometry)几何转 WKT 文本
geom_to_wkt($geometry)
geometry_type(geometry)几何大类:'Point' / 'Line' / 'Polygon'
intersects(geometry_a, geometry_b)两几何是否相交
intersects($geometry, geom_from_wkt('POINT (116.4 39.9)'))
is_empty(geometry)几何是否为空
make_point(x, y)由 X/Y 坐标构造点
make_point(116.4, 39.9)
num_geometries(geometry)多部件几何的部件数(单部件为 1)
num_points(geometry)顶点数
num_points($geometry)
perimeter(geometry)周长(米,大地测量);非面几何为 0
perimeter($geometry)
point_n(geometry, n)第 n 个顶点(从 1 计,负数从尾部数),返回点
point_n($geometry, 1)
start_point(geometry)第一个顶点,返回点
touches(geometry_a, geometry_b)两几何是否仅在边界接触
touches($geometry, geom_from_wkt('POINT (116.4 39.9)'))
within(geometry_a, geometry_b)geometry_a 是否在 geometry_b 内
within($geometry, geom_from_wkt('POINT (116.4 39.9)'))
x(geometry)点的 X 坐标(经度);非点几何取质心
x($geometry)
x_max(geometry)最大 X(经度)
x_min(geometry)最小 X(经度)
y(geometry)点的 Y 坐标(纬度);非点几何取质心
y($geometry)
y_max(geometry)最大 Y(纬度)
y_min(geometry)最小 Y(纬度)

聚合(14)

签名说明
array_agg(expression[, group_by[, filter]])把所有值收集为数组
array_agg("population")
concatenate(expression[, group_by[, filter[, concatenator]]])把所有非 NULL 值连接为字符串,concatenator 为分隔符(默认 ,)
concatenate("name", NULL, NULL, ', ')
count([expression[, group_by[, filter]]])要素数量;给出 expression 时只统计其值非 NULL 的要素
count("name")
count_distinct(expression[, group_by[, filter]])expression 的不重复值数量(不含 NULL)
count_distinct("population")
count_missing(expression[, group_by[, filter]])expression 为 NULL 的要素数量
count_missing("population")
majority(expression[, group_by[, filter]])众数(出现最多的值)
majority("population")
maximum(expression[, group_by[, filter]])最大值(数值或字符串,忽略 NULL)
maximum("population")
mean(expression[, group_by[, filter]])平均值(忽略 NULL)
mean("population")
median(expression[, group_by[, filter]])中位数(忽略 NULL)
median("population")
minimum(expression[, group_by[, filter]])最小值(数值或字符串,忽略 NULL)
minimum("population")
minority(expression[, group_by[, filter]])少数值(出现最少的值)
minority("population")
range(expression[, group_by[, filter]])极差(最大值 - 最小值)
range("population")
stdev(expression[, group_by[, filter]])总体标准差(忽略 NULL)
stdev("population")
sum(expression[, group_by[, filter]])求和(忽略 NULL)
sum("population")

数组(14)

签名说明
array(a, b, ...)由参数构造数组
array(1, 2, 'a')
array_append(array, value)在尾部追加元素
array_contains(array, value)数组是否包含 value
array_contains(array(1,2,3), 2) → true
array_distinct(array)去重
array_find(array, value)元素首次出现的索引(从 0 计),不存在返回 -1
array_find(array('a','b'), 'b') → 1
array_first(array)第一个元素;空数组返回 NULL
array_get(array, index)取第 index 个元素(从 0 计,负数从尾部数);越界返回 NULL
array_get(array('a','b','c'), 1) → 'b'
array_last(array)最后一个元素;空数组返回 NULL
array_length(array)元素个数
array_length(array(1,2,3)) → 3
array_mean(array)数值元素平均值(忽略 NULL)
array_prepend(array, value)在头部插入元素
array_reverse(array)反转数组
array_sort(array[, ascending = true])排序(NULL 排在最前)
array_sort(array(3,1,2)) → array(1,2,3)
array_sum(array)数值元素求和(忽略 NULL)

记录与属性(3)

签名说明
attribute(name)按名称读取当前要素的属性值;字段不存在返回 NULL
attribute('name')
uuid([format = 'WithBraces'])生成随机 UUID;format 为 'WithoutBraces' 时不带花括号
uuid()
var(name)读取变量(等价于 @name);不存在返回 NULL
var('map_scale')

颜色(2)

签名说明
color_rgb(red, green, blue)由 RGB 分量(0~255)生成颜色字符串 'r,g,b'
color_rgb(255, 127, 0) → '255,127,0'
color_rgba(red, green, blue, alpha)由 RGBA 分量(0~255)生成颜色字符串 'r,g,b,a'
color_rgba(255, 127, 0, 200) → '255,127,0,200'
这篇有帮助吗?

没找到答案?问 AI 客服

QGISAI 的功能、格式、账号和会员问题,都可以直接问。