Skip to content

Instantly share code, notes, and snippets.

@maxcollombin
Created August 24, 2023 12:56
Show Gist options
  • Save maxcollombin/cf7a54a13dea99df5e8e55a35b835da4 to your computer and use it in GitHub Desktop.
Save maxcollombin/cf7a54a13dea99df5e8e55a35b835da4 to your computer and use it in GitHub Desktop.
This script allows to convert a WKT geometry to a BBOX string with WIDTH and HEIGHT to be used in a WMS request.
-- WKT TO BBOX
-- This script allows to convert a WKT geometry to a BBOX string with WIDTH and HEIGHT to be used in a WMS request.
WITH input_geom AS (
SELECT ST_SetSRID('POLYGON((7.210196256637572 46.079631239023286,7.2218477725982675 46.07266495408345,7.22425103187561 46.07465966398232,7.212899923324584 46.08162569712192,7.210196256637572 46.079631239023286))'::geometry, 4326) AS geom
)
SELECT
'BBOX='||
ST_XMin(ST_Transform(geom, 2056)) ||','||
ST_YMin(ST_Transform(geom, 2056)) ||','||
ST_XMax(ST_Transform(geom, 2056)) ||','||
ST_YMax(ST_Transform(geom, 2056)) ||'&WIDTH='||
ROUND(ST_XMax(ST_Transform(geom, 2056))-ST_XMin(ST_Transform(geom, 2056)))||'&HEIGHT='||
ROUND(ST_YMax(ST_Transform(geom, 2056))-ST_YMin(ST_Transform(geom, 2056))) AS BBOX_W_H
FROM input_geom;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment