matlab繪製中國地圖

使用mapshow+shapefile的方法:

%% mapchina
clc;clear;clf
% shapefile中都包含了國界和省界
fnshp_L='E:\ChinaMap\國界與省界\bou2_4l.shp';%ShapeType: 'PolyLine'
fnshp_P='E:\ChinaMap\國界與省界\bou2_4p.shp';%ShapeType: 'Polygon'
infoL = shapeinfo(fnshp_L);
infoP = shapeinfo(fnshp_P);
readL=shaperead(fnshp_L);
readP=shaperead(fnshp_P);
subplot(121);mapshow(readL);title('PolyLine of China')
subplot(122);mapshow(readP);title('Polygon of China')

繪製圖形如下:

第二種方法使用geoshow+shapefile的方法:

%% geoshow+shapefile
clc;clear;clf
% shapefile中都包含了國界和省界
fnshp_L='E:\ChinaMap\國界與省界\bou2_4l.shp';%ShapeType: 'PolyLine'
fnshp_P='E:\ChinaMap\國界與省界\bou2_4p.shp';%ShapeType: 'Polygon'
readL=shaperead(fnshp_L);%該文件中沒有每個省的名稱
readP=shaperead(fnshp_P);%該文件中有每個省的名稱
subplot(121);worldmap('China');geoshow(fnshp_L);title('PolyLine of China')
subplot(122);worldmap('China');geoshow(fnshp_P);title('Polygon of China')
繪製的圖形如下:

注意如果上面沒有加worldmap的命令,那麼和第一種方法繪製出來的圖形是一樣的.

如果只想顯示安徽省的,那麼可以這樣做:

%bbox=[114,29;120,35];%安徽的地理位置
%S = shaperead(fnshp_P,'BoundingBox',bbox);
S = shaperead(fnshp_P);
n=1;
while (n>=1 && n<=numel(S))
    sname=S(n).NAME;
    if ( ~strcmp(sname,'安徽省'))
        S(n)=[];
        n=n-1;
    end
        n=n+1;
end
mapshow(S);
顯示的結果如下:

如果使用geoshow,最好將數據中(X,Y)座標轉爲(lon,lat):

bbox=[114,29;120,35];%安徽的地理位置
S = shaperead(fnshp_P,'BoundingBox',bbox,'UseGeoCoords', true);%只讀一部分數據
n=1;
while (n>=1 && n<=numel(S))
    sname=S(n).NAME;
    if ( ~strcmp(sname,'安徽省'))
        S(n)=[];
        n=n-1;
    end
        n=n+1;
end
worldmap([29 35],[114 120]) %worldmap(latlim, lonlim)
geoshow(S)
繪製的地圖爲:

其實,上面的繪製任意省的地圖方式還可以進一步簡化:讀取shapefile文件時使用'selector'選擇你想要的省份就可以了:

S = shaperead(fnshp_P, 'UseGeoCoords', true,...
  'Selector',{@(NAME) strcmp(NAME,'安徽省'), 'NAME'});
worldmap([29 35],[114 120]) %worldmap(latlim, lonlim)
geoshow(S)

繪製的結果和上面的圖是一樣的.

有時我們希望修改地圖的某些屬性,例如投影方式,那麼可是使用getm和setm來實現,請看下面的例子:

S = shaperead(fnshp_P, 'UseGeoCoords', true,'Selector',{@(NAME) strcmp(NAME,'安徽省'), 'NAME'});
wm=worldmap([29 35],[114 120]); %worldmap(latlim, lonlim)
%getm(wm)%查看當期地圖的所有屬性
setm(wm,'mapprojection','eqacylin');%可以使用maps查看所有的地圖投影方式
gs=geoshow(S);
繪製的圖形如下:

如果不想使用額外的shapefile繪圖,matlab自帶的shapefile也可:

wm=worldmap('world');
land=shaperead('landareas','UseGeoCoords',true);
geoshow(wm,land,'FaceColor',[0.5 0.7 0.5]);
lakes=shaperead('worldlakes','UseGeoCoords',true);
geoshow(lakes,'FaceColor','blue');
rivers=shaperead('worldrivers','UseGeoCoords',true);
geoshow(rivers,'Color','blue');
cities=shaperead('worldcities','UseGeoCoords',true);
geoshow(cities,'Marker','.','Color','red');
繪製的結果如下:

下面再給出一個例子:

maps %查看當前可用的地圖投影方式

%%  導入數據,全球海岸線
load coast

%% 繪圖
axesm robinson
patchm(lat,long,'g');
 %% 設置屬性
setm(gca);%查看當前可以設置的所有圖形座標軸(map axes)的屬性
setm(gca,'Frame','on');%使框架可見
getm(gca,'Frame');%使用getm可以獲取指定的圖形座標軸的屬性
setm(gca,'Grid','on');%打開網格
setm(gca,'MLabelLocation',60);%標上經度刻度標籤,每隔60度
setm(gca,'MeridianLabel','on');%設置經度刻度標籤可見
setm(gca,'PLabelLocation',[-90:30:90])%標上經度刻度標籤,[-90:30:90]
setm(gca,'ParallelLabel','on');%設置經度刻度標籤可見
setm(gca,'MLabelParallel','south');%將經度刻度標籤放在南方,即下部  
setm(gca,'Origin',[0,90,0]);%設置地圖的中心位置和繞中心點和地心點的軸旋轉角度[latitude longitude orientation]

繪製的圖形如下:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章