Skip to main content
74 votes
Accepted

What is the default plugin folder location in QGIS 3?

Go to menu Settings -> User profiles -> Open active profile folder From there, you can go to python -> plugins. That's the plugin folder for QGIS v3. On my GNU/Linux machine: /home/USER/.local/...
Germán Carrillo's user avatar
63 votes
Accepted

Using QGIS Processing algorithms from PyQGIS standalone scripts (outside of GUI)

You can run a QGIS Processing algorithm in standalone (no GUI) mode in this way: import sys from qgis.core import ( QgsApplication, QgsProcessingFeedback, QgsVectorLayer ) # See ...
Germán Carrillo's user avatar
46 votes
Accepted

Was QgsMapLayerRegistry being removed in QGIS 3?

Yes it has been removed, but alternatives exist. Changes of the 3.0 API are listed here: https://qgis.org/api/api_break.html QgsMapLayerRegistry: Its functionality has been moved to QgsProject. ...
Matthias Kuhn's user avatar
33 votes
Accepted

Getting the output layer reference returned by Processing tool

Please note that all solutions explained here are related to vector layer used as the input and QGIS native algorithms. The answer doesn't explain the results of other providers' tools. (GRASS, GDAL, ...
Kadir Şahbaz's user avatar
33 votes
Accepted

Writing a python processing script with QGIS 3.0

With the transition from QGIS2.x to QGIS3.x the whole processing framework has been reworked and large parts of it run now as C++ classes that you can interact with using Python. Unfortunately the ...
root676's user avatar
  • 2,385
32 votes

Getting list of layer names using PyQGIS

Since version 3, QgsMapLayerRegistry funcionalities have been moved to QgsProject: https://qgis.org/api/api_break.html Update for QGIS3.x: from qgis.core import QgsProject names = [layer.name() for ...
RafDouglas C. Tommasi's user avatar
30 votes
Accepted

Getting layer extent using PyQGIS

The answer is almost completely contained in a post I recently wrote. The extent is returned as a QgsRectangle() object with the following code: layer = iface.activeLayer() # load the layer as you ...
mgri's user avatar
  • 16.3k
29 votes

Creating sectors for telecom towers

Telecom cell site plan and optimization using QGIS Create Points or Site Locations: Create database in CSV (make sure antenna sizes are sorted in descending order: highest first to lowest last, such ...
Vamsi Krishna Uppala's user avatar
27 votes

How do I fix "name 'iface' is not defined"?

The iface module is automatically imported when directly working from the Python Console. In the other cases (for example, when running a script from the Processing Toolbox), you need to manually ...
mgri's user avatar
  • 16.3k
26 votes

Iterating over map layers using PyQGIS

For QGIS 3, QgsMapLayerRegistry's functionality has been moved to QgsProject. So, for iterating over map layers, you should use that structure: layers = QgsProject.instance().mapLayers() ....
Kadir Şahbaz's user avatar
25 votes

Was QgsMapLayerRegistry being removed in QGIS 3?

You can do the same thing easily just do: QgsProject.instance().addMapLayer(your_Qgs_whaterver_Layer) for more details, see the following: https://qgis.org/api/classQgsProject.html#...
Abhijit Gujar's user avatar
25 votes
Accepted

How to fix QGIS being extremely slow?

So after taking into account what ThomasG77 told me to fix my QGIS, I've found that indeed creating a new profile fixes the slowdown, but I didn't want to have to create a new profile everytime it got ...
EnGIsNowhere's user avatar
25 votes
Accepted

Protecting QGIS Plugin source code

QGIS is licensed under the GPLv2, this same license applies to QGIS plugins. QGIS has written a blog post that clarifies on the licensing as well as on the commercial aspects of this. With respect to ...
Matthias Kuhn's user avatar
24 votes

Creating Virtual Layer with PyQGIS

For QGIS 3, instead use QgsProject: from qgis.core import QgsVectorLayer, QgsProject sql_query = "SELECT * FROM road WHERE type = 'Expressway'" vlayer = QgsVectorLayer(f"?query={...
rwalk's user avatar
  • 241
23 votes
Accepted

Reading and setting label settings in PyQGIS

I found the answer for QGIS 3.0: layer_settings = QgsPalLayerSettings() text_format = QgsTextFormat() text_format.setFont(QFont("Arial", 12)) text_format.setSize(12) buffer_settings = ...
Kezah's user avatar
  • 866
23 votes
Accepted

'pyrcc5' is not recognized as an internal or external command

Try running a batch file with the following: @echo off call "C:\Program Files\QGIS 3.0\bin\o4w_env.bat" call "C:\Program Files\QGIS 3.0\bin\qt5_env.bat" call "C:\Program Files\QGIS 3.0\bin\py3_env....
Kezah's user avatar
  • 866
22 votes

Duplicating layer in memory using PyQGIS

In QGIS 3 you can make a copy of a layer without saving any reference to the parent layer in this way: layer.selectAll() clone_layer = processing.run("native:saveselectedfeatures", {'INPUT': ...
Leo Cardona's user avatar
  • 1,363
22 votes
Accepted

Making layer or group private/hidden/invisible in Layers Panel of QGIS

Just to clarify, private/hidden layers/groups will not appear in the Layers Panel, but they can still be seen in the canvas or used in joins/relations with other layers. Native approach (QGIS API) You ...
Germán Carrillo's user avatar
21 votes

How to get the map canvas CRS in EPSG code?

In QGIS3 API changed: iface.mapCanvas().mapSettings().destinationCrs().authid() 'EPSG:3045'
Peter Petrik's user avatar
  • 1,465
21 votes
Accepted

Loading style (*.qml file) to layer via PyQGIS

I have my qml styles in a fix path (c:/pyqgis_data/Styles/). So, to load arrows_red_orange.qml (exclusively for point layers), next lines at the Python Console of QGIS work: >>>layer = ...
xunilk's user avatar
  • 30k
21 votes
Accepted

Importing NULL in PyQGIS

The NULL class is defined in qgis.core: import qgis.core if f["SAMPLE"] == qgis.core.NULL: ... Normally you should be able to do a normal is None check however due to tool we use to ...
Nathan W's user avatar
  • 34.8k
21 votes

How to read user defined project variables using Python

Since QGIS 3.0 you need also to specify the project when setting and reading a variable: project = QgsProject.instance() QgsExpressionContextUtils.setProjectVariable(project,'myvar','Hello World!') ...
srjskam's user avatar
  • 381
21 votes
Accepted

"AttributeError: 'QgisInterface' object has no attribute 'legendInterface'" in PyQGIS 3

You could just replace layers = iface.legendInterface().layers() with layers = [layer for layer in QgsProject.instance().mapLayers().values()]
Joseph's user avatar
  • 76k
21 votes
Accepted

Difference between QgsPoint, QgsPointXY and QgsGeometry.fromPointXY() in PyQGIS

QgsPoint is a point class which supports Z (3D) and M values. (What are Z and M?) x, y, z, m = 10, 10, 20, 5 QgsPoint(x, y) # 2D # <QgsPoint: Point (10 10)> QgsPoint(x, y, z) # 3D # <...
Kadir Şahbaz's user avatar
21 votes
Accepted

Optimizing intersections and rubber band representations with PyQGIS

Nice question! You can apply several optimizations, like these 5: 1. Remove any print() statement. Print statements are nice for debugging (although there are better options, see this answer). However,...
Germán Carrillo's user avatar
20 votes
Accepted

Duplicating layer in memory using PyQGIS

The following code works for me from both the Python Console and plugin. It takes the features from the source input layer and copies the attributes to a memory layer (in this case, a polygon layer ...
Joseph's user avatar
  • 76k
20 votes
Accepted

Maximum value of field of vector layer using PyQGIS

It is not necessary to get a complete list of field values. In QgsVectorLayer exists 'maximumValue' method. So, this works well and it's shorter: layer = iface.activeLayer() idx = layer....
xunilk's user avatar
  • 30k
19 votes

Applying categorized symbol to each feature using PyQGIS

For those who'd like to try the code above for the PyQGIS3 version, here is my adaptation (it works for the 'active layer' if run from the console ...). # provide file name index and field's unique ...
A. Jean's user avatar
  • 321
19 votes

Getting or assigning vertex id to polygon vertices and use these as vertex label using PyQGIS

I know this is forever after you asked, but I found your question when I attempted to do the same thing. At least in QGIS Version 3.8.0, this has been made incredibly easy. Layer Properties -> ...
Saijin_Naib - No To OpenAI's user avatar
19 votes
Accepted

Creating predefined values in field using PyQGIS

You need to assign and configure a ValueMap widget to your layer's field in this way: QGIS 3.x fieldIndex = layer.fields().indexFromName( 'myField' ) editor_widget_setup = QgsEditorWidgetSetup( '...
Germán Carrillo's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible