提取KML文件上 点 的经纬度

问题:

在google earth上创建一个矩形框kml文件,然后获取kml文件上矩形框四个角点的经纬度。

方法一:借助arc gis软件

1.在Google earth上创建kml

(1)
在这里插入图片描述
(2)位置另存为kml格式(不是kmz哦)

在这里插入图片描述

2.在ArcGIS上导入kml文件

kml文件转为图层文件
在这里插入图片描述

3.图层多边形文件转为线要素

在这里插入图片描述

4.线要素转为点要素

在这里插入图片描述

5.点要素添加点要素的经纬度

在这里插入图片描述

6. 查看点要素的属性表

(point_x,point_y)分别为点的经纬度
在这里插入图片描述

方法二:read_kml函数

来源:https://www.mathworks.com/matlabcentral/fileexchange/13026-read_kml
经测试,很好用

function [x,y,z] = read_kml(fileName)
% READ_KML Reads in (x,y,z) from a GoogleEarth kml file.
%
%  I have tried to make this code as robust as possible, but it may crash
%  or give unexpected resutls if the file is not formatted exactly as
%  expected.
%
% Example:
%   [x,y,z] = read_kml('test.kml');
%
% where test.kml looks like:
% <?xml version="1.0" encoding="UTF-8"?>
% <kml xmlns="http://earth.google.com/kml/2.1">
% <Placemark>
% 	<name>test_length</name>
% 	<description>junk</description>
% 	<LineString>
% 		<tessellate>1</tessellate>
% 		<coordinates>
% -73.65138440596144,40.45517368645169,0 -73.39056199144957,40.52146569128411,0 -73.05890757388369,40.59561213913959,0 -72.80519929505505,40.66961872411046,0 -72.61180114704385,40.72997510603909,0 -72.43718187249095,40.77509309196679,0 </coordinates>
% 	</LineString>
% </Placemark>
% </kml>
%
% afarris@usgs.gov 2016March09, now can read mulitple sets of coordinates 
% afarris@usgs.gov 2006November

%% open the data file and find the beginning of the data
fid=fopen(fileName);
if fid < 0
    error('could not find file')
end

% This loop reads the data file one line at a time. If if finds the word
% <coordinates>, it knows there is data until it reads the word
% </coordinates>.  After loading this data, it keeps reading the file,
% looking for another instance of <coordinates> until it finds the word
% </kml> which signals that the end of the file has been reached.
% Some files have all the data on one line, others have newline charecters
% in various points in the file.  I hope this code that works in all cases.

done=0;
endoffile = 0;
ar = 1;

while endoffile == 0
    while done == 0
        junk = fgetl(fid);
        f = strfind(junk,'<coordinates>');
        ff = strfind(junk,'</kml>');
        if ~isempty(f)
            done = 1;
        elseif  ~isempty(ff)
            endoffile = 1;
            done = 1;
        end
    end
    if endoffile
        break
    end
    % 'junk' either ends with the word '<coordinates>' OR 
    % some data follows the word '<coordinates>'  
    if (f + 13) >= length(junk)  
        % no data on this line
        % done2 is set to zero so the next loop will read the data
        done2 = 0;
    else
        % there is some data in this line following '<coordinates>'
        clear f2
        f2 = strfind(junk,'</coordinates>');
        if ~isempty(f2) 
            %all data is on this line
            % there may be multiple sets of data on this one line
            % I read them all
            for i = 1 : size(f2,2)
                alldata{ar} = junk(f(i)+13:f2(i)-1);
                % I add in whitespace b/c sometimes it is missing
                alldata{ar+1} = ' ';
                ar = ar+2;
            end
            % done2 is set to one because the next loop does not need to run
            done2 = 1;
        else
            % only some data is on this line
            alldata{ar} = junk(f+13:end);
            % I add in whitespace b/c sometimes it is missing
            alldata{ar+1} = ' ';
            ar = ar+2;
            % done2 is set to zero so the next loop will read the rest of the data
            done2 = 0;
        end
        % check to see if at end of the file
        ff = strfind(junk,'</kml>');
        if  ~isempty(ff)
            % no more data
            endoffile = 1;
            break
        else
            % need to keep looking for more data
            done = 0;
        end
    end

    % If not all the data was on the line with the word <coordiate>, 
    % read in the data
    while done2 == 0
        % read in line from data file
        junk = fgetl(fid);
        f = strfind(junk,'</coordinates>');
        if isempty(f) == 1 
            % no ending signal, just add this data to the rest 
            alldata{ar} = junk;
            ar = ar + 1;
        else
            % ending signal is present
            done = 0;
            if f < 20
                % </coordinates> is in the begining of the line, ergo no data 
                % on this line; just end the loop
                done2 = 1;
            else 
                % the ending signal (</coordinates>) is present: remove it, 
                % add data to the rest and signal the end of the loop
                f2 = strfind(junk,'</coordinates>');
                alldata{ar} = junk(1:f2-1);
                ar = ar + 1;
                done2 = 1;
                disp('done with line')
            end
        end
        % check to see if at end of the file
        ff = strfind(junk,'</kml>');
        if  ~isempty(ff)
            % no more data
            endoffile = 1;
            break
        else
            % need to keep looking for more data
            done = 0;
        end
    end
