Our recent blog post about visualising OS MasterMapÂ® Topography Layer Building Height Attribute (BHA) data in ArcGIS and ArcGlobe prompted a number of questions about whether it’s possible to do something similar in open source software. In this post we’ll show you how to achieve something similar using QGIS and the freely available Qgis2ThreeJS plugin.
Please see the previous post for information on BHA data coverage, an explanation of the different height attributes supplied by Ordnance Survey (OS) and for details of further information sources; including the excellent Getting Started Guide produced by OS. Please remember this is an alpha release of the data and OS do not guarantee that BHA is error free or accurate. Additionally the dataset is not yet subject to update and maintenance.
- Download the following datasets for your area of interest from Digimap using the OS Data Download application:
- OS MasterMapÂ® Topography Layer: select the â€˜File Geodatabaseâ€™ format for your data as this format does not require any conversion to use it in QGIS;
- OS Terrainâ„¢ 50 DTM: this will be used as the base (surface) heights for the area;
- BHA data (BHA data is found in the â€˜OS MasterMapâ€™ group): select CSV as the format;
- Optionally download any additional data you may wish to use as a backdrop, such as OS VectorMapÂ® Local Raster or OS 1:25,000 Scale Colour Raster;
- Open QGIS and load in the OS MasterMapÂ® Topography Layer, OS Terrainâ„¢ 50 DTM and your backdrop map data.
Preparing BHA data for use
If your downloaded BHA data is made up of more than one CSV file we recommend merging them all together in to a single CSV file first to make subsequent processing easier and quicker. Use a text editor such as Notepad or TextPad rather than Excel, as Excel can change the formatting of numbers which contain leading zeros.
Each object in MasterMap Topography Layer has a unique identifier called a Topographic Identifier, or TOID for short. TOIDs supplied by OS take the format of a 13 or 16 digit number prefixed with â€˜osgbâ€™ e.g. â€˜osgb1000039581300â€² or â€˜osgb1000002489201973â€². Some applications, such as ArcGIS, automatically strip off the â€˜osgbâ€™ prefix and add three leading zeros to any TOID that has only 13 digits to make them all 16 characters long. Additionally this same formatting is applied to the File Geodatabase format of MasterMap supplied through Digimap. In order to make it easier to join BHA data to building features in MasterMapÂ® Topography Layer the BHA files supplied by EDINA have two TOID values:
- os_topo_toid_digimapÂ is the TOID formatted to match TOIDs in ArcGIS and in the File Geodatabase format supplied through Digimap.
- os_topo_toid is the original TOID as supplied by Ordnance Survey
You should check the TOID values in your MasterMap data and those in the BHA data to ensure that there is a common field that you can use to match on; we will use os_topo_toid_digimap as this field in the BHA data matches the TOID values in the MasterMap data downloaded in File Geodatabase format from Digimap.
Before BHA data can be loaded in to QGIS it is necessary to create a small text file (called filename.csvt, where ‘filename’ is the name of your BHA csv file) that specifies the data type of each field so that QGIS handles it correctly. Specifically the .csvt file is used to ensure that QGIS treats the two TOIDÂ values as text rather than numbers, and all height values as numbers. The steps required are detailed below:
- Create a new file called filename.csvt (replacing ‘filename‘ with the name of your BHA csv file) in the same folder as the BHA csv file you wish to import.
- Open the file in a text editor such as Notepad or TextPad.
- Copy and paste the following text in to the file:
- Save your changes to the file. Ensure it is saved in the same folder as the CSV file you wish to import.
- Add your BHA CSV file to QGIS through the Add Vector Layer function; this will add the data as a table in the QGIS project.
Creating a heighted buildings dataset
In order to create a new heighted buildings dataset from the building features in OS MasterMap Topography Layer and the BHA data we use the GIS â€˜joinâ€™ function. A join links these two datasets together through a common unique identifier (the TOID) resulting in a set of buildings with height values stored as additional attributes.
- Bring up the Layer Properties dialog for the Topographic Area layer in the MasterMap data either by double clicking on the layer in the Layer panel or by right clicking on the layer and selecting Properties from the pop-up menu.
- Select the ‘Joins’ tab on the left hand side to display the join panel:
- Press the green plus button to add a new join:
- Select your BHA dataset as the ‘Join Layer’.
- Select the correct TOID field that matches the TOIDs in your MasterMap data; as mentioned above we’re using os_topo_toid_digimap as the formatting of this matches the TOIDs supplied in the MasterMap data downloaded from Digimap in File Geodatabase format.
- In the ‘Target Field’ select the attribute column that contains the TOIDs in your MasterMapÂ® data; by default this is called ‘TOID’ in MasterMap downloaded from Digimap.
- Leave the checkbox selected to ‘cache join layer in virtual memory’ as this will speed up query and display of the data.
- Press OK to create the join.
- Press OK on the Layer Properties dialog to close the window.
- Open the attribute table for the TopographicArea MasterMap layer to verify the join has worked. You will see the additional BHA columns at the end of the attribute table. Note you will see a lot of ‘null’ values in these additional columns as BHA values are only available for ‘building’ features (the TopograhicArea feature class contains features for everything, not just buildings).
- Having joined the datasets together, before we can create a heighted buildings dataset we need to select only those buildings which now have height information. This is done using the QGIS ‘Select features using an expression’ button:
- We are looking to select only features which have a value for the height attribute we wish to use in the 3D visualisation. As mentioned in the previous post, we have found that the ‘RelH2′ attribute provides a good representation of the height of buildings relative to one another. The expression used is shown below. Note the field name, in quotes below, is automatically created by QGIS by adding the BHA table name (NT27) to the attribute column name (RelH2) with an underscore between them:
"NT27_relh2" IS NOT Null
- Having selected just the buildings that include height information we can now export these features as a new dataset by right clicking on the TopographicArea dataset in the Layer panel and selecting ‘Save As…’ from the pop-up menu.
- Save the dataset as a new Shapefile in a suitable location, selecting the checkboxes ‘save only selected features’ and ‘add saved file to map’.
- The newly created heighted buildings dataset will be added to your QGIS project; now it’s time to visualise it in 3D.
Visualising the data in 3D
So far we have downloaded OS MasterMapÂ® Topography Layer and BHA data for the same area and joined the two together to create a new dataset containing just the building features which now include the various height attributes published by OS. We also downloaded OS Terrainâ„¢ 50 DTM to use as the surface heights, 1:25,000 Colour Raster and OS VectorMapÂ® Local Raster to drape over the surface.
Visualising the data in 3D in QGIS is achieved using the Qgis2ThreeJS plugin, which can be installed using the QGIS plugin manager if you don’t have it already. The steps below describe how to use the plugin to create a 3D model:
- Ensure you have all the data loaded in to your QGIS project that you wish to include in the 3D model, as a minimum you should have your DTM, the heighted buildings dataset and a suitable map layer to drape over the DTM.
- Turn off all layers in the Layers panel apart from the surface you wish to drape over the DTM; the buildings will be styled using the Qgis2ThreeJS plugin.
- Launch the plugin, which can be found on the Web toolbar.
- Using the ‘DEM’ panel of the plugin select your DTM data as the ‘DEM Layer’, leave all other settings at their default values. Tip: by default the surface has a vertical exaggeration of 1.5, if you wish to reduce or increase this, the setting is configured on the ‘World’ panel of the plugin.
- In the ‘Polygon’ panel of the plugin select your heighted buildings dataset and complete the following settings:
- Z coordinate: set to ‘Height from surface’ – this will ensure the buildings sit on the DTM surface.
- Under ‘Style’: ensure the ‘Object type’ is set to ‘Extruded’ and select the height attribute you wish to use for the extrusion using the ‘Height’ drop down; as mentioned above, we’re using the RelH2 attribute from OS which is in the column ‘NT27_relh2′ in our data.
- Select suitable colours and transparency, we used a medium grey colour with 10% transparency to give a glasshouse effect.
- Optionally specify an ‘Output HTML file path’ to save the resultant files. Whilst you’re experimenting we recommend you leave this blank and the plugin will save the data in a temporary location, when you’re happy with the result you can use this setting to save your final visualisation.
- Press ‘Run’ to create the 3D model. Once it’s finished processing the model will open in your default web browser.
- The plugin uses the extents of the current QGIS map canvas, so the bigger the area being displayed, the bigger the generated 3D scene and the slower it will display. We have found that areas of up to 10kmÂ² display okay, anything bigger tends to be a little slow to respond.
- If you wish to define specific extents for your 3D scene instead of using the map canvas extents this can be done on the ‘World’ panel of the plugin.
- Applying a vertical exaggeration to your buildings is achieved through the ‘Multiplier’ setting on the ‘Polygon’ panel of the plugin.
- You can create 3D models of multiple layers. For example in the screen shot below the trees were created by selecting the ‘Positioned Non Coniferous Trees’ from the OS MasterMap Topographic Point layer. These were then added to the QGIS project twice. Using the plugin one of these layers was extruded as a brown cylinder with a radius of 0.75m and a height of 3m to form the trunk; the other was extruded as a green sphere with a radius of 4.5m and a z coordinate of 4.5m (i.e. the height above the ground surface of the centre of the sphere) to form the tree canopy:
OS VectorMapÂ® Local Raster draped on top of OS Terrainâ„¢ 50, with buildings from OS MasterMapÂ® Topography Layer Building Height Attribute and Positioned Non Coniferous Trees extruded on top
The finished visualisation
The screen grab below shows the final visualisation centred on the south side of Edinburgh using OS 1:25,000 Colour Raster as the surface layer.
1:25,000 Colour Raster draped on top of OS Terrainâ„¢ 50, with buildings from OS MasterMapÂ® Topography Layer extruded on top using Building Height Attribute data.