Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip_verbose: add support for lang query parameter #79

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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