⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kml2mif.m

📁 MATLAB语言下的(有源码):Mike Zero系列软件的前后处理 GE中岸线与MIKE岸线相互转换 Mapinfo与MIKE中数据相互转换.(对学习MIKE与学习MATLAB编程人皆有帮助,且有说
💻 M
字号:
function Kml2Mif(UserPath)
%转换Kml文件为Mif格式文件
%%
%打开文件
[KmlFileName,KmlFilePath]=uigetfile(strcat(UserPath,'*.Kml'),'请选择需转换的Kml文件:');
if strcmp(KmlFilePath(length(KmlFilePath)),'\')~=1
    KmlFilePath(length(KmlFilePath)+1)='\';
end
KmlFilePathName=strcat(KmlFilePath,KmlFileName); %Kml 文件绝对路径引用
LineName=KmlFileName(1:length(KmlFileName)-4);
MifFileName=strcat(LineName,'.Mif');
MifFilePathName=strcat(KmlFilePath,MifFileName); %Mif 文件绝对路径引用,生成的同名Mif文件存于与Kml文件相同目录中。
MidFileName=strcat(LineName,'.Mid');
MidFilePathName=strcat(KmlFilePath,MidFileName); %Mid 文件绝对路径引用,生成的同名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);
    %如果存在文件夹,则以文件夹名称作为一个mif文件保存该文件夹内所有对象
    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
        %读取当前对象名称字段,结果保存于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));
        %读取当前对象类型字段,结果保存于Object_Style变量中
        %存储形式为:1代表区域,2代表点,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
        %读取经纬度信息,经度、纬度、高程保存于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);
        %输出当前对象信息至总mif文件
        if Object_Style==1 %输出区域
            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 %输出点
            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
        %输出当前对象信息至分mif文件
        if Tag_Folder_Exist==1
            if Object_Style==1 %输出区域
                fprintf(Fid_Current_Mif,'Region  1\n');
                fprintf(Fid_Current_Mif,'  %.0f\n',M);
                for c1=1:M
                    fprintf(Fid_Current_Mif,'%.10f %.10f\n',Object_Lon(c1),Object_Lat(c1));
                end
                fprintf(Fid_Current_Mif,'    Pen (1,2,6332416) \n');
                fprintf(Fid_Current_Mif,'    Center %.10f %.10f\n',mean(Object_Lon),mean(Object_Lat));
                fprintf(Fid_Current_Mid,'"%s","%.3f"\n',Object_Name,mean(Object_Elv));
            elseif Object_Style==2 %输出点
                fprintf(Fid_Current_Mif,'Point %.10f %.10f\n',Object_Lon,Object_Lat);
                fprintf(Fid_Current_Mif,'    Symbol (35,255,12)\n');
                fprintf(Fid_Current_Mid,'"%s","%.3f"\n',Object_Name,mean(Object_Elv));
            elseif Object_Style==3 %输出折线
                fprintf(Fid_Current_Mif,'Pline %.0f\n',M);
                for c1=1:M
                    fprintf(Fid_Current_Mif,'%.10f %.10f\n',Object_Lon(c1),Object_Lat(c1));
                end
                fprintf(Fid_Current_Mif,'    Pen (3,21,16777072)\n');
                fprintf(Fid_Current_Mid,'"%s","%.3f"\n',Object_Name,mean(Object_Elv));
            end
        end
    end
end
fclose(Fid_Kml);
fclose(Fid_Mif);
fclose(Fid_Mid);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -