So I thought that I would post the solution that I came up with based on the excellent guidance of @Babel.
My source dataset was a spreadsheet of geocoded EV chargers across the country that came in as strings. So my first step was to create binned fields as follows:
-- handle definition
try(coalesce(to_int(attribute( @feature, 'Number of')), 0), 0)
-- handle_bin definition
case
when attribute( @feature, 'handles') > 10 then 5
when attribute( @feature, 'handles') >= 8 then 4
when attribute( @feature, 'handles') >= 4 then 3
when attribute( @feature, 'handles') >= 2 then 2
when attribute( @feature, 'handles') >= 1 then 1
else 0
end
-- power definition
try(coalesce(to_int(attribute( @feature, 'Power Ra_1')), 0), 0)
-- power_bin definition
case
when attribute( @feature, 'power') >= 250 then 5
when attribute( @feature, 'power') >= 150 then 4
when attribute( @feature, 'power') >= 100 then 3
when attribute( @feature, 'power') >= 50 then 2
when attribute( @feature, 'power') >= 1 then 1
else 0
end
After this, it was a pretty simple matter to follow Babel's instructions to create a grid (I used 10km for speed since my country is very large, might increase resolution later but it does not change anything in the code) and then add a calculated field as folllows
-- DCTravelScore definition
coalesce(
array_sum(
array_foreach(
overlay_nearest (
'Australia_chargers_xlsx_2c8dfa1c_99e3_4eeb_ae67_988ade0ad84e',
@feature, -- available since QGIS 3.28
max_distance:= 100000, -- define the distance
limit:= -1
),
with_variable(
'distance',
distance($geometry, geometry(@element)),
coalesce(attribute (@element, 'handle_bin'), 0) *
coalesce(attribute (@element, 'power_bin'), 0) *
case
when @distance <= 5000.0 then 5
when @distance <= 10000.0 then 4
when @distance <= 20000.0 then 3
when @distance <= 50000.0 then 2
when @distance <= 100000.0 then 1
else 0
end
)
)
), 0
)
Not sure whether there is a more elegant way to do binning like this? At least the with_variable
function exists to create a variable in local scope without jumping into full script mode!