0

I am writing Python code to take two vector layers as input add different symbology to it and then export that as a image. The vectors layers have been added to the map canvas but while exporting only the second vector file in the image not both vector file.

  import os

  w_layer = r"path to first vector layer"

  o_layer = r"path to second vector layer"

  image_location = r"export image path"

  wlayer = QgsVectorLayer(w_layer, "W layer", "ogr")

  QgsProject.instance().addMapLayer(wlayer)

  wlayer.loadNamedStyle(r"path for qml file to display a particular symbology")



  olayer = QgsVectorLayer(o_layer, "o layer", "ogr")

  QgsProject.instance().addMapLayer(olayer)

  olayer.loadNamedStyle(r"path to qml file to display a particular symbology")




   vlayer = iface.addMapLayers(wlayer , olayer)

   settings = QgsMapSettings()

   settings.setLayers([vlayer])

   settings.setBackgroundColor(QColor(255, 255, 255))

   settings.setOutputSize(QSize(800, 600))

   settings.setExtent(vlayer.extent())



   render = QgsMapRendererParallelJob(settings)


   def finished():

      img = render.renderedImage()


      img.save(image_location, "png")

   render.finished.connect(finished)


   render.start() `
4
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works. – Ian Turton Jul 17 '20 at 8:16
  • 1
    Buggy sample iface.addMapLayers does not exist (remove it) So settings.setLayers([vlayer]) can't work. Use settings.setLayers([wlayer, olayer]) – ThomasG77 Jul 17 '20 at 8:21
  • 1
    Thanks @ThomasG77 , it worked – rohit sharma Jul 17 '20 at 9:41
  • Made an answer with same content so the question can be set to answered – ThomasG77 Jul 17 '20 at 18:42
0

In the original code, iface.addMapLayers does not exist not anywhere in the doc e.g https://qgis.org/api/classQgisInterface.html. So, simply remove it.

Because of the previous remark, settings.setLayers([vlayer]) can't work as vlayer content is None.

Use settings.setLayers([wlayer, olayer]) instead to solve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.