MATLAB读写shapefile以及提取shapefile范围内的数据

shapefile文件下载

全世界

下载链接1:http://www.naturalearthdata.com
下载链接2:https://tapiquen-sig.jimdofree.com/english-version/free-downloads/world/
在这里插入图片描述

各国区划

下载链接:https://www.diva-gis.org/gdata
在这里插入图片描述

MATLAB读取shaplefile并提取该区域的数据

步骤一,读取shapefile文件

首先,使用shaperead函数读取shapefile文件,识别出shp文件的经纬度信息。读取后
下图的X、Y代表了shp的经度、纬度。

s = shaperead('IND_adm0.shp');

在这里插入图片描述

步骤二,根据待提取数据的经纬度判断是否在shp范围

使用inpolygon函数判断待提取数据经纬度是否在shp范围内。

%判断是否在shape范围内,其中s.X和s.Y为shp的经度、纬度,x和y分别是待提取数据的经纬度
%in为满足条件的数据,是一个逻辑数组
in = inpolygon(x,y,s.X,s.Y);

我们下载的是印度区域的shapefile,截取数据并绘图后,如下所示:
在这里插入图片描述

shapefile写入

与读入shp相比,输出shp文件相比麻烦一些,需要自己建立一个struct,然后利用shapewrite函数将struct写入shp文件中。

india = shaperead('IND_adm0.shp');

%必要字段
sites.Geometry = india.Geometry;                %元素类型,Point,Line,Polygon等
sites.BoundingBox = india.BoundingBox;          %[minx,miny; maxx,maxy];
sites.X = india.X;                              %坐标X,经度
sites.Y = india.Y;                              %坐标Y,纬度

%附加字段
%这部分属于自定义内容,不定义也不影响使用,只是不会生成dbf文件
sites.NAME = 'AERONET_SITES';

shapewrite(sites,'test_shape'); %生成shp,dbf,shx三个文件

在这里插入图片描述

若是多个独立polygon可以使用如下的方式生成shapefile文件

for i=1:5
	Map(i).Geometry = 'Polygon';
	Map(i).BoundingBox = [100*i,100*i;100*i+100,100*i+100];
	Map(i).X = [100*i,100*i+100,100*i+100,100*i,NaN];
	Map(i).Y = [100*i,100*i,100*i+100,100*i+100,NaN];
	Map(i).Id = i;
end
shapewrite(Map,'Example');

在这里插入图片描述

参考
https://www.mathworks.com/help/map/ref/shaperead.html?s_tid=srchtitle
https://www.mathworks.com/help/matlab/ref/inpolygon.html?s_tid=srchtitle
https://blog.csdn.net/qq_37844142/article/details/83474760

猜你喜欢

转载自blog.csdn.net/wokaowokaowokao12345/article/details/108978732