Developing OpenLayers based maps

Introduction

What is 'OpenLayers'?

OpenLayers is an open-source JavaScript library for displaying maps and map data in web browsers. It is simmilar to Google Maps, Bing Maps, Leaflet and many other libraries. Like Leaflet it is open-source.
The library renders data like the OpenGIS Consortium’s Web Mapping Service (WMS), Web Feature Service (WFS) protocols and other standards like GeoJSON, GML and many others.

It was developed by a software company MetaCarta ahead of the O'Reilly 'Where 2.0' conference in June 2006, supposedly as an alternative to Google Maps.

Installing build tools for developing OpenLayers based maps on Ubuntu 22.04

Prerequisites on Ubuntu 22.04

Before installing the build tools and sources for developing OpenLayers based mappings on a plain Ubuntu 22.04 system make sure 'curl' and a version of 'node.js' later than version 14 are installed, for instance by running:
sudo apt install curl
sudo snap install node --classic --channel=19
Install npm:
curl -0 -L https://npmjs.org/install.sh | sudo sh
Warning! Be aware that it is risky to pipe a shell script fetched from the internet into a root shell, only do this from a site you trust and on a system that is disposable.
Then install 'git':
sudo apt install git

Install OpenLayers 'getting started'

Now you are able to create the OpenLayers 'getting started app':
npm create ol-app <your app name> 
cd <your app name>
In order to make the vite development server run on IP address 0.0.0.0 instead of 'localhost' add '--host' to the "start": "vite" line in 'package.json'.
And start the development server:
npm start
If you point your browser to the development server the result should look like:
Jezus lives!

Directory structure and files

At this moment the following directories and files will have been created in your application directory:
myapp/
├── .github
├── .gitignore
├── index.html
├── main.js
├── style.css
├── node_modules
│   ├── .bin
│   .
│   .
│   ├── xml-utils
│   └── zstddec
├── package.json
├── package-lock.json
├── readme.md
├── style.css
└── vite.config.js
The most important files are:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/x-icon" href="https://openlayers.org/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Using OpenLayers with Vite</title>
  </head>
  <body>
    <div id="map"></div>
    <script type="module" src="./main.js"></script>
  </body>
</html>
And

main.js

import './style.css';
import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';

const map = new Map({
  target: 'map',
  layers: [
    new TileLayer({
      source: new OSM()
    })
  ],
  view: new View({
    center: [0, 0],
    zoom: 2
  })
});
The main.js file shows some basic concepts of OpenLayers:

Under the hood

In order to gain some more understanding of the anatomy of the application you can look at the network traffic between your browser and the development server (depends on your browser, but in Firefox: run 'developer tools > network' and 'save as har' then filter the 'url' lines):

Jezus lives!
The traffic will look like:
          "url": "http://192.168.50.97:5173/",
          "url": "http://192.168.50.97:5173/@vite/client", ............
          "url": "http://192.168.50.97:5173/main.js",
          "url": "http://192.168.50.97:5173/style.css", 
          "url": "http://192.168.50.97:5173/node_modules/.vite/deps/ol.js?v=ef44a998", ........
		  
          "url": "http://192.168.50.97:5173/node_modules/.vite/deps/chunk-ZRN6TD7N.js?v=ef44a998",
          "url": "http://192.168.50.97:5173/node_modules/.vite/deps/chunk-N7KRV6Z5.js?v=ef44a998",
          "url": "http://192.168.50.97:5173/node_modules/.vite/deps/chunk-4TDXP2QV.js?v=ef44a998",
          "url": "https://tile.openstreetmap.org/3/4/4.png",
          "url": "https://tile.openstreetmap.org/3/4/3.png",
.
.
          "url": "https://tile.openstreetmap.org/3/5/5.png",
          "url": "https://tile.openstreetmap.org/3/2/5.png",
          "url": "https://openlayers.org/favicon.ico", ............. 
          "url": "https://tile.openstreetmap.org/3/6/3.png",
          "url": "https://tile.openstreetmap.org/3/6/4.png",
.
          "url": "https://tile.openstreetmap.org/3/0/5.png",
          "url": "https://tile.openstreetmap.org/3/0/2.png",
Your browser will load the index.html, main.js javascript and then the openlayer modules followed by the map image which is build of 'png' files which are retreived from the openstreetmap website.

How to get data

Get data (for example) here:
https://www.naturalearthdata.com/downloads/

This can be viewed using:
https://www.qgis.org/en/site/

And converted into geojson using:
https://gdal.org/programs/ogr2ogr.html

For instance:
ogr2ogr -f "GeoJSON" countries.ne.geojson ne_50m_admin_0_countries.shp

A good start to learn openlayers is the workshop:
https://openlayers.org/workshop/en/

Some links:

OpenLayers Quick Start
Great symple introduction to OpenLayers