-- Function: public.askml(text, text, geometry)

-- DROP FUNCTION public.askml(text, text, geometry);

CREATE OR REPLACE FUNCTION public.askml(name text, description text, the_geom geometry)
  RETURNS text AS
$BODY$
DECLARE
	result text;
BEGIN
	result := '<?xml version=''1.0'' encoding=''UTF-8''?>' || E'\n' ||
                  '<kml xmlns=''http://earth.google.com/kml/2.1''>' || E'\n' ||
                  '<Document>' || E'\n' ||
                  '<name>' || quote_literal(name) || '</name>' || E'\n' ||
                  '<description>' || quote_literal(description) || '</description>' || E'\n\n' ||
                  '<Style id=''defaultStyle''>' || E'\n' ||
                  '  <LineStyle>' || E'\n' ||
                  '    <color>ff00ff00</color>' || E'\n' ||
                  '    <width>1</width>' || E'\n' ||
                  '  </LineStyle>' || E'\n' ||
                  '  <PolyStyle>' || E'\n' ||
                  '    <color>5f00ff00</color>' || E'\n' ||
                  '  </PolyStyle>' || E'\n' ||
                  '</Style>' || E'\n\n' ||
                  '<Placemark>' || E'\n' ||
                  '<styleUrl>#defaultStyle</styleUrl>' || E'\n';
        
        result := result || askml(the_geom) || E'\n';

        result := result || 
                  '</Placemark>' || E'\n\n' ||
                  '</Document>' || E'\n' ||
                  '</kml>';
	RETURN result;
END;
$BODY$
  LANGUAGE 'plpgsql' IMMUTABLE;
