Learn how to create and navigate to map bookmarks using python in QGIS as part of this preview chapter, Creating Dynamic Maps from QGIS Python Programming CookBook. With 140 short, reusable recipes to automate geospatial processes in QGIS, the QGIS Python Programming CookBook teaches readers how to use Python and QGIS to create and transform data, produce appealing GIS visualizations, and build complex map layouts.
Creating a Map Bookmark
Map Bookmarks allow you to save a location on a map in QGIS so you can quickly jump to points you need to view repeatedly without manually panning and zooming the map. PyQGIS does not contain API commands to read, write, and zoom to bookmarks. But fortunately QGIS stores the bookmarks in an SQLite database. Python has a built-in SQLite library we can use to manipulate bookmarks using the database API.
Getting ready
You can download a census tract polygon shapefile to use with this recipe here:
https://geospatialpython.googlecode.com/files/GIS_CensusTract.zip
Extract it to your qgis_data directory. We are going to create a bookmark which uses an area of interest within this shapefile. So you can manually load it to test the bookmark out.
How to do it…
We will access the QGIS configuration variables to get the path of the user database which stores the bookmarks.Then we’ll connect to that database and execute an SQL query which inserts a bookmark. Finally we’ll commit the changes to the database.
- First, using the QGISPythonConsole, we must import Python’s built-in SQLite library:
import sqlite3
- Next we get the path to the database:
dbPath = QgsApplication.qgisUserDbFilePath()
- Now we connect to the database:
db = sqlite3.connect(dbPath)
- Then we need a database cursor to manipulate the database:
cursor = db.cursor()
- Now we can execute the SQL query which is a string. In the VALUES portion of the query we will leave the bookmark id as NULL but give it a name, then leave the project name NULL, and set the extents:
cursor.execute(“””INSERT INTO tbl_bookmarks(
   bookmark_id, name, project_name,
   xmin, ymin, xmax, ymax,
   projection_srid)
   VALUES(NULL, “BSL”, NULL,
          -89.51715550010032,
          30.233838337125075,
          -89.27257255649518,
          30.381717490617945,
          4269)”””)
- Then we commit the changes:
db.commit()
- To test the map bookmark, load the census tract layer onto the map by dragging and dropping it from your file system into QGIS.
- Next click the View menu in QGIS and select ShowBookmarks.
- Then select the BSL bookmark and click the ZoomTo
- Verify that the map snapped to an area of interest close to the polygons with OBJECTIDs from 4625 to 4627.
How it works…
Even when QGIS doesn’t provide a high-level API, you can almost always use Python to dig deeper and access the information you want. QGIS is built on open-source software and therefore no part of the program is truly off limits.
Navigating to a Map Bookmark
Map bookmarks store important locations on a map so you can quickly find them later.You can programmatically navigate to bookmarks by using the python sqlite3 library to access the bookmarks database table in the QGIS user database and then using the PyQGIS canvas API.
Getting ready
We will use a census tract layer to test out the bookmark navigation. You can download the zipped shapefile here:
https://geospatialpython.googlecode.com/files/GIS_CensusTract.zip
Manually load this layer into QGIS after extracting it from the zip file. Also make sure you completed the previous recipe titled Creating a Map Bookmark. You will need a bookmark named BSL for an area of interest in the above shapefile.
How to do it…
We will retrieve a bookmark from the QGIS User database and then set the map extent to that bookmark.
- First import the python SQLite library:
import sqlite3
- Next get the location of the user database from the QGIS data:
dbPath = QgsApplication.qgisUserDbFilePath()
- Now we connect to the database:
db = sqlite3.connect(dbPath)
- Then we need a database cursor to run queries:
cursor = db.cursor()
- Now we can get the bookmark information for the bookmark named BSL:
cursor.execute(“””SELECT * FROM tbl_bookmarks WHERE name=’BSL'”””)
- Now we’ll get the complete results from the query:
row = cursor.fetchone()
- Then we split the values of the result into multiple variables:
id,mark_name,project,xmin,ymin,xmax,ymax,srid = row
- Now we can use the extents from the bookmark to create a QGIS extent rectangle:
rect = QgsRectangle(xmin, ymin, xmax, ymax)
- Next we reference the map canvas:
canvas = qgis.utils.iface.mapCanvas()
- Finally we set the extent of the canvas to the rectangle and then refresh the canvas:
canvas.setExtent(rect)
canvas.refresh()
How it works…
Reading and writing bookmarks with SQLite is straight forward even though its not part of the main PyQGIS API. Notice that bookmarks have a placeholder for a project name which you can use to filter bookmarks by project if needed.
QGIS Python Programming CookBook
<– Previous: Symbolizing Layers   |    Next: Scale and Layer Visibility –>