?? kml2mif.asv
字號(hào):
function Kml2Mif(UserPath)
%轉(zhuǎn)換Kml文件為Mif格式文件
%%
%打開文件
[KmlFileName,KmlFilePath]=uigetfile(strcat(UserPath,'*.Kml'),'請(qǐng)選擇需轉(zhuǎn)換的Kml文件:');
if strcmp(KmlFilePath(length(KmlFilePath)),'\')~=1
KmlFilePath(length(KmlFilePath)+1)='\';
end
KmlFilePathName=strcat(KmlFilePath,KmlFileName); %Kml 文件絕對(duì)路徑引用
LineName=KmlFileName(1:length(KmlFileName)-4);
MifFileName=strcat(LineName,'.Mif');
MifFilePathName=strcat(KmlFilePath,MifFileName); %Mif 文件絕對(duì)路徑引用,生成的同名Mif文件存于與Kml文件相同目錄中。
MidFileName=strcat(LineName,'.Mid');
MidFilePathName=strcat(KmlFilePath,MidFileName); %Mid 文件絕對(duì)路徑引用,生成的同名Mid文件存于與Kml文件相同目錄中。
Fid_Kml=fopen(KmlFilePathName,'r','native','utf-8');
if Fid_Kml==-1
error(strcat(KmlFilePathName,'無法打開!'));
end
Fid_Mif=fopen(MifFilePathName,'w');
if Fid_Mif==-1
error(strcat(MifFilePathName,'無法打開!'));
end
Fid_Mid=fopen(MidFilePathName,'w');
if Fid_Mid==-1
error(strcat(MidFilePathName,'無法打開!'));
end
%%
Fid_Str=fopen('Mifstr.txt','r');
if Fid_Str==-1
error('Mifstr.txt無法打開!');
end
%
%寫入Mif文件頭
while feof(Fid_Str)~=1
TempStr=fgetl(Fid_Str);
fprintf(Fid_Mif,'%s\n',TempStr);
end
fclose(Fid_Str);
%%
Tag_Folder_Exist=0;
%
while feof(Fid_Kml)~=1
CurrentStr=fgetl(Fid_Kml);
%如果存在文件夾,則以文件夾名稱作為一個(gè)mif文件保存該文件夾內(nèi)所有對(duì)象
Tag_Folder_Start=findstr(CurrentStr,'<Folder>');
Tag_Folder_End=findstr(CurrentStr,'</Folder>');
if length(Tag_Folder_Start)==1
Tag_Folder_Exist=1;
CurrentStr=fgetl(Fid_Kml);
Tag_FolderName_Start=findstr(CurrentStr,'<name>');
Tag_FolderName_End=findstr(CurrentStr,'</name>');
FolderNameStr=CurrentStr((Tag_FolderName_Start+6):(Tag_FolderName_End-1));
FolderMifFileName=strcat(KmlFilePath,FolderNameStr,'.Mif');
FolderMidFileName=strcat(KmlFilePath,FolderNameStr,'.Mid');
Fid_Current_Mif=fopen(FolderMifFileName,'w');
Fid_Current_Mid=fopen(FolderMidFileName,'w');
%寫入Mif文件頭
Fid_Str=fopen('Mifstr.txt','r');
while feof(Fid_Str)~=1
TempStr=fgetl(Fid_Str);
fprintf(Fid_Current_Mif,'%s\n',TempStr);
end
fclose(Fid_Str);
end
if (length(Tag_Folder_End)==1)&(Tag_Folder_Exist==1)
fclose(Fid_Current_Mif);
fclose(Fid_Current_Mid);
Tag_Folder_Exist=0;
end
%
Tag_Object_Start=findstr(CurrentStr,'<Placemark>');
if length(Tag_Object_Start)==1
%讀取當(dāng)前對(duì)象名稱字段,結(jié)果保存于Object_Name變量中
CurrentStr=fgetl(Fid_Kml);
Tag_Name_Start=findstr(CurrentStr,'<name>');
Tag_Name_End=findstr(CurrentStr,'</name>');
while (length(Tag_Name_Start)~=1)&(length(Tag_Name_End)~=1)
CurrentStr=fgetl(Fid_Kml);
Tag_Name_Start=findstr(CurrentStr,'<name>');
Tag_Name_End=findstr(CurrentStr,'</name>');
end
Object_Name=CurrentStr((Tag_Name_Start+6):(Tag_Name_End-1));
%讀取當(dāng)前對(duì)象類型字段,結(jié)果保存于Object_Style變量中
%存儲(chǔ)形式為:1代表區(qū)域,2代表點(diǎn),3代表曲線
CurrentStr=fgetl(Fid_Kml);
Object_Style_Polygon=findstr(CurrentStr,'<Polygon>');
Object_Style_Point=findstr(CurrentStr,'<Point>');
Object_Style_Line=findstr(CurrentStr,'<LineString>');
switch 1
case length(Object_Style_Polygon)==1
Object_Style=1;
case length(Object_Style_Point)==1
Object_Style=2;
case length(Object_Style_Line)==1
Object_Style=3;
otherwise
Object_Style=0;
end
while Object_Style==0
CurrentStr=fgetl(Fid_Kml);
Object_Style_Polygon=findstr(CurrentStr,'<Polygon>');
Object_Style_Point=findstr(CurrentStr,'<Point>');
Object_Style_Line=findstr(CurrentStr,'<LineString>');
switch 1
case length(Object_Style_Polygon)==1
Object_Style=1;
case length(Object_Style_Point)==1
Object_Style=2;
case length(Object_Style_Line)==1
Object_Style=3;
otherwise
Object_Style=0;
end
end
%讀取經(jīng)緯度信息,經(jīng)度、緯度、高程保存于Object_Lon,Object_Lat、Object_Elv變量中
CurrentStr=fgetl(Fid_Kml);
Tag_Coor_Start=findstr(CurrentStr,'<coordinates>');
Tag_Coor_End=findstr(CurrentStr,'</coordinates>');
while length(Tag_Coor_Start)~=1
CurrentStr=fgetl(Fid_Kml);
Tag_Coor_Start=findstr(CurrentStr,'<coordinates>');
Tag_Coor_End=findstr(CurrentStr,'</coordinates>');
end
if length(Tag_Coor_End)==1
CoorData=str2num(CurrentStr((Tag_Coor_Start+13):(Tag_Coor_End-1)));
else
CurrentStr=fgetl(Fid_Kml);
Tag_Coor_End=findstr(CurrentStr,'</coordinates>');
CoorData=str2num(CurrentStr(1:(Tag_Coor_End-1)));
end
N=length(CoorData);
Object_Lon=CoorData(1:3:N);
Object_Lat=CoorData(2:3:N);
Object_Elv=CoorData(3:3:N);
M=length(Object_Lon);
%輸出當(dāng)前對(duì)象信息至總mif文件
if Object_Style==1 %輸出區(qū)域
fprintf(Fid_Mif,'Region 1\n');
fprintf(Fid_Mif,' %.0f\n',M);
for c1=1:M
fprintf(Fid_Mif,'%.10f %.10f\n',Object_Lon(c1),Object_Lat(c1));
end
fprintf(Fid_Mif,' Pen (1,2,6332416) \n');
fprintf(Fid_Mif,' Center %.10f %.10f\n',mean(Object_Lon),mean(Object_Lat));
fprintf(Fid_Mid,'"%s","%.3f"\n',Object_Name,mean(Object_Elv));
elseif Object_Style==2 %輸出點(diǎn)
fprintf(Fid_Mif,'Point %.10f %.10f\n',Object_Lon,Object_Lat);
fprintf(Fid_Mif,' Symbol (35,255,12)\n');
fprintf(Fid_Mid,'"%s","%.3f"\n',Object_Name,mean(Object_Elv));
elseif Object_Style==3 %輸出折線
fprintf(Fid_Mif,'Pline %.0f\n',M);
for c1=1:M
fprintf(Fid_Mif,'%.10f %.10f\n',Object_Lon(c1),Object_Lat(c1));
end
fprintf(Fid_Mif,' Pen (3,21,16777072)\n');
fprintf(Fid_Mid,'"%s","%.3f"\n',Object_Name,mean(Object_Elv));
end
end
end
fclose(Fid_Kml);
fclose(Fid_Mif);
fclose(Fid_Mid);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -