Skip to content

Instantly share code, notes, and snippets.

View dbauszus-glx's full-sized avatar

Dennis Bauszus dbauszus-glx

View GitHub Profile
@dbauszus-glx
dbauszus-glx / cluster_1.sql
Created April 20, 2018 17:06
Cluster comparisson
SELECT
dbscan_cid,
st_union(geom) geom
FROM (
SELECT
ST_ClusterDBSCAN(geom, 0.23, 1) OVER () dbscan_cid,
geom
FROM gb_retailpoint
) kmeans GROUP BY dbscan_cid;
@dbauszus-glx
dbauszus-glx / cluster_2.sql
Created April 20, 2018 17:21
KMeans DBScan nested group by to json.
SELECT
SUM(count)::integer count,
JSON_Agg(JSON_Build_Object(cat, count)) cat,
ST_AsGeoJson(ST_PointOnSurface(ST_Union(geom))) geomj
FROM (
SELECT
COUNT(cat) count,
ST_Union(geom) geom,
cat,
kmeans_cid,
@dbauszus-glx
dbauszus-glx / mvt_creation.sql
Last active December 25, 2019 19:12
Create an MVT with properties from PostGIS and store in MVT cache table.
INSERT INTO dev.natural_earth_countries__mvts (z, x, y, mvt, tile)
SELECT
5,
10,
13,
ST_AsMVT(tile, 'COUNTRIES', 4096, 'geom') mvt,
ST_MakeEnvelope(
${-m + (x * r)},
${ m - (y * r)},
${-m + (x * r) + r},
create table if not exists natural_earth_countries__mvts
(
z integer not null,
x integer not null,
y integer not null,
mvt bytea,
tile geometry(Polygon,3857),
constraint dev_natural_earth_countries__mvts_z_x_y_pk
primary key (z, x, y)
)
module.exports = async (layer, table, id) => {
const q = `
DELETE FROM ${table}__mvts
WHERE
ST_Intersects(
tile,
(SELECT ${layer.geom_3857} FROM ${table} WHERE ${layer.qID} = $1)
);`;
// Set jsrender module for server-side templates.
const jsr = require('jsrender');
module.exports = {route, view};
function route(fastify) {
fastify.route({
method: 'GET',
url: '/',
const env = require('../mod/env');
const sql_filter = require('../mod/pg/sql_filter');
const sql_fields = require('../mod/pg/sql_fields');
fastify.route({
method: 'GET',
url: '/api/layer/table',
preValidation: fastify.auth([
const env = require('../mod/env');
module.exports = fastify => (req, res, next, access = {}) => {
// Delete query token param if string 'null' instead of null.
if (req.query.token === 'null') {
delete req.query.token;
}
// Public access without token.
const fastify = require('fastify')({
trustProxy: true,
logger: {
level: process.env.LOG_LEVEL || 'error',
prettifier: require('pino-pretty'),
prettyPrint: {
errorProps: 'hint, detail',
levelFirst: true,
crlf: true
}
@dbauszus-glx
dbauszus-glx / OL.selectFeatureById.js
Created June 15, 2019 15:35
Select a feature at pixel from XYZ endpoint with OpenLayers
let currentFeature;
map.on('click', select);
function select(e) {
sourceVector.clear();
map.removeInteraction(drawInteraction);