首页 > 解决方案 > 为使用 Qgis 中的聚合函数连接的每个值创建索引

问题描述

这是我在堆栈溢出中的第一篇文章。如果您能帮助我解决以下问题,我将非常高兴:

我正在使用 qgis 计算器,特别是聚合函数,来获取穿过我的管道层的电缆的名称(id),因为有时有不止一根电缆穿过管道。代码如下:

aggregate(
   layer:='cables',
   aggregate:='concatenate', 
   expression:= to_string(name), 
   concatenator:=',',
   filter:=within(geometry(@parent),buffer($geometry,0.2))
)

这段代码在我的管道层中填充了一个名为“电缆”的字段,如下所示:

CLS00083,CLS00084,CLS000309

例子

我现在想索引每个已填充的值,使其如下所示:

1:CBL00083,2:CBL00084,3:CBL000309,4:CBL000310 etc. 

例子

有没有办法使用聚合函数来实现这一点?

非常感谢你的帮助

标签: indexingexpressiongisaggregate-functionsqgis

解决方案


Use this expression:

array_to_string (
    with_variable (
        'array',
        
            aggregate(
                layer:='cables',
                aggregate:='array_agg', 
                expression:= to_string(name), 
                concatenator:=','
            ),
            array_foreach (
                @array,
                (to_int (array_find (@array, @element))+1) || ': '  ||  @element
)))

I suppose cables and ducts are on different layers. If not, you could use the easier array_agg function instead of aggregate, something like:

array_to_string(
    with_variable (
        'array',
        array_agg(to_string(name)),
        array_foreach (
            @array,
            (to_int (array_find (@array, @element))+1) || ': '  ||  @element
)))

By the way: for GIS-related questions, better use GIS SE.


推荐阅读