plotm matlab,MATLAB画地图的工具:worldmap和m_map
Mapping
Toolbox是Matlab提供的,一整套包含许多函数跟图形用户界面的工具箱;它可以帮助用户分析几何空间方面的数据;并以地图的形式显示出来!
worldmap命令
help worldmap WORLDMAP 创建世界上给定区域的坐标
WORLDMAP 区域 或 WORLDMAP(区域名) 新建一个空的地图坐标,这个投影模型和限制与此区域相适应 . REGION
可能是一个字符串或则 cell array of strings. Permissible strings 包括大陆,
国家,岛屿,比如 'World', 'North Pole', 'South Pole', and
'Pacific'.
>> worldmap world
%绘制世界地图坐标或
>> worldmap ('world)
1、示例一:带有海岸线的世界地图
coast.m在目录D:Program
FilesMATLABR2010atoolboxmapmapdemos
>> %
worldmap('World')
load coast
plotm(lat, long)
coast中包含lat和long两个double型向量9865*1,9865*1
>> help plotm
PLOTM 投影二维直线和点到地图坐标
PLOTM(lat,lon)将线型对象投影到最近使用的地图坐标.
输入的维度(latitude)和经度(longitude)数据必须与最近使用地图坐标单位一致。
PLOTM will clear
the current map if the hold state is off.
>> help load
LOAD 从 MAT-file加载数据到工作空间.
S =
LOAD(FILENAME) 加载来自a MAT-file
文件的变量到结构数组或来自ascii文件的数据到双精度数组.
2、实例二:带有陆地、主要湖泊河流和城市和人口密集区的世界地图
landareas
landareas.shp
shx、dbf在D:Program
FilesMATLABR2010atoolboxmapmapdemos目录下
>> % Worldmap with land areas, major lakes and rivers, and cities and
% populated places
ax = worldmap('World');
setm(ax, 'Origin', [0 180 0])
land = shaperead('landareas', 'UseGeoCoords', true);
geoshow(ax, 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')
geoshow(ax,
land, 'FaceColor', [0.5 0.7 0.5])
陆地的表面颜色
geoshow(cities, 'Marker', '.',
'Color', 'red')
城市用'.'标记
H =
WORLDMAP(...)返回地图投影的句柄
>> help setm
SETM 设置地图坐标和图形对象的属性
SETM(H,'MapAxesPropertyName',PropertyValue,...)
H 有效的地图坐标句柄,
sets the map properties specified in the input list.
The
map properties
must be recognized by AXESM.
SETM(H, 'MapPosition', POSITION)
H 为有效的地图
text object, uses a two or three element
position vector
specifying[维度,经度,海拔] [latitude, longitude,
altitude]. 对于两个元素的向量, altitude = 0 is
assumed.
SETM(H, 'Graticule', LAT, LON, ALT)
where H is a valid projected surface
object, uses LAT and LON 相同大小的矩阵specify the 格网点.
The input ALT can be either a scalar to
specify the altitude plane, a matrix of the same size as LAT and
LON, or an empty matrix. If omitted, ALT =
0 is assumed.
>>
help shaperead
SHAPEREAD
从shapefile中读取向量特征和属性
S = SHAPEREAD(FILENAME)
返回 N×1 结构数组, S,
containing one
element for each non-null geographic feature in
the shapefile. S
is a "mapstruct" geographic data structure array
and combines
coordinates/geometry, expressed in terms of map X and Y,
with non-spatial feature
attributes.
Name Description of Value
Purpose
Attributes Cell array of
attribute Omit attributes
that are
names
not listed. Use
{} to omit
all attributes. Also sets
the order of attributes in
the structure array.
UseGeoCoords Scalar logical 如果为true,
替换X和Y field names with 'Lon'
and 'Lat', respectively.默认为
false.
>> help geoshow
GEOSHOW
显示地图纬度和经度数据
>> help geoshow
GEOSHOW Display map latitude and longitude data
GEOSHOW(LAT, LON) or
GEOSHOW(LAT, LON, ..., 'DisplayType', DISPLAYTYPE, ...)
投影并显示纬度和经度向量,
LAT and LON, 使用存储在地图坐标系中的投影模型.如果无投影模型, 纬度和经度使用默认Plate Carree 投影.LAT
and LON may contain embedded NaNs, delimiting individual lines or
polygon parts. DISPLAYTYPE can be 'point', 'line', or 'polygon'
anddefaults to 'line'.
projects and displays the latitude
and longitude vectors, LAT and LON, using
the projection stored in the axes. If there is no
projection, the latitudes and longitudes
are projected using a default Plate Carree projection.LAT and LON
may contain embedded NaNs, delimiting individual lines
or polygon parts. DISPLAYTYPE can be 'point',
'line', or 'polygon' anddefaults to 'line'.
*用MATLAB画世界地图并加散点
clear;clc;
worldmap([-55 -30],[-110 -60]);
h1=axesm('MapProjection','eqdcylin','maplatlimit',[-58
-30],'maplonlimit',[-110
-60],'frame','on','parallellabel','on','meridianlabel','on','flinewidth',1,'plabellocation',5,'mlabellocation',10);
setm(h1,'fedgecolor',[.5 .6 .6],
'fontname','Arial','fontsize',7);
grid off;
h=geoshow('landareas.shp');
x=
y=
scatterm(y,x,'filled','k')
加等值线
clear;clc;
A=xlsread('2010作图2','5','N2:P26');
[x,y]=meshgrid(-106.5:-81.5,-46.5:-35.5);
z=abs(griddata(A(:,1),A(:,2),A(:,3),x,y,'v4'));
subplot(2,2,1)%figure(1)
worldmap([-55 -30],[-110 -60]);
h1=axesm('MapProjection','eqdcylin','maplatlimit',[-58
-30],'maplonlimit',[-110
-60],'frame','on','parallellabel','on','meridianlabel','on','flinewidth',1,'plabellocation',5,'mlabellocation',10);
setm(h1,'fedgecolor',[.5 .6 .6],
'fontname','Arial','fontsize',7);
grid off;
h=geoshow('landareas.shp');
scatterm(A(:,2),A(:,1),'filled','k')
[c,h] =contourm(y,x,z,[0.6,0.8,0.9],'k-'); %等值线
hold
on;[cc,hh]=contourm(y,x,z,[0.1,0.2,0.3,0.4,0.5,0.7],'k-');
clabel(c,h)
title('5月')
1、用matlab画世界地图
[matlab自带的例子]
ax = worldmap('World'); setm(ax, 'Origin', [0 180 0]) land =
shaperead('landareas', 'UseGeoCoords', true); geoshow(ax, 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')
2、matlab的m_map工具箱及添加行政边界底图
对习惯使用matlab的人来说,m_map是一个很好的绘制地图的免费工具箱。可以选择的投影种类近20种,包括常用的Lambert、Mercator、UTM等。可以测量距离(
m_lldist, m_xydist
),绘制等值线(m_contour),等值线填充图(m_contourf),矢量图(m_quiver),栅格图(m_pcolor)等,并与相应的matlab函数语法类似,很容易使用。
m_map通过m_coast提供1/4
degree分辨率的全球海岸线,通过下载GSHHS可以得到更高分辨率的海岸线数据。
除海岸线外,还可以利用已有的GIS行政边界资料,在地图上添加行政边界底图。具体作法为:下载.shp格式的行政边界文件(
国家基础地理信息系统的下载服务),将下载的.shp文件通过mapinfo转换成.dxf文件,利用已编译好的fortran程序,读取经纬度信息,输出.dat文件。导入matlab空间,可以直接m_plot,也可以调用m_plotbndry()。
通过以上方法可以实现.shp在matlab绘图中的应用。
3、[原创ZHOU
Feng]在matlab中利用worldmap画中国区域图时加上台湾和钓鱼岛
Matlab是我们常用的一个画图和计算、仿真工具,在我们海洋科研中,经常在画图时需要加上底图(譬如海岸线,国界、省界线等等)。常用的一个工具包是M_map。但这里我要讲的是利用matlab自带的一个画图工具包话底图,这个工具包就是worldmap.
worldmap的一般用法约为:
>> figure; worldmap('china'); polcmap;
或者
>> figure; worldmap china; polcmap;
如果要加入颜色的画,一般可以这样:
>> figure
worldmap('china','patch')
scaleruler
这时候问题就出来了。因为鬼子偷偷的讲台湾和大陆用两种颜色表示;甚至,如果你放大图片的画,会发现钓鱼岛也是不同颜色。这个是我们不能忍受的!!!!!
于是我就费了一点时间,琢磨这个画图,然后修改。下面是这个脚本程序,在matlab中执行就可,这时候台湾和钓鱼岛和大陆就是一个颜色了^_^。
用兴趣的朋友可以把这个用法举一反三((ZHOU
Feng)zhoufeng@sio.org.cn。matlab版本是6.5)。
不过老实说,我不太用matlab自带的这个画图包,用M_map比较多一点。试验一下,感觉还可以用用。
画上述图的代码如下:
% 把台湾和大陆合成一个文件保存起来,这样画图用patch就是一种颜色(Zhou Feng, 2008-06-30, SOED,
Hangzhou)。
%
% by ZHOU Feng
% zhoufeng@sio.org.cn
% SOED, 2nd Institute of Oceanography
% 2008-06-30
s1 = worldhi('china');
s2 = worldhi('taiwan');
disp(s1);
% add Taiwan together (ZHOU Feng)zhoufeng@sio.org.cn
s = s1;
s.lat = [s1.lat;NaN; s2.lat];
s.long= [s1.long; NaN;s2.long];
% add the Diaoyu Island (钓鱼岛)
% 钓鱼岛群岛由钓鱼岛、黄尾岛、赤尾岛、南小岛、北小岛、大南小岛、大北小岛和飞濑岛等岛屿组成,总面积约7平方公里。
% 地理位置:东经123°-124°34′北纬25°40′-26°。
%
% ---这里的分辨率只有两块 --
s3 = worldhi('japan'); %(ZHOU
Feng)zhoufeng@sio.org.cn
x = s3.long;
y = s3.lat;
idx = find(x>123.0 & x<124.5);
idy = find(y> 25.5 & y< 26.0);
m = length(idy);
id=[];
for i=1:m
tmp=find(idx == idy(i));
if isempty(tmp)
else
id = [id;
idx(tmp)]; %(ZHOU Feng)zhoufeng@sio.org.cn
end
end
%%longd = x(id);
%%latd = y(id);
%
% find nan
dtmp = find(diff(id)>1);
if isempty(dtmp)
disp('no change')
elseif length(dtmp)==1
idnew = [id(1:dtmp); id(dtmp+1)-1;id(dtmp+1:end)];
else
for j=1:length(dtmp)
idnew = [id(1:dtmp(j)+j-1); id(dtmp(j)+1)+j-1;
id(dtmp(j)+j+1:end)]; % 未试验,可能有误
end
end
longd = x(idnew);
latd = y(idnew);
s.lat = [s.lat; NaN; latd];
s.long= [s.long; NaN; longd];
worldmap china
h = displaym(s);
polcmap
OK!!!!!!!!!!!!!!!