3

I want to add some fields in a temporary layer in order to use it in another process.

What I tried to do is below :

    shp_result = processing.run(
        'native:pixelstopoints',
        {
            'INPUT_RASTER': raster_redim['OUTPUT'],
            'RASTER_BAND': 1,
            'FIELD_NAME': 'VALUE',
            'OUTPUT': 'TEMPORARY_OUTPUT'
        },
        is_child_algorithm=True,
        context=context,
        feedback=feedback)

    # On ajoute 2 autres champs pour contenir les coordonnées X, Y
    # du point de cours d'eau associé au point d'intérêt
    self.maCouche = QgsVectorLayer(shp_result['OUTPUT'], self.nom[0], 'ogr')

    prov = self.maCouche.dataProvider()
    prov.addAttributes([QgsField("X", QVariant.Double)])
    prov.addAttributes([QgsField("Y", QVariant.Double)])
    self.maCouche.updateFields()

    dist_fNul = processing.run(
        'grass7:v.distance',
        {
            'from': self.maCouche.source(),
            'from_type':[0,1,3],
            'to': parameters[self.MASK],
            'to_type':[0,1,3],
            'dmax':-1,
            'dmin':-1,
            'upload':[1,2,3],
            'column':['VALUE', 'X', 'Y'],
            'to_column':None,
            'from_output': parameters[self.OUTPUT],
            'output':'TEMPORARY_OUTPUT',
            'GRASS_REGION_PARAMETER':None,
            'GRASS_SNAP_TOLERANCE_PARAMETER':-1,
            'GRASS_MIN_AREA_PARAMETER':0.0001,
            'GRASS_OUTPUT_TYPE_PARAMETER':0,
            'GRASS_VECTOR_DSCO':'',
            'GRASS_VECTOR_LCO':'',
            'GRASS_VECTOR_EXPORT_NOCAT':False
        },
        is_child_algorithm=True,
        context=context,
        feedback=feedback
        )

But the final result is not what I expect : only the 'VALUE' field is created instead of giving me tree fields 'VALUE', 'X', 'Y' (in dist_fNul).

6
  • Are the new fields generated before you run grass7:v.distance?
    – Joseph
    Commented Jul 4, 2019 at 10:26
  • @Joseph yes, fields are well generated
    – HydroGeo
    Commented Jul 4, 2019 at 10:39
  • Finally, I decided to save the resulting file of "shp_result" and use it clearly in the second process and, once this is finished, I delete the saved file.
    – HydroGeo
    Commented Jul 5, 2019 at 6:49
  • Glad you found a possible solution, you should post it as an answer. Saying that, I wonder why your original method didn't work...
    – Joseph
    Commented Jul 5, 2019 at 9:15
  • @Joseph yes, I have not been understood why the results wasn't what I needed. How to post it as an answer ?
    – HydroGeo
    Commented Jul 5, 2019 at 10:31

1 Answer 1

2

I want to add some fields in a temporary layer in order to use it in another process.

What I tried to do is below :

 shp_result = processing.run(
    'native:pixelstopoints',
    {
        'INPUT_RASTER': raster_redim['OUTPUT'],
        'RASTER_BAND': 1,
        'FIELD_NAME': 'VALUE',
        'OUTPUT': 'TEMPORARY_OUTPUT'
    },
    is_child_algorithm=True,
    context=context,
    feedback=feedback)

# On ajoute 2 autres champs pour contenir les coordonnées X, Y
# du point de cours d'eau associé au point d'intérêt
self.maCouche = QgsVectorLayer(shp_result['OUTPUT'], self.nom[0], 'ogr')

prov = self.maCouche.dataProvider()
prov.addAttributes([QgsField("X", QVariant.Double)])
prov.addAttributes([QgsField("Y", QVariant.Double)])
self.maCouche.updateFields()

dist_fNul = processing.run(
    'grass7:v.distance',
    {
        'from': self.maCouche.source(),
        'from_type':[0,1,3],
        'to': parameters[self.MASK],
        'to_type':[0,1,3],
        'dmax':-1,
        'dmin':-1,
        'upload':[1,2,3],
        'column':['VALUE', 'X', 'Y'],
        'to_column':None,
        'from_output': parameters[self.OUTPUT],
        'output':'TEMPORARY_OUTPUT',
        'GRASS_REGION_PARAMETER':None,
        'GRASS_SNAP_TOLERANCE_PARAMETER':-1,
        'GRASS_MIN_AREA_PARAMETER':0.0001,
        'GRASS_OUTPUT_TYPE_PARAMETER':0,
        'GRASS_VECTOR_DSCO':'',
        'GRASS_VECTOR_LCO':'',
        'GRASS_VECTOR_EXPORT_NOCAT':False
    },
    is_child_algorithm=True,
    context=context,
    feedback=feedback
    )

But the final result is not what I expect : only the 'VALUE' field is created instead of giving me tree fields 'VALUE', 'X', 'Y' (in dist_fNul).

* Alternative method *

Finally, I decided to save the resulting file of "shp_result" and use it clearly in the second process and, once this is finished, I delete the saved file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.