Questions tagged [pyqgis]

Python bindings for QGIS.

Filter by
Sorted by
Tagged with
0
votes
0answers
68 views

Convert .ui GUI file to python with QGIS Plugin Builder

I am using the QGIS plugin builder 3 with Qt Designer to create the GUI for my plugin. I've got a basic GUI done with QtDesigner and I need to dynamically populate a container in my GUI using python. ...
3
votes
1answer
43 views

Recreating multiple layers selection in QGIS Python plugin window

I want to have a list of filtered layers (like only raster or vector ones) the user can select from like we have in QgsProcessingParameterMultipleLayers yet without Processing algorithm script (in ...
2
votes
1answer
85 views

Clip geometry in PyQGIS by not using a layer as an input

I have two vector geometries, geom1 and geom2. They do not exist as individual layers, but rather are derived out of additional scripting in PyQGIS. I would like to clip geom1 with geom2. From what I ...
2
votes
1answer
51 views

How to add or select layer in QGIS plugin (like they do it in QgsProcessingAlgorithm)?

I want to have this add from the file or select layer in my plugin widget like: I think it can be done with some combination of QgsFileWidget and QgsMapLayerComboBox yet one just selects a file path ...
1
vote
0answers
46 views

Run a Python script exported from a QGIS model

I would like understand why a Python script exported from a QGIS model can be load from the ProcessToolBox, but not from the main Python button in QGIS. Indeed I would be able to edit and run it from ...
3
votes
1answer
70 views

Rename layer after gdal:rasterize

I rasterized a layer using gdal:rasterize like this: raster = processing.run( "gdal:rasterize", {...}, is_child_algorithm=True, context=context, feedback=feedback ) But ...
1
vote
1answer
32 views

Creating QgsFeatureSink without using parameterAsSink

I know I can create a (QgsFeatureSink, string) tuple by calling (sink, dest_id) = self.parameterAsSink(...) However that requires me to pass an output layer as a parameter: self.addParameter( ...
7
votes
1answer
102 views

Getting QgsVectorLayer from QgsFeatureSink

I have created a feature sink like so: (sink, dest_id) = self.parameterAsSink(...) And added a number of features to it. When I'm done processing, I want to post-process this layer using gdal:...
0
votes
1answer
40 views

Exporting .qml file using QGIS API with specific categories

I need to save a style file (.qml) that will only contain the "Symbology", "Labeling", "Forms" categories. With PyQGIS I can save a named style like this: lyr = iface....
3
votes
1answer
55 views

Allow multiple inputs from one inputbox in QGIS Processing Script

I am trying to build a processing script using "Create new script from template". I want the user to have the opportunity to insert multiple values into one field. def initAlgorithm(): ...
4
votes
0answers
44 views

Prevent PyQGIS from converting EPSG code to OGC URN

I'm trying to access the Sentinel Hub's WFS via PyQGIS. Here's my code: base_url = 'https://services.sentinel-hub.com/ogc/wfs/<my_service_endpoint>?' params = 'SRSNAME=EPSG:4326&TYPENAMES=...
2
votes
1answer
40 views

GDAL - csv to TIFF, struggling with gdal.Rasterize

I am looking to interpolate some dataset via IDW with gdal. I am first trying to create a tiff out of my data to then be able to interpolate it. My code is returning no error however the file created ...
3
votes
1answer
41 views

Only list visible fields

I can list all fields with lyr = iface.activeLayer() fieldlist = [f for f in lyr.fields()] Are there any method to list only the fields which i checked in "Organize columns". Something like:...
2
votes
3answers
103 views

Get nearest neighbour on same or another layer to connect points by drawin a line in QGIS

In my project I try to find the shortest way from a point to a specific line (street). Fortunately, this is no big deal in QGIS 3.0 using "Points along lines" and "Distance to Nearest ...
1
vote
0answers
35 views

How to obtain output in a scratch memory layer when using processing.run() in qgis plugins

I am writing a python plugin for QGIS and I need to use an inbuilt tool "Distance to nearest hub (line to hub)" in the QGIS processing toolbox. The following line is supposed to give me the ...
0
votes
1answer
74 views

Loading and writing CSV file in QTableView with Python 3.10 for QGIS Plugin

I know there are a lot of similar questions... but I can't implement them. How can I load a CSV file and write that into a QTableView? My first step was to browse it. def select_input_file(self): ...
1
vote
1answer
46 views

Passing selected layer-feature inside QgisAlgorithm (in code)

In our algorithm, we do three steps: call Algorithm A AlgoA, takeResultLayer into lyr from AlgoA output, for each feature of lyr we want to run Algorithm B AlgoB . Code of my processAlgorithm looks ...
0
votes
0answers
30 views

Grouping Polygons By Population Using Graph Network Analysis

I have tried to implement the solution that FelixIP provided as an answer to this question. What I am trying to achieve is this. I am using QGIS and have attempted to create a processing script that ...
0
votes
1answer
149 views

Facilitated menu setup in QGIS

I want to have it so that when desktop QGIS icon is selected, to open QGIS, that I can based on script running after that select different project set-up when it comes to menu and Tabs set-up. Less or ...
0
votes
1answer
35 views

Multiplying user's input values from column \"Shape_Leng\" using field calculator in PyQGIS for a QGIS Plugin?

I have trouble to calculate a new column as an output based on a simple calculation of user's various input multiply it with the length column of a polyline vector inputs. I have created a plugin but ...
3
votes
1answer
44 views

Removing table join using PyQGIS

I joined a table to a layer in QGIS using code from this thread: Joining layers with PyQGIS 3. Now I would like to remove the join. How can I do it?
1
vote
1answer
44 views

Change resolution when exporting map canvas as image with python

I want to create a georeference png of the map canvas in QGIS using Python with: mapCanvas().saveAsImage('filename.png') I want to be able to change the resolution from the default of 96 dpi. Is ...
1
vote
1answer
50 views

Updating layer attribute in QGIS processing script

In a QGIS processing script I try to update an attribute value in a feature with changeAttributeValue but I am not able to. I'm following: https://courses.spatialthoughts.com/pyqgis-in-a-day.html#...
1
vote
1answer
54 views

Define the extent of QgsRasterCalculator

I have several raster files (.tif) in a folder that have different resolution and extent but are all in the same study area. Before merging them, I want to bring them to the same resolution (5 m) and ...
3
votes
1answer
68 views

Is it possible to define a projection for QgsPointXY()

I am creating a QgsRectangle like this: llc = QgsPointXY(319660,4624088) # lower left corner in UTM urc = QgsPointXY(396425,4742843) # upper right corner in UTM aoi = QgsRectangle(llc,urc) ...
1
vote
1answer
51 views

Adding new attribute to QgsFeature object

I have tried reading the docs located here but there is no mention of any method that allows you to add new attributes to the feature only modify existing ones. Something along the lines of: ...
0
votes
0answers
41 views

Joining Layers with PyQGIS [duplicate]

I need to make a LEFT JOIN (using a Python QGIS script) between two layers: a shape (point): s_node a csv (table): t_infos Can you show me how to do that? The result (the joined layers) has to be in ...
3
votes
1answer
43 views

Appending Rasterlayer as SUBDATASET into existing geopackage using PyQGIS

I struggle with appending a RASTER Subdataset to an existing Geopackage using Python code. Meanwhile I can save one Raster to a new Geopackage but that's not my plan. from qgis import processing ...
2
votes
1answer
96 views

Creating empty layers in a GeoPackage using PyQGIS

I want to create empty geopackage layers with a defined schema. This seems to just work with Fiona regardless of whether I have spatial or non-spatial layers: def create_blank_gpkg_layer( gpkg: ...
0
votes
0answers
27 views

GPX track appears scaled down and in center of world using standalone PyQGIS

I'm trying to load a GPX-track as a layer in PyQGIS (standalone) but the track appears entirely on the wrong scale and position (scaled down in the middle of the equator). When I load the file ...
1
vote
0answers
31 views

Problems registering events in a geometry generator

I have a problem with a geometry generator I wrote to display lines in a specific manner. The code is similar to this simplified example: # This contains geometries in a simili-layer line_cache = {} ...
4
votes
1answer
49 views

Getting vector layer from layers within group using PyQGIS

It's not difficult to get a list of layers within a group in the Layers panel of a QGIS 3.x project: root = QgsProject.instance().layerTreeRoot() groupName = "GROUP_name" group = root....
0
votes
1answer
33 views

How to check if a QgsGeometry is polygonal? [duplicate]

In my PyQGIS code I have a QgsGeometry. It could be of any geometry type, maybe it is a Point, maybe a Polygon, maybe a MultiPolygon25D. How can I check if it is any kind of polygonal geometry without ...
0
votes
0answers
38 views

PyQGIS raster calculator code not working

I'm trying to write the raster calculator as part of a Python script for use with my other code. The below code runs with no errors but no output file is put in the directory. Can anyone advise me ...
0
votes
0answers
18 views

Why am I getting "Segmentation fault (core dumped) error"? [duplicate]

Any ideas why my code is producing an error here? I am running Ubuntu which may have something to do with it. The code is very simple and the problem only occurs when I try to load a vector layer. ...
0
votes
0answers
16 views

Use QGIS Align Rasters Function in standalone Python script

I have been using QGIS align rasters function within the QGIS window to resample my rasters to the same offset. However, I would now like to move this to a standalone python script. Is there a way to ...
3
votes
1answer
116 views

Custom Python Expression function defined as macro

Is it possible to define a custom Python Expression function like in the project macros? Each time when I try to enable the macros and write any code, I get a security warning saying that Python ...
4
votes
1answer
65 views

Preserve `QgsLayerTreeViewIndicator` on moving node in the layer tree

I am using the script below to display an icon next to my layer in the layer tree view, which works fine. However, when a user moves the position of that node, the indicator disappears. layer1 = ...
3
votes
0answers
47 views

Defining a custom Python Expression function with default parameters

Is it possible to use a variable with default value (value2) defined in a custom Python expression function? @qgsfunction(args='auto', group='UserFunction') def my_test(value1, feature, parent, value2=...
0
votes
0answers
22 views

processing.run('qgis:linestopolygons', ...) does not work properly

I am writing a QGIS plugin and for part of the process, I want to transform a Line layer (connected lines) into a Polygon layer. For this, I am using processing.run('qgis:linestopolygons') command. ...
4
votes
2answers
105 views

Multiple extract by attribute using PyQGIS

I am trying to extract only three polygons from my attribute table from a vector layer. The column which contains the values I want is called "ID". import processing layer = r'C:\mypath....\...
1
vote
0answers
28 views

Using Temporary Layer/not loaded Layer in Field Calculator

I'm writing a processing script with the given tools from the toolbox. Now I want to use the Field Calculator to add a column to an output layer of a tool. The Field Calculator/column should contain a ...
4
votes
1answer
65 views

Capturing CTRL+Key In KeyPressEvent in PyQGIS QgsMaptool

I created a Line with help of QgsMapTool and I'm able to capture the single key in keyPressEvent like this: class buildingEditTool(QgsMapToolEdit): def keyPressEvent(self, event): ...
3
votes
1answer
67 views

PyQGIS - create a centroid point symbol layer via Geometry Generator on a polygon layer

I'm trying to created a Geometry Generator symbol layer (point) that creates centroids from myLayer, a polygon map layer. Sounds easy, right? Here is my code - I think I'm missing a line to declare ...
0
votes
0answers
18 views

Access QGIS Server Properties via Plugin

For a QGIS Plugin, the User has to provide some input regarding QGIS Server (WMS capabilities) and I dont want to mirror the input options in the plugin. Can i jump from the plugin directly into the ...
4
votes
1answer
43 views

Capturing Layer Undo button or Ctrl+Z in PyQGIS

I added some features in a layer. If I try to undo features from QGIS menu or Ctrl+Z can I capture that trigger? I need like this: def fun(): print('UNDO') iface.undo.triggered.connect(fun)
5
votes
1answer
90 views

Adding Yandex Satellite TMS via QGIS Python console

I add a TileMapService layer to QGIS map window using a Python console. But added layer is not displayed. What is wrong? tmsLayer_name = 'YandexSat' uri = "url=http://sat04.maps.yandex.net/tiles?...
0
votes
1answer
45 views

Determine if a point lies within a polygon/shape in QGIS

I am working with a dataset that records violence cases (CSV file). All violence cases are geocoded to a latitude/longitude address so that they can be coordinated to any given shapes/polygons. What ...
0
votes
0answers
25 views

Why precision is reduced when I print the QgsGeometry points?

I was trying to extract latitudes and longitudes from QgsGeomtery. But I noticed that no all digits we being printed. Can anybody identify why and how can I resolve this problem? For example is this ...
3
votes
2answers
167 views

Populating line attribute fields with attributes from point features snapped to their start_point or end_point

I have a line layer (MultiLineString) and 4 point layers (MultiPoint). My line layer has 2 attribute fields, start_pt and end_pt, I need them populated with an identifier attribute (string data ...