【Matlab】读取".shp"文件

您所在的位置:网站首页 matlab获取文件后缀 【Matlab】读取".shp"文件

【Matlab】读取".shp"文件

2023-08-16 07:25| 来源: 网络整理| 查看: 265

写在最前

本文主要介绍了 Matlab 中函数 shaperead 的用法以及如何用shp文件制作真值图。 该函数可以用于读取 “.shp” 文件。

代码

% 标准开头

clc clear close all

% 读取文件 “*.shp”

shpPath='*.shp'; shp=shaperead(shpPath); dataSet=shp;

% 获取shp文件的大小,即shp文件中有多少个{‘Polygon’}

polygon_num=size(shp,1);

% 初始化一个数组,存放shp文件中的类的名字

class=cell(50,1);

% 初始化变量,k用来计算类的个数,同时也是类的标号, % 即第一次出现的类名标号为“1”,依次类推 % count用来计算类名是“其他”的类的个数

k=1; count=0;

% 利用for循环在{‘Polygon’}中逐个判断,返回: % dataSet . ID:类名的标号 % class:所有的类名(不重复)

for i=1:polygon_num % 第i个区域的类名,如'茵陈蒿' type=dataSet(i).x0xC00xE00xD00xCD; for j=1:k % 相同返回“1”,否则返回“0” tmp=strcmpi(class(j),type); % 如果相同,即在之前得到的class中有这个类, % 将dataSet(i).Id设为j,即该类在class中的序号, % 跳出循环“for j=1:k” if tmp ~= 0 dataSet(i).Id = j; break; end % 如果不同,即在class中填入新的类名, % 同时将该类在class中的序号写到dataSet(i).Id中 if j==k && tmp==0 class(k,1)=cellstr(type); dataSet(i).Id = k; k=k+1; continue; end end % 如果得到类名为“其他”,count计数则加1 if strcmpi(type,'其它')==1 count=count+1; end end

% 如果在所有的区域中没有“其他”这个类, % 那么就将类别为空“”的区域设为“其他” % (一般不会有这样的事情发生)

if count==0 for i=1:polygon_num type=dataSet(i).x0xC00xE00xD00xCD; if strcmpi(type,'')==1 dataSet(i).x0xC00xE00xD00xCD='其它'; end end end

% 根据四至范围制作真值图 % 四至范围 % (.19202583,.74128107) --------------------------(.19352531,.74128107) % % % % % % (.19202583,.74005221) --------------------------(.19352531,.74005221)

height=*; width=*; startRow=*.74128107; endRow=*.74005221; per_height=(endRow-startRow)/(height-1); startCol=*.19202583; endCol=*.19352531; per_width=(endCol-startCol)/(width-1); zuobiaoLat=zeros(height,1); zuobiaoLon=zeros(width,1); for i=1:height row=startRow+(i-1)*per_height; zuobiaoLat(i)=row; end for i=1:width col=startCol+(i-1)*per_width; zuobiaoLon(i)=col; end ClassGraph=zeros(height,width); for i=1:height row=zuobiaoLat(i); ClassGraph(i,:)=Line( row,zuobiaoLon,dataSet ); end save('*.mat','ClassGraph');

Line.m 如下:

function [ Result_Line ] = Line( Lat,Lon,data ) %POTINPOLYGON 此处显示有关此函数的摘要 % 此处显示详细说明 % 每行经纬度的判断 row = Lat; width=size(Lon,1); rows=repmat(row,1,width); cols=Lon'; Result_Line=zeros(1,width); dotinpoly=1; k=1; while dotinpoly == 1 polygonSize=size(data(k).X,2); rangeX=data(k).X; rangeY=data(k).Y; rangeX(1,polygonSize)=rangeX(1,1); rangeY(1,polygonSize)=rangeY(1,1); is_in = inpolygon(cols,rows,rangeX,rangeY); index=find(is_in ~=0); num=length(index); if num > 0 Result_Line(index)=data(k).Id; end k=k+1; if k>size(data,1) dotinpoly = 2 ; end end end

祝您浏览愉快!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3