Reputation: 2095
I'm using postgresql and postgis plugin.
I have stored data with this scheme: table_id is primary key, properties is a jsonb and geom as geometry(GeometryZ,4326)
;
If I make this request: select table_id, properties, geom from nametable
return all info with table_id as integer, properties as jsonb and geom as geometry(GeometryZ,4326).
I would like a query where properties return table_id, a list of column where name of column is the key of value and his value and geom. For example, if properties has name and density return a response like this: table_id | name | density | geom |
, but I dont know the name of properties so I think that should be a query inside that query that get name of keys.
The closest I've been to get it, its with this query: select jsonb_object_keys(properties) as key from nametable;
Thanks
EDIT:
First, I have stored a field as jsonb in my postgressql database, so I would like extract that jsonb to columns. But have stored differents tables that contains differents properties into jsonb column.
So, the idea is get a query where select table_id, properties(extracted in multiple columns) and geom.
1 - With this I have the name of keys: select jsonb_object_keys(properties) as key from nametable group by key;
2 - With keys get in column all values of each key.
3 - Return a query where when I call it, return me table_id, column of properties extracted from jsonb, geom from nametable;
My problem is that I dont know how generate that query with sub-queries.
Upvotes: 5
Views: 2107
Reputation: 2095
Thanks all people that help me.
Here is the code with queries to work with a tile server.
I used this query to get an array of keys:
const sql =
SELECT ARRAY_AGG(f) as keys FROM (SELECT jsonb_object_keys(properties) f FROM ${options.layerName} group by f) u
;
And later, a function to create a query to get each property as a column called generateSQL
,
/** CONSTANTS **/
const TILE_SIZE = 256;
const PROJECTION_STRING = '+init=epsg:3857';
/** LIBRARIES **/
var zlib = require('zlib');
var express = require('express');
var mapnik = require('mapnik');
var Promise = require('promise');
var SphericalMercator = require('sphericalmercator');
const { pool } = require('../postgressql/config');
var mercator = new SphericalMercator({
size: TILE_SIZE
});
mapnik.register_default_input_plugins();
var app = express();
app.get('/:namelayer/:z/:x/:y.pbf', (req, res, next) => {
var options = {
x: parseInt(req.params.x),
y: parseInt(req.params.y),
z: parseInt(req.params.z),
layerName: req.params.namelayer
};
const sql = `SELECT ARRAY_AGG(f) as keys FROM (SELECT jsonb_object_keys(properties) f FROM ${options.layerName} group by f) u`;
try {
pool.query(sql, (error, results) => {
if (error) {
return res.status(500).json({
ok: false,
message: error
});
}
const keys = (results && results.rows && results.rows.length > 0 && results.rows[0].keys && results.rows[0].keys.length >0) ? results.rows[0].keys.slice() : [];
const sql = generateSQL(options, keys);
makeVectorTile(options, sql).then( (vectorTile) => {
zlib.deflate(vectorTile, (err, data) => {
if (err) {
return res.status(500).send(err.message);
}
res.setHeader('Content-Encoding', 'deflate');
res.setHeader('Content-Type', 'application/x-protobuf');
res.setHeader('Access-Control-Allow-Origin', '*');
return res.send(data);
});
});
});
} catch (e) {
res.status(404).send({
error: e.toString(),
});
}
});
function generateSQL(options, keys) {
if (keys.length === 0) {
return `select table_id, geom from ${options.layerName}`;
} else {
let sql = "";
keys.forEach( key => {
sql = sql + `(properties->>'${key}') as ${key},`;
});
sql = `select table_id, ${sql} geom from ${options.layerName}`
return sql;
}
};
function makeVectorTile(options, sql) {
var extent = mercator.bbox(options.x, options.y, options.z, false, '3857');
var map = new mapnik.Map(TILE_SIZE, TILE_SIZE, PROJECTION_STRING);
map.extent = extent;
var layer = new mapnik.Layer(options.layerName);
layer.datasource = new mapnik.Datasource({
type: process.env.DB_TYPE,
dbname: process.env.DB_DATABASE,
// table: options.layerName,
table: `(${sql}) as tile`,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
});
layer.styles = ['default'];
map.add_layer(layer);
return new Promise( (resolve, reject) => {
var vtile = new mapnik.VectorTile(parseInt(options.z), parseInt(options.x), parseInt(options.y));
map.render(vtile, function (err, vtile) {
if (err) {
return reject(err);
}
console.log(`${vtile.getData().length} KB`);
resolve(vtile.getData());
});
});
};
module.exports = app;
Upvotes: 2