Skip to content

Commit

Permalink
feat(tile): allow building tiles from idSource with attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
fxi committed Feb 24, 2025
1 parent a0f7e5b commit d70d7f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion api/modules/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isString, isEmpty } from "@fxi/mx_valid";
import { isArray, isString, isEmpty, isNotEmpty } from "@fxi/mx_valid";
import path from "path";
import fs from "fs";
import zlib from "zlib";
Expand Down Expand Up @@ -285,6 +285,7 @@ function attrToPgCol(attribute = "gid", attributes = [], opt) {
attributes.push("gid");
}

attributes = attributes.filter(isNotEmpty);
return toPgColumn(getDistinct(attributes));
}

Expand Down
37 changes: 27 additions & 10 deletions api/modules/tile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { redisGet, redisSet, pgRead } from "#mapx/db";
import { parseTemplate, attrToPgCol, asArray } from "#mapx/helpers";
import { isSourceId, isEmpty, isBoolean } from "@fxi/mx_valid";
import { isSourceId, isViewId, isEmpty, isBoolean } from "@fxi/mx_valid";
import { templates } from "#mapx/template";
import { getSourceLastTimestamp } from "#mapx/db_utils";
import { getParamsValidator } from "#mapx/route_validation";
Expand All @@ -18,8 +18,16 @@ const gzip = util.promisify(zlib.gzip);
* Get tile
*/
const validateParamsHandlerText = getParamsValidator({
required: ["idView"],
expected: ["timestamp", "skipCache", "usePostgisTiles"],
required: [],
expected: [
"idView",
"timestamp",
"skipCache",
"usePostgisTiles",
"idSource",
"attribute",
"attributes",
],
});

export const mwGet = [validateParamsHandlerText, handlerTile];
Expand All @@ -29,14 +37,24 @@ export default { mwGet };
export async function handlerTile(req, res) {
try {
const data = req.query;
const sqlViewInfo = templates.getViewSourceAndAttributes;
const resultView = await pgRead.query(sqlViewInfo, [data.idView]);
const modeView = isViewId(data.idView);

if (resultView.rowCount !== 1) {
throw Error("Error fetching view source and attribute");
}
if (modeView) {
const sqlViewInfo = templates.getViewSourceAndAttributes;
const resultView = await pgRead.query(sqlViewInfo, [data.idView]);

if (resultView.rowCount !== 1) {
throw Error("Error fetching view source and attribute");
}

const [viewSrcConfig] = resultView.rows;

const [viewSrcConfig] = resultView.rows;
Object.assign(data, viewSrcConfig);
} else {
if (isSourceId(data.idSource)) {
data.layer = data.idSource;
}
}

/*
* viewSrcAttr attributes:
Expand All @@ -46,7 +64,6 @@ export async function handlerTile(req, res) {
* mask (optional) : secondary source to use as mask
* usePostgisTiles
*/
Object.assign(data, viewSrcConfig);

data.geom = "geom";
/*
Expand Down

0 comments on commit d70d7f9

Please # to comment.