Generate a polygons QGIS layer to use as a Atlas coverage layer’

Proposition: B&Bs, Hotels and other potential clients are looking for simple A4 printed maps of their region, including walking- and bicycling routes, tourist attractions, pubs, but also things like medical provisions, shops etc.. The clients location can be used as a focal point and there should be 4 maps for each block surrounding the focal point (NW, SW, NE and SE), but also a 5th central map for general use.

Solution: If I can make 5 polygons in one QGIS layer, I could use this as a coverage layer for generating an QGis Atlas. See script below…

You will have 4 polygons with the focus point in the middle (so you get an northeastern block, a souteastern block, etc..) and a fifth central block for general use. You’ll find the schema below. All blocks have overlaps in the middle, where the starting focal point is.

After some arithmetics I managed to produce a Python-script that is reusable, e.g. it takes the coordinates of the focal point as input and produces 5 polygons in one layer.
It is not a memory layer, but a persistent shape based layer. The same layer is used everytime. It deletes the existing feature polygons and recreates them on the fly, albeit with a different focal point.
You can use the script within any existing QGis project. The arithmetics and coordinates assume you have EPSG:3857 as you project CRS.
Next blog I generate the Atlas…

from PyQt4.QtCore import *
## create the memory layer and add to the registry 
myLayer = QgsVectorLayer("C:/Users/Pim/OneDrive/GIS/Atlas_5_pages_generate.shp", "Atlas_5_pages_generate", "ogr") 
pr = myLayer.dataProvider() 
caps = pr.capabilities()
myLayer.startEditing()
if caps & QgsVectorDataProvider.DeleteFeatures: 
 res = myLayer.dataProvider().deleteFeatures([0,1,2,3,4])
polyleftunder = QgsFeature() 
polyleftabove = QgsFeature() 
polyrightunder = QgsFeature() 
polyrightabove = QgsFeature() 
polycentral = QgsFeature()
lon = 497577.10 
lat = 6832001.12
a= 2476.5 
b = 26712.33 
c = 19678.334 
d =2243.666
pointsleftunder = [QgsPoint(lon-b,lat-c),QgsPoint(lon-b,lat+d),QgsPoint(lon+a,lat+d),QgsPoint(lon+a,lat-c)] polyleftunder.setGeometry(QgsGeometry.fromPolygon([pointsleftunder])) polyleftunder.setAttributes(["ZW"]) pr.addFeatures([polyleftunder])
pointsleftabove = [QgsPoint(lon-b,lat-d),QgsPoint(lon-b,lat+c),QgsPoint(lon+a,lat+c),QgsPoint(lon+a,lat-d)] polyleftabove.setGeometry(QgsGeometry.fromPolygon([pointsleftabove])) 
polyleftabove.setAttributes(["NW"]) pr.addFeatures([polyleftabove])
pointsrightabove = [QgsPoint(lon-a,lat-d),QgsPoint(lon-a,lat+c),QgsPoint(lon+b,lat+c),QgsPoint(lon+b,lat-d)] polyrightabove.setGeometry(QgsGeometry.fromPolygon([pointsrightabove])) polyrightabove.setAttributes(["NO"]) pr.addFeatures([polyrightabove])
pointsrightunder = [QgsPoint(lon-a,lat-c),QgsPoint(lon-a,lat+d),QgsPoint(lon+b,lat+d),QgsPoint(lon+b,lat-c)] polyrightunder.setGeometry(QgsGeometry.fromPolygon([pointsrightunder])) polyrightunder.setAttributes(["ZO"]) pr.addFeatures([polyrightunder])pointscentral = [QgsPoint(lon-((b+a)/2),lat-((c+d)/2)),QgsPoint(lon-((b+a)/2),lat+((c+d)/2)),QgsPoint(lon+((b+a)/2),lat+((c+d)/2)),QgsPoint(lon+((b+a)/2),lat-((c+d)/2))]
polycentral.setGeometry(QgsGeometry.fromPolygon([pointscentral])) polycentral.setAttributes([\"Centraal\"])
pr.addFeatures([polycentral])
myLayer.commitChanges()
myLayer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayer(myLayer)