-
Notifications
You must be signed in to change notification settings - Fork 110
/
measure.m
executable file
·383 lines (320 loc) · 11.2 KB
/
measure.m
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
function measure(DEM,varargin)
%MEASURE take interactive measurements along a polyline
%
% Syntax
%
% measure(DEM)
% measure(DEM,pn,pv,...)
%
% Description
%
% This simple gui lets you measure distances and slopes along a
% polyline.
%
% Input arguments
%
% DEM digital elevation model (class: GRIDobj)
%
% parameter name, parameter value
%
% 'xyprecision' displayed precision of x and y coordinates
% (C-style format string, default '%6.0f')
% 'slopeunit' unit of slope ({'degree'},'tan','percent')
% 'showhelp' show help window ({true} or false)
% 'position' nx2 matrix with x and y coordinates
% 'colormap' choose colormap. Default is parula
% 'water' by default empty. Provide a GRIDobj with water depth
% to show water surface and plot and save profiles of
% topography and water surface.
%
%
% See also: IMDISTLINE, IMROI, IMPOLY, GRIDobj/DEMPROFILE
%
% Author: Wolfgang Schwanghart (w.schwanghart[at]geo.uni-potsdam.de)
% Date: 12. February, 2022
% 24. November, 2014: added structure field nodes if profile is saved to
% workspace. This allows to continue working with the measure tool with an
% existing profile by calling
% measure(DEM,'position',p.nodes)
% 12. February, 2022: add water option to display water surfaces in
% profiles. Increased resolution of profiles
% get DEM coordinates
[X,Y] = getcoordinates(DEM);
X = X(:);
Y = Y(:);
dx = X(2)-X(1);
dy = Y(2)-Y(1);
xmax = max(X);
ymax = max(Y);
xmin = min(X);
ymin = min(Y);
% default position
defpos = [xmin + (xmax-xmin)*[1/3; 2/3] ymin + (ymax-ymin)*[2/3; 1/3]];
% choose colormap
if license('test', 'map_toolbox')
cmap = demcmap([max(DEM) min(DEM)]-1,255);
else
cmap = parula(255);
end
p = inputParser;
p.FunctionName = 'STREAMobj';
addParamValue(p,'xyprecision','%6.0f',@(x) ischar(x));
addParamValue(p,'slopeunit','degree',@(x) ischar(validatestring(x,{'tan','degree','percent'})));
addParamValue(p,'reset',false);
addParamValue(p,'colormap',cmap);
addParamValue(p,'showhelp',true,@(x) isscalar(x));
addParamValue(p,'position',defpos,@(x) numel(x) >= 4 && size(x,2) == 2 && ...
(max(x(:,1)) <= xmax && min(x(:,1)) >= xmin && ...
max(x(:,2)) <= ymax && min(x(:,2)) >= ymin));
addParamValue(p,'water',[]);
parse(p,varargin{:});
if ~p.Results.reset
if isempty(p.Results.water)
imageschs(DEM,DEM,'colormap',p.Results.colormap)
else
imageschs(DEM,p.Results.water,'colormap',flowcolor)
end
zoom reset
end
hax = gca;
hf = gcf;
title('')
fcn = makeConstrainToRectFcn('impoly',get(gca,'XLim'),get(gca,'YLim'));
h = impoly(hax,p.Results.position,'closed',false,'PositionConstraintFcn',fcn);
htext = [];
htextedge = [];
totdist = [];
elev = [];
segdist = [];
switch p.Results.slopeunit
case 'tan'
slopefun = @(x) x;
slopesign = '';
case 'degree'
slopefun = @(x) atand(x);
slopesign = '';
case 'percent'
slopefun = @(x) 100*x;
slopesign = '';
end
textformat = {'HorizontalAlignment','left',...
'VerticalAlignment','bottom',...
'BackgroundColor',[0 0 0],...
'FontSize',8,...
'Color',[1 1 1]};
textformatedge = {'HorizontalAlignment','center',...
'VerticalAlignment','middle',...
'BackgroundColor',[1 1 1],...
'FontSize',8,...
'Color',[0 0 0]};
% nr of nodes
showpointtext = false;
nrnodes = size(getPosition(h),1);
showedgetext = nrnodes < 5;
id = addNewPositionCallback(h,@getinfo);
iptcallback = get(hf,'WindowKeyPressFcn');
set(hf,'WindowKeyPressFcn', @toggleview)
% iptcallbackr = get(hf,'WindowKeyReleaseFcn');
% set(hf,'WindowKeyReleaseFcn', @releasefun);
pos = getPosition(h);
getinfo(pos);
if p.Results.showhelp
showhelp;
end
function pos = getinfo(pos)
% GETINFO callback when polyline is modified
nrpos = size(pos,1);
if nrpos == 1;
delete(h);
if ~isempty(htext)
delete(htext)
end
if ~isempty(htextedge)
delete(htextedge)
end
title('measurement tool aborted')
set(hf,'WindowKeyPressFcn',{})
return
end
segdist = sqrt(sum(diff(pos).^2,2));
totdist = sum(segdist);
ix = coord2indsub(pos);
elev = DEM.Z(ix);
slop = slopefun(-diff(elev)./segdist);
title(['distance: ' num2str(totdist)]);
if showpointtext
axlim = axis;
xoffset = axlim(2)-axlim(1);
yoffset = axlim(4)-axlim(3);
textpos = [pos(:,1)+(xoffset*0.01),pos(:,2)+(yoffset*0.01)];
for r = 1:size(pos,1)
textstr = ['x: ' num2str(pos(r,1),p.Results.xyprecision) ...
'\newline' 'y: ' num2str(pos(r,2),p.Results.xyprecision) ...
'\newline' 'z: ' num2str(elev(r))];
try
set(htext(r),'Position',textpos(r,:),'String',textstr);
catch
htext(r) = text(textpos(r,1),textpos(r,2),textstr,textformat{:});
end
end
if nrpos<numel(htext)
delete(htext(nrpos+1:end));
htext(nrpos+1:end) = [];
end
else
delete(htext);
htext = [];
end
if showedgetext
for r = 1:size(pos,1)-1
textstredge = ['d: ' num2str(segdist(r),p.Results.xyprecision) ...
'\newline' 'slope: ' num2str(slop(r)) slopesign];
textposedge = (pos(r,:)+pos(r+1,:))/2;
try
set(htextedge(r),'Position',textposedge,'String',textstredge);
catch
htextedge(r) = text(textposedge(1),textposedge(2),textstredge,textformatedge{:});
end
end
if (nrpos-1)<numel(htextedge)
delete(htextedge(nrpos:end));
htextedge(nrpos:end) = [];
end
else
delete(htextedge)
htextedge = [];
end
end
function ix = coord2indsub(pos)
% COORD2INDSUB modified version of GRIDobj/coord2ind
IX1 = (pos(:,1)-X(1))./dx + 1;
IX2 = (pos(:,2)-Y(1))./dy + 1;
IX1 = max(min(IX1,DEM.size(2)),1);
IX2 = max(min(IX2,DEM.size(1)),1);
IX1 = round(IX1);
IX2 = round(IX2);
ix = sub2ind(DEM.size,IX2,IX1);
end
function toggleview(varargin)
% TOGGLEVIEW callback for key press
iptcallback(varargin{:});
switch varargin{2}.Key
case 't'
showpointtext = ~showpointtext;
getinfo(getPosition(h));
case 'd'
showedgetext = ~showedgetext;
getinfo(getPosition(h));
case 'i'
zoom(1.5);
case 'o'
zoom(2/3);
case 'r'
setPosition(h,p.Results.position);
case 'l'
zoomtoroi
case 'p'
n = ceil((totdist/DEM.cellsize)*5);
posn = getPosition(h);
[dn,z] = demprofile(DEM,n,posn(:,1),posn(:,2));
if ~isempty(p.Results.water)
[~,zw] = demprofile(p.Results.water,n,posn(:,1),posn(:,2));
end
posfig = get(hf,'OuterPosition');
hfprofile = figure('OuterPosition',posfig.*[1 1 .5 .5]);
ax = gca;
plot(ax,dn,z,'k');
hold on
if ~isempty(p.Results.water)
plot(ax,dn,z+zw,'b');
end
plot(ax,cumsum([0;segdist(:)]),elev,'sr');
hold off
xlabel('distance [m]');
ylabel('elevation [m]');
case 's'
% save profile structure array to workspace
n = ceil((totdist/DEM.cellsize)*5);
posn = getPosition(h);
[S.distance,S.z,S.x,S.y] = demprofile(DEM,n,posn(:,1),posn(:,2));
if ~isempty(p.Results.water)
[~,S.zw] = demprofile(DEM+p.Results.water,n,posn(:,1),posn(:,2));
end
S.nodes = posn;
answer = inputdlg('Enter variable name:', 'Export profile to workspace', 1, {'profile_struct'});
try
if isvarname(answer{1})
assignin('base',answer{1},S);
end
catch
end
case 'h'
showhelp
end
end
% function releasefun(varargin)
% TOGGLEVIEW callback for key press
% iptcallbackr(varargin{:});
% switch varargin{2}.Key
% case 'p'
% pan off
% end
% end
function showhelp
str = ['Access tools with these keys \n' ...
'a: add new vertex (move cursor over edge) \n' ...
't: toggle location information \n' ...
'd: toggle distance information \n' ...
'o: zoom out \n' ...
'i: zoom in \n' ...
'l: zoom to polyline \n' ...
'p: plot profile \n' ...
's: export profile to workspace \n' ...
'r: reset \n' ...
'h: show this help'];
helpdlg(sprintf(str),'measure tool help')
end
function zoomtoroi
% zoom to polyline
posn = getPosition(h);
axlim = axis(hax);
% get x/y axis ratio
ratio = (axlim(2)-axlim(1))/(axlim(4)-axlim(3));
maxpos = max(posn);
minpos = min(posn);
ranpos = maxpos-minpos;
ratiop = ranpos(1)/ranpos(2);
if any(ranpos*1.2 > [xmax-xmin ymax-ymin])
axis([xmin xmax ymin ymax])
return
end
if ratiop >= ratio
xlims = [minpos(1) maxpos(1)] + [-.1 .1].*ranpos(1);
ywidth = (xlims(2)-xlims(1))/ratio;
ylims = (maxpos(2)+minpos(2))/2 + [-ywidth/2 +ywidth/2];
if ylims(2) > ymax;
ylims(2) = ymax;
ylims(1) = ylims(2)-ywidth;
end
if ylims(1) < ymin;
ylims(1) = ymin;
ylims(2) = ylims(1)+ywidth;
end
newaxlim = [xlims ylims];
else
ylims = [minpos(2) maxpos(2)] + [-.1 .1].*ranpos(2);
xwidth = (ylims(2)-ylims(1))*ratio;
xlims = (maxpos(1)+minpos(1))/2 + [-xwidth/2 +xwidth/2];
if xlims(2) > xmax;
xlims(2) = xmax;
xlims(1) = xlims(2)-xwidth;
end
if xlims(1) < xmin;
xlims(1) = xmin;
xlims(2) = xlims(1)+xwidth;
end
newaxlim = [xlims ylims];
end
axis(hax,newaxlim)
end
end