中国区域地图叠加绘制

0、声明

  未经声明的matlab版本是在2016版本上进行测试使用的,有小伙伴反映2022版本的matlab无法使用的情况,将内容更新在drawmap更新2


1、数据准备

  地理信息地图在进行绘制的过程中,叠加上行政区域,更方便进行位置定位,更有利于针对问题进行具体分析。


  本文提供了两个Matlab函数,方便在画图时进行行政区域地图叠加操作。


  中国区域各种行政划分河流数据等shp文件,请自行下载,百度云链接链接:https://pan.baidu.com/s/1N6MIFYPHTz8Xpf3gCjvutw 提取码:082d


2、相关Matlab代码及使用方法

  叠加中国区域行政地图的函数如下:


  将下边函数复制,加载到matlab环境中,在需要进行叠加行政地图的区域使用drawmap(‘省’)即可完成行政区域的叠加操作,结果如下图所示。

function drawmap(Province, varargin)

% drawmap: Add administrative region information for a specified province on the canvas
% use whith default configuration    -->  drawmap('湖北省')
% use whithout global map            -->  drawmap('湖北省', 'global', 0)

% ------------------------------------------------------------ %
% Author:          Muyeqingfeng
% e-mail address:  muyeqingfeng@126.com
% Release date:    2023/05/05

ip = inputParser;
ip.addParamValue('global', 1);
parse(ip,varargin{:});  

% decoding the shp file wiht GBK
feature('DefaultCharacterSet', 'GBK');
path = 'shp_path';
China1=shaperead([path,'省.shp']);
China2=shaperead([path,'市.shp']);

idx1 = 0;
idx2 = [];
% Provincial  boundary infomation 
for i=1:length(China1)
    if strcmp(China1(i).x0xCA0xA1,Province), idx1=i;end
end
% Market boundary information
for j=1:length(China2)
    if strcmp(China2(j).x0xCA0xA1,Province), idx2=[idx2,j];end
end

if ip.Results.global == 1
    % the whole china info
    plot([China1(:).X],[China1(:).Y],'color',[0.2 0.2 0.2],'linewidth',1.0); hold on
end
% the special Province
plot([China1(idx1).X],[China1(idx1).Y],'color','r','linewidth',1.5); hold on
% all of the city of the special Province
plot([China2(idx2).X],[China2(idx2).Y],'color','k','linewidth',0.8); hold on
set(gca,'Layer','top');
end

函数调用结果展示

figure;drawmap('湖北省')

在这里插入图片描述

figure;drawmap('湖北省','global', 0)

在这里插入图片描述

3、图片固定位置信息标注

  以下提供了一个函数,在绘制图像时,在图像固定比例处进行信息标注,使得图像信息显得整齐一致

function text_info(varargin)
% text_info: Print information in a fixed position on the image
% use with the default configuration                               --->  text_info('text', 'the info need to print')
% change the position the display the info                         --->  text_into('xi', 0.1, 'yi', 0.2)
% change the color/fontsize of the display the info                --->  text_into('color', 'r', 'ftsiz', 20)

% ------------------------------------------------------------ %
% Author:          Muyeqingfeng
% e-mail address:  muyeqingfeng@126.com
% Release date:    2023/05/05

ip = inputParser;
ip.addParamValue('xi', 0.1);
ip.addParamValue('yi', 0.1);
ip.addParamValue('text', 'test');
ip.addParamValue('ftsiz', 30);
ip.addParamValue('color', 'k');
parse(ip,varargin{:});  

hh=get(gca);
X=hh.XLim;
Y=hh.YLim;
k3=[ip.Results.xi ip.Results.yi];
x_3=X(1)+k3(1)*(X(2)-X(1));
y_3=Y(1)+k3(2)*(Y(2)-Y(1));
text(double(x_3),double(y_3),ip.Results.text,'color',ip.Results.color,'fontweight','bold','fontsize',ip.Results.ftsiz-12);

end

函数调用结果展示

figure;drawmap('湖北省')
text_info('text', '湖北省')

在这里插入图片描述

figure;drawmap('湖北省', 'global', 0)
text_info('text', '湖北省', 'xi', 0.7,'yi', 0.9,'color', 'r')

在这里插入图片描述
在这里插入图片描述

drawmap更新

    drawmap更新,只画整个中国的图,不重点标出某一省份

function drawmap(varargin)

