Skip to content

Commit

Permalink
Map layer now using OS maps
Browse files Browse the repository at this point in the history
This aligns with the map layer from the main website.
  • Loading branch information
DilwoarH committed Jan 3, 2025
1 parent a93d20a commit c61b6f1
Show file tree
Hide file tree
Showing 6 changed files with 11,471 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"start:development": "NODE_ENV=development node index.js",
"start:local:watch": "NODE_ENV=test npm run start:watch",
"mock:api": "node ./test/mock-api/index.js",
"build": "npm run scss && cp -R src/assets/downloadable/. public/downloadable && npm run build-webpack",
"build": "npm run scss && cp -R src/assets/downloadable/. public/downloadable && cp -R src/assets/static/. public/static && npm run build-webpack",
"build-webpack": "webpack",
"serve-webpack": "webpack serve",
"scss": "sass --quiet-deps --load-path=./ src/assets/scss:public/stylesheets",
Expand Down
24 changes: 22 additions & 2 deletions src/assets/js/map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import parse from 'wellknown'
import maplibregl from 'maplibre-gl'
import { getApiToken } from './os-api-token.js'

const lineColor = '#000000'
const fillColor = '#008'
Expand Down Expand Up @@ -30,10 +31,26 @@ export class Map {
this.bbox = opts.boundingBox ?? null
this.map = new maplibregl.Map({
container: opts.containerId,
style: 'https://api.maptiler.com/maps/basic-v2/style.json?key=ncAXR9XEn7JgHBLguAUw',
style: '/public/static/OS_VTS_3857_3D.json',
zoom: 11,
center: [-0.1298779, 51.4959698],
interactive: opts.interactive ?? true
interactive: opts.interactive ?? true,
transformRequest: (url, resourceType) => {
if (url.indexOf('api.os.uk') > -1) {
if (!/[?&]key=/.test(url)) url += '?key=null'

const requestToMake = {
url: url + '&srs=3857'
}

const token = getApiToken()
requestToMake.headers = {
Authorization: 'Bearer ' + token
}

return requestToMake
}
}
})

// Add map controls
Expand Down Expand Up @@ -293,6 +310,9 @@ export const createMapFromServerContext = async () => {
wktFormat: geoJsonUrl === undefined
}

// fetch initial token
await getApiToken()

// if the geoJsonUrl is provided, generate the paginated GeoJSON links
if (geoJsonUrl) {
options.data = await generatePaginatedGeoJsonLinks(geoJsonUrl)
Expand Down
33 changes: 33 additions & 0 deletions src/assets/js/os-api-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// copied from https://github.com/digital-land/digital-land.info/blob/main/assets/javascripts/osApiToken.js

const API_ACCESS_TOKEN_URL = '/api/os/get-access-token'
let apiToken = {
access_token: '',
expires_in: 0,
issued_at: 0
}

let makingRequest = false

export const getApiToken = () => {
const tokenCheckBuffer = 30 * 1000
const tokenExpires = parseInt(apiToken.expires_in) * 1000 + parseInt(apiToken.issued_at)
if (Date.now() > tokenExpires - tokenCheckBuffer && !makingRequest) {
getFreshApiToken()
}
return apiToken.access_token
}

export const getFreshApiToken = () => {
return new Promise((resolve, reject) => {
makingRequest = true
fetch(API_ACCESS_TOKEN_URL)
.then(res => res.json())

Check failure on line 25 in src/assets/js/os-api-token.js

View workflow job for this annotation

GitHub Actions / run-tests / test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'then') ❯ src/assets/js/os-api-token.js:25:7 ❯ getFreshApiToken src/assets/js/os-api-token.js:22:10 ❯ Module.getApiToken src/assets/js/os-api-token.js:16:5 ❯ Module.createMapFromServerContext src/assets/js/map.js:314:9 ❯ test/unit/javascript/map.test.js:164:25 ❯ node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/@vitest/runner/dist/index.js:61:7 ❯ runTest node_modules/@vitest/runner/dist/index.js:986:17 This error originated in "test/unit/javascript/map.test.js" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "test/unit/javascript/map.test.js". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 25 in src/assets/js/os-api-token.js

View workflow job for this annotation

GitHub Actions / test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'then') ❯ src/assets/js/os-api-token.js:25:7 ❯ getFreshApiToken src/assets/js/os-api-token.js:22:10 ❯ Module.getApiToken src/assets/js/os-api-token.js:16:5 ❯ Module.createMapFromServerContext src/assets/js/map.js:314:9 ❯ test/unit/javascript/map.test.js:164:25 ❯ node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/@vitest/runner/dist/index.js:61:7 ❯ runTest node_modules/@vitest/runner/dist/index.js:986:17 This error originated in "test/unit/javascript/map.test.js" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "test/unit/javascript/map.test.js". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
.then(res => {
apiToken = res
makingRequest = false
resolve(apiToken.access_token)
}
)
})
}
Loading

0 comments on commit c61b6f1

Please sign in to comment.