Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I can't seem to find any way to take the logarithm (natural or base 10) of a field in QGIS. I have gotten to the field calculator but there is not a button for log and I can't seem find any formula that works.

I'm using 1.4.0 on linux currently. I couldn't find anything in release notes about this being added since, but does anyone know if it's in a newer release?

Many thanks!

share|improve this question
    
It's not in the currently supported list of functions but if you open a request to add it on hub.qgis.org/projects/quantum-gis I'm sure someone could add it for you. –  Nathan W Oct 5 '11 at 8:55
    
@NathanW or if they can add an equation parser. I'd love for a feature that does the following Average(some_field_1, some_field_2, ...) / sumof_elements(some_row1) * sumof_elements(some_row2). Where the average could be on the fields of the same line as the value getting calculated. –  dassouki Oct 5 '11 at 11:56
    
there is already a equation/expression parser (see qgis.org/api/classQgsExpression.html). It's just a matter of expanding it to include the functions. I also have thought about adding Python support like Arc to the field calculator but just have no time right now. –  Nathan W Oct 5 '11 at 12:30

5 Answers 5

up vote 3 down vote accepted

As a workaround to the lack of this in QGIS you could open the .dbf file of your shapefile set (export it to shapefile first if you are using a different format) and open it in Libre Office Calc (or openoffice, MS Excel 97/2003) to calculate the field in a new column with one of the following formulas:

=LOG(Number,Base)
=LOG10(Number)
=LN(Number)

You can use practically all spreadsheet functions this way as long your feature count doesn't go into the millions and you don't reorder or delete any rows.

Make sure the header of the column is DBF compatible

  1. field Name (10 characters max)
  2. data type (most likely N for numeric or C for character strings)
  3. field length (including decimals)
  4. number of decimals (optional for N)

Example:

VALUE_LOG,N,24,12

Backup the original .dbf before you make any saves and save/replace the opened one as a dBase (.dbf) file.

One .dbf may only be opened for editing by one program at a time so make sure you unload the shapefile first in qgis before loading it in calc and vice versa.

share|improve this answer
    
Thanks, Max. I figured this solution yesterday with OpenOffice Calc (Libre seems less stable for some reason on my machine) but in any case, it worked perfectly. I didn't know how to structure the column headers (which your post now explains), so I created the new columns in qGIS first and then just change the values in OpenOffice. Thanks for the help. –  computermacgyver Oct 8 '11 at 14:38
    
P.S. To future readers, it is also important to check for zero values, which are undefined for the logarithm; so something like =IF(REF=0;-1;LN(REF)) might be appropriate where REF is the cell upon which you are calculating the logarithm and -1 is some special value that you want to assign for zero values. –  computermacgyver Oct 8 '11 at 20:48

This is the list of actual functions supported by the QGIS field calculator.

If you find something obvious missing, like the log function, please file a feature request ticket in the QGIS bug tracker, example

http://hub.qgis.org/issues/4365

QGIS field calculator functions:

'string' literal string value

number number

NULL null value

sqrt(a) square root

sin(a) sinus of a

cos(a) cosinus of b

tan(a) tangens of a

asin(a) arcussinus of a

acos(a) arcuscosinus of a

atan(a) arcustangens of a

to int(a) convert string a to integer

to real(a) convert string a to real

to string(a) convert number a to string

lower(a) convert string a to lower case

upper(a) convert string a to upper case

length(a) length of string a

atan2(y,x) arcustangens of y/x using the signs of the two arguments to determine the quadrant of the result.

replace(a,replacethis,withthat) replace replacethis with withthat in string a

regexp_replace(a,replacethis,withthat) replace the regular expression replacethis with withthat in string a

substr(a,from,len) len characters of string a starting from from (first character index is 1)

a || b concatenate strings a and b

$rownum number current row

$area area of polygon

$perimeter perimeter of polygon

$length area of line

$id feature id

$x x coordinate of point

$y y coordinate of point

xat(n) x coordinate of nth point of a line (indizes start at 0; negative values apply to the last index)

yat(n) y coordinate of nth point of a line (indizes start at 0; negative values apply to the last index)

a ^ b a raised to the power of b

a * b a multiplied by b

a / b a divided by b

a + b a plus b

a - b a minus b

+a positive sign

-a negative value of a

share|improve this answer
    
Thanks Giovanni! I created a wiki page listing all these functions for future reference: qgis.org/wiki/List_of_Field_Calculator_Functions –  underdark Oct 7 '11 at 17:32

I suspect it's going to be hard because you have to write some python code.

You could write a python plugin where you can access the attribute table and use any python function you want in setting an attribute value.

Or, instead of writing a plugin, you could run the code directly in the python console. This should save you some time, but if you need to run this function repeatedly a plugin is more efficient in the long run.

This is a link to the section on accessing the attribute tables from the tutorial on python plugins. This is the documentation for the log function in python.

share|improve this answer

Asked functions added to QGIS

http://hub.qgis.org/issues/4365#note-1

This is how it (the QGIS project) works... just ask, if it make sense it will be probably be added in a short time ;)

share|improve this answer
    
And it's already fixed! –  Mike T Oct 12 '11 at 0:40

I'm using qgis 1.7 and it seems that there is not a log function in the expression field.

You can try using gvSIG. It has natural logarithm in its expression field.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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