Questions tagged [pyqgis]

Python bindings for QGIS.

Filter by
Sorted by
Tagged with
1 vote
1 answer
57 views

Storing Map Canvas (current view) so it may be recalled and restored later using QGIS

Is there any way in QGIS3 to save "settings" for the current map canvas/view so the very same view can be restored later?
  • 231
1 vote
0 answers
32 views

Python macros does not work properly in QGIS [duplicate]

I've found many similar problems in gis.stackexchange, but no solutions worked for me. I want to execute a python macros with QGIS 3.22.14 and python39 installed via OSGEO4W and windows 11 home. The ...
  • 165
4 votes
1 answer
121 views

Python macros not working in QGIS

I want to execute a Python macro with QGIS 3.22.14 and python39 installed via OSGEO4W and windows 11 home. The purpose is to run a script automatically when I open a project in QGIS.The problem with ...
  • 165
3 votes
1 answer
96 views

Add temporary vector layer with specific attributes from another layer to QGIS plugin

I am creating a plugin that generates virtual layers from other existing layers. I tried to make the process easier for me to make attribute tables by sql query but it doesn't work because layer ...
2 votes
2 answers
175 views

Counting seconds using QTimer in PyQGIS [closed]

I am having a hard time counting seconds in PyQGIS using QTimer. The current code waits for 1 second and prints 5 times "1" as output. I desire that after every second the output should be ...
4 votes
1 answer
110 views

Calculate the angle between line segments in a multilinestring or linestring in QGIS

I need to calculate the angle between line segments in a long chain of linestring as in the figure below: The way I approached this is: I assigned the id to the feature using:$id Now each feature ...
1 vote
2 answers
126 views

How to add delay and not cause the program to lag. I currently tried using time.sleep but it causes program to crash

I'm new to PyQGIS. I want to create points and update the location after 5 seconds and the code for that is given below. The issue I'm having is that during 5 seconds the script becomes unresponsive ...
0 votes
1 answer
99 views

Unable to run standalone PyQGIS script with launch.bat file

https://www.qgistutorials.com/ru/docs/running_qgis_jobs.html I want to run a script in a file called my_plugin.py. The script is: print ('Hello QGIS!') The path for the script is: C:\OSGeo4W\apps\...
  • 165
5 votes
1 answer
112 views

Selecting rows in attribute table by date (QGIS)

I want to select rows in an attribute table by date using PYQGIS. The column is called “Date.” This is my script: import qgis.utils from qgis.utils import iface layer = iface.activeLayer() layer....
  • 95
3 votes
2 answers
40 views

Generating an atlas, setHideCoverage doesn't work

I'm trying to print my layout. Sadly, I cannot figure out how to hide the coverage layer. The setHideCoverage(True) - line doesn't change my output My code so far: project = QgsProject.instance() ...
3 votes
1 answer
69 views

Removing legend items without specifying the layer group with PyQGIS

How can I remove multiple layers from legend in the MapComposer without removing them from the canvas and without having to specified their group ? In the code below I have to add group = root_group....
  • 311
0 votes
2 answers
44 views

PyQGIS changing the scale

I am building a script where I need to set my scale of my added vector layer. I get no errors with my script as written but the scale does not change. FYI - I run this script through the plugin ...
  • 183
4 votes
1 answer
49 views

Properly updating the QGIS default style database from Python

Let's say I copied an existing color ramp from my QGIS profile's default style database, inverted it and saved it under a new name in the database: >>> default_style = QgsStyle.defaultStyle() ...
  • 9,848
0 votes
1 answer
46 views

Creating polygon via a PyQGIS script? [closed]

I am totally new to PyQGIS. On my computer I have QGIS 3.X installed, from what I understand, Python 3 can already be used to use PyQGIS. It is my need to create a script to georeference a polygon ...
3 votes
1 answer
36 views

Save Vector layer on disk with help of PyQGIS

I have a vector layer, which I ma adding it to a subgroup "streets_greoup". My code also save it to the disk. But it still show temporary layer when I close my project and saved feature data ...
  • 55
5 votes
0 answers
39 views

Fully updating QGIS' GUI after changing a layer style via Python

Let's say I changed a layer's style via PyQGIS: # load "world" easter egg layer and then: layer = iface.activeLayer() layer.renderer().symbol().setColor(QColor.fromRgb(50,50,250)) Result ...
  • 9,848
3 votes
1 answer
60 views

Layout title not showing when exporting PDF in PyQGIS

I have some shapefiles with distribution (range) maps for some species of butterflies. I want to load them, give each map a title, and export each map as a PDF with the name in accordance with the ...
  • 173
