[Qgis-user] Accessing iface from jupyter to create map from QGIS
noshin prachi
noshin.prachi at gmail.com
Tue Dec 5 20:52:45 PST 2023
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20231206/1f908a8f/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 28288 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20231206/1f908a8f/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Day-1.png
Type: image/png
Size: 62857 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20231206/1f908a8f/attachment-0003.png>
-------------- next part --------------
#!/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[ ]:
More information about the QGIS-User
mailing list