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 of strings output as the content of the<svg>
elementviewbox
- (opt) value of SVG viewBox attribute ( x y width height )width
(opt) - width of viewheight
(opt) - height of viewstyle
(opt) - specifies CSS styling at the document level- see
svgStyle
function
- see
def
(opt) - specifies a definition- see
svgLinearGradient
function
- see
Returns an SVG viewBox
attribute value determined from the envelope of a geometry.
The PostGIS ST_Extent
aggregate function can be use to determine the overall
envelope of the geometries being encoded.
extent
- (opt) ageometry
providing the envelope to encode.
Returns an SVG linearGradient
definition element.
The element is provided as a def
to svgDoc
.
The CSS fill
property value refers 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
- (opt) class attributeid
- (opt) id attributestyle
- (opt) style attribute valueattr
- (opt) additional attributestitle
- (opt) title
Encodes an array of XY ordinates as an SVG polygon
.
pts
- array of X Y ordinatesclass
- (opt) class attributeid
- (opt) id attributestyle
- (opt) style attribute valueattr
- (opt) additional attributestitle
- (opt) 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
- (opt) class attributeid
- (opt) id attributestyle
- (opt) style attribute valueattr
- (opt) additional attributestitle
- (opt) title
Encodes an array of XY ordinates as an SVG polygon
.
loc
- Point geometry giving location of textcontent
- text valueclass
- (opt) class attributeid
- (opt) id attributestyle
- (opt) style attribute valueattr
- (opt) additional attributestitle
- (opt) 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 H,S,L values as a CSS HSL function hsl(H,S,L)
Returns a random integer from the range [lo, hi] (inclusive)
Returns a random item from an array of integers