Re: [Qgis-user] Accessing iface from jupyter to create map from QGIS
On Thu, 7 Dec 2023 at 13:13, noshin prachi via QGIS-User < qgis-user@lists.osgeo.org> wrote: > Thank you Adam for your response. I have imported iface from from > qgis.utils import iface. I am working in qgis environment. It is my > first work with qgis and I am finding it difficult to understand. Can you > please share where I am going wrong? Is there any other way to activate a > layer from multiple layers and create a map? I have google street map , > shape file and dfsu file in these layers which are required to be added in > the map. > iface is ONLY available when you're running the QGIS desktop application. It contains methods for interacting with the parts of the desktop application, which have no meaning when run from elsewhere (such as in a notebook). In this situation you should avoid iface and use only the classes from qgis.core instead. Hope that helps! Nyall > > On Wed, 6 Dec 2023 at 18:10, Adam Nielsen via QGIS-User < > qgis-user@lists.osgeo.org> wrote: > >> > While trying to activate with iface.setactivelayer I am facing an error. >> >> The error is complaining that iface is None. Your code doesn't show >> what value you assigned to this variable - where did you set iface and >> what value did you set it to? I think you forgot to set it and that is >> why you are getting this error. >> >> Cheers, >> Adam. >> ___ >> QGIS-User mailing list >> QGIS-User@lists.osgeo.org >> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user >> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user >> > ___ > QGIS-User mailing list > QGIS-User@lists.osgeo.org > List info: https://lists.osgeo.org/mailman/listinfo/qgis-user > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user > ___ QGIS-User mailing list QGIS-User@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-user Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] Accessing iface from jupyter to create map from QGIS
Thank you Adam for your response. I have imported iface from from qgis.utils import iface. I am working in qgis environment. It is my first work with qgis and I am finding it difficult to understand. Can you please share where I am going wrong? Is there any other way to activate a layer from multiple layers and create a map? I have google street map , shape file and dfsu file in these layers which are required to be added in the map. On Wed, 6 Dec 2023 at 18:10, Adam Nielsen via QGIS-User < qgis-user@lists.osgeo.org> wrote: > > While trying to activate with iface.setactivelayer I am facing an error. > > The error is complaining that iface is None. Your code doesn't show > what value you assigned to this variable - where did you set iface and > what value did you set it to? I think you forgot to set it and that is > why you are getting this error. > > Cheers, > Adam. > ___ > QGIS-User mailing list > QGIS-User@lists.osgeo.org > List info: https://lists.osgeo.org/mailman/listinfo/qgis-user > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user > (base) C:\Users\FFWS>conda activate qgis (qgis) C:\Users\FFWS>jupyter notebook [I 2023-12-06 15:20:33.915 ServerApp] Package notebook took 0.s to import [I 2023-12-06 15:20:33.962 ServerApp] Package jupyter_lsp took 0.0473s to import [W 2023-12-06 15:20:33.962 ServerApp] A `_jupyter_server_extension_points` function was not found in jupyter_lsp. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server. [I 2023-12-06 15:20:33.993 ServerApp] Package jupyter_server_terminals took 0.0269s to import [I 2023-12-06 15:20:33.993 ServerApp] Package jupyterlab took 0.s to import [I 2023-12-06 15:20:34.087 ServerApp] Package notebook_shim took 0.s to import [W 2023-12-06 15:20:34.087 ServerApp] A `_jupyter_server_extension_points` function was not found in notebook_shim. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server. [I 2023-12-06 15:20:34.087 ServerApp] jupyter_lsp | extension was successfully linked. [I 2023-12-06 15:20:34.102 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2023-12-06 15:20:34.118 ServerApp] jupyterlab | extension was successfully linked. [I 2023-12-06 15:20:34.118 ServerApp] notebook | extension was successfully linked. [I 2023-12-06 15:20:34.477 ServerApp] notebook_shim | extension was successfully linked. [I 2023-12-06 15:20:34.509 ServerApp] notebook_shim | extension was successfully loaded. [I 2023-12-06 15:20:34.509 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2023-12-06 15:20:34.509 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2023-12-06 15:20:34.524 LabApp] JupyterLab extension loaded from C:\Users\FFWS\anaconda3\envs\qgis\lib\site-packages\jupyterlab [I 2023-12-06 15:20:34.524 LabApp] JupyterLab application directory is C:\Users\FFWS\anaconda3\envs\qgis\share\jupyter\lab [I 2023-12-06 15:20:34.524 LabApp] Extension Manager is 'pypi'. [I 2023-12-06 15:20:34.524 ServerApp] jupyterlab | extension was successfully loaded. [I 2023-12-06 15:20:34.524 ServerApp] notebook | extension was successfully loaded. [I 2023-12-06 15:20:34.524 ServerApp] Serving notebooks from local directory: C:\Users\FFWS [I 2023-12-06 15:20:34.524 ServerApp] Jupyter Server 2.10.0 is running at: [I 2023-12-06 15:20:34.524 ServerApp] http://localhost:/tree?token=99f5ac6fd1ec2cac299b0dcc61f5392edd69a7bbed37f1eb [I 2023-12-06 15:20:34.524 ServerApp] http://127.0.0.1:/tree?token=99f5ac6fd1ec2cac299b0dcc61f5392edd69a7bbed37f1eb [I 2023-12-06 15:20:34.524 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 2023-12-06 15:20:34.571 ServerApp] To access the server, open this file in a browser: file:///C:/Users/FFWS/AppData/Roaming/jupyter/runtime/jpserver-5788-open.html Or copy and paste one of these URLs: http://localhost:/tree?token=99f5ac6fd1ec2cac299b0dcc61f5392edd69a7bbed37f1eb http://127.0.0.1:/tree?token=99f5ac6fd1ec2cac299b0dcc61f5392edd69a7bbed37f1eb [I 2023-12-06 15:20:34.665 ServerApp] Skipped non-installed server(s): bash-language-server, dockerfile-language-server-nodejs, javascript-typescript-langserver, jedi-language-server, julia-language-server, pyright, python-language-server, python-lsp-server, r-languageserver, sql-language-server, texlab, typescript-language-server, unified-language-server, vscode-css-languageserver-bin, vscode-html-languageserver-bin, vscode-json-languageserver-bin, yaml-language-server [I 2023-12-06 15:20:41.701 ServerApp] Creating new notebook in [I 2023-12-06 15:20:48.258 ServerApp] Kernel started: 67fc13ce-4b7f-4808-88c7-72f2aff472c6 [I
Re: [Qgis-user] Accessing iface from jupyter to create map from QGIS
> While trying to activate with iface.setactivelayer I am facing an error. The error is complaining that iface is None. Your code doesn't show what value you assigned to this variable - where did you set iface and what value did you set it to? I think you forgot to set it and that is why you are getting this error. Cheers, Adam. ___ QGIS-User mailing list QGIS-User@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-user Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
[Qgis-user] Accessing iface from jupyter to create map from QGIS
Dear all, I am trying to create a map from qgis. I have added a few layers and created a layout in qgis. All I want is to create a script to get a map from the layer on the layout that I have created in qgis. I have attached the code. I am thinking of activating each layer at a time so that only the activated layer will be on the layout and a map will get generated. While trying to activate with iface.setactivelayer I am facing an error. So no layer is added to the layout. I have added the code and image of the generated map. [image: Day-1.png] [image: image.png] How to access the layer in qgis? how to deactivate the first layer and then move to the next layer for the next map. Regards, Noshin #!/usr/bin/env python # coding: utf-8 # In[61]: import os from datetime import datetime, timedelta from qgis.core import ( QgsGeometry, QgsMapSettings, QgsPrintLayout, QgsMapSettings, QgsMapRendererParallelJob, QgsLayoutItemLabel, QgsLayoutItemLegend, QgsLayoutItemMap, QgsLayoutItemPolygon, QgsLayoutItemScaleBar, QgsLayoutExporter, QgsLayoutItem, QgsLayoutPoint, QgsLayoutSize, QgsUnitTypes, QgsProject, QgsFillSymbol, QgsAbstractValidityCheck, check, ) from qgis.PyQt.QtGui import ( QPolygonF, QColor, ) from qgis.PyQt.QtCore import ( QPointF, QRectF, QSize, ) # In[70]: project_path = r"C:\Users\FFWS\Desktop\Boundary_Generation_For_2D\2D Overland Flow Model-Boundary Generation\2D Overland Flow Model-Boundary Generation\bin\Debug\C2_FloodMap_SERM_V01.qgz" project_instance = QgsProject.instance() project_instance.read(project_path) print(project_instance.fileName()) # In[71]: map_layers = QgsProject.instance().mapLayersByName('V1054_SWRM_SW_2D PS_LR_Dike_Updt_statBaseFlood Map_Day01')[0] iface.setActiveLayer(map_layers) # In[64]: projectInstance = QgsProject.instance() layoutmanager = projectInstance.layoutManager() layout = layoutmanager.layoutByName("SW_Day_1") # In[65]: map_label = QgsLayoutItemLabel(layout) # In[67]: firstDay ="2023-11-25 09:00:00" dateTimeObj = datetime.strptime(firstDay, "%Y-%m-%d %I:%M:%S" ) secDay=(dateTimeObj+ timedelta(days = 1)) PreparedDayValue = secDay.strftime('%Y-%m-%d %I:%M:%S') map_label.setText("Valid for: " +firstDay.strip('"')+ " "+"to" +" "+PreparedDayValue + ", Prepared on: " + firstDay) # In[68]: layout.removeLayoutItem(map_label) layout.addLayoutItem(map_label) # In[69]: map_label.attemptMove(QgsLayoutPoint(1.6, 0.5, QgsUnitTypes.LayoutInches)) exporter = QgsLayoutExporter(layout) exporter.exportToImage(r"C:\Users\FFWS\Desktop\Boundary_Generation_For_2D\2D Overland Flow Model-Boundary Generation\2D Overland Flow Model-Boundary Generation\bin\Debug\Day-1.png", QgsLayoutExporter.ImageExportSettings() ) # In[ ]: ___ QGIS-User mailing list QGIS-User@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-user Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user