diff --git a/updates/36/36012/utils/dml.sql b/updates/36/36012/utils/dml.sql index 20f85c301f..569378e10b 100644 --- a/updates/36/36012/utils/dml.sql +++ b/updates/36/36012/utils/dml.sql @@ -198,4 +198,31 @@ UPDATE config_param_system set value = gw_fct_json_object_set_key(value::json, ' UPDATE config_param_system set value = replace(value, 'sector_id >=0', 'sector_id >0') where parameter = 'basic_selector_tab_sector'; -UPDATE link SET muni_id = c.muni_id FROM connec c WHERE connec_id = feature_id; \ No newline at end of file +UPDATE link SET muni_id = c.muni_id FROM connec c WHERE connec_id = feature_id; + +-- 20/08/2024 +INSERT INTO config_function (id, function_name, "style", layermanager, actions) VALUES(3303, 'gw_fct_getinpdata', '{ + "style": { + "point": { + "style": "categorized", + "field": "fid", + "width": 2, + "transparency": 0.5 + }, + "line": { + "style": "categorized", + "field": "result_id", + "width": 2, + "transparency": 0.5 + }, + "polygon": { + "style": "categorized", + "field": "fid", + "width": 2, + "transparency": 0.5 + } + } +}'::json, NULL, NULL); + +INSERT INTO sys_function (id,function_name,project_type,function_type,input_params,return_type,descript,sys_role,"source") + VALUES (3310,'gw_fct_getinpdata','utils','function','json','json','The function retrieves GeoJSON data for nodes and arcs based on selected result IDs and returns it in a structured JSON format.','role_epa','core'); diff --git a/utils/fct/gw_fct_getinpdata.sql b/utils/fct/gw_fct_getinpdata.sql new file mode 100644 index 0000000000..5115eaddd6 --- /dev/null +++ b/utils/fct/gw_fct_getinpdata.sql @@ -0,0 +1,71 @@ +/* +This file is part of Giswater 3 +The program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. +This version of Giswater is provided by Giswater Association +*/ + +--FUNCTION CODE: 3310 + +CREATE OR REPLACE FUNCTION gw_fct_getinpdata(result_ids TEXT[]) +RETURNS json +LANGUAGE plpgsql +AS $function$ +DECLARE + v_status TEXT := 'Accepted'; + v_message TEXT := 'Data retrieved successfully'; + v_node JSON; + v_result_point JSON; + v_result_line JSON; + v_result_polygon JSON := '{"geometryType":"Polygon","features":[]}'; -- Without polygon, empty + +BEGIN + -- Check if result_ids is NULL or has an invalid value + IF result_ids IS NULL OR array_length(result_ids, 1) IS NULL THEN + RETURN gw_fct_json_create_return( + '{"status":"Failed", "message":{"level":3, "text":"Invalid input"}}'::json, 2218, null, null, null + ); + END IF; + + -- Get GeoJSON from rpt_inp_node (Point) + SELECT json_build_object( + 'geometryType', 'Point', + 'layerName', 'Rpt INP Node', + 'features', json_agg(ST_AsGeoJSON(t.*)::json) + ) + INTO v_node + FROM rpt_inp_node t + WHERE result_id = ANY(result_ids); + + -- Get GeoJSON from rpt_inp_arc (LineString) + SELECT json_build_object( + 'geometryType', 'LineString', + 'layerName', 'Rpt INP Arc', + 'features', json_agg(ST_AsGeoJSON(t.*)::json) + ) + INTO v_result_line + FROM rpt_inp_arc t + WHERE result_id = ANY(result_ids); + + -- Construct the JSON + RETURN gw_fct_json_create_return( + ('{"status":"' || v_status || '", "message":{"level":3, "text":"' || v_message || '"},' || + '"body":{"data":{' || + '"point":' || COALESCE(v_node::text, '{"geometryType":"Point","features":[]}') || ',' || + '"line":' || COALESCE(v_result_line::text, '{"geometryType":"LineString","features":[]}') || ',' || + '"polygon":' || COALESCE(v_result_polygon::text, '{"geometryType":"Polygon","features":[]}') || '}}}' + )::json, 2218, null, null, null + ); + +EXCEPTION + WHEN OTHERS THEN + RETURN gw_fct_json_create_return( + json_build_object( + 'status', 'Failed', + 'message', json_build_object( + 'level', 3, + 'text', replace(SQLERRM, '"', '\"') + ) + ), 2218, null, null, null + ); +END; +$function$; \ No newline at end of file