This is a follow-up post on https://kartoza.com/en/blog/create-a-custom-reference-grid-in-qgis-composer/ .
This post outlines how to make dynamic grids that are aligned to the lat long graticule.
Create one grid to show the lines with intervals in decimal degrees.
Create another grid to show the labels in the center of the visible grid cells. Note the offset is set to half the interval and we don't draw the lines.
Navigate to the label setting for the second grid and choose the following.
Click expression builder on custom.
Click on the function editor and create a new function. Populate the contents with following https://gist.github.com/NyakudyaA/c2cf728e2906288e0448f82cb3c5a077 and save your changes.
Click on the expression and enter the following:
WHEN @grid_axis = 'y' THEN
substr('ABCDEFGHIJKLMNOPQRST',(@grid_number -to_int(map_y_min( 'A4 portrait', 'main')/0.016666666667)*0.016666666667+0.008333333000)/0.016666666667,1)
WHEN @grid_axis = 'x' THEN
round( ( @grid_number - to_int( map_x_min( 'A4 portrait', 'main') /0.016666666667)*0.016666666667+0.008333333000 ) / 0.016666666667,2)
The parameters for the map labels are explained below:
* A = Composer title
* B = Item ID for the map
* C = Grid Interval in lat long (calculated as (1/60) to get a minute grid)
* D = Grid offset in lat long ( calculated as half of the Grid interval)
* E = Custom python function ( Copied from https://gist.github.com/NyakudyaA/c2cf728e2906288e0448f82cb3c5a077 )
Your map will be labeled accordingly now.
NB: The script for labeling assumes that your map CRS is 3857. So it will transform the bounding box from EPSG:3857 to EPSG:4326. If your map is using another CRS you can change the following https://gist.github.com/NyakudyaA/c2cf728e2906288e0448f82cb3c5a077#file-map_grids-py-L15
The script also assumes the map has an item id assigned. Many users do not know the significance of adding the Item ID for each map element.
Share on Twitter Share on Facebook