1 function [] = kml2text(fileName,outName,thinning_factor)
\r
2 %kml2text reads a Google Earth kml file (lat,lon,z) from a fire perimeter
\r
3 %file and saves a text file for the marching algorithm used in
\r
6 %% open the data file and find the beginning of the data
\r
7 %fileName='UT-SLD-HU2S Patch Springs 8-12-2013 2123.kml';
\r
10 fid=fopen(fileName);
\r
12 error('could not find input file')
\r
16 %% Read in data and look for the <coordinate> string
\r
19 readlines{i,:}=fgetl(fid);
\r
20 f{i}=findstr(readlines{i,:},'<coordinates>');
\r
21 if isempty(f{i}==1) % set to zero if no <coordinates> tag
\r
22 coord_tag_line(i)=0;
\r
23 else %set coord_tag_line to one for lines with <coordinates>
\r
24 coord_tag_line(i)=f{i};
\r
27 if readlines{i,:} == -1 % when passes through the end of the file fgetl returns -1 if that happens set done to 1 and finish teh loop
\r
32 %% find line numbers of the file where <coordinate> string and data are
\r
33 coord_tag_lines=find(coord_tag_line==4);
\r
34 %% find lines with <coordinate> string only, the lines with actual coordinates will be one below heance +1
\r
35 coord_lines=find(coord_tag_line==6)+1;
\r
39 %% read coordinate data lines w/o tags
\r
40 for ii=1:size(coord_lines)
\r
41 rawdata=readlines{coord_lines(ii)};
\r
42 alldata{ar} = rawdata(6:(size(rawdata,2)-16));
\r
47 %% get the data into neat vectors
\r
49 % turn alldata into regular vector so it is easier to work with
\r
50 data = cell2mat(alldata);
\r
51 % now find all commas
\r
52 fComma = strfind(data, ',');
\r
54 fSpace = strfind(data,' ');
\r
56 fTab = strfind(data,' ');
\r
60 % have to do first point seperately b/c line may not begin with a space
\r
62 lon(a) = str2num(data(1:fComma(fC)-1));
\r
63 lat(a) = str2num(data(fComma(fC)+1:fComma(fC+1)-1));
\r
64 z(a) = str2num(data(fComma(fC+1)+1:fSpace(1)-1));
\r
68 % go thru all the points in the line
\r
69 for fC = 3: 2: length(fComma)
\r
71 lon(a) = str2num(data(fSpace(fS)+1:fComma(fC)-1));
\r
72 lat(a) = str2num(data(fComma(fC)+1:fComma(fC+1)-1));
\r
73 if fS < length(fSpace)
\r
74 z(a) = str2num(data(fComma(fC+1)+1:fSpace(fS+1)-1 ));
\r
76 % have to handle last point seperatly b/c line may not end with
\r
78 z(a) = str2num(data(fComma(fC+1)+1:end ));
\r
84 %%Process points with the <coordinate> tag
\r
88 for ii=1:size(coord_tag_lines)
\r
89 rawdata=readlines{coord_tag_lines(ii)};
\r
90 alldata2{br} = rawdata(17:(size(rawdata,2)-14));
\r
94 % data2=cell2mat(alldata2);
\r
96 % fComma2 = strfind(data2, ',');
\r
98 % fSpace2 = strfind(data2,' ');
\r
100 % fTab2 = strfind(data2,' ');
\r
102 % lon_point(b) = str2num(data2(1:fComma2(fC)-1));
\r
103 % lat_point(b) = str2num(data2(fComma2(fC)+1:fComma2(fC+1)-1));
\r
104 % z_point(b) = str2num(data2(fComma2(fC+1)+1:end));
\r
110 lat=reshape(lat,max(a,b),min(a,b));
\r
111 lon=reshape(lon,max(a,b),min(a,b));
\r
112 z=reshape(z,max(a,b),min(a,b));
\r
116 fileid=fopen(outName,'w');
\r
117 for i=1:thinning_factor:size(lat,1);
\r
118 fprintf(fileid,'%21.16f %21.16f %1.0f\n',lon(i,1),lat(i,1),z(i,1));
\r
120 display ('Output file written successfuly')
\r