Skip to main content
11 votes
Accepted

Translating raster from floating point to int16 increases size

Your 5 Mb inputs are compressed and your outputs are not. Your 40 Mb inputs may be compressed, but didn't compress well (very noisy data leads to poor compression, a random raster can actually ...
user2856's user avatar
  • 67.1k
10 votes
Accepted

Hiding/showing layers in legend does not work in QGIS standalone

QgsLayerTreeMapCanvasBridge must be in global scope. But you define bridge in add_legend method's scope. Therefore, variable bridge is gone after add_legend finished. One of the solutions: Add bridge ...
Kadir Şahbaz's user avatar
10 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
9 votes

PyQt signal when layer name is changed in PyQGIS

Use the nameChanged signal of QgsLayerTreeNode class. from qgis.core import QgsProject def name_changed(node, name): print(f"New Name: {name}") QgsProject.instance().layerTreeRoot()...
Kadir Şahbaz's user avatar
8 votes

Determining on which side of line points are located using QGIS

You can also use QGIS expressions for this. See below for the 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 ...
Babel's user avatar
  • 73.1k
8 votes
Accepted

Was PyQGIS released with the first QGIS version?

No, the Python API was introduced earlier. The first stable release with QGIS 1.0 in 2009. At 1.0 we provide a stable API from which you can develop custom solutions in Python or C++. See: https://...
MrXsquared's user avatar
  • 34.9k
8 votes

Deleting shapefile after removing it with removeMapLayer using PyQGIS

Even if you keep in mind several things regarding your code: work with a real path to the shapefile instead of "XXXX/input1.shp" i.e. apply either source_layer.source() or source_layer....
Taras's user avatar
  • 33.3k
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
  • 33.3k
8 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
  • 33.3k
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
  • 33.3k
7 votes
Accepted

Editing the coding of QGIS plugin

Navigate to Plugin folder (Settings > User Profiles > Open Active Profile Folder). Go to python/plugins/ImportPhotos folder. Open ImportPhotos.py file. Find rel_path = self.selected_folder + os....
Kadir Şahbaz's user avatar
7 votes
Accepted

Setting QgsVectorLayer created from CSV in editing mode with PyQGIS

A delimited text layer is not editable in QGIS by default. You have to export it as a spatial format like GeoPackage, ShapeFile etc. You can check what capabilities a layer has using the ...
Kadir Şahbaz's user avatar
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.9k
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
  • 42.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
  • 17.4k
7 votes

Looping every features pair with PyQGIS

You can use zip and indexing: layer = iface.activeLayer() featurelist = [f for f in layer.getFeatures()] #List all features for featurepair in zip(featurelist[::2], featurelist[1::2]): f1, f2 = ...
Bera's user avatar
  • 74.7k
7 votes
Accepted

Using QgsProcessingParameterEnum in QGIS Python processing script

Set usesStaticStrings=True when you create the parameter: class ExampleProcessingAlgorithm(QgsProcessingAlgorithm): # etc... def initAlgorithm(self, config=None): self.addParameter( ...
user2856's user avatar
  • 67.1k
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
  • 67.1k
6 votes

Unexpected behavior when creating Virtual layer using PyQGIS

You need to remove the single quotes, or use double quotes for field name instead. "?query=select Subtype, sum(Length) from 'roads' group by Subtype" or "?query=select \"Subtype\&...
Kadir Şahbaz's user avatar
6 votes
Accepted

Getting only joined fields using PyQGIS

One can detect which fields have been joined by means of the QgsFields class and its method fieldOrigin: Returns the field's origin (value from an enumeration). from qgis.core import QgsProject ...
Taras's user avatar
  • 33.3k
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
  • 33.3k
6 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
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
  • 5,249
6 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
6 votes
Accepted

Showing feature count in layer name in QGIS

Here is a PyQGIS solution. QgsVectorLayer inherits a special method called setName() from the QgsMapLayer class. Set the display name of the layer. If it is a one-time action For a single layer : # ...
Taras's user avatar
  • 33.3k
6 votes
Accepted

TypeError when converting QgsMultiLineString to QgsGeometry using PyQGIS

QgsMultiLineString is a child class of QgsAbstractGeometry which can be used to create a QgsGeometry using the following constructor QgsGeometry(QgsAbstractGeometry geom) This means that you can ...
Kalak's user avatar
  • 3,880
6 votes

"Could not load source layer for INPUT" error when reprojecting flt-file with PyQGIS

I would agree with @BenW's comment, that your binary floating-point files can not be found under this path inputfolderpath + '/' + fltfile_name. However, I could also say, that your input does not ...
Taras's user avatar
  • 33.3k
5 votes

Setting QGIS to "show feature count" always when layer is loaded?

For any future person reading this thread, there is now an option to show feature count by default when new layer is added. Go to Settings -> Options -> Canvas & Legend -> tick the box ...
Pawel's user avatar
  • 408
5 votes
Accepted

AttributeError: 'QgsFieldComboBox' object has no attribute 'setPlaceholderText'

The QGIS widget classes do inherit from the standard Qt classes most of the time. In this case QgsFieldComboBox inherits QComboBox, but the issue most likely is that the setPlaceHolderText method was ...
CodeBard's user avatar
  • 3,571
5 votes

NameError: name '__file__' is not defined when getting the path of the current script using PyQGIS

That may be a valid pyqgis script, but it's not a valid processing script. You should run it from the console script editor, not the processing script editor. If your script didn't raise that error (...
user2856's user avatar
  • 67.1k

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