<div dir="ltr">Hey Raymond,<div><br></div><div>Thank you very much for your answer. It worked. Are you aware of any resources, online trainings, books or anything else which focuses on the pyqgis3? What would be your recommendation on this.</div><div><br></div><div>Cheers</div><div>Dimitris</div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 24, 2018 at 10:09 AM Raymond Nijssen <<a href="mailto:r.nijssen@terglobo.nl">r.nijssen@terglobo.nl</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Dimitris,<br>
<br>
It is indeed hard to learn pyqgis 3 with outdated docs.<br>
<br>
The following code worked for me (suggested by Martin Dobias):<br>
<br>
settings = QgsMapSettings()<br>
settings.setOutputSize(QSize(512,512))<br>
settings.setExtent(layer.extent())<br>
settings.setLayers([layer])<br>
<br>
job = QgsMapRendererSequentialJob(settings)<br>
job.start()<br>
job.waitForFinished()<br>
img = job.renderedImage()<br>
img.save("/tmp/rendered.png")<br>
<br>
When you call job.start(), it will start rendering in background and<br>
immediately return. That's why you need to call job.waitForFinished()<br>
to block your code until the final image is ready. This approach has a<br>
downside that it blocks the main thread and therefore the GUI will be<br>
blocked until rendering has finished. If you would like to avoid this<br>
blocking, after job.start() you could have a slot that would listen to<br>
finished() signal coming from "job" - and save the image there. This<br>
approach makes the code a bit more complex, but the GUI does not get<br>
blocked and you can even start multiple jobs at a time in parallel.<br>
<br>
Hope it helps you. Good luck learning pyqgis!<br>
<br>
Raymond<br>
<br>
<br>
<br>
On 24-10-18 09:47, Dimitris Kar wrote:<br>
> I am trying to learn and work with PyQGIS 3.<br>
> <br>
> It is a very challenging task cause documentation is really [limited and <br>
> out of date][1].<br>
> <br>
> What I am trying to do is to:<br>
> <br>
>   1. create a project<br>
>   2. add a layer<br>
>   3. save the project<br>
>   4. export it as a png<br>
> <br>
> I have done the first 3 steps (see code below).<br>
> <br>
>      import sys<br>
>      import qgis<br>
>      from qgis.core import QgsVectorLayer<br>
>      from qgis.core import QgsApplication<br>
>      from qgis.core import QgsProject<br>
>      from qgis.core import QgsMapRendererJob, QgsMapSettings<br>
>      from qgis.gui import QgsMapCanvas<br>
>      from PyQt5.QtGui import QImage, QColor, QPainter<br>
>      from PyQt5.QtCore import QSize<br>
> <br>
>      # PYTHON APPLICATIONS<br>
> <br>
>      # prefix path: the location where qgis is installed in the system<br>
>      # the easiest way to find the prefix path to run the following <br>
> commaand from the python console of qgis: QgsApplication.prefixPath()<br>
> <br>
>      #1. CREATE THE APPLICATION<br>
>      QgsApplication.setPrefixPath('/usr', True)<br>
>      # second parameter into false disables the gui<br>
>      qgs = QgsApplication([], False)<br>
>      #load providers<br>
>      qgs.initQgis()<br>
> <br>
>      #2. CREATE THE PROJECT<br>
>      # create project<br>
>      project_name = '/home/dkar/workspaces/qgis/data/test_project_2.qgz'<br>
>      project = QgsProject.instance()<br>
>      new_project = project.write(project_name)<br>
> <br>
>      #3. READ/OPEN THE PROJECT<br>
>      # modify the project e.g. adding more layers and save it.<br>
>      new_project = project.read(project.fileName())<br>
> <br>
> <br>
>      #4. ADD A VECTORLAYER<br>
>      # add a layer in the QgsProviderRegistry<br>
>      <br>
> layer=QgsVectorLayer("/home/dkar/workspaces/qgis/data/pakistan_1.shp", <br>
> "pakistan", "ogr")<br>
> <br>
>      if not layer.isValid():<br>
>          print("Layer failed")<br>
> <br>
>      #5. ADD LAYER TO THE REGISTRY<br>
>      project.addMapLayer(layer, True)<br>
>      project.write('/home/dkar/workspaces/qgis/data/test_project_2.qgz')<br>
> <br>
>      #6. SAVE THE PROJECT<br>
>      all_layers = QgsProject.instance().mapLayers()<br>
> <br>
> <br>
>      img = QImage(QSize (800, 600), QImage.Format_ARGB32_Premultiplied)<br>
>      # set image's backgroud color<br>
>      color = QColor(255, 255, 255)<br>
>      img.fill(color.rgb())<br>
> <br>
>      #create painter<br>
>      p = QPainter()<br>
>      p.begin(img)<br>
>      p.setRenderHint(QPainter.Antialiasing)<br>
> <br>
>      # THIS IS WHERE THE ISSUE APPEARS<br>
>      # THIS IS WHERE THE ISSUE APPEARS<br>
>      render = QgsMapRendererJob() # THIS IS WHERE THE ISSUE APPEARS<br>
> <br>
>      # set layer set (am I using here the prohect?)<br>
>      lst = [<a href="http://layer.id" rel="noreferrer" target="_blank">layer.id</a> <<a href="http://layer.id" rel="noreferrer" target="_blank">http://layer.id</a>>()] # cause "layer" is the name of <br>
> the variable<br>
>      render.setLayerSet(lst)<br>
> <br>
>      # to remove the provider and layer registries from memory<br>
>      qgs.exitQgis()<br>
> <br>
> <br>
> <br>
> But when I try to do the last part, following the instructions from the <br>
> existing documentation. I get issues because the specific function <br>
> (`QgsMapRenderer`) doesn't exist anymore in QGIS 3.<br>
> <br>
> I am not sure how to solve the issue.<br>
> <br>
> I see in the documentation that this function:<br>
> <br>
>      QgsMapRenderer<br>
> <br>
> was replaced by<br>
> <br>
>      QgsMapRendererJob<br>
> <br>
> But then when I try to import and use it in the code as:<br>
> <br>
>      from qgis.core import QgsMapRendererJob, QgsMapSettings<br>
>          render = QgsMapRendererJob()<br>
> <br>
> I get an error message:<br>
> <br>
>      TypeError: qgis._core.QgsMapRendererJob represents a C++ abstract <br>
> class and cannot be instantiated<br>
> <br>
> Any idea how to proceed with this?<br>
> <br>
>    [1]: <br>
> <a href="https://docs.qgis.org/testing/pdf/en/QGIS-testing-PyQGISDeveloperCookbook-en.pdf" rel="noreferrer" target="_blank">https://docs.qgis.org/testing/pdf/en/QGIS-testing-PyQGISDeveloperCookbook-en.pdf</a><br>
> <br>
> _______________________________________________<br>
> QGIS-Developer mailing list<br>
> <a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
> List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
> Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
> <br>
<br>
-- <br>
Terglobo<br>
Fahrenheitstraat 1<br>
5223 BJ 's-Hertogenbosch<br>
The Netherlands<br>
+31 (0) 6 25 31 49 83<br>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a></blockquote></div>