3 votes
2 answers
117 views

Select layer by name and select by expression

I want to select a layer by name and selectByExpression to select features. layer = QgsProject.instance().mapLayersByName('OS_traces')[0].id() layer.selectByExpression(f"{attribute_name} ='{...
3 votes
2 answers
98 views

Include the processing tool "exporttospreadsheet" into my plugin

I made a Plugin containing features for a specific workflow. As the last function I wanted to add the qgis processing tool "ExportToSpreadsheet" (Layertool) into my plugin. It's a native ...
3 votes
1 answer
52 views

QGIS Python loop integer with select by expression

I want to use a loop to select values with expression in a layer. The values contain the letters OS and a number (example: "OS1","OS2"). I want to use the loop to increase the ...
1 vote
0 answers
52 views

Generate widgets automatically for a generic layer's fields attributes in a custom feature form in QGIS

I'm trying to create a custom attribute form in QGIS useful to modify the current layer. I have already created a custom interface in Qt-Designer with QLabels and QLineEdits. In particular, a generic ...
  • 11
1 vote
1 answer
92 views

How do I redraw certain parts of this polygon in a polygon?

I have a polygon with certain data, and I need, to repaint them in a different color according to the Area column, and leave the rest unchanged. I found how to recolor the entire polygon using QColor, ...
2 votes
1 answer
54 views

Importing JSON as point layer into QGIS

I have a JSON file created by an API request. I wish to import this file directly into QGIS (ideally with a model or a Python script) so that with one click the data are on the project. I have tried ...
8 votes
1 answer
175 views

PyQGIS to set masked symbol layer and mask source

I have a PyQGIS script to open a GPKG-layer (contour lines) along with a qml-file in QGIS. In the style file I have enabled label masking and using the contour lines as mask layer: Making my labels ...
  • 1,279
4 votes
1 answer
65 views

Enabling 'reuse last entered value' when creating adding field with PyQGIS

When I create a vector and add fields: fields = QgsFields() fields.append(QgsField("True_Class", QVariant.Int)) How can I set the "True_Class" field to 'reuse entered value' as if ...
2 votes
0 answers
56 views

Setup QGIS in PyCharm

I had been working with PyCharm 2019 with QGIS, and the setup was working fine, Now I've upgraded PyCharm to 2022.2.4 Pro, and QGIS core modules are not able import. Here is my bat file. I am using ...
  • 53
1 vote
0 answers
58 views

Speed up intersection on large polygons with PyQGIS?

Is there a way to speed up my script? I am new to PyQGIS. I am using QGIS 3.16. What I need: I have 50 polygons (up to 1.675.000 hectares, mean 420.000 ha; one shapefile per polygon) and for each of ...
2 votes
1 answer
19 views

Applying style files to vectors and rasters in PyQGIS 3.22

I am seeing different behaviours when applying style files to rasters and vectors. This code opens a vector, fixes the geometry and adds that to the map with a preset style from a qml file: #add, fix ...
2 votes
0 answers
49 views

Programmatically set symbology for KML-based vector layer in QGIS

I have a bunch of data points in a KML file, and I need to programmatically create a QGIS layer from that file. I also need to set the appearance of the resulting LineString. I assume this must be ...
3 votes
1 answer
92 views

Zooming to a specific layer in PyQGIS 3.22 with a basemap

I'm trying to open a raster image of 15x15Km with a google basemap with PyQGIS. When the code finishes, I want the map canvas to zoom into the raster image. It almost works and I can add rasters and ...
5 votes
1 answer
105 views

Setting styles for layer with QML file from URL using PyQGIS

I was wondering how to import and apply a style from an URL to a layer with PyQGIS. My first thought was to do this but it didn't workout : import urllib from urllib import request vlayer = iface....
  • 1,796
3 votes
1 answer
84 views

Getting source path of layer file in PyQGIS

I need the exact source path of layer files as shown by QGIS Layer Properties Information Window: I know how to retrieve the layer source URI (and using the data provider gives the same result): ...
  • 908
0 votes
0 answers
37 views

More optimised alternative to gdal.Translate to save WMS to TIFF

I'm currently writing a plugin which at some point saves an image of dynamic extent and resolution from WMS as a TIFF. I'm using the following code to archieve this: ds=gdal.Translate(localrasterpath,&...
  • 11
3 votes
1 answer
75 views

'Text on Symbols' vector legend how to call 'Set Labels from Expression...' or similar from PyQGIS

I am trying to programmatically set vector legend Text on Symbols with PyQGIS. This can be manually performed from Layer Properties -> Legend -> Text on Symbols -> Set Labels from Expression.....
  • 33
2 votes
2 answers
96 views

KeyErrors in Python script

I'm using a script in the QGIS python console to extract an HTML attributes code in KMLs files that convert to JSON, but for some of them, I get a KeyErrors. This is the script that I used: https://...
  • 45
5 votes
1 answer
84 views

Create Buffer but keep original Attribute table

I want to create a buffer around a number of features. It is important for me to keep the attributes of the features in the new buffer.shp layer. When I try to write a new VectorLayer with a ...
  • 127
0 votes
0 answers
62 views

Reducing the size of bounding box using PyQGIS

I am using QGIS 3.24. I am writing a function to see the which points are intersecting which census_blocks from a polygon layer. spatialIndex = QgsSpatialIndex(polyLyr_cb) for a_point in ...
  • 137
1 vote
1 answer
47 views

Failure to display label styles from QML style file for vector layer using PyQGIS

The following PyQGIS doesn't seem to work to load my label style (from a QML style file) for a vector layer created from WKT LINESTRINGs. The vector layer does display but not the line label styles. ...
  • 395
2 votes
1 answer
53 views

Segmentizing MultiLineString by distance using PyQGIS

I want to segmentize a MultiLineString layer by a given maximum distance. Result shall be parts of the input linestring with this maximum length. I am using QGIS 3.16 and want a pure PyQGIS solution. ...
  • 29.9k
1 vote
1 answer
82 views

Reduce QGIS Docker image size

I am using the Docker image from Dockerhub to run a few Python scripts that use the following modules: qgis.core qgis.gui qgis.PyQt.QtGui Uncompressed that Docker image is > 8GB in size. Is there ...
  • 111
7 votes
1 answer
105 views

PyQGIS - How to automatically change the styles of DXF layers when adding the layers to a QGIS project?

I'm trying to change the style of all my DXF layers using the QgsProject layersAdded signal while adding the layers to QGIS 3.28.1 (Windows). For this purpose I wrote some lines of Python macro code ...
  • 4,506
1 vote
0 answers
36 views

Downloading libraries in the virtual env on button click in QGIS Plugin

I am creating a plugin in QGIS 3.24 using Python. i want to install the libraries such as numpy, pandas, etc on the click of a button in Plugin which will in turn download the libraries in the virtual ...
  • 137
2 votes
1 answer
63 views

Adding coordinates in different CRS to attribute table with PyQGIS

I have a point layer in EPSG:23700, but I want to save coordinates into the attribute table in a different CRS (EPSG: 4326), without transforming/changing the layer (I want to keep it in EPSG:23700). ...
  • 23
2 votes
1 answer
88 views

Downloading library on button click in QGIS plugin

I am building a plugin in QGIS 3.24 using Python. I want to create a button in the plugin which will in turn download the dependencies such as Pandas, NumPy, GeoPandas, Networkx etc. in the system if ...
  • 137
0 votes
1 answer
28 views

Set the default action to run when cliquing a feature with PyQGIS

With help from Define a action with Python directly (QGIS), I managed to write a piece of Python code that associates an action called 'Open File' to a given layer: # add action to open linked ...
3 votes
1 answer
73 views

Searching for values in different layers without nested for loops using PyQGIS

I have a working process that uses a nested for loop to look through each feature of a point layer and search for a matching lat/lon of multiple line layers. It works fine but can take longer than I ...
  • 409
4 votes
0 answers
85 views
+50

Custom layer legend properties don't show up on QGIS Server

In QGIS there is no UI option to remove Band name from raster layer legends and invert their order. That's why a made a PyQGIS script to do that from Python console. This works great and shows up in ...
  • 908
3 votes
2 answers
122 views

Using "Toggle Editing" button from PyQGIS

I am building a plugin in Qgis 3.24 Now, I want to access the "Toggle Editing" button from the Vector ToolBar using Python. Is it possible? Edit - I want to access rotate feature, Scale ...
  • 137
4 votes
2 answers
131 views

Creating perpendicular line between two points inside polygon using PyQGIS

I'm looking for a solution in PyQGIS to create a cut line at the midpoint of another line that connects two points within a polygon. The objective is to cut the polygon into two parts. In fact, this ...
  • 673
6 votes
2 answers
155 views

Filtering QgsCheckableComboBox items in PyQGIS

I have a very long list of values and I would like to make things easier for the user by allowing to filter the items of the QgsCheckableCombobox with a lineEdit for instance. With this code I have ...
  • 347