Hot answers tagged

10 votes

Creating spatial index in standalone script as part of output vector layer using PyQGIS

If you use layer_out.dataProvider().createSpatialIndex() you don't get that error. To get the error you must use layer_out.createSpatialIndex(). The code in the question doesn't throw the error: ...
Kadir Şahbaz's user avatar
10 votes

PyQGIS to set masked symbol layer and mask source

Label masking information is not stored in the qml-file? I guess "Masked symbol layers" and "Mask sources" settings are not stored in both qml file and Geopackage. I just realized ...
Kadir Şahbaz's user avatar
10 votes
Accepted

Performing basic labelling with PyQGIS

There is no problem with @Kezah's answer. I've edited the script and it works. Please check it. Avoid using startEditing() and dataProvider().addFeatures() together. Use either one. If you construct a ...
Kadir Şahbaz's user avatar
8 votes
Accepted

TypeError when converting shapefiles to KMLs using PyQGIS

According to the documentation the first argument should be a QgsVectorLayer, which is why you are getting the unexpected type error. You are also missing the QgsCoordinateReferenceSystem parameter. ...
Matt's user avatar
  • 14.9k
8 votes
Accepted

Extract points from a selected polygon in model builder [QGIS 3.30]

This can be achieved very simply by using the Extract selected features algorithm:
Matt's user avatar
  • 14.9k
7 votes

Writing italic form scientific name in map legend?

Since the new QGIS 3.30 version and the integration of this feature request : "Support for HTML in text of legend items", it's now possible to format like this : with this legend title : ...
J. Monticolo's user avatar
  • 15.7k
7 votes
Accepted

Selecting rows in attribute table by date (QGIS)

In your use case, QGIS considers it as a string. You need to convert it to datetime format. Use this: expression = "Date = to_datetime('2021-09-06 20:36:59', 'yyyy-MM-dd HH:mm:ss')" layer....
Kadir Şahbaz's user avatar
7 votes
Accepted

Wildly incorrect Lat/Long from UTM conversion

Those points are in EPSG:3857 (WGS 84 / Pseudo-Mercator) not EPSG:32617 (WGS 84/ UTM zone 17N). EPSG:3857 is the CRS of the Google satellite basemap. Lon, Lats are: X,Y,id, -82.7077395893134,41....
user2856's user avatar
  • 64.9k
7 votes

Is there a way to access named PostgreSQL connections via PyQGIS in order to access and load spatial layers?

