mirror of
				https://github.com/qgis/QGIS.git
				synced 2025-10-31 00:06:02 -04:00 
			
		
		
		
	All pointer based methods have been removed. Now we have only: void setGeometry( const QgsGeometry& geom ) and QgsGeometry geometry() const Benefits include avoiding a whole lot of tricky pointer lifetime issues, potential memory leaks, and finally closing #777, which has survived for over 9 years!... Impacts on PyQGIS code: - no more need for the messy g = QgsGeometry( feature.geometry() ) workaround, just use g = feature.geometry() instead - IMPORTANT: you can no longer test whether a feature has geometry using `if f.geometry():`, since QgsFeature::geometry() will *always* return an object. Instead, use `if not f.geometry().isEmpty():`, or preferably the new method `if not f.hasGeometry():` Fix #777
		
			
				
	
	
		
			90 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| # -*- coding: utf-8 -*-
 | |
| 
 | |
| """
 | |
| ***************************************************************************
 | |
|     ToolsTest
 | |
|     ---------------------
 | |
|     Date                 : July 2017
 | |
|     Copyright            : (C) 2017 by Nyall Dawson
 | |
|     Email                : nyall dot dawson at gmail dot com
 | |
| ***************************************************************************
 | |
| *                                                                         *
 | |
| *   This program is free software; you can redistribute it and/or modify  *
 | |
| *   it under the terms of the GNU General Public License as published by  *
 | |
| *   the Free Software Foundation; either version 2 of the License, or     *
 | |
| *   (at your option) any later version.                                   *
 | |
| *                                                                         *
 | |
| ***************************************************************************
 | |
| """
 | |
| 
 | |
| __author__ = 'Nyall Dawson'
 | |
| __date__ = 'July 2016'
 | |
| __copyright__ = '(C) 2016, Nyall Dawson'
 | |
| 
 | |
| # This will get replaced with a git SHA1 when you do a git archive
 | |
| 
 | |
| __revision__ = '$Format:%H$'
 | |
| 
 | |
| from qgis.testing import start_app, unittest
 | |
| from processing.tests.TestData import points2
 | |
| from processing.tools import vector
 | |
| from qgis.core import (QgsVectorLayer, QgsFeatureRequest)
 | |
| from processing.core.ProcessingConfig import ProcessingConfig
 | |
| 
 | |
| start_app()
 | |
| 
 | |
| 
 | |
| class VectorTest(unittest.TestCase):
 | |
| 
 | |
|     def testFeatures(self):
 | |
|         ProcessingConfig.initialize()
 | |
| 
 | |
|         test_data = points2()
 | |
|         test_layer = QgsVectorLayer(test_data, 'test', 'ogr')
 | |
| 
 | |
|         # test with all features
 | |
|         features = vector.features(test_layer)
 | |
|         self.assertEqual(len(features), 8)
 | |
|         self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7]))
 | |
| 
 | |
|         # test with selected features
 | |
|         previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
 | |
|         ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
 | |
|         test_layer.selectByIds([2, 4, 6])
 | |
|         features = vector.features(test_layer)
 | |
|         self.assertEqual(len(features), 3)
 | |
|         self.assertEqual(set([f.id() for f in features]), set([2, 4, 6]))
 | |
| 
 | |
|         # selection, but not using selected features
 | |
|         ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, False)
 | |
|         test_layer.selectByIds([2, 4, 6])
 | |
|         features = vector.features(test_layer)
 | |
|         self.assertEqual(len(features), 8)
 | |
|         self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7]))
 | |
| 
 | |
|         # using selected features, but no selection
 | |
|         ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
 | |
|         test_layer.removeSelection()
 | |
|         features = vector.features(test_layer)
 | |
|         self.assertEqual(len(features), 8)
 | |
|         self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7]))
 | |
| 
 | |
|         # test that feature request is honored
 | |
|         ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, False)
 | |
|         features = vector.features(test_layer, QgsFeatureRequest().setFilterFids([1, 3, 5]))
 | |
|         self.assertEqual(set([f.id() for f in features]), set([1, 3, 5]))
 | |
| 
 | |
|         # test that feature request is honored when using selections
 | |
|         ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
 | |
|         test_layer.selectByIds([2, 4, 6])
 | |
|         features = vector.features(test_layer, QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))
 | |
|         self.assertTrue(all([not f.hasGeometry() for f in features]))
 | |
|         features = vector.features(test_layer, QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))
 | |
|         self.assertEqual(set([f.id() for f in features]), set([2, 4, 6]))
 | |
| 
 | |
|         ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value)
 | |
| 
 | |
| 
 | |
| if __name__ == '__main__':
 | |
|     unittest.main()
 |