source: issm/trunk/src/m/kml/kml_file_write.m@ 6561

Last change on this file since 6561 was 6561, checked in by jschierm, 14 years ago

Extracted general function kml_file_write.m from kml_mesh_write.m.

File size: 2.9 KB
Line 
1%
2% write a kml file of the kml objects.
3%
4% [fid]=kml_file_write(kobj,filek,indent)
5%
6% where the required input is:
7% kobj (kml_object, kml object to be written)
8% or
9% kobj (cell array, array of kml objects)
10%
11% the optional input is:
12% filek (char, name of .kml file)
13% or
14% filek (numeric, file ID of already-open file,
15% noting 1=stdout and 2=stderr)
16% indent (char, indention string)
17%
18% and the optional output is:
19% fid (numeric, file ID of still-open file)
20%
21function [fid]=kml_file_write(kobj,filek,indent)
22
23if ~nargin
24 help kml_file_write
25 return
26end
27
28%% process input data
29
30if ~iscell(kobj)
31 kobj={kobj};
32end
33
34fid=0;
35if ~exist('filek' ,'var') || (~ischar(filek) && ~isnumeric(filek))
36 filek='';
37elseif isnumeric(filek)
38 fid=filek;
39 if (fid == 1)
40 filek='stdout';
41 elseif (fid == 2)
42 filek='stderr';
43 else
44 filek='';
45 end
46end
47
48if ~exist('indent','var') || ~ischar(indent)
49 indent=' ';
50end
51
52%% write kml file
53
54% open file and write header data (if necessary)
55
56if ~fid
57 if isempty(filek)
58 filek=input('kml file to write? ','s');
59 end
60 [pathstr,name,ext,versn] = fileparts(filek);
61 if isempty(ext)
62 ext='.kml';
63 end
64 filek=fullfile(pathstr,[name ext versn]);
65
66 display(sprintf('Opening kml file ''%s''.',filek));
67 fid=fopen(sprintf('%s',filek),'w');
68 if (fid < 0)
69 error('File ''%s'' could not be opened.',filek);
70 end
71
72 fprintf(fid,'<?xml version="1.0" encoding="UTF-8"?>\n');
73 fprintf(fid,'<kml xmlns="http://www.opengis.net/kml/2.2">\n');
74end
75
76% write kml objects
77
78if ~isempty(filek)
79 display(sprintf('Writing to kml file ''%s'':',filek));
80else
81 display(sprintf('Writing to kml file id=%d:',fid));
82end
83for i=1:numel(kobj)
84 if isa(kobj{i},'kml_object')
85 display(sprintf(' Writing object %d of class ''%s'' and size %s.',...
86 i,class(kobj{i}),string_size(kobj{i})));
87 kml_write(kobj{i},fid,indent);
88 else
89 if ~isempty(inputname(1))
90 warning('Object ''%s{%d}'' is a ''%s'' class object, not ''%s''.',...
91 inputname(1),i,class(kobj{i}),'kml_object');
92 else
93 warning('Object {%d} is a ''%s'' class object, not ''%s''.',...
94 i,class(kobj{i}),'kml_object');
95 end
96 end
97end
98
99% write trailer data and close file (if necessary)
100
101if ~nargout && (fid >= 3)
102 fprintf(fid,'</kml>\n');
103
104 if (fclose(fid) < 0)
105 if ~isempty(filek)
106 error('File ''%s'' could not be closed.',filek);
107 else
108 error('File id=%d could not be closed.',fid);
109 end
110 else
111 if ~isempty(filek)
112 disp(['End of file ''' filek ''' successfully written.']);
113 else
114 disp(['End of file successfully written.']);
115 end
116 end
117end
118
119end
120
Note: See TracBrowser for help on using the repository browser.