Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE OR REPLACE FUNCTION export_to_geojson(
- table_name TEXT,
- geom_column TEXT DEFAULT 'geom')
- RETURNS TEXT AS
- $MAIN$
- DECLARE
- json_text text;
- BEGIN
- CREATE OR REPLACE FUNCTION row_to_geojson(
- jrow JSONB,
- geom_column TEXT DEFAULT 'geom')
- RETURNS TEXT AS
- $$
- DECLARE
- json_props jsonb;
- json_geom jsonb;
- json_type jsonb;
- BEGIN
- RAISE NOTICE 'jrow = ''%''', jrow;
- RAISE NOTICE 'geom_column = ''%''', geom_column;
- IF NOT jrow ? geom_column THEN
- RAISE EXCEPTION 'geometry column ''%'' is missing', geom_column;
- END IF;
- json_geom := ST_AsGeoJSON((jrow ->> geom_column)::geometry)::jsonb;
- json_geom := jsonb_build_object('geometry', json_geom);
- json_props := jsonb_build_object('properties', jrow - geom_column);
- json_type := jsonb_build_object('type', 'Feature');
- return (json_type || json_geom || json_props)::text;
- END;
- $$
- LANGUAGE plpgsql;
- EXECUTE format('SELECT row_to_geojson(to_jsonb(%I.*), ''%I'') FROM %I', table_name, geom_column, table_name) INTO json_text;
- return (json_text)::text;
- END;
- $MAIN$
- LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement