QGIS CartographicText

Guide on how to post process Topo so that text renders in QGIS

The following is a guide for post processing OSMM Cartographic Text so that it can be rendered within QGIS.

The best way to style the cartographic text within QGIS is to use the Data Driven advanced styling techniques. However, this requires there to be specific attributes that can be read by QGIS for the styling.
The core attributes are

Font Size
Font Colour
Font Family
Anchor Position
Orientation

Each of this needs to be created in the data to match what QGIS expects.

Font Size - in the Cartographic Text data there is an attribute called 'height' and from the Technical Guide it is described as 'The height is expressed as the distance on the ground covered by the text,
in metres. ' So we can use this value for the Font Size but we need to explicitly tell QGIS that this is 'in map units' and not 'point units'

Font Colour - there are four distinct colours for the Cartographic Text.

717171 - standard text

71B6D1 - water text, this includes both Inland Water and Tidal Water

FF00FF - this is for the Political or Administrative text

DA5A5F - for the Historic text

Because these do not exist in the data we need to create a new attribute and populate it for QGIS

1. Add Anchor Position

Firstly, we need to create a new anchor column which tells QGIS which position to locate the text.

ALTER TABLE osmm.cartographictext ADD COLUMN anchor VARCHAR;
COMMIT;
 
update osmm.cartographictext set anchor = 'Below Left' where anchorposition = 0;
COMMIT;
update osmm.cartographictext set anchor = 'Left' where anchorposition = 1;
COMMIT;
update osmm.cartographictext set anchor = 'Above Left' where anchorposition = 2;
COMMIT;
update osmm.cartographictext set anchor = 'Below' where anchorposition = 3;
COMMIT;
update osmm.cartographictext set anchor = 'Over' where anchorposition = 4;
COMMIT;
update osmm.cartographictext set anchor = 'Above' where anchorposition = 5;
COMMIT;
update osmm.cartographictext set anchor = 'Below Right' where anchorposition = 6;
COMMIT;
update osmm.cartographictext set anchor = 'Right' where anchorposition = 7;
COMMIT;
update osmm.cartographictext set anchor = 'Above Right' where anchorposition = 8;
COMMIT;

2. Vertical and Horizontal Placement

Then we add two extra columns for vertical and horizontal placement and update those fields based on the anchor position.

ALTER TABLE osmm.cartographictext ADD COLUMN vertical VARCHAR;
COMMIT;
ALTER TABLE osmm.cartographictext ADD COLUMN horizontal VARCHAR;
COMMIT;

update osmm.cartographictext set vertical = 'Bottom' where anchorposition = 0;
COMMIT;
update osmm.cartographictext set horizontal = 'Left' where anchorposition = 0;
COMMIT;
update osmm.cartographictext set vertical = 'Half' where anchorposition = 1;
COMMIT;
update osmm.cartographictext set horizontal = 'Left' where anchorposition = 1;
COMMIT;
update osmm.cartographictext set vertical = 'Top' where anchorposition = 2;
COMMIT;
update osmm.cartographictext set horizontal = 'Left' where anchorposition = 2;
COMMIT;
update osmm.cartographictext set vertical = 'Bottom' where anchorposition = 3;
COMMIT;
update osmm.cartographictext set horizontal = 'Center' where anchorposition = 3;
COMMIT;
update osmm.cartographictext set vertical = 'Half' where anchorposition = 4;
COMMIT;
update osmm.cartographictext set horizontal = 'Center' where anchorposition = 4;
COMMIT; 
update osmm.cartographictext set vertical = 'Top' where anchorposition = 5;
COMMIT;
update osmm.cartographictext set horizontal = 'Center' where anchorposition = 5;
COMMIT; 
update osmm.cartographictext set vertical = 'Bottom' where anchorposition = 6;
COMMIT;
update osmm.cartographictext set horizontal = 'Right' where anchorposition = 6;
COMMIT; 
update osmm.cartographictext set vertical = 'Half' where anchorposition = 7;
COMMIT;
update osmm.cartographictext set horizontal = 'Right' where anchorposition = 7;
COMMIT;
update osmm.cartographictext set vertical = 'Top' where anchorposition = 8;
COMMIT;
update osmm.cartographictext set horizontal = 'Right' where anchorposition = 8;
COMMIT;

3. Colour

Next we need to add a new column to tell QGIS what colour the text should be.

ALTER TABLE osmm.cartographictext ADD COLUMN fontcolour VARCHAR;
COMMIT;
 
update osmm.cartographictext set fontcolour = '#000000';
COMMIT;
update osmm.cartographictext set fontcolour = '#0099FF' where descriptivegroup = '{"Inland Water"}';
COMMIT;
update osmm.cartographictext set fontcolour = '#0099FF' where descriptivegroup = '{"Tidal Water"}';
COMMIT;
update osmm.cartographictext set fontcolour = '#FF00FF' where descriptivegroup = '{"Political Or Administrative"}';
COMMIT;

4. Font Name

Next we add a new column for the font family name, this is based on the attribute 'font' already within the data.

ALTER TABLE osmm.cartographictext ADD COLUMN fontname VARCHAR;
COMMIT;
 
update osmm.cartographictext set fontname = 'Times New Roman' where font = 0;
COMMIT;
update osmm.cartographictext set fontname = 'Arial' where font = 1;
COMMIT;
update osmm.cartographictext set fontname = 'Arial' where font = 2;
COMMIT;
 
ALTER TABLE osmm.cartographictext ADD COLUMN orientationdegrees VARCHAR;
COMMIT;
 
update osmm.cartographictext set orientationdegrees = (orientation/10);
COMMIT;

5. Add XY coordinate for placement

Also need X Y coordinates so we can use the anchor position

ALTER TABLE osmm.cartographictext ADD COLUMN x_coordinate NUMERIC;
COMMIT;
update osmm.cartographictext set x_coordinate = ST_X(wkb_geometry);
COMMIT;
 
ALTER TABLE osmm.cartographictext ADD COLUMN y_coordinate NUMERIC;
COMMIT;
update osmm.cartographictext set y_coordinate = ST_Y(wkb_geometry);
COMMIT;

6. Orientation

Within the Cartographic Text data there is an attributed called 'orientation' however this needs to be divided by 10 to give you a valid degree value. So we can create a new orientation degree column and update it.

ALTER TABLE cartographictext ADD COLUMN "orientationdeg" VARCHAR;
COMMIT;

UPDATE cambridge.cartographictext SET orientationdeg = orientation/10;
COMMIT;

END

After post processing the data can now be rendered in QGIS using the data drive styling functions.