Thursday, August 24, 2023

Matlab: Render a single-band file and export it as an image example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
clear;
clc;

% Source Directory
SouDir='input';
% Destination Directory
DesDir='output';

% Manitoba Boundary
manitshap='D:\MB.Temp.Prec.20230805\files\boundary.shp';
% All Files
files=dir(fullfile(SouDir, '*.tif'));

% Loop to Export figures
for ii=1:numel(files)
    filepath=fullfile(files(ii).folder, files(ii).name);
    [~, fname, ~]=fileparts(filepath);

    yymm=strsplit(fname, '_');
    yymm=yymm{2};
    yymm=[yymm(1:4), '-', yymm(5:6)];

    otpath=fullfile(DesDir, [fname, '.png']);

    [A, R]=readgeoraster(filepath, "OutputType","double");
    latlim=R.LatitudeLimits;
    lonlim=R.LongitudeLimits;
    worldmap(latlim, lonlim);
    setm(gca, 'mapprojection', 'mercator');
    geoshow(A, R,"DisplayType","surface");
    % setm(gca,"Frame","on","Grid","on")
    plabel off;
    setm(gca,"PLineLocation", 3);
    mlabel off;
    setm(gca,"MLineLocation", 3);
    cb=colorbar('east');
    clim([-1.346 25.911]);
    colorbar('off');
    colormap('jet');

    hold on;
    mani=readgeotable(manitshap);
    kk=geoshow(mani, 'FaceColor', 'none');
    geoshow(49.8954, -97.1385, "Marker","*");
    textm(49.8954, -97.1385,"  Winnipeg");



    textm((max(latlim)-min(latlim))*0.93+min(latlim), ...
        (max(lonlim)-min(lonlim))*0.02+min(lonlim), yymm, ...
        "Color", "black","FontSize", 30);


    %set(gcf,'outerposition',get(0,'screensize'));
    %export_fig(otpath, '-png', '-painters', '-r300')
    %pause(3);
    export_fig(otpath, '-png', '-r400','-q300');
    close all;


end

disp('****************************************************');

No comments:

Post a Comment