Here a standalone recipe. We've seen you are using "Rule-based" symbology. Instead of doing the case for this particular case, we've made the sample for "Single Symbol" and at the end made the transformation to "Rule-based" symbology
Another approach using QgsPoint and QgsPolyline, a list of QgsPoints (standalone script using a memory layer but you should replace vl with the reference to your own layer)
vl = QgsVectorLayer("LineString", "temp", "memory")
pr = vl.dataProvider()
When I run the following code in the Python console of QGIS,
the dialog window has the same behavior as the dialog windows in the processing toolbox.
dlg = QDialog(iface.mainWindow())
You can achieve this by giving the dialog a parent (here, the main window of QGIS).
A child dialog always stays on top of its parent window.
You should change your code by replacing def onFeatureIdentified(feature): with def onFeatureIdentified(self, feature):
If I'm not wrong your onFeatureIdentified is a method of your plugin: it expects something like def myfunction(self, arg1, arg2, ...):
Instead of comparing every feature with the keys in the dictionary, I saved the whole dictionary as a csv and simply used the QGIS algorithm Join Attribute Table (probably some SQL plays its part here as well!?) to join the csv to the vector.
#create csv from dictionary
header_list = ["keys", "values"]
Add the following script as a new function: (How to add custom function)
from qgis.core import *
from qgis.gui import *
def cumulative_sum(field, feature, parent):
total += feature[field]
Then you can use it in Expression tab:
cumulative_sum('comp') # don't use double ...
Look at the following methods as I'm not sure you should use setClassificationField (PS: not certain as only supposed from the API doc)
The part that calculate the value for the "Find" button should be the following. It's a port of the C++ ...
You can use below recipe
layer = iface.activeLayer()
fieldName = 'ADM0_A3' # Change
fieldIndex = layer.fields().indexFromName(fieldName)
categories = 
for u in layer.uniqueValues(fieldIndex):
symbol = QgsSymbol.defaultSymbol(layer.geometryType())
category = QgsRendererCategory(u, symbol, str(u))
renderer = ...
Add the following lines to save the map as image. This is a minimal solution.
Add these lines to __init__ method:
self.actionSaveAsImage = QAction("Save As Image", self)
And add this as a new method to the class: (change image path)
So here's the complete code to achieve what I wanted :
couche = self.dlg.mMapLayerComboBox_2.currentText()
champ = self.dlg.mFieldComboBox.currentText()
path = self.get_enregistrement()
p = path + "/" + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + "_" + couche + "_" + champ
#separation de la couche active par le champ defini ...
So despite the very elaborate answer by fastest which for some reason didn't work for QVariant.Integer and QVariant.Double I ended up using a simpler solution. It might be a bit hacky but it's a classical case of "works-for-me":
for feat in layer.getFeatures():
for field in feat.fields()....
So is it possible to save a loaded CSV layer as temporary shapefile with PyQGIS? If so, how can this be done?
Yes, it's possible to copy from your CSV to memory layer with QGIS 3. You can find below a recipe.
# Here we take the selected layer but you may want to get it through another way
original_layer = iface.activeLayer()
csv_provider = original_layer....
one way I found to solve the issue of replacing the NULL values with zeros or similar values is the following:
I define a function that maps the NULL value to some other value of choice, depending on the QVariant type:
if qvariant_type == QVariant.Invalid or qvariant_type >= QVariant.Int and qvariant_type <= ...