

Right-click the parcels layer, click Properties and switch to the Actions tab.

Load the sf.qgz project from your data package. The parcel layer has an attribute block_num that refers to the block that parcel belongs to. We will define an action for the parcels layer so when a user clicks on a parcel, all parcels that belong to the same block will be selected. Actions are integrated into the QGIS GUI and allow you to execute PyQGIS code on vector layers.Īctions are defined at the layer level. For pyqgis scripts elsewhere, we have to explicitly import the modules (classes) that we want to use.Īctions in QGIS provide a quick and easy way to trigger custom behavior in response to a user’s action - such as click on a feature in the canvas or an attribute value in the attribute table.Īctions provide an easy way to add custom behavior to QGIS without having to write plugins. When we ran the code snippets in the Python Console, we did not have to import any modules since they are done automatically when the console starts. Note the imports at the top - including iface. We connect this code to the signal iface.initializationCompleted signal when the main window is loaded.Ĭreate a new file named startup.py with the following code. We can read that variable and set the main window’s title with it.

The version name is stored in a global QGIS variable called qgis_version. If you are running multiple versions of QGIS, a very useful customization is to display the QGIS version number and name in the main window. This file is very useful in customizing QGIS interface with techniques learnt in the previous section. QGIS looks for a file named startup.py in the user’s Python home directory, and if it is found, executes it. It is possible to execute some PyQGIS code every time QGIS starts. _init_(color, type, seats) self.range_km = range_km my_car = Sedan( 'blue', 'automatic', 5) print(my_car.color) print(my_car.seats) my_car.start() my_future_car = ElectricSedan( 'red', 'automatic', 5, 500) print(my_future_car.color) print(my_future_car.seats) print(my_future_car.range_km) my_future_car.start() _init_(color, type) ats = seats class ElectricSedan(Sedan): def _init_( self, color, type, seats, range_km): super(). type = type self.started = False self.stopped = False def start( self): print( 'Car Started') self.started = True self.stopped = False def stop( self): print( 'Car Stopped') self.stopped = True sefl.started = False # Instantiate the class my_car = Car( 'blue', 'automatic') print(my_car) # Call a method my_car.start() # Check the value of an instance variable print( 'Car Started?', my_car.started) # Check the value of a class variable print( 'Car model', Car.model) # Inheritance class Sedan(Car): def _init_( self, color, type, seats): super(). To harness the full power of the PyQGIS API, we must first understand how classes work.Ĭlass Car: model = 'Civic' def _init_( self, color, type): lor = color self. This gives you a preview of the power of the API. mitChanges(): This method saves the edit buffer and also disables the editing mode.Here we pass on index 1 for the second attribute. It takes the index of the attribute to be deleted. leteAttribute(1): The deleteAttribute() is a method from QgsVectorLayer class.layer.startEditing(): This is equivalent to putting the layer in the editing mode.The method returns the reference to the layer which is saved in the layer variable. We will learn more about iface in the QGIS Interface API section. layer = iface.activeLayer(): This line uses the iface object and runs the activeLayer() method which returns the currently selected layer in QGIS.You will see that the 2nd column is now deleted from the attribute table. Layer = iface.activeLayer() layer.startEditing() leteAttribute( 1) mitChanges() Make sure you have selected the shoreline layer in the Layers panel before running the code. Click the Run Script button to execute it. Open the Editor and enter the following code. We will now do this task - but using only Python code. QGIS Provides an API to accomplish all of this using Python code. Click the Save edits button and click the Toggle Editing mode to stop editing.Select the SDE_SFGIS_ column and click OK. In the Attribute Table, click the Toggle Editing mode button.Right-click the shoreline layer and click Open Attribute Table.This can be done using the QGIS GUI as follows Let’s say we want to delete the 2nd column ( SDE_SFGIS_) from the layer. Let’s try out the API to perform some GIS data management tasks.īrowse to the data directory and load the shoreline.shp layer. This allows developers to write code to build new tools, customize the interface and automate workflows. Almost every operation that you can do using QGIS - can be done using the API. QGIS provides a Python API (Application Programming Interface), commonly known as PyQGIS.
