Advertisement
Guest User

Untitled

a guest
Sep 9th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION export_to_geojson(
  2.   table_name TEXT,
  3.   geom_column TEXT DEFAULT 'geom')
  4. RETURNS TEXT AS
  5. $MAIN$
  6. DECLARE
  7.  json_text text;
  8. BEGIN
  9.  CREATE OR REPLACE FUNCTION row_to_geojson(
  10.    jrow JSONB,
  11.    geom_column TEXT DEFAULT 'geom')
  12.  RETURNS TEXT AS
  13.  $$
  14.  DECLARE
  15.   json_props jsonb;
  16.   json_geom jsonb;
  17.   json_type jsonb;
  18.  BEGIN
  19.   RAISE NOTICE 'jrow = ''%''', jrow;
  20.   RAISE NOTICE 'geom_column = ''%''', geom_column;
  21.   IF NOT jrow ? geom_column THEN
  22.     RAISE EXCEPTION 'geometry column ''%'' is missing', geom_column;
  23.   END IF;
  24.   json_geom := ST_AsGeoJSON((jrow ->> geom_column)::geometry)::jsonb;
  25.   json_geom := jsonb_build_object('geometry', json_geom);
  26.   json_props := jsonb_build_object('properties', jrow - geom_column);
  27.   json_type := jsonb_build_object('type', 'Feature');
  28.   return (json_type || json_geom || json_props)::text;
  29.  END;
  30.  $$
  31.  LANGUAGE plpgsql;
  32.  EXECUTE format('SELECT row_to_geojson(to_jsonb(%I.*), ''%I'') FROM %I', table_name, geom_column, table_name) INTO json_text;
  33.  return (json_text)::text;
  34.  
  35. END;
  36. $MAIN$
  37. LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement