Hot answers tagged

73 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

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,375
32 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
30 votes

Generating equal sized polygons along line with PyQGIS

Interesting question! It's something I've wanted to try myself, so gave it a go. You can do this in PostGRES/POSTGIS with a function which generates a set of polygons. In my case, I have a table ...
Steven Kay's user avatar
  • 20.3k
30 votes
Accepted

Getting layer extent in 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.1k
28 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
28 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
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

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.1k
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

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
24 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
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
  • 856
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,343
22 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
  • 221
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
22 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
  • 856
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,375
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
  • 75.3k
20 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
  • 29.7k
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
  • 75.3k
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
  • 29.7k
20 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
  • 371
20 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
19 votes
Accepted

Activating layer by its name in PyQGIS

TL;DR To get all features of a layer by the layer name you do not need to activate it. Just use name = 'counties' layer = QgsProject.instance().mapLayersByName( name )[0] poles = layer.getFeatures() ...
Matthias Kuhn's user avatar
19 votes
Accepted

Iterating over layers and exporting them as PNG images with PyQGIS in standalone script

In order to solve this question, we need to use timers or something that delays the execution of the script, so the canvas can reflect the layer arrangement at the time the map is exported. In other ...
Germán Carrillo'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
19 votes
Accepted

Make QGIS python plugin for both versions 2.x and 3.x?

Documentation Here you can find what is new and what is break under the PyQGIS API. To get details about how to port Python2 to Python3 go there You can find some detail about testing from QGIS2 to ...
Hugo Roussaffa's user avatar

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