I need to style a survey parcel polygon shapefile, based on whether the polygon is a mineral claim or not. Unfortunately, the only information on whether a polygon is a mineral claim or not is contained in the "TITLE" field of the attribute table, which gives the full legal name of the surveyed parcel. For instance, 'DISTRICT LOT 5639, BEING AWARD NO. 2 MINERAL CLAIM, KDYD'. I need an expression that selects any feature containing the text 'MINERAL CLAIM' in the "TITLE" field.
You just gotta use the
"TITLE" LIKE '%MINERAL CLAIM%'
% symbol acts like a wildcard.
LIKE is case-sensitive, whereas
ILIKE is not.
I had this exact problem and solved it from the python console with regex. While regex can be tricky it's very powerful. And you'll be left with a tool you can use with more difficult match cases. Here are the docs. and here is a nice online machine for testing your regex strings.
Firstly here is the quick script I run to check my regex strings in qgis
import re RES_STRING='MINERAL CLAIM' REGEX_HAYSTACK='DISTRICT LOT 5639, BEING AWARD NO. 2 MINERAL CLAIM, KDYD' REGEX_STRING=re.compile(RES_STRING) print "searching for "+RES_STRING+" in "+REGEX_HAYSTACK REGEX_MATCH = REGEX_STRING.search(REGEX_HAYSTACK) if REGEX_MATCH: print "found '"+REGEX_MATCH.group()+"'" else: print "No match found"
Once you're happy with your regex matching you could wrap it up in a function to provide a selection for all the features that match. Below is a function to do just that.
def select_by_regex(input_layer,attribute_name,regex_string): import re RES_STRING=regex_string attribute_name_idx = input_layer.fieldNameIndex(attribute_name) if attribute_name_idx<0: raise valueError("cannot find attribute"+attribute_name) else: fids= for feature in input_layer.getFeatures(): REGEX_HAYSTACK=feature[attribute_name_idx] REGEX_STRING=re.compile(RES_STRING) REGEX_MATCH = REGEX_STRING.search(REGEX_HAYSTACK) if REGEX_MATCH: fids.append(feature.id()) else: pass input_layer.setSelectedFeatures(fids) #USAGE BIT input_layer = QgsVectorLayer('path/to/shape/file.shp','layer name', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(input_layer) regex_string='MINERAL CLAIM' attribute_name='TITLE' select_by_regex(input_layer,attribute_name,regex_string)
You will need to save this into a file and run it from the qgis python ide.
(untested but pretty confident)