1

I'm writing a PyQGIS Processing plugin that takes a series of inputs and uses them to run other PyQGIS processing plugins that I wrote. The problem I'm having is that the layers my plugin returns don't show up in the layers panel, similar to this guy's problem. How do I resolve this issue?

Here's my processAlgorithm method:

def processAlgorithm(self, parameters, context, feedback):
    #unpack parameters arguement
pin_dropper_alg_params = {
    #like 20 input params
    QScoutPinDropperAlgorithm.DROPPED_PINS_OUTPUT: "memory:"  # I promise I read this somewhere
}

# this processing algorithm produces a vector layer of pin geometry type
pin_drop_out = processing.run("QScout:droppins", pin_dropper_alg_params,
                              context=context, feedback=feedback, is_child_algorithm=True)

pin_drop_out = pin_drop_out[QScoutPinDropperAlgorithm.DROPPED_PINS_OUTPUT]

vals_raster = self.parameterAsFile(parameters, QScoutValueGrabberAlgorithm.RASTER_INPUT, context)

# VALUE GRABBER PARAMS
grab_alg_params = {
    QScoutValueGrabberAlgorithm.RASTER_INPUT: vals_raster,
    QScoutValueGrabberAlgorithm.POINTS_INPUT: pin_drop_out,
    QScoutValueGrabberAlgorithm.POINTS_WITH_VALUES_OUTPUT: parameters[self.DROP_AND_GRAB_POINTS_OUT]
}

# this processing algorithm produces a vector layer of pin geometry type
points_layer_id = processing.runAndLoadResults("QScout:valuegrab", grab_alg_params,
                                            context=context, feedback=feedback)
points_layer_id = points_layer_id[QScoutValueGrabberAlgorithm.POINTS_WITH_VALUES_OUTPUT]
points_layer = QgsProject.instance().mapLayer(points_layer_id)

# GRID AGGREGATOR PARAMS
grid_w = self.parameterAsDouble(parameters, QScoutGridAggregatorAlgorithm.GRID_CELL_W_INPUT, context)
grid_h = self.parameterAsDouble(parameters, QScoutGridAggregatorAlgorithm.GRID_CELL_H_INPUT, context)
ag_idx = self.parameterAsEnum(parameters, QScoutGridAggregatorAlgorithm.AGGREGATION_FUNCTION_INPUT, context)

# this is a bit wonky
fields_to_use_list = map(lambda f: f.strip(), fields_to_use.split(","))
ag_fields_list = points_layer.fields()
regexes = "|".join(map(lambda r: "(%s)" % r, [ROW_REGEX, COL_REGEX, VINE_REGEX, PANEL_REGEX]))
ag_fields_list = filter(lambda f:
                   (not fields_to_use or f.name() in fields_to_use_list)
                   and not re.match(regexes, f.name())
                   and (f.type() == QVariant.Int or f.type() == QVariant.Double),
                   ag_fields_list)
ag_fields = ",".join(map(lambda f: f.name(), ag_fields_list))
# for field in ag_fields_list:
#     ag_fields.append(field)

grid_ag_alg_params = {
    # some input params
    QScoutGridAggregatorAlgorithm.AGGREGATE_GRID_OUTPUT: parameters[self.DROP_AND_GRAB_GRID_OUT]
}

# this plugin produces a vector layer of polygon geometry type
grid_alg_out = processing.runAndLoadResults("QScout:gridaggregator", grid_ag_alg_params,
                                            context=context, feedback=feedback)

ag_layer_id = grid_alg_out[QScoutGridAggregatorAlgorithm.AGGREGATE_GRID_OUTPUT]

return {self.DROP_AND_GRAB_POINTS_OUT: points_layer_id, self.DROP_AND_GRAB_GRID_OUT: ag_layer_id}

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • 2
    You can also take a look to this thread: https://gis.stackexchange.com/questions/345145/processing-script-returns-no-layer and https://gis.stackexchange.com/questions/278406/getting-layertree-reference-in-processing-script-in-qgis-3 – eurojam Dec 18 '20 at 06:11
  • That was very helpful - thanks! – Joshua Evans Dec 19 '20 at 19:14

0 Answers0