Skip to content

Commit

Permalink
remove mvt_cache reference; create mvt query
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Nov 20, 2023
1 parent 4cdeddf commit b62c156
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 40 deletions.
22 changes: 19 additions & 3 deletions lib/layer/format/mvt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default layer => {
// Assign empty style object if nullish.
layer.style ??= {}

// Set default layer params if nullish.
layer.params ??= {}

layer.reload = () => {

//layer.source.clear()
Expand Down Expand Up @@ -42,13 +45,26 @@ export default layer => {

if ((!tableZ || !layer.display) && !layer.clones?.size) return layer.source.clear()

const url = `${layer.mapview.host}/api/layer/mvt/${tileCoord[0]}/${tileCoord[1]}/${tileCoord[2]}?` + mapp.utils.paramString({
// Create a set of feature properties for styling.
layer.params.fields = [...new Set([
// Array.isArray(layer.style.theme?.fields) ?
// layer.style.theme.fields : layer.style.theme?.field,
layer.style.theme?.field,
layer.style.label?.field
].flat().filter(field => !!field))]

const url = `${layer.mapview.host}/api/query?${mapp.utils.paramString({
template: 'mvt',
z: tileCoord[0],
x: tileCoord[1],
y: tileCoord[2],
locale: layer.mapview.locale.key,
layer: layer.key,
srid: layer.mapview.srid,
table: tableZ,
filter: layer.filter?.current
})
filter: layer.filter?.current,
...layer.params
})}`

return url
}
Expand Down
25 changes: 0 additions & 25 deletions lib/location/decorate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default location => {
trash,
update,
syncFields,
mvt_cache,
updateCallbacks: [],
})

Expand Down Expand Up @@ -49,8 +48,6 @@ async function update() {

if (!Object.keys(newValues).length) return;

await this.mvt_cache()

await mapp.utils.xhr({
method: 'POST',
url:
Expand All @@ -64,8 +61,6 @@ async function update() {
body: JSON.stringify(newValues),
});

await this.mvt_cache()

// Update entry.values with newValues.
// Return dependents from updated entries.
const dependents = this.infoj
Expand Down Expand Up @@ -126,30 +121,10 @@ function flyTo (maxZoom) {
});
}

async function mvt_cache() {

if (!this.layer?.mvt_cache) return;

await mapp.utils.xhr(`${this.layer.mapview.host}/api/query?` +
mapp.utils.paramString({
template: 'mvt_cache_delete_intersects',
locale: this.layer.mapview.locale.key,
layer: this.layer.key,
mvt_cache: this.layer.mvt_cache,
table: this.table,
qID: this.layer.qID,
id: this.id,
geom: this.layer.geom
}))
}

async function trash() {

if(!confirm(mapp.dictionary.confirm_delete)) return;

// Must clear cache before removing location from source.
await this.mvt_cache()

await mapp.utils.xhr(`${this.layer.mapview.host}/api/location/delete?` +
mapp.utils.paramString({
locale: this.layer.mapview.locale.key,
Expand Down
2 changes: 0 additions & 2 deletions lib/location/get.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ export default async function (location, list = location.layer.mapview.locations

mapp.location.decorate(Object.assign(location, { infoj }))

location.new && await location.mvt_cache()

// Assign location to mapview.
list[location.hook] = location

Expand Down
5 changes: 5 additions & 0 deletions mod/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ module.exports = async (req, res) => {
return res.send(rows.map(row=>Object.values(row)))
}

if (req.params.mvt || template?.mvt) {

return res.send(rows[0].mvt)
}

// Send the infoj object with values back to the client.
res.send(rows.length === 1 && rows[0] || rows)

Expand Down
40 changes: 40 additions & 0 deletions mod/workspace/templates/mvt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = _ => {

// Get fields array from query params.
const fields = _.fields?.split(',')
.map(field => _.workspace.templates[field]?.template || field)
.filter(field => !!field)

// Push label (cluster) into fields
_.label && field.push(_.workspace.templates[_.label]?.template || _.label)

let
x = parseInt(_.x),
y = parseInt(_.y),
z = parseInt(_.z)

const tile = `
SELECT
ST_AsMVT(tile, '${_.layer.key}', 4096, 'geom') mvt
FROM (
SELECT
${_.layer.qID || null} as id,
${Array.isArray(fields) ? fields.toString() + ',' : ''}
ST_AsMVTGeom(
${_.geom || _.layer.geom},
ST_TileEnvelope(${z},${x},${y}),
4096,
1024,
false
) geom
FROM ${_.table}
WHERE
${_.layer.z_field && `${_layer.z_field} < ${z} AND` ||''}
ST_Intersects(
ST_TileEnvelope(${z},${x},${y}),
${_.geom || _.layer.geom}
)
) tile`

return tile
}
7 changes: 0 additions & 7 deletions mod/workspace/templates/mvt_cache_delete_intersects.js

This file was deleted.

7 changes: 4 additions & 3 deletions mod/workspace/templates/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ module.exports = {
layer_extent: {
template: require('./layer_extent'),
},
mvt: {
render: require('./mvt'),
mvt: true
},
mvt_cache: {
admin: true,
render: require('./mvt_cache'),
},
mvt_cache_delete_intersects: {
template: require('./mvt_cache_delete_intersects'),
}
}

0 comments on commit b62c156

Please sign in to comment.