3

I created a model that takes my point shapelayer, creates polygons from these points using minimum bounding geometry then adds a field named 'Zonekey' to attribute table;

from qgis.core import QgsProcessing from qgis.core import QgsProcessingAlgorithm from qgis.core import QgsProcessingMultiStepFeedback from qgis.core import QgsProcessingParameterVectorLayer from qgis.core import QgsProcessingParameterFeatureSink import processing class Mz_tool(QgsProcessingAlgorithm): def initAlgorithm(self, config=None): self.addParameter(QgsProcessingParameterVectorLayer('addlayer', 'myLayer', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None)) self.addParameter(QgsProcessingParameterFeatureSink('Result', 'Result', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None)) def processAlgorithm(self, parameters, context, model_feedback): # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the # overall progress through the model feedback = QgsProcessingMultiStepFeedback(6, model_feedback) results = {} outputs = {} # Create categorized renderer from styles alg_params = { 'CASE_SENSITIVE': False, 'FIELD': 'Product', 'INPUT': parameters['addlayer'], 'STYLE': '', 'TOLERANT': False } outputs['CreateCategorizedRendererFromStyles'] = processing.run('native:categorizeusingstyle', alg_params, context=context, feedback=feedback, is_child_algorithm=True) feedback.setCurrentStep(1) if feedback.isCanceled(): return {} # Minimum bounding geometry alg_params = { 'FIELD': 'Product', 'INPUT': parameters['addlayer'], 'TYPE': 3, 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT } outputs['MinimumBoundingGeometry'] = processing.run('qgis:minimumboundinggeometry', alg_params, context=context, feedback=feedback, is_child_algorithm=True) feedback.setCurrentStep(2) if feedback.isCanceled(): return {} # Zonekey alg_params = { 'FIELD_LENGTH': 100, 'FIELD_NAME': 'Zonekey', 'FIELD_PRECISION': 0, 'FIELD_TYPE': 2, 'INPUT': outputs['MinimumBoundingGeometry']['OUTPUT'], 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT } outputs['Zonekey'] = processing.run('qgis:addfieldtoattributestable', alg_params, context=context, feedback=feedback, is_child_algorithm=True) feedback.setCurrentStep(3) if feedback.isCanceled(): return {} # Drop field(s) alg_params = { 'COLUMN': 'area', 'INPUT': outputs['Zonekey']['OUTPUT'], 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT } outputs['DropFields'] = processing.run('qgis:deletecolumn', alg_params, context=context, feedback=feedback, is_child_algorithm=True) feedback.setCurrentStep(4) if feedback.isCanceled(): return {} # Drop field(s) alg_params = { 'COLUMN': 'perimeter', 'INPUT': outputs['DropFields']['OUTPUT'], 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT } outputs['DropFields'] = processing.run('qgis:deletecolumn', alg_params, context=context, feedback=feedback, is_child_algorithm=True) feedback.setCurrentStep(5) if feedback.isCanceled(): return {} # Drop field(s) alg_params = { 'COLUMN': 'id', 'INPUT': outputs['DropFields']['OUTPUT'], 'OUTPUT': parameters['Result'] } outputs['DropFields'] = processing.run('qgis:deletecolumn', alg_params, context=context, feedback=feedback, is_child_algorithm=True) results['Result'] = outputs['DropFields']['OUTPUT'] return results def name(self): return 'mz_tool' def displayName(self): return 'mz_tool' def group(self): return '' def groupId(self): return '' def createInstance(self): return Mz_tool() 

I have another Python code that I can use to fill my Zonekey with random strings requested from random.org's API.

import requests lyr = iface.activeLayer() count = lyr.featureCount() reqData = { "jsonrpc":"2.0", "method":"generateStrings", "params":{"apiKey":"myapikeygoeshere", "n":(llen), #number of strings to be produced equals to feature count using llen "length":20, "characters":"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "replacement":True }, "id":539 } response = requests.post('https://api.random.org/json-rpc/2/invoke', json = reqData) response.raise_for_status() json = response.json() data = json['result']['random']['data'] lyr.startEditing() i = 0 for f in lyr.getFeatures(): f['Zonekey'] = data[i] lyr.updateFeature(f) i+=1 lyr.commitChanges() 

Currently I am running the first script then proceed to the second one, but I am looking for a way to combine these two processes into one Python script that I can use from Processing toolbox.

How would I get the 'Zonekey' field created using the first script filled with the strings from the second script? Since I run them one by one I tried to add the entire second script to the first one, but I it didn't work. I also tried not exporting my model and using Advanced Python Field Calculator in the model to insert my code, but couldn't get it to work either.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.