-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpos2kml.m
107 lines (97 loc) · 3.89 KB
/
pos2kml.m
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function pos2kml(lat,lon,hgt,att,out_file,opt)
% brief Generate google earth kml file
% author yinflying(yinflying@foxmail.com)
% date 2018-10-07
% Input: lat (nx1,double) : latitude vector
% lon (nx1,double) : longitude vector
% hgt (nx1,double or []) : height vector
% att (nx1,integer or []) : attributes vector, stand for
% different color points, only 7 color supported.
% out_file (string) : out kml file path and name
% opt (struct, optional): more options
% .line(0 or 1): show or hide line between points
% defalut 1
% .line_color(string): line color, html color, e.g.
% '#ff0000ff'
% .line_width(double): line width, e.g. 5 , default 2
% Call Example:
% 1. opt.line = 1; opt.line_color = '#FF00FFFF'; opt.line_width = 5;
% pos2kml(lat,lon,[],[],'out.kml',opt);
% 2. pos2kml(lat,lon,[],ones(length(lat),1),'out.kml');
%
% history: 2018/10/07 created
% 2018/10/13 add line support
% Default 7 color points(corresponding to 1~7 in att)
all_color = {'FF7093DB','FF00FF00','FF00FFFF','FFFFFF00','FFE47833',...
'FF0000FF','FF8E236B'};
if isempty(att)
att = zeros(length(lat),1);
end
if isempty(hgt)
hgt = zeros(length(lat),1);
end
if length(lat) ~= length(lon) || length(lat) ~= length(att)
fprintf('ERROR: vector length are not equal !\n');
end
[fdir,~,~] = fileparts(out_file);
if ~isempty(fdir) && ~exist(fdir,'dir')
mkdir(fdir);
end
if ~exist('opt','var') || ~isfield(opt,'line')
opt.line = 1;
end
if ~exist('opt','var') || ~isfield(opt,'line_color')
opt.line_color = '#FF00FFFF';
end
if ~exist('opt','var') || ~isfield(opt,'line_width')
opt.line_width = 2;
end
icon_url = 'http://maps.google.com/mapfiles/kml/pal2/icon18.png';
fileID = fopen(out_file,'w');
fprintf(fileID,'<?xml version="1.0" encoding="UTF-8"?>\n');
fprintf(fileID,'<kml xmlns="http://earth.google.com/kml/2.1">\n');
fprintf(fileID,'<Document>\n');
% Define Style
for i = 1:size(all_color,2)
fprintf(fileID,'<Style id="P%.0f">\n',i-1);
fprintf(fileID,' <IconStyle>\n');
fprintf(fileID,' <color>%s</color>\n',all_color{i});
fprintf(fileID,' <scale>0.2</scale>\n');
fprintf(fileID,' <Icon><href>%s</href></Icon>\n',icon_url);
fprintf(fileID,' </IconStyle>\n');
fprintf(fileID,'</Style>\n');
end
fprintf(fileID,'<Folder>\n');
fprintf(fileID,' <name>Rover Position</name>\n');
fprintf(fileID,'<Placemark>\n');
fprintf(fileID,'<LineString>\n');
fprintf(fileID,'<coordinates>\n');
for i = 1:size(lat,1)
fprintf(fileID,'%.9f,%.9f,%.9f\n',lon(i),lat(i),hgt(i));
end
fprintf(fileID,'</coordinates>\n');
fprintf(fileID,'</LineString>\n');
if opt.line == 1
fprintf(fileID,'<Style>\n');
fprintf(fileID,' <LineStyle>\n');
fprintf(fileID,' <color>%s</color>\n',opt.line_color);
fprintf(fileID,' <width>%f</width>\n',opt.line_width);
fprintf(fileID,' </LineStyle>\n');
fprintf(fileID,'</Style>\n');
end
fprintf(fileID,'</Placemark>\n');
for i = 1:size(lat,1)
fprintf(fileID,'<Placemark>\n');
fprintf(fileID,...
'<styleUrl>#P%.0f</styleUrl>\n',mod(att(i),length(all_color)));
fprintf(fileID,'<Point>\n');
fprintf(fileID,...
'<coordinates>%.9f,%.9f,%.9f</coordinates>\n',lon(i),lat(i),hgt(i));
fprintf(fileID,'</Point>\n');
fprintf(fileID,'</Placemark>\n');
end
fprintf(fileID,'</Folder>\n');
fprintf(fileID,'</Document>\n');
fprintf(fileID,'</kml>\n');
fclose(fileID);
end