% drawmap: Add administrative region information for a specified province on the canvas
% use whith default configuration  -->  drawmap('湖北省')
% use whithout global map            -->  drawmap('湖北省', 'global', 0)

% ------------------------------------------------------------ %
% Author:             Muyeqingfeng
% e-mail address: muyeqingfeng@126.com
% Release date:    2023/05/05

ip = inputParser;
ip.addParamValue('global', 1)
ip.addParamValue('Province', 0);
parse(ip,varargin{:});  

feature('DefaultCharacterSet', 'GBK');
path = 'D:\My_Document\Code\Practice\data\全国shp\最新2021年全国行政区划\';
China1=shaperead([path,'省.shp']);
China2=shaperead([path,'市.shp']);

idx1 = 0;
idx2 = [];

if ip.Results.Province ~= 0
    % Provincial  boundary infomation 
    for i=1:length(China1)
        if strcmp(China1(i).x0xCA0xA1,ip.Results.Province), idx1=i;end
    end
    
    % Market boundary information
    for j=1:length(China2)
        if strcmp(China2(j).x0xCA0xA1,ip.Results.Province), idx2=[idx2,j];end
    end
end


if ip.Results.global == 1
    % the whole china info
    plot([China1(:).X],[China1(:).Y],'color',[0.2 0.2 0.2],'linewidth',1.0); hold on
end


if ip.Results.Province ~= 0
    % the special Province
    plot([China1(idx1).X],[China1(idx1).Y],'color','r','linewidth',1.5); hold on
    % all of the city of the special Province
    plot([China2(idx2).X],[China2(idx2).Y],'color','k','linewidth',0.8); hold on
end
set(gca,'Layer','top');
end

在这里插入图片描述

drawmap更新1

function drawmap(varargin)

% drawmap: Add administrative region information for a specified province on the canvas
% use whith default configuration  -->  drawmap('湖北省')
% use whithout global map            -->  drawmap('湖北省', 'global', 0)

% ------------------------------------------------------------ %
% Author:             Muyeqingfeng
% e-mail address: muyeqingfeng@126.com
% Release date:    2023/05/05

ip = inputParser;
ip.addParamValue('global', 1)
ip.addParamValue('Province', 0);
ip.addParamValue('City', 0);
ip.addParamValue('County', 0);
parse(ip,varargin{:});  

% feature('DefaultCharacterSet', 'UTF-8');
feature('DefaultCharacterSet', 'GBK');
path = './China/';
China1=shaperead([path,'省.shp']);
China2=shaperead([path,'市.shp']);
China3=shaperead([path,'县.shp']);

idx1 = 0;
idx2 = [];
idx3 = [];

if ip.Results.Province ~= 0
    % Provincial  boundary infomation 
    for i=1:length(China1)
        if strcmp(China1(i).x0xCA0xA1,ip.Results.Province), idx1=i;end
    end
    
    % Market boundary information
    for j=1:length(China2)
        if strcmp(China2(j).x0xCA0xA1,ip.Results.Province), idx2=[idx2,j];end
    end
    
    for k=1:length(China3)
        if strcmp(China3(k).x0xCA0xA1,ip.Results.Province), idx3=[idx3,k];end
    end
end


if ip.Results.global == 1
    % the whole china info
    plot([China1(:).X],[China1(:).Y],'color',[0.2 0.2 0.2],'linewidth',1.0); hold on
end


if  ip.Results.County ~= 0
% all of the city of the special Province
    plot([China3(idx3).X],[China3(idx3).Y],'color',[0.7,0.7,0.7],'linewidth',0.8); hold on
    set(gca,'Layer','top');
end

if  ip.Results.City ~= 0
% all of the city of the special Province
    plot([China2(idx2).X],[China2(idx2).Y],'color','k','linewidth',1); hold on
    set(gca,'Layer','top');
end

if ip.Results.Province ~= 0
    % the special Province
    plot([China1(idx1).X],[China1(idx1).Y],'color',[124, 0, 30]./255,'linewidth',1.5); hold on
end
  • 测试
set(gcf,'color','w','position',[0 0 800 1200])
axes('position', [0.2 0.2 0.6 0.4])
drawmap('Province','河南省','global',0,'city',1,'county',1)

在这里插入图片描述

drawmap更新2

  • Matlab version == 2022a
function drawmap(varargin)

