C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > Matlab绘制地图

Matlab绘制中国地图超全教程详解

作者:slandarer

这篇文章主要介绍了如何利用Matlab绘制中国地图,文中的示例代码讲解详细,对我们学习Matlab有一定的帮助,感兴趣的小伙伴可以学习一下

依旧需要用到Mapping Toolbox不会安装的可以看我上一篇

虽然我们只读取shp文件,但需要保证文件夹里还有shx文件及dbf文件

各省边界线绘图

provinces=shaperead('bou2_4l.shp','UseGeoCoords',true);

% 绘图
worldmap('China');
geoshow(provinces)

省份填色图

provinces=shaperead('bou2_4p.shp','UseGeoCoords',true);

% 绘图
worldmap('China');
faceColors=makesymbolspec('Polygon',{'INDEX',[1 numel(provinces)],'FaceColor',polcmap(numel(provinces))});
geoshow(provinces,'DisplayType','polygon','SymbolSpec',faceColors)

改个颜色:

provinces=shaperead('bou2_4p.shp','UseGeoCoords',true);

% 插值定义颜色
C=[222,238,209;126,190,174;144,213,220;
    33,118,155;30,69,128;20,49,127]./255;
C1(:,1)=interp1(0:5,C(:,1),linspace(0,5,numel(provinces)),'linear')';
C1(:,2)=interp1(0:5,C(:,2),linspace(0,5,numel(provinces)),'linear')';
C1(:,3)=interp1(0:5,C(:,3),linspace(0,5,numel(provinces)),'linear')';

% 绘图
worldmap('China');
faceColors=makesymbolspec('Polygon',{'INDEX',[1 numel(provinces)],'FaceColor',C1});
geoshow(provinces,'DisplayType','polygon','SymbolSpec',faceColors)

标注省份名称

provinces=shaperead('bou2_4p.shp','UseGeoCoords',true);

% 插值定义颜色
C=[222,238,209;126,190,174;144,213,220;
    33,118,155;30,69,128;20,49,127]./255;
C1(:,1)=interp1(0:5,C(:,1),linspace(0,5,numel(provinces)),'linear')';
C1(:,2)=interp1(0:5,C(:,2),linspace(0,5,numel(provinces)),'linear')';
C1(:,3)=interp1(0:5,C(:,3),linspace(0,5,numel(provinces)),'linear')';

% 绘图
worldmap('China');
faceColors=makesymbolspec('Polygon',{'INDEX',[1 numel(provinces)],'FaceColor',C1});
geoshow(provinces,'DisplayType','polygon','SymbolSpec',faceColors)

% 读取首都和省级行政中心位置信息并标记
pnames=shaperead('res1_4m.shp','UseGeoCoords',true);
for i=1:numel(pnames)
    textm(pnames(i).Lat,pnames(i).Lon,pnames(i).NAME, ...
   'HorizontalAlignment','center','FontSize',5,'color','k')
end

单独绘制某个省份:

worldmap([42,55],[120,137]);

% 从中国省份数据文件中获取黑龙江省
P=shaperead('bou2_4p.shp','UseGeoCoords',true,...
  'Selector',{@(name) strcmp(name,'黑龙江省'),'NAME'});
patchm(P.Lat,P.Lon, [0.5 0.7 0.5])

中国公路交通图

borders=shaperead('bou1_4p.shp','UseGeoCoords',true);
roads=shaperead('roa_4m.shp','UseGeoCoords',true);
worldmap('China');

geoshow(borders,'FaceColor',[239,238,234]./255)
geoshow(roads,'Color',[250,227,158]./255,'LineWidth',1.5)

中国铁路交通图

borders=shaperead('bou1_4p.shp','UseGeoCoords',true);
rails=shaperead('rai_4m.shp','UseGeoCoords',true);
worldmap('China');

geoshow(borders,'FaceColor',[239,238,234]./255)
geoshow(rails,'Color',[.2,.2,.2],'LineWidth',1,'LineStyle','-.')

中国河流图

borders=shaperead('bou1_4p.shp','UseGeoCoords',true);
riversp=shaperead('hyd1_4p.shp','UseGeoCoords',true);
riversl=shaperead('hyd1_4l.shp','UseGeoCoords',true);
worldmap('China');

