3

I am not totally new to QGIS but still working my way through. I simply need to convert the field qn of my Shapefile which is now string to integer in order to use the v.to. 3d GRASS tool.

I know there are similar threads but I've tried the "Refactor fields" tool, the conversion tool and even adding a new field after tweaking the CSV in Excel but nothing seems to work.

enter image description here

The data set contains some characters which I cannot find and replace in Excel. I think that is way the Refactor fields doesn't work. enter image description here

2
  • 1
    You can't convert decimal values to integer.
    – Erik
    Feb 11, 2022 at 10:03
  • 1
    You state that it's a shapefile, but then reference Excel and CSV... You can't change the type of a dBase field, and using Excel on a shapefile will often corrupt the dataset. If CSV is involved, it's not a shapefile (which is a specific binary format, not a generic term for a data source)., and the problem is different.
    – Vince
    Feb 11, 2022 at 11:46

2 Answers 2

5

I dont think it is possible to change it, but you can add a new integer field and calculate it from qn field.

Or use Refactor Fields, change Type to integer. It will create a new layer though:

enter image description here

2

You're probably right that there are some unusual characters in that column confusing QGIS. Otherwise it would have treated qn as a float field the moment it imported.

It looks like some of your numbers are written with a comma as the decimal place. I don't know if QGIS can sometimes handle those, but they probably aren't helping here.

The Refactor Fields Tool should work with changing the type from Text (string) to Whole number (integer - 64bit). However, it will silently discard any rows it can't convert. It would by good to identify those rows instead, so you can fix them.

You can write an Expression to give you a more specific result. Try using the Field Calculator tool, like this:

enter image description here

  1. Set the Input layer to Microrilievipianura_2.
  2. Set the Field name to something like qn_float while you experiment.
  3. Set the Result field type to Float.
  4. Set the Expression to to_real("qn").
  5. Click on Run.

If it works, good. If it fails, and I think it will, you will get a results log that includes the line:

Evaluation error in expression "to_real("qn")": Cannot convert '<something>' to double

So try this again, and this time set the Expression to:

try(to_real("qn"))

This very handy function will return the output of to_real if it succeeded, and NULL if it returned an error. You can then sort or filter the qn_float column to find these NULL values and try to work out what is the problem in the qn column.

If you may have NULL in the column for other reasons, you can also specify a default value to output in the event of error:

try(to_real("qn"), -9.99)

Did this help you clean up your table so you can use it? Once you are confident with your Expression in the Field Calculator, you can then go back to the Refactor Fields Tool and use this style of Expression as the Source Expression for each field you want to convert. This way you can clean up all columns in a table in one operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.