Monday, 2 October 2017

qgis - Create a grid with all polygons labelled - Index style


I've been looking at creating an indexed grid and have used vector--grid, mmqgis--create grid and FSC Tools.


They all work fine but don't label the grid squares. With the one created by vector grid, there is left/bottom/right and top so if we sort by top then the highest value is the top left grid square -which I want called A1.



The next in the list is A2 and then when the top value changes it's row B etc.


Is there some switch or other tool that can add these labels?


I noticed there's a QGIS 2x plugin (https://plugins.qgis.org/plugins/createnamedgrid/) but I haven't found one for v3.


I have tried the process in Creating indexed vector grid in QGIS? but it doesn't work as expected.


enter image description here


I wonder if this has something to do with the row ID's being assigned based on the order the table was written in...it should be Top (highest to lowest) and Right (lowest to highest) to get the grid in the right order to test this. I have tried the sort and number plugin and changed 15 in the solution to 36 to represent the number of squares left to right but it still doesn't work.



Answer



With the QGIS grid tool, you can create a grid with "top", "bottom", "left", "right" fields (same as mmQGIS plugin) with respective grid feature coordinates.


The maximum grid size with this code is :




  • rows : 26 (A to Z) + 26 * 26 (AA to ZZ) = 702

  • columns : unlimited


With the following label formula, you can achieve what you want :


Here, the 999 value is the size of my grid in layer units (width or height of the grid cell, I precise it in the code each time if you have rectangular grid cells for example).


CASE
WHEN floor(((maximum("top") - "top" ) / 999) / 26) > 0 --height
THEN char(floor(((maximum("top") - "top" ) / 999) / 25) + 64) --height
ELSE ''
END

||
char(((maximum("top") - "top") / 999) % 26 + 65) --height
||
to_string(("right" - minimum("left")) / 999) --width

Results :


A1, A2, ...
B1, B2, ...
...
AA1, AA2, ...

AB1, AB2, ...



  • Inverted solution :



    • rows : unlimited

    • columns : 26 (A to Z) + 26 * 26 (AA to ZZ) = 702





-- first column letter for after the column "Z"
CASE
WHEN floor((("right" - minimum("right")) / 999) / 26) > 0 --width
THEN char(floor((("right" - minimum("right")) / 999) / 25) + 64) --width
ELSE ''
END
|| -- concatenate
-- the column value converted into a letter between A - Z
char((("right" - minimum("right")) / 999) % 26 + 65) --width

|| -- concatenate
-- the row value converted in string
to_string((maximum("top") - "bottom") / 999) --height

Results :


A1, B1, ..., AA1, AB1, ...
A2, B2, ..., AA2, AB2, ...

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...