Skip to content

Instantly share code, notes, and snippets.

@DenisCarriere
Last active October 13, 2015 22:45
Show Gist options
  • Save DenisCarriere/f3ff25e9f2abe8432bc1 to your computer and use it in GitHub Desktop.
Save DenisCarriere/f3ff25e9f2abe8432bc1 to your computer and use it in GitHub Desktop.
QGIS Field Calculator - Starts with number?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
QGIS Field Calculator - Starts with number?
QGIS Version: 1.8.3
Created by: Denis Carriere
Function Editor
===============
1. Open Field Calculator in QGIS
2. Add the Python code from below to your Function Editor
3. Save file
4. Run Script
Expression
==========
1. Create a new field / Update an existing field
2. Output field name (ex: "first_number")
3. Output field type
- Whole number (integer): True=1, False=0
- Text (string): true, false
4. Create an expression using the 'Field & Values' which
will detect if the first character is an integer or not.
starts_with_number( "address" )
Example
=======
Address.shp contains (ex: "address") field.
- 453 Booth Street
- 1552 Payette Drive
- Ottawa River
Results of the output field (ex: "first_number"):
- true
- true
- false
"""
from qgis.core import *
from qgis.gui import *
@qgsfunction(args="auto", group='Custom')
def starts_with_number(value, feature, parent):
try:
int(value[0])
return True
except:
return False
@NathanW2
Copy link

Can also do it using the regex_match function:

regexp_match('1Test', '^[0-9].*') -> 1
regexp_match('a1Test', '^[0-9].*') -> 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment