Unable to export map with pyQGIS3
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to learn and work with pyQGIS3. It's a very challenging task because documentation is really limited and out of date.
What I am trying to do is to:
- create a project
- add a layer
- save the project
- export it as a png
I have done the first 3 steps (see code below).
import sys
import qgis
from qgis.core import QgsVectorLayer
from qgis.core import QgsApplication
from qgis.core import QgsProject
from qgis.core import QgsMapRendererJob, QgsMapSettings
from qgis.gui import QgsMapCanvas
from PyQt5.QtGui import QImage, QColor, QPainter
from PyQt5.QtCore import QSize
# PYTHON APPLICATIONS
# prefix path: the location where qgis is installed in the system
# the easiest way to find the prefix path to run the following commaand from the python console of qgis: QgsApplication.prefixPath()
#1. CREATE THE APPLICATION
QgsApplication.setPrefixPath('/usr', True)
# second parameter into false disables the gui
qgs = QgsApplication(, False)
#load providers
qgs.initQgis()
#2. CREATE THE PROJECT
# create project
project_name = '/home/dkar/workspaces/qgis/data/test_project_2.qgz'
project = QgsProject.instance()
new_project = project.write(project_name)
#3. READ/OPEN THE PROJECT
# modify the project e.g. adding more layers and save it.
new_project = project.read(project.fileName())
#4. ADD A VECTORLAYER
# add a layer in the QgsProviderRegistry
layer=QgsVectorLayer("/home/dkar/workspaces/qgis/data/pakistan_1.shp", "pakistan", "ogr")
if not layer.isValid():
print("Layer failed")
#5. ADD LAYER TO THE REGISTRY
project.addMapLayer(layer, True)
project.write('/home/dkar/workspaces/qgis/data/test_project_2.qgz')
#6. SAVE THE PROJECT
all_layers = QgsProject.instance().mapLayers()
img = QImage(QSize (800, 600), QImage.Format_ARGB32_Premultiplied)
# set image's backgroud color
color = QColor(255, 255, 255)
img.fill(color.rgb())
#create painter
p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)
# THIS IS WHERE THE ISSUE APPEARS
# THIS IS WHERE THE ISSUE APPEARS
render = QgsMapRendererJob() # THIS IS WHERE THE ISSUE APPEARS
# set layer set (am I using here the prohect?)
lst = [layer.id()] # cause "layer" is the name of the variable
render.setLayerSet(lst)
# to remove the provider and layer registries from memory
qgs.exitQgis()
But when I try to do the last part, following the instructions from the existing documentation, I get issues because the specific function (QgsMapRenderer) doesn't exist anymore in QGIS3. I am not sure how to solve the issue.
I see in the documentation that this function:
QgsMapRenderer
was replaced by
QgsMapRendererJob
But then when I try to import and and use it in the code as:
from qgis.core import QgsMapRendererJob, QgsMapSettings
render = QgsMapRendererJob()
I get the error message:
TypeError: qgis._core.QgsMapRendererJob represents a C++ abstract class and cannot be instantiated
Any idea how to proceed with this?
qgis python pyqgis-3
add a comment |Â
up vote
1
down vote
favorite
I am trying to learn and work with pyQGIS3. It's a very challenging task because documentation is really limited and out of date.
What I am trying to do is to:
- create a project
- add a layer
- save the project
- export it as a png
I have done the first 3 steps (see code below).
import sys
import qgis
from qgis.core import QgsVectorLayer
from qgis.core import QgsApplication
from qgis.core import QgsProject
from qgis.core import QgsMapRendererJob, QgsMapSettings
from qgis.gui import QgsMapCanvas
from PyQt5.QtGui import QImage, QColor, QPainter
from PyQt5.QtCore import QSize
# PYTHON APPLICATIONS
# prefix path: the location where qgis is installed in the system
# the easiest way to find the prefix path to run the following commaand from the python console of qgis: QgsApplication.prefixPath()
#1. CREATE THE APPLICATION
QgsApplication.setPrefixPath('/usr', True)
# second parameter into false disables the gui
qgs = QgsApplication(, False)
#load providers
qgs.initQgis()
#2. CREATE THE PROJECT
# create project
project_name = '/home/dkar/workspaces/qgis/data/test_project_2.qgz'
project = QgsProject.instance()
new_project = project.write(project_name)
#3. READ/OPEN THE PROJECT
# modify the project e.g. adding more layers and save it.
new_project = project.read(project.fileName())
#4. ADD A VECTORLAYER
# add a layer in the QgsProviderRegistry
layer=QgsVectorLayer("/home/dkar/workspaces/qgis/data/pakistan_1.shp", "pakistan", "ogr")
if not layer.isValid():
print("Layer failed")
#5. ADD LAYER TO THE REGISTRY
project.addMapLayer(layer, True)
project.write('/home/dkar/workspaces/qgis/data/test_project_2.qgz')
#6. SAVE THE PROJECT
all_layers = QgsProject.instance().mapLayers()
img = QImage(QSize (800, 600), QImage.Format_ARGB32_Premultiplied)
# set image's backgroud color
color = QColor(255, 255, 255)
img.fill(color.rgb())
#create painter
p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)
# THIS IS WHERE THE ISSUE APPEARS
# THIS IS WHERE THE ISSUE APPEARS
render = QgsMapRendererJob() # THIS IS WHERE THE ISSUE APPEARS
# set layer set (am I using here the prohect?)
lst = [layer.id()] # cause "layer" is the name of the variable
render.setLayerSet(lst)
# to remove the provider and layer registries from memory
qgs.exitQgis()
But when I try to do the last part, following the instructions from the existing documentation, I get issues because the specific function (QgsMapRenderer) doesn't exist anymore in QGIS3. I am not sure how to solve the issue.
I see in the documentation that this function:
QgsMapRenderer
was replaced by
QgsMapRendererJob
But then when I try to import and and use it in the code as:
from qgis.core import QgsMapRendererJob, QgsMapSettings
render = QgsMapRendererJob()
I get the error message:
TypeError: qgis._core.QgsMapRendererJob represents a C++ abstract class and cannot be instantiated
Any idea how to proceed with this?
qgis python pyqgis-3
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to learn and work with pyQGIS3. It's a very challenging task because documentation is really limited and out of date.
What I am trying to do is to:
- create a project
- add a layer
- save the project
- export it as a png
I have done the first 3 steps (see code below).
import sys
import qgis
from qgis.core import QgsVectorLayer
from qgis.core import QgsApplication
from qgis.core import QgsProject
from qgis.core import QgsMapRendererJob, QgsMapSettings
from qgis.gui import QgsMapCanvas
from PyQt5.QtGui import QImage, QColor, QPainter
from PyQt5.QtCore import QSize
# PYTHON APPLICATIONS
# prefix path: the location where qgis is installed in the system
# the easiest way to find the prefix path to run the following commaand from the python console of qgis: QgsApplication.prefixPath()
#1. CREATE THE APPLICATION
QgsApplication.setPrefixPath('/usr', True)
# second parameter into false disables the gui
qgs = QgsApplication(, False)
#load providers
qgs.initQgis()
#2. CREATE THE PROJECT
# create project
project_name = '/home/dkar/workspaces/qgis/data/test_project_2.qgz'
project = QgsProject.instance()
new_project = project.write(project_name)
#3. READ/OPEN THE PROJECT
# modify the project e.g. adding more layers and save it.
new_project = project.read(project.fileName())
#4. ADD A VECTORLAYER
# add a layer in the QgsProviderRegistry
layer=QgsVectorLayer("/home/dkar/workspaces/qgis/data/pakistan_1.shp", "pakistan", "ogr")
if not layer.isValid():
print("Layer failed")
#5. ADD LAYER TO THE REGISTRY
project.addMapLayer(layer, True)
project.write('/home/dkar/workspaces/qgis/data/test_project_2.qgz')
#6. SAVE THE PROJECT
all_layers = QgsProject.instance().mapLayers()
img = QImage(QSize (800, 600), QImage.Format_ARGB32_Premultiplied)
# set image's backgroud color
color = QColor(255, 255, 255)
img.fill(color.rgb())
#create painter
p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)
# THIS IS WHERE THE ISSUE APPEARS
# THIS IS WHERE THE ISSUE APPEARS
render = QgsMapRendererJob() # THIS IS WHERE THE ISSUE APPEARS
# set layer set (am I using here the prohect?)
lst = [layer.id()] # cause "layer" is the name of the variable
render.setLayerSet(lst)
# to remove the provider and layer registries from memory
qgs.exitQgis()
But when I try to do the last part, following the instructions from the existing documentation, I get issues because the specific function (QgsMapRenderer) doesn't exist anymore in QGIS3. I am not sure how to solve the issue.
I see in the documentation that this function:
QgsMapRenderer
was replaced by
QgsMapRendererJob
But then when I try to import and and use it in the code as:
from qgis.core import QgsMapRendererJob, QgsMapSettings
render = QgsMapRendererJob()
I get the error message:
TypeError: qgis._core.QgsMapRendererJob represents a C++ abstract class and cannot be instantiated
Any idea how to proceed with this?
qgis python pyqgis-3
I am trying to learn and work with pyQGIS3. It's a very challenging task because documentation is really limited and out of date.
What I am trying to do is to:
- create a project
- add a layer
- save the project
- export it as a png
I have done the first 3 steps (see code below).
import sys
import qgis
from qgis.core import QgsVectorLayer
from qgis.core import QgsApplication
from qgis.core import QgsProject
from qgis.core import QgsMapRendererJob, QgsMapSettings
from qgis.gui import QgsMapCanvas
from PyQt5.QtGui import QImage, QColor, QPainter
from PyQt5.QtCore import QSize
# PYTHON APPLICATIONS
# prefix path: the location where qgis is installed in the system
# the easiest way to find the prefix path to run the following commaand from the python console of qgis: QgsApplication.prefixPath()
#1. CREATE THE APPLICATION
QgsApplication.setPrefixPath('/usr', True)
# second parameter into false disables the gui
qgs = QgsApplication(, False)
#load providers
qgs.initQgis()
#2. CREATE THE PROJECT
# create project
project_name = '/home/dkar/workspaces/qgis/data/test_project_2.qgz'
project = QgsProject.instance()
new_project = project.write(project_name)
#3. READ/OPEN THE PROJECT
# modify the project e.g. adding more layers and save it.
new_project = project.read(project.fileName())
#4. ADD A VECTORLAYER
# add a layer in the QgsProviderRegistry
layer=QgsVectorLayer("/home/dkar/workspaces/qgis/data/pakistan_1.shp", "pakistan", "ogr")
if not layer.isValid():
print("Layer failed")
#5. ADD LAYER TO THE REGISTRY
project.addMapLayer(layer, True)
project.write('/home/dkar/workspaces/qgis/data/test_project_2.qgz')
#6. SAVE THE PROJECT
all_layers = QgsProject.instance().mapLayers()
img = QImage(QSize (800, 600), QImage.Format_ARGB32_Premultiplied)
# set image's backgroud color
color = QColor(255, 255, 255)
img.fill(color.rgb())
#create painter
p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)
# THIS IS WHERE THE ISSUE APPEARS
# THIS IS WHERE THE ISSUE APPEARS
render = QgsMapRendererJob() # THIS IS WHERE THE ISSUE APPEARS
# set layer set (am I using here the prohect?)
lst = [layer.id()] # cause "layer" is the name of the variable
render.setLayerSet(lst)
# to remove the provider and layer registries from memory
qgs.exitQgis()
But when I try to do the last part, following the instructions from the existing documentation, I get issues because the specific function (QgsMapRenderer) doesn't exist anymore in QGIS3. I am not sure how to solve the issue.
I see in the documentation that this function:
QgsMapRenderer
was replaced by
QgsMapRendererJob
But then when I try to import and and use it in the code as:
from qgis.core import QgsMapRendererJob, QgsMapSettings
render = QgsMapRendererJob()
I get the error message:
TypeError: qgis._core.QgsMapRendererJob represents a C++ abstract class and cannot be instantiated
Any idea how to proceed with this?
qgis python pyqgis-3
qgis python pyqgis-3
edited 14 mins ago
lambertj
1,5431421
1,5431421
asked 1 hour ago
user1919
1,13811031
1,13811031
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
Well, the error message is correct. According to the QGIS API documentation, QgsMapRendererJob
is an abstract class. So you cannot directly use it. Instead you need to use one of the subclasses:
QgsMapRendererSequentialJob
- renders map in one background thread to an imageQgsMapRendererParallelJob
- renders map in multiple background threads to an imageQgsMapRendererCustomPainterJob
- renders map with given QPainter in one background thread
https://www.qgis.org/api/classQgsMapRendererJob.html#details
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Well, the error message is correct. According to the QGIS API documentation, QgsMapRendererJob
is an abstract class. So you cannot directly use it. Instead you need to use one of the subclasses:
QgsMapRendererSequentialJob
- renders map in one background thread to an imageQgsMapRendererParallelJob
- renders map in multiple background threads to an imageQgsMapRendererCustomPainterJob
- renders map with given QPainter in one background thread
https://www.qgis.org/api/classQgsMapRendererJob.html#details
add a comment |Â
up vote
3
down vote
Well, the error message is correct. According to the QGIS API documentation, QgsMapRendererJob
is an abstract class. So you cannot directly use it. Instead you need to use one of the subclasses:
QgsMapRendererSequentialJob
- renders map in one background thread to an imageQgsMapRendererParallelJob
- renders map in multiple background threads to an imageQgsMapRendererCustomPainterJob
- renders map with given QPainter in one background thread
https://www.qgis.org/api/classQgsMapRendererJob.html#details
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Well, the error message is correct. According to the QGIS API documentation, QgsMapRendererJob
is an abstract class. So you cannot directly use it. Instead you need to use one of the subclasses:
QgsMapRendererSequentialJob
- renders map in one background thread to an imageQgsMapRendererParallelJob
- renders map in multiple background threads to an imageQgsMapRendererCustomPainterJob
- renders map with given QPainter in one background thread
https://www.qgis.org/api/classQgsMapRendererJob.html#details
Well, the error message is correct. According to the QGIS API documentation, QgsMapRendererJob
is an abstract class. So you cannot directly use it. Instead you need to use one of the subclasses:
QgsMapRendererSequentialJob
- renders map in one background thread to an imageQgsMapRendererParallelJob
- renders map in multiple background threads to an imageQgsMapRendererCustomPainterJob
- renders map with given QPainter in one background thread
https://www.qgis.org/api/classQgsMapRendererJob.html#details
answered 1 hour ago
Thomas
1,287416
1,287416
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f299892%2funable-to-export-map-with-pyqgis3%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password