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