-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwriteArrowsToShp.m
More file actions
164 lines (135 loc) · 4.7 KB
/
writeArrowsToShp.m
File metadata and controls
164 lines (135 loc) · 4.7 KB
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
function writeArrowsToShp(filename, X, Y, U, V, S)
% writeArrowsToShp - write arrows to a shape file
%
% SYNTAX:
%
% writeArrowsToShp(filename, X, Y, U, V)
%
% INPUTS:
%
% filename: Name of shapefile to write to
%
% X, Y, U, V: Same as the parameters with corresponding names for
% the quiver function
%
% S: A scaling factor. Use 'auto' to automatically scale,
% otherwise a number to multiply the speeds by to give the
% arrow length in metres. E.g. if this is 2 then an arrow
% for speed 1.5 is drawn 3 m long, one for 2.4 is drawn 4.8
% etc. NOTE that this is NOT the same as the factor used in
% quiver but should allow a consistent value to be used
% across different outputs (e.g. different timeframes)
%
%----------------
% STEP 0: CHECK INPUTS
%----------------
if nargin < 5
error('Must supply five inputs');
elseif nargin < 6
S = 'auto';
end
i_checkInputs(filename, X, Y, U, V, S);
%----------------
% STEP 1: Work out scaling factor if needed
%----------------
if ischar(S)
scaleFactor = i_calcAutoScale(X, Y, U, V)
else
scaleFactor = S;
end
% And apply
U = U*scaleFactor;
V = V*scaleFactor;
%----------------
% STEP 2: Create the shape structure
%----------------
% Arrow coordinates and bounding box
valsX = cell(size(U));
valsY = valsX;
bbox = valsX;
geom = valsX;
id = valsX;
for i = 1:numel(valsX)
[valsX{i}, valsY{i}] = i_makeArrow(X(i), Y(i), U(i), V(i));
bbox{i} = [min(valsX{i}), min(valsY{i}); max(valsX{i}), max(valsY{i})];
geom{i} = 'Line';
id{i} = i;
end
sh = struct('Geometry', geom, ...
'BoundingBox', bbox, ...
'X', valsX, ...
'Y', valsY, ...
'ID', id);
%----------------
% STEP 3: Write the file
%----------------
shapewrite(sh, filename);
%--------------------------------------------------------------------------
function [arrowX, arrowY] = i_makeArrow(x, y, u, v)
% Make arrow...
% Easiest thing to do is make an arrow of the right length pointing north
% and then rotate it as needed... So need to find the right angle and the
% length
arrLength = sqrt(u.^2 + v.^2);
arrAngle = atan2d(u, v);
% Arrow itself...
headLength = 0.3;
headWidth = 0.4;
arrowE = [0, 0, -headWidth*arrLength/2, 0, headWidth*arrLength/2, NaN];
arrowN = [0, arrLength, arrLength*(1-headLength), arrLength, arrLength*(1-headLength), NaN];
% And rotate...
arrowX = arrowE * cosd(arrAngle) + arrowN * sind(arrAngle);
arrowY = -arrowE * sind(arrAngle) + arrowN * cosd(arrAngle);
% And move...
arrowX = arrowX + x;
arrowY = arrowY + y;
%--------------------------------------------------------------------------
function factor = i_calcAutoScale(X, Y, U, V)
% Work out auto scaling for arrows. Can't do exactly the same as quiver as
% not entirely clear what it does and low level code is pcoded
% Some measure of spacing of X and Y points?
% Start with the total area
dX = (max(X(:))-min(X(:)));
dY = (max(Y(:))-min(Y(:)));
totalArea = dY * dX;
% And so each point has this
areaPerPoint = totalArea / numel(X);
% And then split according to X/Y ratio
ratio = dY/dX;
% Basically solving: xPerPoint * (xPerPoint * ratio) = areaPerPoint
xPerPoint = sqrt(areaPerPoint / ratio);
yPerPoint = areaPerPoint / xPerPoint;
% Find maximum ratio of U/X for both positive and negative directions?
maxURatio = max(abs(U(:)) / xPerPoint);
maxVRatio = max(abs(V(:)) / yPerPoint);
% And so...
factor = 1/max(maxURatio, maxVRatio);
% But just to be careful
if ~isfinite(factor)
factor = 1;
end
%--------------------------------------------------------------------------
function i_checkInputs(filename, X, Y, U, V, S)
% Check inputs to the function
% Filename
if ~ischar(filename) || isempty(filename) || size(filename, 1) ~= 1
error('First input (filename) must be a 1 x n string');
end
% X, Y, U and V must all be double and the same size?
if ~isa(X, 'double')
error('Second input (X) must be numeric');
elseif ~isa(Y, 'double')
error('Third input (Y) must be numeric');
elseif ~isa(U, 'double')
error('Fourth input (U) must be numeric');
elseif ~isa(V, 'double')
error('Fifth input (V) must be numeric');
elseif ~isequal(size(X), size(Y)) || ~isequal(size(X), size(U)) || ~isequal(size(X), size(V))
error('Inputs two to five (X, Y, U and V) must all be the same size');
end
% And S must be scalar and non-negative
if ischar(S) && size(S, 1) == 1 && strcmpi(S, 'auto')
% This is ok...
elseif ~isnumeric(S) || numel(S) ~= 1 || ~isfinite(S) || S <= 0
error('Sixth input (S) must be ''auto'' or a positive value');
end