Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import re
  2. RES_STRING='MINERAL CLAIM'
  3. REGEX_HAYSTACK='DISTRICT LOT 5639, BEING AWARD NO. 2 MINERAL CLAIM, KDYD'
  4.  
  5. REGEX_STRING=re.compile(RES_STRING)
  6. print "searching for "+RES_STRING+" in "+REGEX_HAYSTACK
  7. REGEX_MATCH = REGEX_STRING.search(REGEX_HAYSTACK)
  8. if REGEX_MATCH:
  9. print "found '"+REGEX_MATCH.group()+"'"
  10. else:
  11. print "No match found"
  12.  
  13. def select_by_regex(input_layer,attribute_name,regex_string):
  14. import re
  15. RES_STRING=regex_string
  16. attribute_name_idx = input_layer.fieldNameIndex(attribute_name)
  17. if attribute_name_idx<0:
  18. raise valueError("cannot find attribute"+attribute_name)
  19. else:
  20. fids=[]
  21. for feature in input_layer.getFeatures():
  22. REGEX_HAYSTACK=feature[attribute_name_idx]
  23. REGEX_STRING=re.compile(RES_STRING)
  24. REGEX_MATCH = REGEX_STRING.search(REGEX_HAYSTACK)
  25. if REGEX_MATCH:
  26. fids.append(feature.id())
  27. else:
  28. pass
  29. input_layer.setSelectedFeatures(fids)
  30.  
  31.  
  32. #USAGE BIT
  33. input_layer = QgsVectorLayer('path/to/shape/file.shp','layer name', 'ogr')
  34. QgsMapLayerRegistry.instance().addMapLayer(input_layer)
  35. regex_string='MINERAL CLAIM'
  36. attribute_name='TITLE'
  37. select_by_regex(input_layer,attribute_name,regex_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement