One might want to use this X3D conversion as a function, feeding geometry into a function and getting a page in return. In this way, we can reuse the code easily for other tables. Embodied in a function, X3D conversion is as follows:
CREATE OR REPLACE FUNCTION AsX3D_XHTML(geometry) RETURNS character varying AS $BODY$ SELECT regexp_replace( ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns= "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="chrome=1"/> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Point Cloud in a Browser</title> <link rel="stylesheet" type="text/css"
href="http://x3dom.org/x3dom/example/x3dom.css"/> <script type="text/javascript"
src="http://x3dom.org/x3dom/example/x3dom.js">
</script> </head> <body> <h1>Point Cloud in the Browser</h1> <p> Use mouse to rotate, scroll wheel to zoom, and control
(or command) click to pan. </p> <X3D xmlns="http://www.web3d.org/specifications/x3d-namespace"
showStat="false" showLog="false" x="0px" y="0px" width="800px"
height="600px"> <Scene> <Transform> <Shape>'|| ST_AsX3D($1) || '</Shape> </Transform> </Scene> </X3D> </body> </html> ', E'[\\n\\r]+' , '' , 'g' ) As x3dXHTML; $BODY$ LANGUAGE sql VOLATILE COST 100;
In order for the function to work, we need to first use ST_UNION on the geometry parameter to pass to the AsX3D_XHTML function:
copy(
WITH pts AS (
SELECT
PC_Explode(pa) AS pt
FROM giraffe
)
SELECT AsX3D_XHTML(ST_UNION(pt::geometry)) FROM pts) to stdout;
We can now very simply generate the appropriate XHTML directly from the command line or a web framework.