Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing][grass] Fix grass vector algs don't work with memory layers
- Loading branch information
1 parent
b0eb85d
commit a675311
Showing
5 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
python/plugins/processing/tests/Grass7AlgorithmsVectorTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
Grass7AlgorithmsVectorTest.py | ||
----------------------------- | ||
Date : April 2018 | ||
Copyright : (C) 2018 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__ = 'March 2018' | ||
__copyright__ = '(C) 2018, Nyall Dawson' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive | ||
|
||
__revision__ = ':%H$' | ||
|
||
import AlgorithmsTestBase | ||
|
||
import nose2 | ||
import shutil | ||
import os | ||
|
||
from qgis.core import (QgsVectorLayer, | ||
QgsApplication, | ||
QgsFeature, | ||
QgsGeometry, | ||
QgsPointXY, | ||
QgsProcessingContext, | ||
QgsProject, | ||
QgsProcessingFeedback, | ||
QgsProcessingFeatureSourceDefinition) | ||
from qgis.testing import ( | ||
start_app, | ||
unittest | ||
) | ||
from processing.algs.grass7.Grass7Utils import Grass7Utils | ||
|
||
|
||
class TestGrass7AlgorithmsVectorTest(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
start_app() | ||
from processing.core.Processing import Processing | ||
Processing.initialize() | ||
cls.cleanup_paths = [] | ||
|
||
assert Grass7Utils.installedVersion() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
from processing.core.Processing import Processing | ||
Processing.deinitialize() | ||
for path in cls.cleanup_paths: | ||
shutil.rmtree(path) | ||
|
||
def test_definition_file(self): | ||
return 'grass7_algorithms_vector_tests.yaml' | ||
|
||
def testMemoryLayerInput(self): | ||
# create a memory layer and add to project and context | ||
layer = QgsVectorLayer("Point?crs=epsg:3857&field=fldtxt:string&field=fldint:integer", | ||
"testmem", "memory") | ||
self.assertTrue(layer.isValid()) | ||
pr = layer.dataProvider() | ||
f = QgsFeature() | ||
f.setAttributes(["test", 123]) | ||
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(100, 200))) | ||
f2 = QgsFeature() | ||
f2.setAttributes(["test2", 457]) | ||
f2.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(110, 200))) | ||
self.assertTrue(pr.addFeatures([f, f2])) | ||
self.assertEqual(layer.featureCount(), 2) | ||
QgsProject.instance().addMapLayer(layer) | ||
context = QgsProcessingContext() | ||
context.setProject(QgsProject.instance()) | ||
|
||
alg = QgsApplication.processingRegistry().createAlgorithmById('grass7:v.buffer') | ||
self.assertIsNotNone(alg) | ||
temp_file = '/tmp/grass_output.shp' | ||
parameters ={'input':'testmem', | ||
'type':[0,1,4], | ||
'distance':1, | ||
'angle':0, | ||
'scale':1, | ||
'tolerance':0.01, | ||
'-s':False, | ||
'-c':False, | ||
'-t':False, | ||
'output':temp_file, | ||
'GRASS_SNAP_TOLERANCE_PARAMETER':-1,'GRASS_MIN_AREA_PARAMETER':0.0001,'GRASS_OUTPUT_TYPE_PARAMETER':0} | ||
feedback = QgsProcessingFeedback() | ||
|
||
results, ok = alg.run(parameters, context, feedback) | ||
self.assertTrue(ok) | ||
self.assertTrue(os.path.exists(temp_file)) | ||
|
||
# make sure that layer has correct features | ||
res = QgsVectorLayer(temp_file, 'res') | ||
self.assertTrue(res.isValid()) | ||
self.assertEqual(res.featureCount(), 2) | ||
|
||
QgsProject.instance().removeMapLayer(layer) | ||
|
||
def testFeatureSourceInput(self): | ||
# create a memory layer and add to project and context | ||
layer = QgsVectorLayer("Point?crs=epsg:3857&field=fldtxt:string&field=fldint:integer", | ||
"testmem", "memory") | ||
self.assertTrue(layer.isValid()) | ||
pr = layer.dataProvider() | ||
f = QgsFeature() | ||
f.setAttributes(["test", 123]) | ||
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(100, 200))) | ||
f2 = QgsFeature() | ||
f2.setAttributes(["test2", 457]) | ||
f2.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(110, 200))) | ||
self.assertTrue(pr.addFeatures([f, f2])) | ||
self.assertEqual(layer.featureCount(), 2) | ||
|
||
# select first feature | ||
layer.selectByIds([next(layer.getFeatures()).id()]) | ||
self.assertEqual(len(layer.selectedFeatureIds()), 1) | ||
|
||
QgsProject.instance().addMapLayer(layer) | ||
context = QgsProcessingContext() | ||
context.setProject(QgsProject.instance()) | ||
|
||
alg = QgsApplication.processingRegistry().createAlgorithmById('grass7:v.buffer') | ||
self.assertIsNotNone(alg) | ||
temp_file = '/tmp/grass_output.shp' | ||
parameters = {'input': QgsProcessingFeatureSourceDefinition('testmem', True), | ||
'type': [0, 1, 4], | ||
'distance': 1, | ||
'angle': 0, | ||
'scale': 1, | ||
'tolerance': 0.01, | ||
'-s': False, | ||
'-c': False, | ||
'-t': False, | ||
'output': temp_file, | ||
'GRASS_SNAP_TOLERANCE_PARAMETER': -1, 'GRASS_MIN_AREA_PARAMETER': 0.0001, | ||
'GRASS_OUTPUT_TYPE_PARAMETER': 0} | ||
feedback = QgsProcessingFeedback() | ||
|
||
results, ok = alg.run(parameters, context, feedback) | ||
self.assertTrue(ok) | ||
self.assertTrue(os.path.exists(temp_file)) | ||
|
||
# make sure that layer has correct features | ||
res = QgsVectorLayer(temp_file, 'res') | ||
self.assertTrue(res.isValid()) | ||
self.assertEqual(res.featureCount(), 1) | ||
|
||
QgsProject.instance().removeMapLayer(layer) | ||
|
||
if __name__ == '__main__': | ||
nose2.main() |
20 changes: 20 additions & 0 deletions
20
python/plugins/processing/tests/testdata/grass7_algorithms_vector_tests.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# See ../README.md for a description of the file format | ||
|
||
tests: | ||
|
||
# v.* modules | ||
# - algorithm: grass7:r.plane | ||
# name: GRASS7 r.plane | ||
# params: | ||
# GRASS_REGION_PARAMETER: 344500.0,358400.0,6682800.0,6693700.0 | ||
# azimuth: 125 | ||
# dip: 45 | ||
# easting: 351610 | ||
# elevation: 50 | ||
# northing: 6688312 | ||
# type: 1 | ||
# results: | ||
# output: | ||
# hash: a9326678c39b6f925e7f22f6e79a48217100071cc8af85d675f28462 | ||
# type: rasterhash | ||
|