Skip to content

Commit

Permalink
feat(pip_verbose): add support for lang query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Sep 1, 2022
1 parent d0dfbd8 commit 7e96c36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 10 additions & 0 deletions module/pip/StatementPointInPolygonVerbose.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ class StatementPeliasView extends SqliteStatement {
AND abbr = 0
LIMIT 1
) AS name,
(
SELECT name
FROM name
WHERE source = place.source
AND id = place.id
AND lang = @lang
AND tag IN ('default', 'preferred')
AND abbr = 0
LIMIT 1
) AS name_localized,
(
SELECT GROUP_CONCAT(name, CHAR(30))
FROM (
Expand Down
14 changes: 10 additions & 4 deletions server/routes/pip_verbose.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const util = require('./util')
const iso6393 = require('iso-639-3')
const language = {}
iso6393.filter(i => !!i.iso6391 && !!i.iso6393).forEach(i => { language[i.iso6391] = i.iso6393 })

/**
* a verbose view of the PIP data
Expand All @@ -9,13 +12,15 @@ const util = require('./util')
module.exports = function (req, res) {
var service = req.app.locals.service

const lang = util.flatten(req.query.lang)
// inputs
let query = {
lon: parseFloat(util.flatten(req.query.lon)),
lat: parseFloat(util.flatten(req.query.lat)),
limit: 1000,
aliaslimit: parseInt(util.flatten(req.query.aliaslimit), 10) || 0,
wofonly: util.flatten(req.query.wofonly) ? 1 : 0
wofonly: util.flatten(req.query.wofonly) ? 1 : 0,
lang: language[lang] || lang || 'und'
}

// perform query
Expand All @@ -27,20 +32,21 @@ module.exports = function (req, res) {
let resp = {}
rows.forEach(row => {
let centroid = row.centroid.split(',').map(util.floatPrecision7)
let name = row.name_localized || row.name || undefined

let nameAlias = []
if (query.aliaslimit > 0) { nameAlias = (row.names || '').split(String.fromCharCode(30)) }
nameAlias = (nameAlias.length > 1) ? nameAlias.filter(n => n !== row.name).slice(0, query.aliaslimit) : undefined
nameAlias = (nameAlias.length > 1) ? nameAlias.filter(n => n !== name).slice(0, query.aliaslimit) : undefined

let abbrAlias = []
if (query.aliaslimit > 0) { abbrAlias = (row.abbrs || '').split(String.fromCharCode(30)) }
abbrAlias = (abbrAlias.length > 1) ? abbrAlias.filter(n => n !== row.name).slice(0, query.aliaslimit) : undefined
abbrAlias = (abbrAlias.length > 1) ? abbrAlias.filter(n => n !== name).slice(0, query.aliaslimit) : undefined

if (!Array.isArray(resp[row.type])) { resp[row.type] = [] }
resp[row.type].push({
id: row.id,
source: row.source,
name: row.name || undefined,
name: name,
name_alias: nameAlias,
abbr: row.abbr || undefined,
abbr_alias: abbrAlias,
Expand Down

0 comments on commit 7e96c36

Please sign in to comment.