11

Interesting question! I couldn't find any native signal emitted when an attribute table is opened or closed so I would call this solution a fairly inelegant workaround but it seems to work well enough. I found that QApplication has a focusChanged(old, new) signal which is emitted whenever the widget focus changes e.g. opening/ closing dialogs or clicking ...


9

Use this script: selection = [] def selection_changed(selected, deselected, clearAndSelect): global selection # If the user deselects feature(s), remove ID's from list if deselected: selection = [ID for ID in selection if ID not in deselected] print(f"Selection: {selection}") else: # get newly selected IDs ...


8

The input parameters can be accessed as a dictionary object, so parameters['SPECIES_POINTS'] will give you the path to the input layer. The call to the algorithm would then be: pointsReprojected = processing.run("native:reprojectlayer", { 'INPUT': parameters['SPECIES_POINTS'], 'TARGET_CRS': CRS, 'OUTPUT' : ...


8

The easiest approach is to run the algorithm through the GUI, and then look in the Processing "history". You'll see the equivalent Python command shown there for the operation you just performed via the gui.


8

By type name (typeName()): int_types = ["Integer", "Integer64"] int_fields = [field for field in iface.activeLayer().fields() if field.typeName() in int_types] OR by type number (type()): int_types = [2, 4] # 2: Integer, 4: Integer64 int_fields = [field for field in iface.activeLayer().fields() if field.type() in int_types] Be careful. Type of some ...


7

try: from qgis.core import QgsApplication QgsApplication.qgisSettingsDirPath() for show profiles folder https://qgis.org/api/classQgsApplication.html#a89ad50b0d0fd60ccae803f22f6c5cf46


7

Here is the code which is working for me: urlWithParams = 'type=xyz&url=https://a.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857' rlayer = QgsRasterLayer(urlWithParams, 'OpenStreetMap', 'wms') if rlayer.isValid(): QgsProject.instance().addMapLayer(rlayer) else: print('invalid ...


7

From Docs, you can pass a QgsFeatureRequest to getFeatures() https://qgis.org/pyqgis/3.0/core/Vector/QgsVectorLayer.html#qgis.core.QgsVectorLayer.getFeatures QgsFeatureRequest describe the way you retrieve the QgsFeature You can use setSubsetOfAttributes (https://qgis.org/pyqgis/3.0/core/Feature/QgsFeatureRequest.html#qgis.core.QgsFeatureRequest....


7

You should use setRenderer3D method of QgsMapLayer. layer.setRenderer3D(renderer) QgsVectorLayer inherits that method from QgsMapLayer.


7

You can use the QgsVertexMarker and QgsRubberBand classes to show points on the map canvas as temporary markers which are not part of a layer. Here are are a couple of short examples to get you started. Vertex Marker: canvas = iface.mapCanvas() lon = 131.2 lat = -12.5 pnt = QgsPointXY(lon, lat) m = QgsVertexMarker(canvas) m.setCenter(pnt) m.setColor(...


7

NULL is defined in qgis.core from qgis.core import NULL with edit(layer): for feature in layer.selectedFeatures(): feature.setAttribute('distance', NULL) layer.updateFeature(feature)


6

Welcome to GIS.SE! As you thought, there is a way to know if a field exists in a layer not by iterating features but by looking at the layer structure. In the QGIS Layers Panel, select a layer. Then in the Python Console inside QGIS, run the following code: field_name = "my_field" layer = iface.activeLayer() # Gives you the layer you have selected in the ...


6

I'm going to answer my own question in two ways. First, I'll use the example from the PyQGIS Developer Cookbook. I initially struggled with this example but eventually got it working, so here is a simplified example below with some additional annotation and comments. I hard coded some values which you may want to adapt. Here is the full code as an example: ...


6

I tried to understand the behavior of the setFlags method by see on a vector layer the results of : vl = iface.activeLayer() flags = 1 vl.setFlags(QgsMapLayer.LayerFlag(flags)) # result : the layer is identifiable and required I looked at the Python API doc for QgsMapLayer, in these attributes and : Identifiable = 1 (= 2 power 0) Removable = 2 (= 2 power ...


6

Great idea you need place startup.py in C:\Users\<username>\AppData\Roaming\QGIS\QGIS3 and add missing import ,and voilá from qgis.utils import iface from PyQt5.QtWidgets import QAction import webbrowser def open_gis_se(): webbrowser.open('https://gis.stackexchange.com') iface.helpMenu().addSeparator() gis_se_action = QAction('Go to gis....


6

You can get the list of field names in the order you have arranged them (using the Drag and drop designer) in this way: qgis_layer = iface.activeLayer() edit_form_config = qgis_layer.editFormConfig() root_container = edit_form_config.invisibleRootContainer() for field_editor in root_container.findElements(QgsAttributeEditorElement.AeTypeField): print(...


6

QGIS Python Editor version: from qgis.PyQt.QtGui import * layer = iface.activeLayer() attDialog = iface.showAttributeTable(layer) # firstly, you need get some widgets mFilterButton = attDialog.findChild(QToolButton,'mFilterButton') mActionAdvancedFilter = attDialog.findChild(QAction,'mActionAdvancedFilter') mFilterQuery = attDialog.findChild(QLineEdit,'...


6

There is quite a bit of logic involved in your question. Here is some example code which you can run in the Python console to get an idea of how this might work for you. Basically, you should create a custom edit tool which sub-classes QgsMapToolEdit. Then you can re-implement the methods canvasReleaseEvent() and canvasMoveEvent(), and use the snapPoint() ...


6

You should "very often" use QgsFeatureRequest : https://qgis.org/api/classQgsFeatureRequest.html https://qgis.org/pyqgis/master/core/QgsFeatureRequest.html If you need only a subset of attributes, and also maybe you don't need the geometry, you can do something like: expected_fields = ['KKOD', 'KATEGORI'] layer = iface.activeLayer() indexes = [layer....


6

You can use this script. First, select a the layer, then run the script. It adds the result as a memory layer. You should save the result as a new file manually or using a code. input_layer = iface.activeLayer() max_area_x = ["max_area_1", "max_area_2", "max_area_3"] for i in range(len(max_area_x)): # OR for i in [0, 1, 2] input_layer = processing.run("...


5

According to the pyqgis api documentation there is a method layer.selectByIds([s.id() for s in selection]) And for some performance improvements and good practice, don't request any attributes or geometry, if you only need the feature id request = QgsFeatureRequest(QgsExpression('"CODE_MERC" is NULL')).setFlags(QgsFeatureRequest.NoGeometry)....


5

Well, the error message is correct. According to the QGIS API documentation, QgsMapRendererJob is an abstract class. So you cannot directly use it. Instead you need to use one of the subclasses: QgsMapRendererSequentialJob - renders map in one background thread to an image QgsMapRendererParallelJob - renders map in multiple background threads to an ...


5

To change QgsFeature geometry in QGIS 3 you have to call setGeometry() explicitly. geom = feat.geometry() geom.translate(100, 100) feat.setGeometry(geom) Explanation: In QGIS 2 geometry() returns a pointer, so it can be modified in place. QgsGeometry* QgsFeature::geometry() //QGIS 2 In QGIS 3 geometry() returns value instead of a pointer. Additionally ...


5

After some reasearching I found that the key problem seems to be that you first defined sink (sink, dest_id) = self.parameterAsSink( ... and later on overwrite it with the result of processing.run... sink=processing.run("native:buffer", { ... I tried the following which worked for me: #create a temporary layer as a processing result lyr = processing.run(...


5

The correct approach looks somewhat like this: # create the map item map = QgsLayoutItemMap(layout) # setup its initial position on the page map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) # setup an initial extent to view in the map rectangle = QgsRectangle(-13838977, 2369660, -8672298, 6250909) map.setExtent(rectangle) ...


5

The order of when you set the extent, add the map to the layout, and resize the rectangle is important. Steps to add a map to a new layout in QGIS 3. Instantiate the map object with QgsLayoutItemMap(layout) Set a base Rectangle Set the extent Add the map to the layout object, layout.addLayoutItem(map) Now you can Move & Resize the rectangle as ...


5

You attempt to set a name to a deleted object. You need to create a new object : [...] for layout in layouts_list: if layout.name() == layoutName: manager.removeLayout(layout) layout = QgsPrintLayout(project) layout.setName(layoutName) [...]


5

In QGIS 3.4 (Ubuntu), I've created a polygon layer (WGS 84, EPSG: 4326) with a unique "value" field, type decimal (4, 1). Here the layer datas : wkt_geom value Polygon ((0.5 0.6, 0.2 0.4, 0.5 0.2, 0.8 0.4, 0.5 0.6)) 174 Polygon ((0 1, 1 0.8, 1 0, 0 0.2, 0 1),(0.5 0.6, 0.2 0.4, 0.5 0.2, 0.8 0.4, 0.5 0.6)) 92.3 Or just create a diamond shape polygon ...


5

There is a fairly rigid structure for creating a processing script for the QGIS processing tool box, the basis of which is sub-classing and creating an instance of the QgsProcessingAlgorithm class. In addition to the QGIS3 processing script template by @underdark, there is an excellent tutorial by @spatialthoughts, "Writing Python Scripts for Processing ...


5

There are several issues filed on the QGIS Issue Tracking list concerning this problem (this one being the one referencing the possible fix). Until the patch has been released through the official software update repositories you seem to be able to solve the problem manually (according to the proposed fix): locate file dlg_sql_window.py (in <QGIS>/...


Only top voted, non community-wiki answers of a minimum length are eligible