end
fclose(fid);

%% get the data into neat vectors
%  I have to divide the string into X, Y and Z values. 
%  
% This is hard b/c there is no comma between points 
% (just commans between x and y, and between 
% y and z)  ie;  -70.0000,42.0000,0 -70.1000,40.10000,0 -70.2,....
%
% I used to do this by finding commas and spaces, now I use 
% 'strsplit'!  Thank you Matlab!

% 'alldata' is one huge cell
% turn alldata into regular vector so it is easier to work with
data = cell2mat(alldata);
% data is one huge string, split it so there is seperate element for each number
C = strsplit(data,{',',' '});
% sometimes first and/or last element in C is empty, this causes problems
len = size(C,2);
if isempty(C{1}) && isempty(C{end})
    D = C(2:len-1);
elseif isempty(C{1}) && ~isempty(C{end})
     D = C(2:end);
elseif isempty(C{end}) && ~isempty(C{1})
     D = C(1:len-1);
end

% There has GOT to be a better way to split C into 3 variables!
a = 1;
for i = 1 : 3: length(D)-2
    x(a,1) = str2double(D{i});
    a=a+1;
end
a=1;
for i = 2 : 3: length(D)-1
    y(a,1) = str2double(D{i});
    a=a+1;
end
a=1;
for i = 3 : 3: length(D)
    z(a,1) = str2double(D{i});
    a=a+1;
end


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 作为一名程序员,我在2023年对自己的计划包括: 1. 不断学习和提升技能:我会继续学习新的编程语言和框架,以及不断提高编码能力。 2. 参加行业相关的培训和会议:我会参加各种行业相关的培训课程和会议,以便获得最新的行业知识和技能。 3. 参加项目并积累经验:我会参加各种项目,以便积累丰富的经验,并提升个人能力。 4. 在业内建立良好的人脉关系:我会努力与业内的同行建立良好的人脉关系,并与他们保持联系。 5. 寻找更好的发展机会:我会不断寻找更好的发展机会,以便在职业生涯中取得更大的成功。 总之,我在2023年会努力工作,不断学习和提升,并寻找更好的发展机会,以在职业生涯中取得更大的成功。 ### 回答2: 2023年的计划对于我作为一名程序员来说十分重要,我希望能够提升自己的技能水平并取得更好的职业发展。以下是我的计划: 首先,我计划继续学习并掌握新的编程语言和技术。随着科技的不断发展,编程语言和技术也在不断更新换代,作为一名程序员,保持学习的状态是必不可少的。我将花费更多的时间学习目前主流的编程语言,如Python、Java等,并尝试掌握最新的前端和后端技术。通过不断学习,我希望能够拥有更广泛的技术视野和更强大的技术能力。 其次,我计划参与更多的项目和实践。在理论学习之外,实践是提升技能的关键。我计划积极寻找项目机会,无论是个人项目还是团队项目,都可以提供宝贵的实践机会。通过参与各种项目,我可以锻炼解决问题的能力,提高编码和协作能力。同时,我也希望通过实践中的挑战和失败,不断完善自己,进一步提高自己的技术水平和经验。 第三,我计划参加相关的培训和技术交流活动。参加培训和技术交流活动可以与其他程序员交流和学习,了解行业最新动态和趋势。我计划参加各种技术研讨会、讲座和培训班,通过与行业专家和其他程序员的交流,深入了解各种编程技术和最佳实践。同时,我也希望能够积极参与技术社区,与其他程序员分享自己的经验和见解,不断提高自己的影响力和口碑。 最后,我计划在个人项目和开源社区上做出更多的贡献。通过自己的努力,我希望能够在个人项目中实现一些有意义的功能或解决一些实际问题,并将其开源。通过开源社区的贡献,我可以帮助他人解决问题,同时也能够借助其他人的反馈和指导,不断改进自己的代码和设计能力。 总之,2023年对于我作为一名程序员来说是充满挑战和机遇的一年。我将不懈努力,持续学习和实践,不断提升自己的技能水平和职业发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值