<div dir="ltr"><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">I posted this to gis stack exchange. no answers as yet. Below is essentially the same.<br></p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><a href="https://gis.stackexchange.com/questions/382141/create-non-trivial-join-view-via-pyqgis-in-a-qgsprocessingalgorithm">https://gis.stackexchange.com/questions/382141/create-non-trivial-join-view-via-pyqgis-in-a-qgsprocessingalgorithm</a></p><br><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">In the context of QgsProcessingAlgorithm, what is the best way to take information from several layers and output based upon that combination of layers? or: What is wrong with my layer creation?</p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">The overall task/procedure I am trying to accomplish is to take several QGIS layers combine them with a query, then output some files based upon that view/join. I have chosen a processing algorithm above a plugin because it seemed to be better fitting than a plugin.</p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">What I have tried.</p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">Given:</p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">A non-trivial SQL select and join (three tables, one geometry, two attributes) named<span class="gmail-Apple-converted-space"> </span><strong style="margin:0px;padding:0px;border:0px none;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-weight:bold;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit">sql_query</strong>. I am mentioning that it is non-trivial to say that QgsVectorLayerJoinInfo is not something that can be used easily.</p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">A geo package with all the tables used in the query at location: /path/to/test.gpkg The tables of the above geo package placed into a QGIS project.</p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">A joined layer can be created via: The DB Manager plugin/tool's "SQLWindow". The sql_query can be entered into the Query tab, executed, then "Load as new layer"</p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">Or in the python console:</p><pre class="gmail-lang-py gmail-s-code-block gmail-hljs gmail-python" style="padding:12px;border:0px none;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:1.30769;font-size:13px;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;border-radius:5px;letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;word-spacing:0px;text-decoration:none"><code style="margin:0px;padding:0px;border:0px none;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-size:13px;vertical-align:baseline;box-sizing:inherit;background-color:transparent;white-space:inherit">spec = <span class="gmail-hljs-string" style="margin:0px;padding:0px;border:0px none;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-size:13px;vertical-align:baseline;box-sizing:inherit">f'/path/to/test.gpkg|layerid=0|subset=<span class="gmail-hljs-subst" style="margin:0px;padding:0px;border:0px none;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-size:13px;vertical-align:baseline;box-sizing:inherit">{sql_query}</span>'</span>
layer = QgsVectorLayer(spec, <span class="gmail-hljs-string" style="margin:0px;padding:0px;border:0px none;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-size:13px;vertical-align:baseline;box-sizing:inherit">'joined_layer'</span>, providerLib=<span class="gmail-hljs-string" style="margin:0px;padding:0px;border:0px none;font-family:inherit;font-style:inherit;font-variant-caps:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-size:13px;vertical-align:baseline;box-sizing:inherit">'ogr'</span>)
QgsProject.instance().addMapLayer(layer)
</code></pre><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">Or via ogrinfo:<span class="gmail-Apple-converted-space"> </span><code style="margin:0px;padding:2px 4px;border:0px none;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-style:inherit;font-variant-caps:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-size:13px;vertical-align:baseline;box-sizing:inherit;white-space:pre-wrap;border-radius:3px">ogrinfo -q test.gpkg -sql <sql_query></code></p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">In the context of QgsProcessingAlgorithm using same code as what was used in the python console I get:</p><p style="padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><code style="margin:0px;padding:2px 4px;border:0px none;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-style:inherit;font-variant-caps:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-size:13px;vertical-align:baseline;box-sizing:inherit;white-space:pre-wrap;border-radius:3px">ERROR 1: failed to prepare SQL: <echo of sql_query></code></p><p style="margin:0px;padding:0px;border:0px none;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:inherit;line-height:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">In the meantime, I used a workaround. I am guessing I am missing some minor thing. <br></p></div>