-
Notifications
You must be signed in to change notification settings - Fork 14
/
kml2struct.m
96 lines (83 loc) · 2.72 KB
/
kml2struct.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
function kmlStruct = kml2struct(kmlFile)
% kmlStruct = kml2struct(kmlFile)
%
% Import a .kml file as a vector array of shapefile structs, with Geometry, Name,
% Description, Lon, Lat, and BoundaryBox fields. Structs may contain a mix
% of points, lines, and polygons.
%
% .kml files with folder structure will not be presented as such, but will
% appear as a single vector array of structs.
%
%
[FID msg] = fopen(kmlFile,'rt');
if FID<0
error(msg)
end
txt = fread(FID,'uint8=>char')';
fclose(FID);
expr = '<Placemark.+?>.+?</Placemark>';
objectStrings = regexp(txt,expr,'match');
Nos = length(objectStrings);
for ii = 1:Nos
% Find Object Name Field
bucket = regexp(objectStrings{ii},'<name.*?>.+?</name>','match');
if isempty(bucket)
name = 'undefined';
else
% Clip off flags
name = regexprep(bucket{1},'<name.*?>\s*','');
name = regexprep(name,'\s*</name>','');
end
% Find Object Description Field
bucket = regexp(objectStrings{ii},'<description.*?>.+?</description>','match');
if isempty(bucket)
desc = '';
else
% Clip off flags
desc = regexprep(bucket{1},'<description.*?>\s*','');
desc = regexprep(desc,'\s*</description>','');
end
geom = 0;
% Identify Object Type
if ~isempty(regexp(objectStrings{ii},'<Point', 'once'))
geom = 1;
elseif ~isempty(regexp(objectStrings{ii},'<LineString', 'once'))
geom = 2;
elseif ~isempty(regexp(objectStrings{ii},'<Polygon', 'once'))
geom = 3;
end
switch geom
case 1
geometry = 'Point';
case 2
geometry = 'Line';
case 3
geometry = 'Polygon';
otherwise
geometry = '';
end
% Find Coordinate Field
bucket = regexp(objectStrings{ii},'<coordinates.*?>.+?</coordinates>','match');
% Clip off flags
coordStr = regexprep(bucket{1},'<coordinates.*?>(\s+)*','');
coordStr = regexprep(coordStr,'(\s+)*</coordinates>','');
% Split coordinate string by commas or white spaces, and convert string
% to doubles
coordMat = str2double(regexp(coordStr,'[,\s]+','split'));
% Rearrange coordinates to form an x-by-3 matrix
[m,n] = size(coordMat);
coordMat = reshape(coordMat,3,m*n/3)';
% define polygon in clockwise direction, and terminate
[Lat, Lon] = poly2ccw(coordMat(:,2),coordMat(:,1));
if geom==3
Lon = [Lon;NaN];
Lat = [Lat;NaN];
end
% Create structure
kmlStruct(ii).Geometry = geometry;
kmlStruct(ii).Name = name;
kmlStruct(ii).Description = desc;
kmlStruct(ii).Lon = Lon;
kmlStruct(ii).Lat = Lat;
kmlStruct(ii).BoundingBox = [[min(Lon) min(Lat);max(Lon) max(Lat)]];
end