MATLAB reads and writes shapefiles and extracts data within the range of shapefiles

shapefile download

worldwide

Download link 1: http://www.naturalearthdata.com
Download link 2: https://tapiquen-sig.jimdofree.com/english-version/free-downloads/world/
Insert picture description here

National divisions

Download link: https://www.diva-gis.org/gdata
Insert picture description here

MATLAB reads the shadowfile and extracts the data in the area

Step one, read the shapefile

First, use the shaperead function to read the shapefile and identify the latitude and longitude information of the shp file. After reading,
the X and Y in the figure below represent the longitude and latitude of shp.

s = shaperead('IND_adm0.shp');

Insert picture description here

Step 2: Determine whether it is in the shp range according to the latitude and longitude of the data to be extracted

Use the inpolygon function to determine whether the latitude and longitude of the data to be extracted is within the shp range.

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

What we downloaded is the shapefile of India. After intercepting the data and drawing, it looks like this:
Insert picture description here

Shapefile write

Compared with reading in shp, outputting shp files is more troublesome. You need to create a struct yourself, and then use the shapewrite function to write the struct into the shp file.

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三个文件

Insert picture description here

If there are multiple independent polygons, you can use the following method to generate a shapefile file

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');

Insert picture description here

Refer to
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

Guess you like

Origin blog.csdn.net/wokaowokaowokao12345/article/details/108978732