geoshow(borders,'FaceColor',[239,238,234]./255)
geoshow(riversp,'FaceColor',[127,141,181]./255,'EdgeColor',[127,141,181]./255)
geoshow(riversl,'Color',[127,141,181]./255,'LineWidth',1)

组合美化图

美化图一

ax=worldmap('China');
setm(ax,'FFaceColor',[153,179,204]./255)

% 绘制附近大陆
antarctica = shaperead('landareas.shp', 'UseGeoCoords', true,...
  'Selector',{@(name) strcmp(name,'Africa and Eurasia'), 'Name'});
patchm(antarctica.Lat, antarctica.Lon, [0.5 0.7 0.5])

% 绘制中国国界
bordersl=shaperead('bou1_4l.shp','UseGeoCoords',true);
geoshow(bordersl,'Color',[.3,.3,.3],'LineWidth',1)
bordersp=shaperead('bou1_4p.shp','UseGeoCoords',true);
patchm([bordersp.Lat],[bordersp.Lon],[239,238,234]./255);

% 绘制河流
riversp=shaperead('hyd1_4p.shp','UseGeoCoords',true);
riversl=shaperead('hyd1_4l.shp','UseGeoCoords',true);
geoshow(riversp,'FaceColor',[127,141,181]./255,'EdgeColor',[127,141,181]./255)
geoshow(riversl,'Color',[127,141,181]./255,'LineWidth',1)

% 绘制公路
roads=shaperead('roa_4m.shp','UseGeoCoords',true);
geoshow(roads,'Color',[250,227,158]./255,'LineWidth',1.5)

tightmap

美化图二

ax=worldmap('china');
setm(ax,'mapprojection','mercator');

% 绘制国界
bordersl=shaperead('bou1_4l.shp','UseGeoCoords',true);
geoshow(bordersl,'Color',[.3,.3,.3],'LineWidth',1)
% 绘制省份
colorList=[197,228,212;255,254,216;251,197,221;199,205,231]./255;
colorList=repmat(colorList,[250,1]);
provinces=shaperead('bou2_4p.shp','UseGeoCoords',true);
colorList=colorList(1:numel(provinces),:);
faceColors=makesymbolspec('Polygon',{'INDEX',[1 numel(provinces)],'FaceColor',colorList});
geoshow(provinces,'DisplayType','polygon','SymbolSpec',faceColors)

% 首都
pnames=shaperead('res1_4m.shp','UseGeoCoords',true);
geoshow(pnames(1).Lat,pnames(1).Lon,'DisplayType','point','Marker','p',...
    'MarkerEdgeColor',[.8,0,0],'MarkerFaceColor',[.8,0,0])

% 指北针
northarrow('latitude',50,'longitude',80,'scaleratio',.08,'FaceColor',[.4,.4,.4])

% 比例尺
scaleruler on
setm(handlem('scaleruler1'), ...
    'XLoc',-3.2e6,'YLoc',1.8e6, ...
    'MajorTick',0:500:2000,...
    'MinorTick',0:40:200,'FontSize',7)
scaleruler('units','nm')
setm(handlem('scaleruler2'), ...
    'XLoc',-3.15e6, ...
    'YLoc',1.7e6, ...
    'TickDir','down', ...
    'MajorTick',0:250:1000,...
    'MinorTick',0:40:200,...
    'MajorTickLength',km2nm(25),...
    'MinorTickLength',km2nm(12.5),'FontSize',7)

% 小地图
h2=axes('pos',[0.67 0.15 0.13 0.2]);
worldmap([1 22],[106 122])
setm(h2,'FFaceColor',[197,213,236]./255,'FEdgeColor',[.3,.3,.3],'FLineWidth',1.5)
insert1=shaperead('bou2_4l.shp','UseGeoCoords',true);
geoshow(insert1,'Color',[.2,.2,.2],'LineWidth',1)
insert2=shaperead('bou2_4p.shp','UseGeoCoords',true);
geoshow(insert2,'FaceColor','w')
mlabel off 
plabel off
gridm off

代码和数据:

链接:https://pan.baidu.com/s/1ZvMTLVElEWgQ3TFOmDXyCg  提取码:1234

到此这篇关于Matlab绘制中国地图超全教程详解的文章就介绍到这了,更多相关Matlab绘制地图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文