Skip to content

Commit

Permalink
Added detection, processing and serving of mapbox style json files.
Browse files Browse the repository at this point in the history
  • Loading branch information
panaaj committed Sep 27, 2024
1 parent d5919ba commit b34da10
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### 3.1.0

- **Added**: Detection, processing and serving of mapbox style json files. Files served from `/chart-styles`

- **Updated**: Moved the map tile url path to be out from under `resources/charts` to `/chart-tiles`. This better aligns with v2 multiple-provider support.

- **Updated**: Tile url value is now a fully qualified url rather than a relative path.
Expand Down
37 changes: 35 additions & 2 deletions src/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ export function findCharts(chartBaseDir: string) {
const isMbtilesFile = file.name.match(/\.mbtiles$/i)
const filePath = path.resolve(chartBaseDir, file.name)
const isDirectory = file.isDirectory()
const isMbstylesFile = file.name.match(/\.json$/i)
if (isMbtilesFile) {
return openMbtilesFile(filePath, file.name)
} else if (isDirectory) {
return directoryToMapInfo(filePath, file.name)
} else if (isMbstylesFile) {
return openMbstylesFile(filePath, file.name)
} else {
return Promise.resolve(null)
}
Expand Down Expand Up @@ -80,13 +83,13 @@ function openMbtilesFile(file: string, filename: string) {
type: 'tilelayer',
scale: parseInt(res.metadata.scale) || 250000,
v1: {
tilemapUrl: `~basePath~/${identifier}/{z}/{x}/{y}`,
tilemapUrl: `~basePath~/~tilePath~/${identifier}/{z}/{x}/{y}`,
chartLayers: res.metadata.vector_layers
? parseVectorLayers(res.metadata.vector_layers)
: []
},
v2: {
url: `~basePath~/${identifier}/{z}/{x}/{y}`,
url: `~basePath~/~tilePath~/${identifier}/{z}/{x}/{y}`,
layers: res.metadata.vector_layers
? parseVectorLayers(res.metadata.vector_layers)
: []
Expand All @@ -101,6 +104,36 @@ function openMbtilesFile(file: string, filename: string) {
)
}

export function encStyleToId(filename: string) {
return filename.replace('.json', '').replaceAll(' ', '-').toLocaleLowerCase()
}

async function openMbstylesFile(file: string, filename: string) {
const json = JSON.parse(await fs.readFile(file, 'utf8'))
const identifier = encStyleToId(filename)
return {
_flipY: false,
name: json.name,
description: '',
identifier,
bounds: undefined,
minzoom: undefined,
maxzoom: undefined,
format: undefined,
type: 'mapstyleJSON',
scale: 250000,
_filePath: file,
v1: {
tilemapUrl: `~basePath~/~stylePath~/${filename}`,
chartLayers: undefined
},
v2: {
url: `~basePath~/~stylePath~/${filename}`,
layers: undefined
}
}
}

function parseVectorLayers(layers: Array<{ id: string }>) {
return layers.map((l) => l.id)
}
Expand Down
37 changes: 33 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as bluebird from 'bluebird'
import path from 'path'
import fs from 'fs'
import * as _ from 'lodash'
import { findCharts } from './charts'
import { findCharts, encStyleToId } from './charts'
import { ChartProvider, OnlineChartProvider } from './types'
import { Request, Response, Application } from 'express'
import { OutgoingHttpHeaders } from 'http'
Expand Down Expand Up @@ -38,6 +38,7 @@ const MIN_ZOOM = 1
const MAX_ZOOM = 24
let basePath: string
const chartTilesPath = 'chart-tiles'
const chartStylesPath = 'chart-styles'

module.exports = (app: ChartProviderApp): Plugin => {
let chartProviders: { [key: string]: ChartProvider } = {}
Expand Down Expand Up @@ -173,7 +174,7 @@ module.exports = (app: ChartProviderApp): Plugin => {
pluginStarted = true
basePath = `${app.config.ssl ? 'https' : 'http'}://localhost:${
'getExternalPort' in app.config ? app.config.getExternalPort() : 3000
}/${chartTilesPath}`
}`
app.debug('**basePath**', basePath)
app.setPluginStatus('Started')

Expand Down Expand Up @@ -230,6 +231,7 @@ module.exports = (app: ChartProviderApp): Plugin => {
const registerRoutes = () => {
app.debug('** Registering API paths **')

app.debug(`** Registering map tile path (${chartTilesPath} **`)
app.get(
`/${chartTilesPath}/:identifier/:z([0-9]*)/:x([0-9]*)/:y([0-9]*)`,
async (req: Request, res: Response) => {
Expand Down Expand Up @@ -264,6 +266,23 @@ module.exports = (app: ChartProviderApp): Plugin => {
}
)

app.debug(`** Registering MapBox styles path (${chartStylesPath} **`)
app.get(
`/${chartStylesPath}/:style`,
async (req: Request, res: Response) => {
const { style } = req.params
const identifier = encStyleToId(style)
const provider = chartProviders[identifier]
res.sendFile(provider._filePath)
/*res.json({
path: req.path,
style,
identifier,
file: provider._filePath
})*/
}
)

app.debug('** Registering v1 API paths **')

app.get(
Expand Down Expand Up @@ -384,10 +403,20 @@ const sanitizeProvider = (provider: ChartProvider, version = 1) => {
let v
if (version === 1) {
v = _.merge({}, provider.v1)
v.tilemapUrl = v.tilemapUrl.replace('~basePath~', basePath)
v.tilemapUrl = v.tilemapUrl
? v.tilemapUrl
.replace('~basePath~', basePath)
.replace('~stylePath~', chartStylesPath)
.replace('~tilePath~', chartTilesPath)
: ''
} else {
v = _.merge({}, provider.v2)
v.url = v.url ? v.url.replace('~basePath~', basePath) : ''
v.url = v.url
? v.url
.replace('~basePath~', basePath)
.replace('~stylePath~', chartStylesPath)
.replace('~tilePath~', chartTilesPath)
: ''
}
provider = _.omit(provider, [
'_filePath',
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export interface ChartProvider {
scale: number
v1?: {
tilemapUrl: string
chartLayers: string[]
chartLayers?: string[]
}
v2?: {
url: string
layers: string[]
layers?: string[]
}
bounds?: number[]
minzoom?: number
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es6",
"target": "ES2022",
"module": "commonjs",
"outDir": "./plugin",
"esModuleInterop": true,
Expand All @@ -21,7 +21,7 @@
"ignoreCompilerErrors": true,
"excludePrivate": true,
"excludeNotExported": true,
"target": "ES5",
"target": "ES2022",
"moduleResolution": "node",
"preserveConstEnums": true,
"stripInternal": true,
Expand Down

0 comments on commit b34da10

Please sign in to comment.