10
votes
Creating spatial index in standalone script as part of output vector layer using PyQGIS
If you use layer_out.dataProvider().createSpatialIndex() you don't get that error. To get the error you must use layer_out.createSpatialIndex(). The code in the question doesn't throw the error: ...
10
votes
PyQGIS to set masked symbol layer and mask source
Label masking information is not stored in the qml-file?
I guess "Masked symbol layers" and "Mask sources" settings are not stored in both qml file and Geopackage. I just realized ...
10
votes
Accepted
Performing basic labelling with PyQGIS
There is no problem with @Kezah's answer. I've edited the script and it works. Please check it. Avoid using startEditing() and dataProvider().addFeatures() together. Use either one. If you construct a ...
8
votes
Accepted
TypeError when converting shapefiles to KMLs using PyQGIS
According to the documentation the first argument should be a QgsVectorLayer, which is why you are getting the unexpected type error.
You are also missing the QgsCoordinateReferenceSystem parameter.
...
8
votes
Accepted
Extract points from a selected polygon in model builder [QGIS 3.30]
This can be achieved very simply by using the Extract selected features algorithm:
7
votes
Writing italic form scientific name in map legend?
Since the new QGIS 3.30 version and the integration of this feature request : "Support for HTML in text of legend items", it's now possible to format like this :
with this legend title :
...
7
votes
Accepted
Selecting rows in attribute table by date (QGIS)
In your use case, QGIS considers it as a string. You need to convert it to datetime format.
Use this:
expression = "Date = to_datetime('2021-09-06 20:36:59', 'yyyy-MM-dd HH:mm:ss')"
layer....
7
votes
Accepted
Wildly incorrect Lat/Long from UTM conversion
Those points are in EPSG:3857 (WGS 84 / Pseudo-Mercator) not EPSG:32617 (WGS 84/ UTM zone 17N).
EPSG:3857 is the CRS of the Google satellite basemap.
Lon, Lats are:
X,Y,id,
-82.7077395893134,41....
7
votes
Is there a way to access named PostgreSQL connections via PyQGIS in order to access and load spatial layers?
Try this. I have a connection called localhost data so I search for the word data in all the PostgreSQL connections:
from PyQt5.QtCore import QSettings
qs = QSettings()
L = []
for k in sorted(qs....
7
votes
Accepted
How to use QPainter with QgsMapCanvasItem
For the sake of completeness, I add my comment as answer:
The first argument of QgsMapCanvasItem::paint method is a QPainter instance which is already initialized to paint on the correct scene.
...
7
votes
Accepted
Export reports in QGIS using PyQGIS
Use QgsLayoutExporter.exportToPdf directly instead of making a QgsLayoutExporter object.
layout_manager = QgsProject.instance().layoutManager()
report = layout_manager.layoutByName("my_report&...
7
votes
Accepted
PyQGIS Contour Labelling
Perhaps you will have more success working directly with the QgsPalLayerSettings class. The snippet below works for me on a contour layer generated with gdal_contour.
vlayer = iface.activeLayer()
...
7
votes
Accepted
Merging values from two fields into third field using PyQGIS
You can use or like this, i think it is called Ternary operators:
layer = iface.activeLayer()
p = layer.dataProvider()
field_to_update = "field3"
fieldindex = layer.fields().indexOf(...
6
votes
Changing 0 to NULL in QGIS Attribute table
After selecting the layer, use this script in the QGIS's Python Editor (Ctrl+Alt+P):
from qgis.utils import iface
layer = iface.activeLayer()
idx = range(layer.fields().count())
layer.startEditing()...
6
votes
Where can I find AniMove in QGIS 3.4?
For QGIS 3
The "Animove" plugin has been updated to QGIS 3, as can be seen on the plugin page: https://plugins.qgis.org/plugins/sextante_animove/
6
votes
Accepted
QGIS Expression Field Calculator Function Editor doesn't recognise Geometry methods
Seriously, I've had this problem all day, scouring google, 10 minutes after posting the question - of course, I stumble across the answer on the git forums.
The reason why this doesn't work is ...
6
votes
Accepted
Creating event in PyQGIS that notices that group of layers is selected
You can use and modify this script:
from qgis.utils import iface
from qgis.core import QgsLayerTreeNode
view = iface.layerTreeView()
def eventoClickPanelCapas():
selected = view.selectedNodes()
...
6
votes
Show discrete values on legend from layer with Singleband Pseudocolor (PyQGIS 3.22)
You don't set minimum/maximum values. This is why you get the wrong legend. When using GUI, QGIS does it itself.
min_ = stats.minimumValue
max_ = stats.maximumValue
....
renderer = ...
6
votes
Accepted
Set fixed reference scale for QGIS layer with PyQGIS?
You can set a reference scale by getting the renderer of the active layer. You don't need to make a new QgsFeatureRenderer.
lyr = iface.activeLayer()
r = lyr.renderer()
r.setReferenceScale(400_000)
6
votes
Accepted
Is there a way to access named PostgreSQL connections via PyQGIS in order to access and load spatial layers?
Ah I think I actually figured it out from this page:
Getting schema names from PostGIS database in PyQGIS
For connections that exist, you can use:
md = QgsProviderRegistry.instance().providerMetadata('...
6
votes
Why Layer Combinations plugin is not available in QGIS?
This core functionality is called themes. You can save a theme and choose a theme from the theme selector icon above the layer tree. You can also have maps in print layouts that display the layers in ...
6
votes
Accepted
Function to calculate percentage of each feature over the sum that feature's field using PyQGIS
A couple of problems. Firstly, the aggregate method you are using is method of the QgsVectorLayer class, so you need to call it on a QgsVectorLayer object. Then, the second argument needs to be a ...
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 ...
5
votes
Getting QGIS Project data from outside Python script
You have to load a project. QgsProject.instance() returns the empty project, because you didn't load any project.
Add this line: project.read("c:/path/to/project.qgz")
project = QgsProject....
5
votes
Accepted
Adding new field with unique ID for each feature without iterating over all features using PyQGIS
There are some redundancies in your code, and you are mixing layer editing methods with provider methods, which is not recommended. At the end of the day, I don't think you can avoid feature iteration ...
5
votes
How can I export a layer into .shp in qgis when it is not an option in the file types?
In the "Layers" window, left-side of the map view, you will see all layers in your map.
Right-click on the layer you want to export to ESRI Shapefile.
There is an option to Export the layer....
5
votes
Is there a way of preventing QGIS from becoming buggy during a long editing session?
While I do not recognize/experience the specific issues you are experiencing, some thoughts:
Make sure you are running the latest version. At the time of your question (and this answer), the current ...
5
votes
Accepted
Code works in Python IDE but not in QGIS Python editor
You are converting your coordinates to "string" type:
df['Easting'] = df['Easting'].astype("string")
df['Northing'] = df['Northing'].astype("string")
and then making a ...
5
votes
Accepted
Calculating mean with group by based on another column in QGIS Field Calculator
Try the following expression:
mean(expression:="temp", group_by:="month" || "city")
5
votes
Calculating mean with group by based on another column in QGIS Field Calculator
Use this expression:
array_mean (array_agg (temperature, group_by:=city || month))
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
qgis-3 × 4365qgis × 1569
pyqgis × 1101
qgis-plugins × 400
pyqgis-3 × 339
qgis-processing × 304
python × 191
coordinate-system × 150
print-composer × 144
raster × 142
symbology × 139
labeling × 138
qgis-modeler × 130
polygon × 105
attribute-table × 94
layers × 81
line × 78
fields-attributes × 76
shapefile × 73
expression × 72
point × 71
postgis × 69
field-calculator × 66
pyqt5 × 64
installation × 61