% drawmap: Add administrative region information for a specified province on the canvas
% use whith default configuration  -->  drawmap('湖北省')
% use whithout global map            -->  drawmap('湖北省', 'global', 0)

% ------------------------------------------------------------ %
% Author:             Muyeqingfeng
% e-mail address: muyeqingfeng@126.com
% Release date:    2023/05/05

ip = inputParser;
ip.addParamValue('global', 1)
ip.addParamValue('Province', 0);
ip.addParamValue('City', 0);
parse(ip,varargin{:});  

% feature('DefaultCharacterSet', 'UTF-8');
% feature('DefaultCharacterSet', 'GBK');
path = '..\China\';
China1=shaperead([path,'省.shp']);
China2=shaperead([path,'市.shp']);

%{
matlab2016版本使用以下方式更改编码方式,就可以正常使用
feature('DefaultCharacterSet', 'UTF-8');
feature('DefaultCharacterSet', 'CBK');

但是2022版本好像不能解决上述问题,需要读取同路径下的dbf文件进行省份的匹配,然后作图
dbf也可以使用excel直接打开查看
%}
[num1,head1,text1] = xlsread("..\China\.dbf");
[num2,head2,text2] = xlsread("..\China\.dbf");

idx1 = 0;
idx2 = [];

if ip.Results.Province ~= 0
    % Provincial  boundary infomation 
    sheng = text1(2:end,1);
    shi = text2(2:end,1);
    for i=1:length(sheng)
        if strcmp(sheng(i),ip.Results.Province), idx1=i;end
    end
    
    % Market boundary information
    for j=1:length(shi)
        if strcmp(shi(j),ip.Results.Province), idx2=[idx2,j];end
    end
end


if ip.Results.global == 1
    % the whole china info
    plot([China1(:).X],[China1(:).Y],'color',[0.2 0.2 0.2],'linewidth',1.0); hold on
end


if ip.Results.Province ~= 0
    % the special Province
    plot([China1(idx1).X],[China1(idx1).Y],'color','r','linewidth',1.5); hold on
end

if  ip.Results.City ~= 0
% all of the city of the special Province
    plot([China2(idx2).X],[China2(idx2).Y],'color','k','linewidth',0.8); hold on
    set(gca,'Layer','top');
end


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要在 `splitContainer1.Panel2` 控件中叠加绘制100个图像,您可以在 `splitContainer1_Panel2_Paint` 函数中使用 `Graphics` 对象的 `DrawImage` 方法,并将 `ImageAttributes` 对象的 `ColorMatrix` 属性设置为透明度小于 1 的值,从而实现图像叠加效果。 以下是示例代码: ```csharp private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e) { // 加载图像文件 Image image = Image.FromFile("image.jpg"); // 创建 ImageAttributes 对象 ImageAttributes imageAttributes = new ImageAttributes(); // 创建 ColorMatrix 对象,将透明度设置为0.5 ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.Matrix33 = 0.5f; imageAttributes.SetColorMatrix(colorMatrix); // 计算每个图像在控件中的位置和大小 int imageWidth = splitContainer1.Panel2.Width / 10; int imageHeight = splitContainer1.Panel2.Height / 10; Rectangle imageRect = new Rectangle(0, 0, imageWidth, imageHeight); // 在控件中循环绘制每个图像 for (int i = 0; i < 100; i++) { // 计算当前图像在控件中的位置 int x = (i % 10) * imageWidth; int y = (i / 10) * imageHeight; imageRect.Location = new Point(x, y); // 绘制当前图像 e.Graphics.DrawImage(image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); } } ``` 在上面的代码中,我们首先加载了一个图像文件,并创建了一个 `ImageAttributes` 对象。然后,我们创建了一个 `ColorMatrix` 对象,并将其中的 `Matrix33` 属性设置为 0.5,即将透明度设置为 0.5。接下来,我们计算了每个图像在控件中的位置和大小,并使用一个 for 循环在控件中叠加绘制每个图像。在绘制每个图像时,我们使用 `e.Graphics.DrawImage` 方法,并将 `ImageAttributes` 对象作为最后一个参数传递,从而实现图像叠加效果。 请注意,叠加绘制多个图像可能会对性能产生影响,具体取决于图像的大小和计算机的性能。如果您需要叠加绘制更多的图像,可以考虑使用双缓冲技术来提高性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值