I have a layer with counties. I would like to import the bounding box of each county as a spatial bookmark and name it accordingly. Is this possible?
If you want to add details to the bookmarks such as the names or the project names then I think the only method is to access the SQLite database which stores these bookmarks. Using this great link as a guide, you could try running the following where you can define the field name, project name and the CRS:
import sqlite3 layer = iface.activeLayer() dbPath = QgsApplication.qgisUserDbFilePath() # if QGIS v3.14 or later, use following line instead: # dbPath = QgsApplication.qgisUserDatabaseFilePath() db = sqlite3.connect(dbPath) cursor = db.cursor() # Define the following information fieldName = 'Name' projectName = 'Scotland' SRID = '27700' for feat in layer.getFeatures(): rect = feat.geometry().boundingBox() cursor.execute("""INSERT INTO tbl_bookmarks(bookmark_id, name, project_name,xmin, ymin, xmax, ymax,projection_srid) VALUES(NULL, '""" + feat[fieldName] + """', '""" + projectName + """', """ + '%f,%f,%f,%f'%(rect.xMinimum(),rect.yMinimum(),rect.xMaximum(),rect.yMaximum()) + """, """ + SRID + """)""") db.commit()
1. The bookmarks panel is not immediately updated, you will need to add a new bookmark to see the panel refreshed.
2. I had an issue with the Projection CRS. My layer's CRS was set at
EPSG:27700 but setting the projection to the same CRS resulted in a transformation error. I had to set the projection to
EPSG:4326 to rectify it (I also had OTF disabled).