[GIS] Field Calculator giving Error message in QGIS

field-calculatorqgis

Field Calculator: using the x- or yat or (0 "or-1" ) expression, I keep receiving an error:

An error occurred while evaluating the calculation string: Index is
out of range.

I have shut down and restarted a few times, sometimes the error does not return.

Why is this happening and what can I do to fix it?

Best Answer

This could be the solution to your issue.
My line dataset had lines dissolved by an attribute, so that two (or more) lines in separate locations were merged (the dissolve command has an option for this or the merge command may have been used) to a single record of the attribute table.

Dissolve

Merge record

Merge Command

vertexedit

When editing the vertices of such a record you can see the multiparts.

Multipart

When you define the starting vertex (xat(0), yat(0)) of the line there are actually two. I think that this is where the error originates from.

A way to avoid this is to use the 'Explode lines' tool from the processing toolbox (from the "Processing" plugin) or the explode tool from the edit toolbar.
Edit Toolbar
This will create a new record in your attribute table for every line segment meaning there is only one start and end vertex for each record. I used this on my line dataset and the x- or yat or (0 "or-1" ) expressions worked without error.

After your calculations you can dissolve your lines again using any of the dissolve functions from the processing toolbox.

Related Question