AlgorithmsTestBase.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. """
  2. ***************************************************************************
  3. AlgorithmsTest.py
  4. ---------------------
  5. Date : January 2016
  6. Copyright : (C) 2016 by Matthias Kuhn
  7. Email : matthias@opengis.ch
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Matthias Kuhn'
  18. __date__ = 'January 2016'
  19. __copyright__ = '(C) 2016, Matthias Kuhn'
  20. import qgis # NOQA switch sip api
  21. import os
  22. import yaml
  23. import nose2
  24. import shutil
  25. import glob
  26. import hashlib
  27. import tempfile
  28. import re
  29. from osgeo import gdal
  30. from osgeo.gdalconst import GA_ReadOnly
  31. from numpy import nan_to_num
  32. from copy import deepcopy
  33. from qgis.core import (QgsVectorLayer,
  34. QgsRasterLayer,
  35. QgsCoordinateReferenceSystem,
  36. QgsFeatureRequest,
  37. QgsMapLayer,
  38. QgsProject,
  39. QgsApplication,
  40. QgsProcessingContext,
  41. QgsProcessingUtils,
  42. QgsProcessingFeedback)
  43. from qgis.analysis import (QgsNativeAlgorithms)
  44. from qgis.testing import (_UnexpectedSuccess,
  45. start_app,
  46. unittest)
  47. from utilities import unitTestDataPath
  48. import processing
  49. def processingTestDataPath():
  50. return os.path.join(os.path.dirname(__file__), 'testdata')
  51. class AlgorithmsTest:
  52. def test_algorithms(self):
  53. """
  54. This is the main test function. All others will be executed based on the definitions in testdata/algorithm_tests.yaml
  55. """
  56. with open(os.path.join(processingTestDataPath(), self.test_definition_file())) as stream:
  57. algorithm_tests = yaml.load(stream, Loader=yaml.SafeLoader)
  58. if 'tests' in algorithm_tests and algorithm_tests['tests'] is not None:
  59. for idx, algtest in enumerate(algorithm_tests['tests']):
  60. print('About to start {} of {}: "{}"'.format(idx, len(algorithm_tests['tests']), algtest['name']))
  61. yield self.check_algorithm, algtest['name'], algtest
  62. def check_algorithm(self, name, defs):
  63. """
  64. Will run an algorithm definition and check if it generates the expected result
  65. :param name: The identifier name used in the test output heading
  66. :param defs: A python dict containing a test algorithm definition
  67. """
  68. self.vector_layer_params = {}
  69. QgsProject.instance().clear()
  70. if 'project' in defs:
  71. full_project_path = os.path.join(processingTestDataPath(), defs['project'])
  72. project_read_success = QgsProject.instance().read(full_project_path)
  73. self.assertTrue(project_read_success, 'Failed to load project file: ' + defs['project'])
  74. if 'project_crs' in defs:
  75. QgsProject.instance().setCrs(QgsCoordinateReferenceSystem(defs['project_crs']))
  76. else:
  77. QgsProject.instance().setCrs(QgsCoordinateReferenceSystem())
  78. if 'ellipsoid' in defs:
  79. QgsProject.instance().setEllipsoid(defs['ellipsoid'])
  80. else:
  81. QgsProject.instance().setEllipsoid('')
  82. params = self.load_params(defs['params'])
  83. print('Running alg: "{}"'.format(defs['algorithm']))
  84. alg = QgsApplication.processingRegistry().createAlgorithmById(defs['algorithm'])
  85. parameters = {}
  86. if isinstance(params, list):
  87. for param in zip(alg.parameterDefinitions(), params):
  88. parameters[param[0].name()] = param[1]
  89. else:
  90. for k, p in params.items():
  91. parameters[k] = p
  92. for r, p in list(defs['results'].items()):
  93. if 'in_place_result' not in p or not p['in_place_result']:
  94. parameters[r] = self.load_result_param(p)
  95. expectFailure = False
  96. if 'expectedFailure' in defs:
  97. exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
  98. expectFailure = eval(defs['expectedFailure'][-1])
  99. if 'expectedException' in defs:
  100. expectFailure = True
  101. # ignore user setting for invalid geometry handling
  102. context = QgsProcessingContext()
  103. context.setProject(QgsProject.instance())
  104. if 'skipInvalid' in defs and defs['skipInvalid']:
  105. context.setInvalidGeometryCheck(QgsFeatureRequest.GeometrySkipInvalid)
  106. feedback = QgsProcessingFeedback()
  107. print('Algorithm parameters are {}'.format(parameters))
  108. # first check that algorithm accepts the parameters we pass...
  109. ok, msg = alg.checkParameterValues(parameters, context)
  110. self.assertTrue(ok, 'Algorithm failed checkParameterValues with result {}'.format(msg))
  111. if expectFailure:
  112. try:
  113. results, ok = alg.run(parameters, context, feedback)
  114. self.check_results(results, context, parameters, defs['results'])
  115. if ok:
  116. raise _UnexpectedSuccess
  117. except Exception:
  118. pass
  119. else:
  120. results, ok = alg.run(parameters, context, feedback)
  121. self.assertTrue(ok, 'params: {}, results: {}'.format(parameters, results))
  122. self.check_results(results, context, parameters, defs['results'])
  123. def load_params(self, params):
  124. """
  125. Loads an array of parameters
  126. """
  127. if isinstance(params, list):
  128. return [self.load_param(p) for p in params]
  129. elif isinstance(params, dict):
  130. return {key: self.load_param(p, key) for key, p in params.items()}
  131. else:
  132. return params
  133. def load_param(self, param, id=None):
  134. """
  135. Loads a parameter. If it's not a map, the parameter will be returned as-is. If it is a map, it will process the
  136. parameter based on its key `type` and return the appropriate parameter to pass to the algorithm.
  137. """
  138. try:
  139. if param['type'] in ('vector', 'raster', 'table'):
  140. return self.load_layer(id, param).id()
  141. elif param['type'] == 'vrtlayers':
  142. vals = []
  143. for p in param['params']:
  144. p['layer'] = self.load_layer(None, {'type': 'vector', 'name': p['layer']})
  145. vals.append(p)
  146. return vals
  147. elif param['type'] == 'multi':
  148. return [self.load_param(p) for p in param['params']]
  149. elif param['type'] == 'file':
  150. return self.filepath_from_param(param)
  151. elif param['type'] == 'interpolation':
  152. prefix = processingTestDataPath()
  153. tmp = ''
  154. for r in param['name'].split('::|::'):
  155. v = r.split('::~::')
  156. tmp += '{}::~::{}::~::{}::~::{};'.format(os.path.join(prefix, v[0]),
  157. v[1], v[2], v[3])
  158. return tmp[:-1]
  159. except TypeError:
  160. # No type specified, use whatever is there
  161. return param
  162. raise KeyError("Unknown type '{}' specified for parameter".format(param['type']))
  163. def load_result_param(self, param):
  164. """
  165. Loads a result parameter. Creates a temporary destination where the result should go to and returns this location
  166. so it can be sent to the algorithm as parameter.
  167. """
  168. if param['type'] in ['vector', 'file', 'table', 'regex']:
  169. outdir = tempfile.mkdtemp()
  170. self.cleanup_paths.append(outdir)
  171. if isinstance(param['name'], str):
  172. basename = os.path.basename(param['name'])
  173. else:
  174. basename = os.path.basename(param['name'][0])
  175. filepath = self.uri_path_join(outdir, basename)
  176. return filepath
  177. elif param['type'] == 'rasterhash':
  178. outdir = tempfile.mkdtemp()
  179. self.cleanup_paths.append(outdir)
  180. basename = 'raster.tif'
  181. filepath = os.path.join(outdir, basename)
  182. return filepath
  183. elif param['type'] == 'directory':
  184. outdir = tempfile.mkdtemp()
  185. return outdir
  186. raise KeyError("Unknown type '{}' specified for parameter".format(param['type']))
  187. def load_layers(self, id, param):
  188. layers = []
  189. if param['type'] in ('vector', 'table'):
  190. if isinstance(param['name'], str) or 'uri' in param:
  191. layers.append(self.load_layer(id, param))
  192. else:
  193. for n in param['name']:
  194. layer_param = deepcopy(param)
  195. layer_param['name'] = n
  196. layers.append(self.load_layer(id, layer_param))
  197. else:
  198. layers.append(self.load_layer(id, param))
  199. return layers
  200. def load_layer(self, id, param):
  201. """
  202. Loads a layer which was specified as parameter.
  203. """
  204. filepath = self.filepath_from_param(param)
  205. if 'in_place' in param and param['in_place']:
  206. # check if alg modifies layer in place
  207. tmpdir = tempfile.mkdtemp()
  208. self.cleanup_paths.append(tmpdir)
  209. path, file_name = os.path.split(filepath)
  210. base, ext = os.path.splitext(file_name)
  211. for file in glob.glob(os.path.join(path, '{}.*'.format(base))):
  212. shutil.copy(os.path.join(path, file), tmpdir)
  213. filepath = os.path.join(tmpdir, file_name)
  214. self.in_place_layers[id] = filepath
  215. if param['type'] in ('vector', 'table'):
  216. gmlrex = r'\.gml\b'
  217. if re.search(gmlrex, filepath, re.IGNORECASE):
  218. # ewwwww - we have to force SRS detection for GML files, otherwise they'll be loaded
  219. # with no srs
  220. filepath += '|option:FORCE_SRS_DETECTION=YES'
  221. if filepath in self.vector_layer_params:
  222. return self.vector_layer_params[filepath]
  223. options = QgsVectorLayer.LayerOptions()
  224. options.loadDefaultStyle = False
  225. lyr = QgsVectorLayer(filepath, param['name'], 'ogr', options)
  226. self.vector_layer_params[filepath] = lyr
  227. elif param['type'] == 'raster':
  228. options = QgsRasterLayer.LayerOptions()
  229. options.loadDefaultStyle = False
  230. lyr = QgsRasterLayer(filepath, param['name'], 'gdal', options)
  231. self.assertTrue(lyr.isValid(), 'Could not load layer "{}" from param {}'.format(filepath, param))
  232. QgsProject.instance().addMapLayer(lyr)
  233. return lyr
  234. def filepath_from_param(self, param):
  235. """
  236. Creates a filepath from a param
  237. """
  238. prefix = processingTestDataPath()
  239. if 'location' in param and param['location'] == 'qgs':
  240. prefix = unitTestDataPath()
  241. if 'uri' in param:
  242. path = param['uri']
  243. else:
  244. path = param['name']
  245. if not path:
  246. return None
  247. return self.uri_path_join(prefix, path)
  248. def uri_path_join(self, prefix, filepath):
  249. if filepath.startswith('ogr:'):
  250. if not prefix[-1] == os.path.sep:
  251. prefix += os.path.sep
  252. filepath = re.sub(r"dbname='", "dbname='{}".format(prefix), filepath)
  253. else:
  254. filepath = os.path.join(prefix, filepath)
  255. return filepath
  256. def check_results(self, results, context, params, expected):
  257. """
  258. Checks if result produced by an algorithm matches with the expected specification.
  259. """
  260. for id, expected_result in expected.items():
  261. if expected_result['type'] in ('vector', 'table'):
  262. if 'compare' in expected_result and not expected_result['compare']:
  263. # skipping the comparison, so just make sure output is valid
  264. if isinstance(results[id], QgsMapLayer):
  265. result_lyr = results[id]
  266. else:
  267. result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
  268. self.assertTrue(result_lyr.isValid())
  269. continue
  270. expected_lyrs = self.load_layers(id, expected_result)
  271. if 'in_place_result' in expected_result:
  272. result_lyr = QgsProcessingUtils.mapLayerFromString(self.in_place_layers[id], context)
  273. self.assertTrue(result_lyr.isValid(), self.in_place_layers[id])
  274. else:
  275. try:
  276. results[id]
  277. except KeyError as e:
  278. raise KeyError('Expected result {} does not exist in {}'.format(str(e), list(results.keys())))
  279. if isinstance(results[id], QgsMapLayer):
  280. result_lyr = results[id]
  281. else:
  282. string = results[id]
  283. gmlrex = r'\.gml\b'
  284. if re.search(gmlrex, string, re.IGNORECASE):
  285. # ewwwww - we have to force SRS detection for GML files, otherwise they'll be loaded
  286. # with no srs
  287. string += '|option:FORCE_SRS_DETECTION=YES'
  288. result_lyr = QgsProcessingUtils.mapLayerFromString(string, context)
  289. self.assertTrue(result_lyr, results[id])
  290. compare = expected_result.get('compare', {})
  291. pk = expected_result.get('pk', None)
  292. if len(expected_lyrs) == 1:
  293. self.assertLayersEqual(expected_lyrs[0], result_lyr, compare=compare, pk=pk)
  294. else:
  295. res = False
  296. for l in expected_lyrs:
  297. if self.checkLayersEqual(l, result_lyr, compare=compare, pk=pk):
  298. res = True
  299. break
  300. self.assertTrue(res, 'Could not find matching layer in expected results')
  301. elif 'rasterhash' == expected_result['type']:
  302. print("id:{} result:{}".format(id, results[id]))
  303. self.assertTrue(os.path.exists(results[id]), 'File does not exist: {}, {}'.format(results[id], params))
  304. dataset = gdal.Open(results[id], GA_ReadOnly)
  305. dataArray = nan_to_num(dataset.ReadAsArray(0))
  306. strhash = hashlib.sha224(dataArray.data).hexdigest()
  307. if not isinstance(expected_result['hash'], str):
  308. self.assertIn(strhash, expected_result['hash'])
  309. else:
  310. self.assertEqual(strhash, expected_result['hash'])
  311. elif 'file' == expected_result['type']:
  312. result_filepath = results[id]
  313. if isinstance(expected_result.get('name'), list):
  314. # test to see if any match expected
  315. for path in expected_result['name']:
  316. expected_filepath = self.filepath_from_param({'name': path})
  317. if self.checkFilesEqual(expected_filepath, result_filepath):
  318. break
  319. else:
  320. expected_filepath = self.filepath_from_param({'name': expected_result['name'][0]})
  321. else:
  322. expected_filepath = self.filepath_from_param(expected_result)
  323. self.assertFilesEqual(expected_filepath, result_filepath)
  324. elif 'directory' == expected_result['type']:
  325. expected_dirpath = self.filepath_from_param(expected_result)
  326. result_dirpath = results[id]
  327. self.assertDirectoriesEqual(expected_dirpath, result_dirpath)
  328. elif 'regex' == expected_result['type']:
  329. with open(results[id]) as file:
  330. data = file.read()
  331. for rule in expected_result.get('rules', []):
  332. self.assertRegex(data, rule)
  333. class GenericAlgorithmsTest(unittest.TestCase):
  334. """
  335. General (non-provider specific) algorithm tests
  336. """
  337. @classmethod
  338. def setUpClass(cls):
  339. start_app()
  340. cls.cleanup_paths = []
  341. @classmethod
  342. def tearDownClass(cls):
  343. for path in cls.cleanup_paths:
  344. shutil.rmtree(path)
  345. def testAlgorithmCompliance(self):
  346. for p in QgsApplication.processingRegistry().providers():
  347. print('testing provider {}'.format(p.id()))
  348. for a in p.algorithms():
  349. print('testing algorithm {}'.format(a.id()))
  350. self.check_algorithm(a)
  351. def check_algorithm(self, alg):
  352. # check that calling helpUrl() works without error
  353. alg.helpUrl()
  354. if __name__ == '__main__':
  355. nose2.main()