Hot answers tagged

9 votes
Accepted

Use PyQGIS to save memory layer to GeoPackage that does not already exist

You can use the following lines in your PyQGIS script to make an empty GeoPackage. from osgeo import ogr gpkg_path = "c:/test/test.gpkg" ds = ogr.GetDriverByName('GPKG').CreateDataSource(...
Kadir Şahbaz's user avatar
8 votes
Accepted

Adding simple Geomasking (Donut masking) feature using PyQGIS

Preamble: Unfortunatelly I did not read your articles, but I hope I understood your issue correctly. Let's assume there is an input point layer called 'points2', see the image below. The task is the ...
Taras's user avatar
  • 31.3k
7 votes
Accepted

Where is the source code for the Processing Plugin "Buffer"?

The buffer algorithm is implemented in C++: https://github.com/qgis/QGIS/blob/fbc2082b7e25042c5f82cb000c52d84625b31624/src/analysis/processing/qgsalgorithmbuffer.cpp The Buffer.py file you found is ...
bugmenot123's user avatar
  • 10.7k
7 votes
Accepted

Saving and applying layout of columns in QGIS Attribute Table

I can suggest a PyQGIS approach to (1) adjust, (2) export, (3) downgrade and (4) finally import configurations applied to the attribute table of a layer. It is mainly based on the ...
Taras's user avatar
  • 31.3k
7 votes

Saving and applying layout of columns in QGIS Attribute Table

You export the layer as a layer definition file and then just add this file to QGIS to load the layer. For more control and in-place replacement, you can export the table component of the layer style. ...
JGH's user avatar
  • 40.7k
7 votes

Determining on which side of line points are located using QGIS

In PyQGIS 3 there is a handy method available, namely segmentSide() from the QgsGeometryUtils class. Let's assume there are two layers: a line layer with a single feature, and a point layer with nine ...
Taras's user avatar
  • 31.3k
7 votes
Accepted

Adding custom panel to QGIS

Your question is quite broad but here is a short example to get you started. It will make a QgsFileWidget (a native widget for selecting directories/files) and place it in a dock widget on the right ...
Matt's user avatar
  • 14.7k
6 votes

Determining on which side of line points are located using QGIS

You can also use QGIS expressions for this, using this expression to get a 0 or 1 output for each point, depending on which side of the line it is (see below for curved lines). The solution is based ...
Babel's user avatar
  • 67.2k
6 votes

Use PyQGIS to save memory layer to GeoPackage that does not already exist

Just to round out the list of alternatives, the first script can be made to work for a new file by changing the option for gdal:convertformat (wrapper for ogr2ogr) from -update to -overwrite - ...
Tom Brennan's user avatar
  • 3,705
6 votes
Accepted

Editing width of drop down field list in the QGIS Attribute Table

There is a PyQGIS solution I can suggest. Honestly, it was tricky, and not straightforward. The thing you are looking for is the QFrame hidden under AttributeTable / mUpdateExpressionBox / mFieldCombo....
Taras's user avatar
  • 31.3k
5 votes
Accepted

What is QDomNode and QDomDocument in terms of PyQGIS?

Here is what QDomNode and QDomDocument are in the Qt context: The QDomDocumentclass represents the entire XML document. Conceptually, it is the root of the document tree, and provides the primary ...
Kadir Şahbaz's user avatar
5 votes

Use PyQGIS to save memory layer to GeoPackage that does not already exist

The second script works if you change the flag to the correct one which is QgsVectorFileWriter.CreateOrOverwriteFile: # reference to memory layer layer = iface.activeLayer() # save location ...
Louis Cottereau's user avatar
4 votes

Changing Widget type to 'Unique Values' and making it editable using PyQGIS

Try this: layer = iface.activeLayer() # start with a list of your fields to edit mt20_fields = ['POINT_ADDR', 'STEET_ROAD', 'LANDMARK'] # fields must be iterated through: for field in mt20_fields: ...
Andre Geo's user avatar
  • 137
4 votes
Accepted

Calling Turf.js nearest point function equivalent in QGIS using a Python script

You can use the QgsSpatialIndex class which has a nearestNeighbor() method. E.g. to iterate over a point layer, finding the nearest point and distance for each feature, you could do something like: ...
Ben W's user avatar
  • 20.6k
4 votes

Problem to interface a script with python in QGIS

Add import processing to import section to get rid of NameError. Since you use self.parameterAsSource, you get NoneType object .. error. Instead, use self.parameterAsRasterLayer. Use the script below....
Kadir Şahbaz's user avatar
4 votes

What is QDomNode and QDomDocument in terms of PyQGIS?

After certain trials and errors, I was able to perform exporting/importing configurations of the layer's attribute table, see Saving and applying layout of columns in QGIS Attribute Table. Despite a ...
Taras's user avatar
  • 31.3k
4 votes

Editing width of drop down field list in the QGIS Attribute Table

To complement @Taras's answer with a simpler python code (changed with edit): # imports from qgis.utils import iface from qgis.core import QgsProject from PyQt5.QtWidgets import QApplication, QFrame #...
Louis Cottereau's user avatar
3 votes
Accepted

Interpolating polygon values based on neighbouring polygons in QGIS

You dont specify how you want to interpolate. This buffers the grey polygons and use the surrounding polygons values to calculate the average, with the intersection areas as weights. So a polygon with ...
BERA's user avatar
  • 68.8k
3 votes
Accepted

QGIS Action to "enable" (display) a raster layer that is already loaded

You can use the following code in your Python action script. from qgis.utils import iface project = QgsProject.instance() layer_id = '[%@layer_id%]' fid = [%$id%] layer_obj = QgsProject().instance()....
Ben W's user avatar
  • 20.6k
3 votes

_core.QgsProcessingException: Unable to execute algorithm Incorrect parameter value for TARGET_LAYER

I solved my own issue passing target_layer as an URI layer doing: # Define the connection parameters using QgsDataSourceUri uri = QgsDataSourceUri() uri.setConnection(host, port, dbname, user, ...
marc's user avatar
  • 51
3 votes
Accepted

"Incorrect parameter value for LAYERS" error when saving CSV to GeoPackage with PyQGIS

You can make a QgsVectorLayer of your CSV URI which will be accepted by the package algorithm. A couple of things to note: The %dir suffix is missing from your CSV URI in the LAYERS property of the ...
Matt's user avatar
  • 14.7k
3 votes
Accepted

Using label placement via PyQGIS

The QgsVectorLayer class has no method like placement. However, the QgsPalLayerSettings does. I suppose you are trying to apply this LabelPlacement mode. Just add this settings.placement = Qgis....
Taras's user avatar
  • 31.3k
3 votes
Accepted

Refresh one layer style

Probably the call you are missing is layer.triggerRepaint() Try: style_path = r'Path\to\style_file.qml' lyr = iface.activeLayer() lyr.loadNamedStyle(style_path) lyr.triggerRepaint() # update ...
Ben W's user avatar
  • 20.6k
3 votes

Loading multiband raster layer with pseudocolor style (not RGB) using PyQGIS

This is a pretty good tutorial on how to do what you want. edit: this is also how the pyqgis documentation says it should be done documentation raster_file_path = "path/to/raster.ext" ...
Louis Cottereau's user avatar
2 votes

Prevent custom Processing Algorithm from returning output on error

You could use https://qgis.org/pyqgis/3.28/core/QgsProcessingException.html : from qgis.core import QgsProcessingException [...] raise QgsProcessingException("Your message here")
pathmapper's user avatar
  • 1,935
2 votes
Accepted

Setting path for QgsLayoutItemPicture using PyQGIS

You can use QgsLayoutItemPicture.setMode() (see here for available formats): project = QgsProject.instance() manager = project.layoutManager() layout = QgsPrintLayout(project) layout....
Matt's user avatar
  • 14.7k
1 vote

Setting dropdown on feature attribute form using plugin

You can set the value map on an existing field like so: fields = layer.fields() idx = fields.indexFromName(field_name) field_values = ['Dewasa', 'Kanak-kanak'] # use dictionary comprehension to make ...
Matt's user avatar
  • 14.7k
1 vote
Accepted

Run processing tools in Python console

In the params you have the output 'output', which is just a string. But you need to use the variable output Then replace processing.run(algo, params) with the following: processing.runAndLoadResults(...
fsg's user avatar
  • 652
1 vote

Loading multiband raster layer with pseudocolor style (not RGB) using PyQGIS

I found a solution to switch the renderer to a QgsSingleBandPseudoColorRenderer in a single line with default values by using the class widget QgsSingleBandPseudoColorRendererWidget: raster_layer = ...
Louis Cottereau's user avatar
1 vote

Understanding the QGIS API documentation based on the featureCount() method with legendKey parameter

I use QGIS 3.28 so I don't have the legendKeys() method available. I've used legendSymbolItems() instead. And it turned out I don't need countSymbolFeatures() anyway. QGIS API docs cheated me. As I ...
Maryla's user avatar
  • 148

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