Try this. I have a connection called localhost data so I search for the word data in all the PostgreSQL connections: from PyQt5.QtCore import QSettings qs = QSettings() L = [] for k in sorted(qs....
BERA's user avatar
  • 68.9k
7 votes
Accepted

How to use QPainter with QgsMapCanvasItem

For the sake of completeness, I add my comment as answer: The first argument of QgsMapCanvasItem::paint method is a QPainter instance which is already initialized to paint on the correct scene. ...
CodeBard's user avatar
  • 3,306
7 votes
Accepted

Export reports in QGIS using PyQGIS

Use QgsLayoutExporter.exportToPdf directly instead of making a QgsLayoutExporter object. layout_manager = QgsProject.instance().layoutManager() report = layout_manager.layoutByName("my_report&...
Kadir Şahbaz's user avatar
7 votes
Accepted

PyQGIS Contour Labelling

Perhaps you will have more success working directly with the QgsPalLayerSettings class. The snippet below works for me on a contour layer generated with gdal_contour. vlayer = iface.activeLayer() ...
Ben W's user avatar
  • 20.6k
7 votes
Accepted

Merging values from two fields into third field using PyQGIS

You can use or like this, i think it is called Ternary operators: layer = iface.activeLayer() p = layer.dataProvider() field_to_update = "field3" fieldindex = layer.fields().indexOf(...
BERA's user avatar
  • 68.9k
6 votes

Changing 0 to NULL in QGIS Attribute table

After selecting the layer, use this script in the QGIS's Python Editor (Ctrl+Alt+P): from qgis.utils import iface layer = iface.activeLayer() idx = range(layer.fields().count()) layer.startEditing()...
Kadir Şahbaz's user avatar
6 votes

Where can I find AniMove in QGIS 3.4?

For QGIS 3 The "Animove" plugin has been updated to QGIS 3, as can be seen on the plugin page: https://plugins.qgis.org/plugins/sextante_animove/
Kadir Şahbaz's user avatar
6 votes
Accepted

QGIS Expression Field Calculator Function Editor doesn't recognise Geometry methods

Seriously, I've had this problem all day, scouring google, 10 minutes after posting the question - of course, I stumble across the answer on the git forums. The reason why this doesn't work is ...
nr_aus's user avatar
  • 3,385
6 votes
Accepted

Creating event in PyQGIS that notices that group of layers is selected

You can use and modify this script: from qgis.utils import iface from qgis.core import QgsLayerTreeNode view = iface.layerTreeView() def eventoClickPanelCapas(): selected = view.selectedNodes() ...
Kadir Şahbaz's user avatar
6 votes

Show discrete values on legend from layer with Singleband Pseudocolor (PyQGIS 3.22)

You don't set minimum/maximum values. This is why you get the wrong legend. When using GUI, QGIS does it itself. min_ = stats.minimumValue max_ = stats.maximumValue .... renderer = ...
Kadir Şahbaz's user avatar
6 votes
Accepted

Set fixed reference scale for QGIS layer with PyQGIS?

You can set a reference scale by getting the renderer of the active layer. You don't need to make a new QgsFeatureRenderer. lyr = iface.activeLayer() r = lyr.renderer() r.setReferenceScale(400_000)
Kadir Şahbaz's user avatar
6 votes
Accepted

Is there a way to access named PostgreSQL connections via PyQGIS in order to access and load spatial layers?

Ah I think I actually figured it out from this page: Getting schema names from PostGIS database in PyQGIS For connections that exist, you can use: md = QgsProviderRegistry.instance().providerMetadata('...
mw2234's user avatar
  • 99
6 votes

Why Layer Combinations plugin is not available in QGIS?

This core functionality is called themes. You can save a theme and choose a theme from the theme selector icon above the layer tree. You can also have maps in print layouts that display the layers in ...
Houska's user avatar
  • 7,798
6 votes
Accepted

Function to calculate percentage of each feature over the sum that feature's field using PyQGIS

A couple of problems. Firstly, the aggregate method you are using is method of the QgsVectorLayer class, so you need to call it on a QgsVectorLayer object. Then, the second argument needs to be a ...
Ben W's user avatar
  • 20.6k
6 votes
Accepted

AttributeError: 'QgsNetworkAccessManager' object has no attribute 'setTimeout' when calling QGIS plugin

Your QGIS 3.4 is too old. That plugin will only work in QGIS >= 3.6. The error is: AttributeError: 'QgsNetworkAccessManager' object has no attribute 'setTimeout' The setTimeout method was added ...
user2856's user avatar
  • 64.9k
5 votes

Getting QGIS Project data from outside Python script

You have to load a project. QgsProject.instance() returns the empty project, because you didn't load any project. Add this line: project.read("c:/path/to/project.qgz") project = QgsProject....
Kadir Şahbaz's user avatar
5 votes
Accepted

Adding new field with unique ID for each feature without iterating over all features using PyQGIS

There are some redundancies in your code, and you are mixing layer editing methods with provider methods, which is not recommended. At the end of the day, I don't think you can avoid feature iteration ...
Ben W's user avatar
  • 20.6k
5 votes

How can I export a layer into .shp in qgis when it is not an option in the file types?

In the "Layers" window, left-side of the map view, you will see all layers in your map. Right-click on the layer you want to export to ESRI Shapefile. There is an option to Export the layer....
Keagan Allan's user avatar
  • 3,696
5 votes

Is there a way of preventing QGIS from becoming buggy during a long editing session?

While I do not recognize/experience the specific issues you are experiencing, some thoughts: Make sure you are running the latest version. At the time of your question (and this answer), the current ...
Houska's user avatar
  • 7,798
5 votes
Accepted

Code works in Python IDE but not in QGIS Python editor

You are converting your coordinates to "string" type: df['Easting'] = df['Easting'].astype("string") df['Northing'] = df['Northing'].astype("string") and then making a ...
Spacedman's user avatar
  • 62.7k
5 votes
Accepted

Calculating mean with group by based on another column in QGIS Field Calculator

Try the following expression: mean(expression:="temp", group_by:="month" || "city")
Taras's user avatar
  • 31.4k
5 votes

Calculating mean with group by based on another column in QGIS Field Calculator

Use this expression: array_mean (array_agg (temperature, group_by:=city || month))
Babel's user avatar
  • 67.4k

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