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.

This question builds on a previous question I came across at gis.stackexchange: How to easily edit attribute data using Regular Expressions?

Perhaps someone can point me in the right direction?

I have a vector shapefile of contour lines. In the attribute table I have already converted a field called FEET consisting of integer data containing elevation values to a string field called Label. Additionally I successfully appended a ' to the end of the string so that when the data is labeled it will be read as feet.

The next thing I am attempting to do is to insert a comma between the first two values of the string only if the string length is greater than 4, or so that a comma will only appear between the thousands and hundreds digits in the Label field. The length has to be greater than 4 because I'm also counting the '

My code so far is:

case when  length( "Label") >4  then regexp_replace( "Label", '^([0-9])+([0-9]{3}\')$','$1 , $2') else "Label" end

however the regexp_replace function doesn't seem to accept the group variables $1 $2 in the 3rd parameter of the function.

I've looked at http://docs.python.org/2/library/re.html as the field-calculator regex is from what I understand based on python's use of regex but haven't been able to figure this out. Maybe what I'm attempting to do isn't possible yet with this function in field calculator in QGIS 1.8 on mac OSX. Or more likely my syntax is wrong as I'm a novice programmer and somewhat new to regular expressions. Any suggestions however would be appreciated.

much thanks,

-Chris

share|improve this question
    
Does it have to be coded - wouldn't a simple way be to (in Excel, or similar) create a new column which had the label in it formated how you wanted it - all you would have to use is a few concatenate command. –  Andrew Tice Apr 21 '13 at 6:06
    
Yes that would be one way of solving the problem, however I'm trying to solve it from within QGIS or with python. –  chris henrick Apr 21 '13 at 17:00
    
@chrishenrick - did my answer solve it for you? –  Stev_k Nov 26 '13 at 19:15
    
@Stev_k haven't had time to try it yet, will do so soon and let you know. Thanks for your help! –  chris henrick Nov 26 '13 at 22:33

2 Answers 2

You can do digit grouping easily in Python. Here's a function that I've used, which I think I found on StackOverflow:

def digitgroup(n, sep = ','):
    if n == "": n = 0
    s = str(n)[::-1]
    groups = []
    i = 0
    while i < len(s):
        groups.append(s[i:i+3])
        i += 3
    return sep.join(groups)[::-1]
share|improve this answer
    
thanks for the suggestion, dmahr. however I'm unclear on how to implement this function in QGIS. I attempted cutting and pasting this function into the field calculator expression and setting n to my field name but with no luck. Is there a way to implement python scripting in QGIS outside of field calculator? –  chris henrick Mar 26 '13 at 22:05

It's a bit more complicated than that in QGis. As per this article, you have to create your own function definition in Python, then import it into QGis (this can be done automatically on startup). This may seem tricky, but you only need to do it once and it will always be there!

This is the function I used, based on dmahr's above. Not sure how it deals with strings, as I modified the code a bit.

from qgis.utils import qgsfunction
from qgis.core import QGis

@qgsfunction(2, "Python") # number of arguments
def thousands_separator(values, feature, parent): # values are the arguments passed in
    # this is the documentation for your function which you will see in QGis
    """
    Adds a separator to values over 1000\n        
    Parameters: (n, sep) \n                   
    n = expression_field \n
    sep = desired separator (default is ",")
    """ 
    number = values[0]                        # the first argument (which in the
    separator = values[1]                     # labelling case is a field name
    if separator == '':
        separator = ','
    s = str(number)[::-1]
    groups = []
    i = 0
    while i < len(s):
        groups.append(s[i:i+3])
        i += 3
    return separator.join(groups)[::-1]

You then save that in .qgis/python, import the file into QGis using the console, then it will show up in "Python" functions in the label tool.

The expression I used is: thousands_comma( "[field_name]" ,',')

share|improve this answer
    
Also, I didn't see what version you were using - you should upgrade to 2.0 to make this work –  Stev_k Nov 20 '13 at 13:09

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.