1+ clearvars ;
2+ close all ;
3+
4+ %%
5+ addpath(genpath(' ../common/' ));
6+
7+ %% Select map
8+
9+ disp(' Select geotiff map...' );
10+ [fileName , pathName ] = uigetfile({' *.tif' ,' *.tiff' },' Select geotiff map' );
11+ fileNamePath = fullfile(pathName , fileName );
12+ disp(' Map selected:' );
13+ disp([' File name: ' fileName ]);
14+ disp([' Folder: ' pathName ]);
15+
16+ %% Select crop polygon file
17+
18+ [polygonFileName , polygonPathName ] = uigetfile(fullfile(pathName ,' *.mat' ),' Select mat-file with polygon' );
19+
20+ if all(polygonFileName ~= 0 ) && all(polygonPathName ~= 0 )
21+ polygon = load(fullfile(polygonPathName ,polygonFileName ));
22+ if isfield(polygon ,' X' ) && isfield(polygon ,' Y' )
23+ X = polygon .X ;
24+ Y = polygon .Y ;
25+ disp([' Loaded polygon with ' num2str(length(X )) ' coordinates.' ]);
26+ else
27+ disp(' Mat-file does not contain X- and Y-coordinates...' );
28+ end
29+ end
30+
31+ %% Load map
32+
33+ fWaitbar = waitbar(0.25 ,' Loading map...' );
34+ [mapRaster , mapRasterInfo ] = geotiffread(fileNamePath );
35+ mapInfo = geotiffinfo(fileNamePath );
36+ mapMaxChanIdx = 1 ;
37+ if (size(mapRaster ,3 ) >= 3 )
38+ mapMaxChanIdx = 3 ;
39+ end
40+
41+ %%
42+ if (~exist(' fWaitbar' ,' var' ))
43+ fWaitbar = waitbar(0 ,' ' ,' WindowStyle' ,' modal' );
44+ else
45+ if (~isvalid(fWaitbar ))
46+ fWaitbar = waitbar(0 ,' ' ,' WindowStyle' ,' modal' );
47+ end
48+ end
49+ waitbar(0.5 , fWaitbar , ' Copying map to outputmap...' ,' WindowStyle' ,' modal' );
50+ mapRasterCropped = mapRaster ;
51+ mapRasterCroppedInfo = mapRasterInfo ;
52+
53+ waitbar(0.75 , fWaitbar , ' Plot maps...' ,' WindowStyle' ,' modal' );
54+ fMaps = figure(' Name' ,' Crop map' ,' WindowState' ,' maximized' );
55+ ax1 = subplot(1 ,2 ,1 );
56+ hMap1 = mapshow(im2double(mapRaster(: ,: ,1 : mapMaxChanIdx )), mapRasterInfo );
57+ if (size(mapRaster ,3 ) > mapMaxChanIdx )
58+ set(hMap1 ,' AlphaData' ,mapRaster(: ,: ,mapMaxChanIdx + 1 ))
59+ end
60+ title(' Input map' );
61+ xlabel([upper(mapRasterInfo .RowsStartFrom(1 )) lower(mapRasterInfo .RowsStartFrom(2 : end ))])
62+ ylabel([upper(mapRasterInfo .ColumnsStartFrom(1 )) lower(mapRasterInfo .ColumnsStartFrom(2 : end ))])
63+ hold on ;
64+ ax2 = subplot(1 ,2 ,2 );
65+ hMap2 = mapshow(im2double(mapRasterCropped(: ,: ,1 : mapMaxChanIdx )), mapRasterCroppedInfo );
66+ if (size(mapRasterCropped ,3 ) > mapMaxChanIdx )
67+ set(hMap2 ,' AlphaData' ,mapRasterCropped(: ,: ,mapMaxChanIdx + 1 ))
68+ end
69+ title(' Output map' );
70+ xlabel([upper(mapRasterCroppedInfo .RowsStartFrom(1 )) lower(mapRasterCroppedInfo .RowsStartFrom(2 : end ))])
71+ ylabel([upper(mapRasterCroppedInfo .ColumnsStartFrom(1 )) lower(mapRasterCroppedInfo .ColumnsStartFrom(2 : end ))])
72+ hold on ;
73+ drawnow ;
74+
75+ waitbar(1 , fWaitbar , ' Ready for user interaction' ,' WindowStyle' ,' modal' );
76+ pause(0.25 );
77+ close(fWaitbar );
78+
79+ button = 0 ;
80+ updateLines = true ;
81+ while (~isempty(button ))
82+ if (updateLines )
83+ updateLines = false ;
84+ ch = get(ax1 ,' Children' );
85+ for c = 1 : length(ch )
86+ if isa(ch(c ),' matlab.graphics.chart.primitive.Line' )
87+ delete(ch(c ));
88+ end
89+ end
90+ if (exist(' X' ,' var' ))
91+ if (length(X ) > 0 )
92+ plot(ax1 ,[X X(1 )],[Y Y(1 )],' -o' ,' Color' ,[1 1 1 ],' LineWidth' ,2 );
93+ plot(ax1 ,[X X(1 )],[Y Y(1 )],' -o' ,' Color' ,[1 0 0 ],' LineWidth' ,1 );
94+ end
95+ end
96+ end
97+
98+ [x ,y ,button ] = ginput(1 );
99+ thisAxes = gca ;
100+
101+ if (button == 1 ) % Left mouse button
102+ % Add point to polygon
103+ if (thisAxes == ax1 )
104+ if (~exist(' X' ,' var' ))
105+ X = x ;
106+ Y = y ;
107+ else
108+ X(end + 1 ) = x ;
109+ Y(end + 1 ) = y ;
110+ end
111+ updateLines = true ;
112+ end
113+ elseif (button == 2 ) % Shift+mouse button or middle mouse button
114+ % Move neasest point on polygon
115+ if (thisAxes == ax1 )
116+ if (length(X ) > 0 )
117+ [minDist ,minDistIdx ] = min(pdist2([X ' Y ' ],[x y ]));
118+ X(minDistIdx ) = x ;
119+ Y(minDistIdx ) = y ;
120+ updateLines = true ;
121+ end
122+ end
123+ elseif (button == 3 ) % Right mouse button
124+ % Remove nearest point in polygon
125+ if (thisAxes == ax1 )
126+ if (length(X ) > 0 )
127+ [minDist ,minDistIdx ] = min(pdist2([X ' Y ' ],[x y ]));
128+ X(minDistIdx ) = [];
129+ Y(minDistIdx ) = [];
130+ updateLines = true ;
131+ end
132+ end
133+ elseif (button == 32 ) % Space button
134+ % Update map crop
135+ if (length(X ) > 2 )
136+ fWaitbar = waitbar(0.5 ,' Cropping map. Please wait...' );
137+ [ mapRasterCropped , mapRasterCroppedMask , mapRasterCroppedInfo , ~ ] = mapcrop( mapRaster , mapRasterInfo , X , Y , ' native' );
138+ cla(ax2 );
139+ hMap2 = mapshow(ax2 , im2double(mapRasterCropped(: ,: ,1 : mapMaxChanIdx )), mapRasterCroppedInfo );
140+ if (size(mapRasterCropped ,3 ) > mapMaxChanIdx )
141+ set(hMap2 ,' AlphaData' ,mapRasterCroppedMask )
142+ end
143+ close(fWaitbar );
144+ end
145+ end
146+ disp(num2str([x ,y ,button ],' %i ' ));
147+ end
148+ close(fMaps )
149+
150+ fWaitbar = waitbar(0.5 ,' Saving cropped map...' );
151+ [path ,filename ,ext ] = fileparts(fileNamePath );
152+ outputFilePath = fullfile(path , [filename ' _cropped' ext ]);
153+ % geotiffwrite(outputFilePath,mapRasterCropped,mapRasterCroppedInfo);
154+ GeoKeyDirectoryTag = mapInfo .GeoTIFFTags .GeoKeyDirectoryTag ;
155+ saveGeotiffWithMask( outputFilePath , mapRasterCropped , mapRasterCroppedMask , mapRasterCroppedInfo , GeoKeyDirectoryTag ,' saveAsPseudoColor' ,false );
156+
157+ waitbar(0.75 ,fWaitbar ,' Saving polygon...' );
158+ save(fullfile(path , [filename ' _polygon.mat' ]),' X' ,' Y' );
159+
160+ close(fWaitbar );
161+
162+ disp(' Done' );
0 commit comments