Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
On 16 February 2018 at 17:29, kimaidou wrote: > Thanks Nyall, > > Our posts crossed ! > > Ok for the shortcut and the getMapLayer method. > Here you go: https://github.com/qgis/QGIS/pull/6359 ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
Thanks for your explanation ! I also think it makes sense to share source layers in some cache in the future. 2018-02-16 7:36 GMT+01:00 Nyall Dawson : > On 16 February 2018 at 17:29, kimaidou wrote: > > Thanks Nyall, > > > > Our posts crossed ! > > > > Ok for the shortcut and the getMapLayer method. > > > > One question : in my alg, if I use QgsProcessingParameterFeatureSource > > instead of QgsProcessingParameterVectorLayer for my input, can I still > use > > mapLayerFromString and the getFeatures, or is there a > > featureSourceFromString which allows me to loop through the features ? > > There's no method like that. At the moment all vector outputs from > processing algorithms are full vector layers (either disk-based or > memory layers). That's because currently only vector layers and vector > data providers are QgsFeatureSources. It's possible (likely?) that in > future we'll see this change at may need to add that method. [1] > > Nyall > > [1] E.g. I'd like to see QgsFeatureStore become a QgsFeatureSource, > and act as a simple dump for features without the complexity of using > a memory layer. I think there's a lot of value in QgsVectorLayerCache > also becoming a QgsFeatureSource... I think some processing algorithms > could greatly benefit from using a QgsVectorLayerCache, when they are > iterating multiple times of a source layer > > > > > > > > > Michaël > > > > 2018-02-16 7:22 GMT+01:00 Nyall Dawson : > >> > >> On 16 February 2018 at 04:36, kimaidou wrote: > >> > Hi all, > >> > > >> > I have a simple processing python alg which > >> > * generates a buffer with qgis:buffer > >> > * use qgis:fieldcalculator to add an buf_area field -> the output > vector > >> > of > >> > my alg is this buffer+new field. > >> > > >> > I would like to parse the features of the output vector in order to > >> > gather > >> > some information and put it in a new string output. I have not been > able > >> > to > >> > get the layer object to perform a getFeatures on it. > >> > > >> > See the code > >> > http://paste.debian.net/1010463/ > >> > > >> > In line 32, my blayer is None > >> > I think I am not using the good way to get the output vector layer > >> > features? > >> > I tried to use the same way as for reading an input layer, but > oviously > >> > it > >> > is different. > >> > >> blayer = QgsProcessingUtils.mapLayerFromString( area_result[ 'OUTPUT' > >> ]), context ) > >> > >> I'm thinking of adding a shortcut for this to the context, e.g.: > >> > >> blayer = context.getMapLayer( area_result['OUTPUT'] ) > >> > >> What do you think? > >> > >> Nyall > > > > > ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
On 16 February 2018 at 17:29, kimaidou wrote: > Thanks Nyall, > > Our posts crossed ! > > Ok for the shortcut and the getMapLayer method. > > One question : in my alg, if I use QgsProcessingParameterFeatureSource > instead of QgsProcessingParameterVectorLayer for my input, can I still use > mapLayerFromString and the getFeatures, or is there a > featureSourceFromString which allows me to loop through the features ? There's no method like that. At the moment all vector outputs from processing algorithms are full vector layers (either disk-based or memory layers). That's because currently only vector layers and vector data providers are QgsFeatureSources. It's possible (likely?) that in future we'll see this change at may need to add that method. [1] Nyall [1] E.g. I'd like to see QgsFeatureStore become a QgsFeatureSource, and act as a simple dump for features without the complexity of using a memory layer. I think there's a lot of value in QgsVectorLayerCache also becoming a QgsFeatureSource... I think some processing algorithms could greatly benefit from using a QgsVectorLayerCache, when they are iterating multiple times of a source layer > Michaël > > 2018-02-16 7:22 GMT+01:00 Nyall Dawson : >> >> On 16 February 2018 at 04:36, kimaidou wrote: >> > Hi all, >> > >> > I have a simple processing python alg which >> > * generates a buffer with qgis:buffer >> > * use qgis:fieldcalculator to add an buf_area field -> the output vector >> > of >> > my alg is this buffer+new field. >> > >> > I would like to parse the features of the output vector in order to >> > gather >> > some information and put it in a new string output. I have not been able >> > to >> > get the layer object to perform a getFeatures on it. >> > >> > See the code >> > http://paste.debian.net/1010463/ >> > >> > In line 32, my blayer is None >> > I think I am not using the good way to get the output vector layer >> > features? >> > I tried to use the same way as for reading an input layer, but oviously >> > it >> > is different. >> >> blayer = QgsProcessingUtils.mapLayerFromString( area_result[ 'OUTPUT' >> ]), context ) >> >> I'm thinking of adding a shortcut for this to the context, e.g.: >> >> blayer = context.getMapLayer( area_result['OUTPUT'] ) >> >> What do you think? >> >> Nyall > > ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
Thanks Nyall, Our posts crossed ! Ok for the shortcut and the getMapLayer method. One question : in my alg, if I use QgsProcessingParameterFeatureSource instead of QgsProcessingParameterVectorLayer for my input, can I still use mapLayerFromString and the getFeatures, or is there a featureSourceFromString which allows me to loop through the features ? Michaël 2018-02-16 7:22 GMT+01:00 Nyall Dawson : > On 16 February 2018 at 04:36, kimaidou wrote: > > Hi all, > > > > I have a simple processing python alg which > > * generates a buffer with qgis:buffer > > * use qgis:fieldcalculator to add an buf_area field -> the output vector > of > > my alg is this buffer+new field. > > > > I would like to parse the features of the output vector in order to > gather > > some information and put it in a new string output. I have not been able > to > > get the layer object to perform a getFeatures on it. > > > > See the code > > http://paste.debian.net/1010463/ > > > > In line 32, my blayer is None > > I think I am not using the good way to get the output vector layer > features? > > I tried to use the same way as for reading an input layer, but oviously > it > > is different. > > blayer = QgsProcessingUtils.mapLayerFromString( area_result[ 'OUTPUT' > ]), context ) > > I'm thinking of adding a shortcut for this to the context, e.g.: > > blayer = context.getMapLayer( area_result['OUTPUT'] ) > > What do you think? > > Nyall > ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
Found id ! I have to use mapLayerFromString : blayer = QgsProcessingUtils.mapLayerFromString(area_result['OUTPUT'], context) \o/ 2018-02-16 7:11 GMT+01:00 kimaidou : > Hi Salvatore, > > I already tried to use > > blayer = area_result['OUTPUT'] > for feat in blayer.getFeatures(): > > > with no success . If I do so I get > > AttributeError: 'str' object has no attribute 'getFeatures' > > There must be a way to transforme this string representing the layer as a > processing layer object > I use this code inside the processAlgorithm method of my > QgsProcessingAlgorithm class. > > > Michaël > > 2018-02-15 22:39 GMT+01:00 Salvatore Larosa : > >> Hi Michaël, >> >> On Thu, Feb 15, 2018 at 6:36 PM, kimaidou wrote: >> >>> Hi all, >>> >>> I have a simple processing python alg which >>> * generates a buffer with qgis:buffer >>> * use qgis:fieldcalculator to add an buf_area field -> the output vector >>> of my alg is this buffer+new field. >>> >>> I would like to parse the features of the output vector in order to >>> gather some information and put it in a new string output. I have not been >>> able to get the layer object to perform a getFeatures on it. >>> >>> See the code >>> http://paste.debian.net/1010463/ >>> >>> In line 32, my blayer is None >>> I think I am not using the good way to get the output vector layer >>> features? I tried to use the same way as for reading an input layer, but >>> oviously it is different. >>> >> >> What does happen if you put area_result['OUTPUT'] directly instead of >> self.parameterAsSource? >> parameterAsSource() accept a parameter as first argument and area_result >> should be already un QgsVectorLayer. >> >> Where you are trying to execute it from? >> >> >> -- >> Salvatore Larosa >> linkedIn: http://linkedin.com/in/larosasalvatore >> twitter: @lrssvt >> skype: s.larosa >> IRC: lrssvt on freenode >> > > ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
On 16 February 2018 at 04:36, kimaidou wrote: > Hi all, > > I have a simple processing python alg which > * generates a buffer with qgis:buffer > * use qgis:fieldcalculator to add an buf_area field -> the output vector of > my alg is this buffer+new field. > > I would like to parse the features of the output vector in order to gather > some information and put it in a new string output. I have not been able to > get the layer object to perform a getFeatures on it. > > See the code > http://paste.debian.net/1010463/ > > In line 32, my blayer is None > I think I am not using the good way to get the output vector layer features? > I tried to use the same way as for reading an input layer, but oviously it > is different. blayer = QgsProcessingUtils.mapLayerFromString( area_result[ 'OUTPUT' ]), context ) I'm thinking of adding a shortcut for this to the context, e.g.: blayer = context.getMapLayer( area_result['OUTPUT'] ) What do you think? Nyall ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
Hi Salvatore, I already tried to use blayer = area_result['OUTPUT'] for feat in blayer.getFeatures(): with no success . If I do so I get AttributeError: 'str' object has no attribute 'getFeatures' There must be a way to transforme this string representing the layer as a processing layer object I use this code inside the processAlgorithm method of my QgsProcessingAlgorithm class. Michaël 2018-02-15 22:39 GMT+01:00 Salvatore Larosa : > Hi Michaël, > > On Thu, Feb 15, 2018 at 6:36 PM, kimaidou wrote: > >> Hi all, >> >> I have a simple processing python alg which >> * generates a buffer with qgis:buffer >> * use qgis:fieldcalculator to add an buf_area field -> the output vector >> of my alg is this buffer+new field. >> >> I would like to parse the features of the output vector in order to >> gather some information and put it in a new string output. I have not been >> able to get the layer object to perform a getFeatures on it. >> >> See the code >> http://paste.debian.net/1010463/ >> >> In line 32, my blayer is None >> I think I am not using the good way to get the output vector layer >> features? I tried to use the same way as for reading an input layer, but >> oviously it is different. >> > > What does happen if you put area_result['OUTPUT'] directly instead of > self.parameterAsSource? > parameterAsSource() accept a parameter as first argument and area_result > should be already un QgsVectorLayer. > > Where you are trying to execute it from? > > > -- > Salvatore Larosa > linkedIn: http://linkedin.com/in/larosasalvatore > twitter: @lrssvt > skype: s.larosa > IRC: lrssvt on freenode > ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
Hi Salvatore, I already tried to use blayer = area_result['OUTPUT'] for feat in blayer.getFeatures(): with no success . If I do so I get AttributeError: 'str' object has no attribute 'getFeatures' There must be a way to transforme this string representing the layer as a processing layer object I use this code inside the processAlgorithm method of my QgsProcessingAlgorithm class. Michaël 2018-02-15 22:39 GMT+01:00 Salvatore Larosa : > Hi Michaël, > > On Thu, Feb 15, 2018 at 6:36 PM, kimaidou wrote: > >> Hi all, >> >> I have a simple processing python alg which >> * generates a buffer with qgis:buffer >> * use qgis:fieldcalculator to add an buf_area field -> the output vector >> of my alg is this buffer+new field. >> >> I would like to parse the features of the output vector in order to >> gather some information and put it in a new string output. I have not been >> able to get the layer object to perform a getFeatures on it. >> >> See the code >> http://paste.debian.net/1010463/ >> >> In line 32, my blayer is None >> I think I am not using the good way to get the output vector layer >> features? I tried to use the same way as for reading an input layer, but >> oviously it is different. >> > > What does happen if you put area_result['OUTPUT'] directly instead of > self.parameterAsSource? > parameterAsSource() accept a parameter as first argument and area_result > should be already un QgsVectorLayer. > > Where you are trying to execute it from? > > > -- > Salvatore Larosa > linkedIn: http://linkedin.com/in/larosasalvatore > twitter: @lrssvt > skype: s.larosa > IRC: lrssvt on freenode > ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] QGIS3 - processing python script. Read features from output layer
Hi Michaël, On Thu, Feb 15, 2018 at 6:36 PM, kimaidou wrote: > Hi all, > > I have a simple processing python alg which > * generates a buffer with qgis:buffer > * use qgis:fieldcalculator to add an buf_area field -> the output vector > of my alg is this buffer+new field. > > I would like to parse the features of the output vector in order to gather > some information and put it in a new string output. I have not been able to > get the layer object to perform a getFeatures on it. > > See the code > http://paste.debian.net/1010463/ > > In line 32, my blayer is None > I think I am not using the good way to get the output vector layer > features? I tried to use the same way as for reading an input layer, but > oviously it is different. > What does happen if you put area_result['OUTPUT'] directly instead of self.parameterAsSource? parameterAsSource() accept a parameter as first argument and area_result should be already un QgsVectorLayer. Where you are trying to execute it from? -- Salvatore Larosa linkedIn: http://linkedin.com/in/larosasalvatore twitter: @lrssvt skype: s.larosa IRC: lrssvt on freenode ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer