Web mapping is a great medium to publish your GIS data to ther web and make it accessible by other users. Creating a web map is a very different process than creating one in a GIS. GIS users are typically aren’t web programmers and it presents a challenge when one needs to create a web map that is of the same quality as a map creating in a GIS. Fortunately, there are tools available to easily translate your work in QGIS to web maps. In this tutorial, you will learn how to use the QGIS2Web plugin to create a web map using OpenLayers or Leaflet lubraries from your QGIS project.
We will create a openlayers web map of world’s airports.
We will use the Airports dataset from Natural Earth.
Download the Airports shapefile.
For convenience, you may directly download a copy of the datasets from the links below:
Data Source [NATURALEARTH]
ne_10m_airports.zip
. Click OK.qgis2web
will use replicate the QGIS
settings and automatically create the web map without us knowing about web
mapping libraries. When a user clicks on a airport marker, we want an
info-window to disply useful information about the airport. This information
is already present in the attribute table of the ne_10m_airports
layers.
Right-click on the ne_10m_airports
layer and select
Properties.type
,
name
, iata_code
and wikipedia
fields and hide the others. Click
on Text Edit button under the Edit widget column
for `scalerank
field.Hidden
as the
type. Click OK.wikipedia
field.Web View
as the field type. This type indicates that the value
contained in this field is a URL.Virtual Field
. In our case, if we wanted the type
field to
appear at the end of the info window, we can simply add a new virtual field
the end and hide the original type
field. While we are at it - we can
also use an expression to better format the type values. Right-click the
ne_10m_airports
layer and choose Properties. Go to the
Fields tab and click Field Calculator.Type
as the new field name.
Set the field type to Text (String)
with a length of 25
characters.
The field type
contains values such as small
, mid
, large
etc. We can add an expression to change the case of the words to sentence
case and append the word airport for better readability. Enter the
following expression in the Expression box and click
OK.concat( title("type"), ' Airport')
Type
field, you can go ahead and
set the Edit Widget for type
field to Hidden
.ne_10m_airports
layer and select
Properties. Switch to the Style tab. Choose
Categorized
style and our virtual field Type
as the
Column. Click Classify.Ctrl
key and select all categories for
military airports. Once selected, click Delete.3
for the Large Airport
category.2
for Mid Airport
and 1
for Small Airport
.Show
labels for this layer
and choose iata_code
as the value for
Label with. We will also set Rendering option so
that the labels only appear when the user is sufficiently zoomed in. Check
Scale-based visibility under Label options. Enter
1
as the Minimum scale and 10000000
as the maximum
scale. This setting will render the labels only after the user has zoomed
in more than 1:10000000
scale and will be visible till 1:1
scale.Cartographic
. Select From symbol bounds
as Distance
offset from. Click OK.Airports
as the name of the
project.qgis2web
plugin by going to (See Using Plugins for more details on installing
plugins in QGIS). Once the plugin is installed, go to .ne_10m_airports: iata_code
as the field for Label
search. Check the Show popups on hover to allow display of
info-windows on hover. We can also set a basemap so the users have more
context when looking at the airports layer. Select OSM B&W
to use a
black-and-white themed basemap create using OpenStreetMap data. You also
have an option to choose from either OpenLayers
or Leaflet
as the
web mapping library. We will restrict this tutorial to use the
OpenLayers
library. Click Update Preview` to see how your
exported map will look like. Before we do the actual export, we need to set
the Export folder. You can select a folder of your choice and
click Export.qgis2web
plugin has many limitations and it cannot do everything
that the powerful web mapping libraries OpenLayers
and Leaflet
can
do. This process can act as the starting point in your web mapping process
and save you valuable time by creating a basic template from which you can
further customize the web map. To highlight the fact that the output
created from this process can be readily changed to suit your requirement -
we will make a simple change to the web map to zoom to a particular airport
when the user initially loads the map. On your computer, go to the folder
where the web map was exported. Locate the resources
folder and open
qgis2web.js
file in a text editor.map.getView().fit()
function is called and
add the following code after that. This new line of code instructs the web
browser to center the map on the coordinates of Paris. Save the changes to
the qgis2web.js
file.map.getView().setCenter(ol.proj.fromLonLat([2.35, 48.85]))
OpenLayers
or Leaflet
libraries to
customize the web map.qgis-tutorials
and uploaded the
contents of my exported folder to a sub-folder named qgis2web
. You can
access the resulting map at
http://s3.amazonaws.com/qgis-tutorials/qgis2web/index.htmlThis work is licensed under a Creative Commons Attribution 4.0 International License