A collection of PostgreSQL functions which allow easily creating SVG graphics. The main goal of the API is to allow converting PostGIS geometries into styled SVG documents. The functions also support simple geometry generated without PostGIS.
psql < pg-svg-lib.sql
Sometimes function signatures can change.
The old function must be removed.
To generate DROP FUNCTION
commands use this query:
SELECT 'DROP FUNCTION ' || oid::regprocedure
FROM pg_proc
WHERE proname LIKE 'svg%' AND pg_function_is_visible(oid);
Creates an SVG doc element from an array of content elements.
content
- an array oftext
values output as the content of the<svg>
elementviewbox
- [optional] value of SVG viewBox attribute (x y width height
)- see
svgViewbox
- see
width
- [optional] width of viewheight
- [optional] height of viewstyle
- [optional] specifies CSS styling at the document level- see
svgStyle
- see
def
- [optional] specifies a definition
Returns an SVG viewBox
attribute value determined from the envelope of a geometry.
extent
- ageometry
providing the envelope to encode.
The PostGIS ST_Extent
aggregate function can be use to determine the overall
envelope of the geometries being encoded.
Returns an SVG linearGradient
definition element.
The element is provided as a def
to svgDoc
.
A CSS fill
property value can refer to the gradient using the specifier url(#id)
id
- the gradient idcolor1
- the start color of the gradientcolor2
- the end color of the gradient
Encodes a PostGIS geometry as an SVG shape.
geom
- geometry to encodeclass
- [optional] class attributeid
- [optional] id attributestyle
- [optional] style attribute value- see
svgStyle
- see
attr
- [optional] additional attributestitle
- [optional] title
Encodes an array of XY ordinates as an SVG polygon
.
pts
- array of X Y ordinatesclass
- [optional] class attributeid
- [optional] id attributestyle
- [optional] style attribute value- see
svgStyle
- see
attr
- [optional] additional attributestitle
- [optional] title
Encodes an array of XY ordinates as an SVG polygon
.
x
- X location of bottom-left cornery
- Y location of bottom-left cornerwidth
- rectangle widthheight
- rectangle heightclass
- [optional] class attributeid
- [optional] id attributestyle
- [optional] style attribute value- see
svgStyle
- see
attr
- [optional] additional attributestitle
- [optional] title
Encodes text starting at position loc
as an SVG text
element.
loc
- Point geometry giving location of textcontent
- text valueclass
- [optional] class attributeid
- [optional] id attributestyle
- [optional] style attribute valueattr
- [optional] additional attributestitle
- [optional] title
Relevant style CSS properties include:
text-anchor
- value ofstart | middle | end | inherit
font
- full font specifier. E.g.10px Verdana,Helvetica,Arial,sans-serif
font-style
- value ofnormal | italic | oblique
font-weight
- value ofnormal | bold | bolder | lighter | <number>
Encodes an array of name,value pairs as a string of SVG CSS name: value;
properties
param ...
- list of name/value pairs
Common styling CSS properties are given below. For full list see W3C SVG spec.
fill
- fill colorfill-opacity
- opacity of fill; value in [ 0,1 ]stroke
- line colorstroke-dasharray
- dashed line specifier, e.g.2,4,1,4
stroke-dashoffset
- offset of dashesstroke-width
- line widthfill-opacity
- opacity of stroke; value in [ 0,1 ]
CSS color specifiers include:
#RRGGBB
colorname
hsl(h,s,l)
- can use thesvgHSL
function to create thisurl(#id)
- reference to a gradient definition
Encodes Red,Bliue,Green values as a CSS HSL function rgb(R,G,B)
.
red
- a value in 0..255green
- a value in 0..255blue
- a value in 0..255
Encodes Hue,Saturation,Lightness values as a CSS HSL function hsl(H,S,L)
.
hue
- a hue value in degrees (from 0 through 360, with 0 = red, 120 = green, and 240 = blue)saturation
- [optional] a percentage, default 100lightness
- [optional] a percentage, default 50
Returns a random integer from the range [lo, hi] (inclusive)
Returns a random item from an array of integers