-
Notifications
You must be signed in to change notification settings - Fork 4
/
write_files_with_plpython.sql
50 lines (39 loc) · 1.69 KB
/
write_files_with_plpython.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
---------------------------------------------
--Copyright Remi-C 2014
--
-- writting files with python
--This script expects a postgres >= 9.3, Postgis >= 2.0.2 , pointcloud
--------------------------------------------
--found on internet then modified
DROP FUNCTION IF EXISTS write_file (param_bytes bytea, param_filepath text, chmod character varying (4)) ;
CREATE OR REPLACE FUNCTION write_file (param_bytes bytea, param_filepath text, chmod character varying (4))
RETURNS text
AS $$
import os
f = open(param_filepath, 'wb')
chmod_oct = int(chmod,8) # new addition (converts chmod octal code to associated integer value)
os.chmod(param_filepath,chmod_oct) # new addition (changes read/write permissions)
f.write(param_bytes)
f.close() # new addition (ensures file is closed after writing)
return param_filepath
$$ LANGUAGE plpythonu;
DROP FUNCTION IF EXISTS write_file_texte (param_bytes text, param_filepath text, chmod character varying (4));
CREATE OR REPLACE FUNCTION write_file_texte (param_bytes text, param_filepath text, chmod character varying (4))
RETURNS text
AS $$
import os
f = open(param_filepath, 'w')
chmod_oct = int(chmod,8) # new addition (converts chmod octal code to associated integer value)
os.chmod(param_filepath,chmod_oct) # new addition (changes read/write permissions)
f.write(param_bytes)
f.close() # new addition (ensures file is closed after writing)
return param_filepath
$$ LANGUAGE plpythonu;
--Here’s an example of how to use this function:
/*
SELECT write_file(ST_AsTIFF(ST_SetSRID(ST_Transform(rast,931008),931008)), '/tmp/rast_' || rid || '.tif','777')
FROM test_raster.temp_test_interpolation ;
-- SELECT *
-- FROM spatial_ref_sys
-- WHERE proj4text ILIKE '%LAMB93%'
*/