New answers tagged

1 vote

Polygonize error

I could re-produce the error with QGIS 3.35, installed with OSGeo4W. The command log shows that with the default setting QGIS tries to save the polygonozed result into an Interlis 2 file. This format ...
user30184's user avatar
  • 64.6k
1 vote

Calculating width and height given image resolution using PyQGIS

With PyQGIS you can do: rasterlayer = QgsProject.instance().mapLayersByName("nh_68_5")[0] x_pixel_size = rasterlayer.rasterUnitsPerPixelX() y_pixel_size = rasterlayer.rasterUnitsPerPixelY()...
BERA's user avatar
  • 71.2k
3 votes
Accepted

Iterating over the features of a vector layor then performing zonal statistics algorithm on each feature in PyQGIS

You can make use of the QgsFeatureSource.materialize() method. from qgis.analysis import QgsZonalStatistics from qgis.core import QgsRasterLayer, QgsVectorLayer v_layer = QgsVectorLayer("...
Matt's user avatar
  • 16.2k
2 votes
Accepted

Datetime from attributetable to layername in QGIS with Python

You need to access the feature's attribute for the "datetime" field, not the layer's fields directly. You need to make sure that the "datetime" field is available in the layer. for ...
Nil's user avatar
  • 1,492
1 vote
Accepted

Depth Band "> 1.20" isn't being extracted, could the > be affecting the extract?

You can't have a filename on Windows with a > in it. You could do something simple like change: band.replace(' ', '_') to band.replace(' ', '_').replace('>','_') and that would be sufficient to ...
Tom Brennan's user avatar
  • 4,407
0 votes

Centering layer on QGIS map using Python

I agree with the comments that your question lacks some detail, so this is essentially a guess. p = QgsProject.instance() m = p.layoutManager() layout = m.layoutByName('Layout 1') municipios = ...
Matt's user avatar
  • 16.2k
1 vote
Accepted

Resample Raster in PyQGIS with Median Approach

As said by @user2865 the "grass7:r.resamp.stats" module will resample the raster to the desired resolution. processing.run("grass7:r.resamp.stats", {'input':'/path/to/...
eagleadmiral's user avatar
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
  • 32.7k
1 vote

QGIS 3.6 and QGIS 3.10 report different geometry types for the same shapefile

Shapefile does not distinct LineStrings and MultiLineStrings. The corresponding shapefile type is PolyLine https://www.esri.com/content/dam/esrisites/sitecore-archive/Files/Pdfs/library/whitepapers/...
user30184's user avatar
  • 64.6k
5 votes
Accepted

Cloning the map layer using PyQGIS

There are several suggestions regarding your code: adding the same layer l_out one more time to the project, even with the addToLegend = False does not make much sense to me. In the documentation for ...
Taras's user avatar
  • 32.7k
1 vote

Saving selection as new layer after using array for filter expression in PyQGIS

Extending great answers from @Matt, @OP himself, and also solutions mentioned in these threads: Creating temporary layer out of selection in QGIS and Duplicating layer in memory using PyQGIS. I would ...
Taras's user avatar
  • 32.7k
2 votes

Saving selection as new layer after using array for filter expression in PyQGIS

You can simplify your solution by doing away with the loop altogether. layer = iface.activeLayer() array = [35007, 93013] # check ZIP_CODE against multiple values by using the `in` operator # an `f ...
Matt's user avatar
  • 16.2k
3 votes

Saving selection as new layer after using array for filter expression in PyQGIS

I figured most of it out by changing the for loop to collect those selected features and save it to an in-memory layer as below: for i in array: expression = ('ZIP_CODE = ' + str(i)) selection ...
Simon Palmer's user avatar
2 votes
Accepted

How to Delete Output of the Processing Tool in PyQGIS

Sadly, you can't. QGIS does not release the file handlers of those files while the Processing tool is running. https://github.com/qgis/QGIS/issues/33641#issuecomment-790622961 suggests a workaround (...
bugmenot123's user avatar
  • 11.3k
1 vote
Accepted

Changing attributes of a newly created feature in Python form init code

A minimal solution, based on @Matt's great answer: from functools import partial from qgis.PyQt.QtWidgets import QPushButton, QSpinBox def on_ok(layer, feature, spinbox): value = spinbox.value() ...
bugmenot123's user avatar
  • 11.3k
1 vote
Accepted

PyQGIS print map with different aspect ratios

You must get the page project = QgsProject.instance() layout = project.layoutManager().layoutByName('Test') map_item = layout.itemById('Map 1') page = NULL for i,x in enumerate(layout.items()): ...
Xeppit's user avatar
  • 1,255
1 vote
Accepted

Incorrect output when smoothing line that intersects multiline with PyQGIS

There are several suggestions regarding your code: Break your '9 boundary 3 collected' lines with the "Multipart to singleparts": bound_layer = QgsProject.instance().mapLayersByName("...
Taras's user avatar
  • 32.7k
0 votes

PyQGIS layer action: Select features within buffer having the same field value

With the help of this answer, I finally found a solution: from qgis.utils import iface layer_id = '[%@layer_id%]' feature_id = [%$id%] feature_name = '[%name%]' field_name = 'iso_a2' ...
ebcs's user avatar
  • 49
3 votes
Accepted

How to update QGIS display from script run in console

This can be achieved by using a QTimer instead of time.sleep. layer = iface.activeLayer() # instantiate a Qtimer() object and set its interval timer = QTimer() timer.setInterval(300) # milliseconds #...
Matt's user avatar
  • 16.2k
0 votes
Accepted

Error when adding new feature

Finally I could find the solution. The problem lies in the creation of the geometry column, which need to be the following: table_name = 'street' cursor_info.execute(f"CREATE TABLE {table_name} (...
Dennis's user avatar
  • 35
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,693
0 votes

Reclassify by Table using PyQGIS

After looking at the command when running the GUI, I identified that it expects the 'TABLE' to be a list of characters, for example: processing.run("native:reclassifybytable", { 'DATA_TYPE' :...
mazucci's user avatar
  • 95
4 votes
Accepted

Exploding lines with PyQGIS

Let's assume there is a polyline layer called 'to_explode' (three features) with its attribute tables, see the image below. Proceed with Plugins > Python Console > Show Editor and paste the ...
Taras's user avatar
  • 32.7k
0 votes

Python from pathlib import Path crash into QGIS

I partially solve the issue using this: import ntpath and Work fine. Someone have a similar experience about?
gianfranco di pietro's user avatar
0 votes
Accepted

PyQGIS: Transform Lat-Long CSV into a UTM project

Reprojecting your delimited text layer is not the solution to your problem. You need to specify the correct CRS inside the uri when loading the csv file via pyqgis e.g. &crs=EPSG:4326. A good tip ...
Ben W's user avatar
  • 21.2k
2 votes
Accepted

Adding geometry from first row of attribute table to all other rows of attribute table of layer using PyQGIS

Try this. Backup first, it will modify the layer in place. layer = QgsProject.instance().mapLayersByName("thelayer")[0] pr = layer.dataProvider() #Fetch the geometry of the first feature ...
BERA's user avatar
  • 71.2k
2 votes

How to hide or remove the subgroup labels on a QgsLayoutItemLegend with PyQGIS?

The method you are looking for is QgsLegendRenderer.setNodeLegendStyle(). The first argument is a QgsLayerTreeNode object and the second is a QgsLegendStyle.Style Enum object, e.g. QgsLegendStyle....
Ben W's user avatar
  • 21.2k
0 votes

Reprojecting to EPSG:25832 using PyQGIS shifts layer

This appears to be accurate. import qgis import processing import os from processing.core.Processing import Processing Processing.initialize() wkt = 'PROJCS["ETRS_1989_UTM_Zone_32N",GEOGCS[&...
Cary H's user avatar
  • 924
1 vote

Reprojecting to EPSG:25832 using PyQGIS shifts layer

This works in QGIS layer = iface.activeLayer() source_crs = QgsCoordinateReferenceSystem(layer.crs().authid()) custom_wkt = 'PROJCS["ETRS_1989_UTM_Zone_32N",GEOGCS["GCS_ETRS_1989",...
Cary H's user avatar
  • 924
0 votes

Saving GeoPackage with style in PyQGIS

This is adapted from @CaptainDang's plugin and allows for you to save each layer to the single geopackage (qgis v3.22): Usage example: #overwrite any existing geopackages as this is the first layer ...
LeasMaps's user avatar
  • 735

Top 50 recent answers are included