1 | %BOUNDARY class definition
|
---|
2 | %
|
---|
3 | % Usage:
|
---|
4 | % boundary=boundary();
|
---|
5 |
|
---|
6 | classdef boundary
|
---|
7 | properties (SetAccess=public)
|
---|
8 | shppath = '';
|
---|
9 | shpfilename = '';
|
---|
10 | orientation = 'normal'; %other value is 'reverse'
|
---|
11 | proj = epsg2proj(4326); %Proj4.0 projection string. , default is WGS 84 (corresponds to epsg 4326)
|
---|
12 |
|
---|
13 | end
|
---|
14 | methods (Static)
|
---|
15 | function self = loadobj(self) % {{{
|
---|
16 | % This function is directly called by matlab when a model object is
|
---|
17 | % loaded. Update old properties here
|
---|
18 | end% }}}
|
---|
19 | end
|
---|
20 | methods
|
---|
21 | function self = boundary(varargin) % {{{
|
---|
22 | self=setdefaultparameters(self);
|
---|
23 |
|
---|
24 | if nargin > 0,
|
---|
25 | options=pairoptions(varargin{:});
|
---|
26 |
|
---|
27 | %recover field values:
|
---|
28 | self.shppath=getfieldvalue(options,'shppath','');
|
---|
29 | self.shpfilename=getfieldvalue(options,'shpfilename','');
|
---|
30 | self.orientation=getfieldvalue(options,'orientation','normal');
|
---|
31 |
|
---|
32 | %figure out projection string:
|
---|
33 | if exist(options,'epsg'),
|
---|
34 | if exist(options,'proj'),
|
---|
35 | error(['Error in constructor for boundary ' self.shppath '. Cannot supply epsg and proj at the same time!']);
|
---|
36 | end
|
---|
37 | epsg=getfieldvalue(options,'epsg');
|
---|
38 | proj=epsg2proj(epsg); % convert to PROJ.4 format
|
---|
39 | elseif exist(options,'proj'),
|
---|
40 | if exist(options,'epsg'),
|
---|
41 | error(['Error in constructor for boundary ' self.shppath '. Cannot supply proj and epsg at the same time!']);
|
---|
42 | end
|
---|
43 | proj=getfieldvalue(options,'proj');
|
---|
44 | else
|
---|
45 | proj= '+proj=longlat +datum=WGS84 +no_defs';
|
---|
46 | end
|
---|
47 | self.proj=proj;
|
---|
48 | end
|
---|
49 | end % }}}
|
---|
50 | function self = setdefaultparameters(self) % {{{
|
---|
51 | self.shppath='';
|
---|
52 | self.shpfilename='';
|
---|
53 | self.orientation='normal';
|
---|
54 | self.proj='+proj=longlat +datum=WGS84 +no_defs'; %EPSG 4326
|
---|
55 | end % }}}
|
---|
56 | function disp(self) % {{{
|
---|
57 | disp(sprintf(' boundary parameters:'));
|
---|
58 |
|
---|
59 | fielddisplay(self,'shppath','path to filename for this boundary');
|
---|
60 | fielddisplay(self,'shpfilename','shape file name');
|
---|
61 | fielddisplay(self,'orientation','orientation (default is ''normal'', can be ''reverse'')');
|
---|
62 | fielddisplay(self,'proj','shape file projection string (follows PROJ.4 standard)');
|
---|
63 |
|
---|
64 | end % }}}
|
---|
65 | function output=name(self) % {{{
|
---|
66 | output=self.shpfilename;
|
---|
67 | end % }}}
|
---|
68 | function output=edges(self) % {{{
|
---|
69 |
|
---|
70 | %read domain:
|
---|
71 | [path,name,ext]=fileparts(self.shpfilename);
|
---|
72 | if ~strcmpi(ext,'shp'),
|
---|
73 | ext='shp';
|
---|
74 | end
|
---|
75 | output=shpread([self.shppath '/' name '.' ext]);
|
---|
76 |
|
---|
77 | %do we reverse?
|
---|
78 | if strcmpi(self.orientation,'reverse'),
|
---|
79 | output.x=flipud(output.x);
|
---|
80 | output.y=flipud(output.y);
|
---|
81 | end
|
---|
82 | end % }}}
|
---|
83 | function plot(self,varargin) % {{{
|
---|
84 | %recover options
|
---|
85 |
|
---|
86 | options=pairoptions(varargin{:});
|
---|
87 |
|
---|
88 | %parse input:
|
---|
89 | figurenumber=getfieldvalue(options,'figure',1);
|
---|
90 | color=getfieldvalue(options,'color','r');
|
---|
91 | linewidth=getfieldvalue(options,'linewidth',1);
|
---|
92 | markersize=getfieldvalue(options,'markersize',1);
|
---|
93 | unitmultiplier=getfieldvalue(options,'unit',1);
|
---|
94 | radius=getfieldvalue(options,'radius',6371012);
|
---|
95 | aboveground=getfieldvalue(options,'aboveground',1000);
|
---|
96 | offset=getfieldvalue(options,'offset',.1);
|
---|
97 | fontsize=getfieldvalue(options,'fontsize',10);
|
---|
98 | label=getfieldvalue(options,'label','on');
|
---|
99 |
|
---|
100 | if exist(options,'epsg'),
|
---|
101 | if exist(options,'proj'),
|
---|
102 | error('Boundary plot error message: cannot specify epsg and proj at the same time for plot projection');
|
---|
103 | end
|
---|
104 | proj=epsg2proj(getfieldvalue(options,'epsg'));
|
---|
105 | elseif exist(options,'proj'),
|
---|
106 | proj=getfieldvalue(options,'proj');
|
---|
107 | else
|
---|
108 | proj=epsg2proj(4326);
|
---|
109 | end
|
---|
110 |
|
---|
111 | %read domain:
|
---|
112 | [path,name,ext]=fileparts(self.shpfilename);
|
---|
113 | if ~strcmpi(ext,'shp'),
|
---|
114 | ext='shp';
|
---|
115 | end
|
---|
116 | domain=shpread([self.shppath '/' name '.' ext]);
|
---|
117 |
|
---|
118 | %convert boundary to another reference frame: {{{
|
---|
119 | for i=1:length(domain),
|
---|
120 | try,
|
---|
121 | [x,y] = gdaltransform(domain(i).x,domain(i).y,self.proj,proj);
|
---|
122 | catch me
|
---|
123 | disp(me.message());
|
---|
124 | self.disp();
|
---|
125 | end
|
---|
126 | domain(i).x=x; domain(i).y=y;
|
---|
127 | end
|
---|
128 |
|
---|
129 | for i=1:length(domain),
|
---|
130 | hold on;
|
---|
131 | x=domain(i).x*unitmultiplier;
|
---|
132 | y=domain(i).y*unitmultiplier;
|
---|
133 | if length(x)==1,
|
---|
134 | p=plot(x,y,'k*');
|
---|
135 | set(p,'MarkerSize',markersize,'Color',color);
|
---|
136 | if strcmpi(label,'on'),
|
---|
137 | t=text(x,y,self.shpfilename,'FontSize',fontsize);
|
---|
138 | end
|
---|
139 | else
|
---|
140 | p=plot(x,y,'k-');
|
---|
141 | set(p,'MarkerSize',markersize,'Color',color);
|
---|
142 | if strcmpi(label,'on'),
|
---|
143 | text(sum(x)/length(x),sum(y)/length(y),self.shpfilename,'FontSize',fontsize);
|
---|
144 | end
|
---|
145 | end
|
---|
146 | set(p,'Color',color);
|
---|
147 | set(p,'LineWidth',linewidth);
|
---|
148 | end
|
---|
149 | %}}}
|
---|
150 | end % }}}
|
---|
151 | function checkconsistency(self,varargin) % {{{
|
---|
152 | %recover options
|
---|
153 | options=pairoptions(varargin{:});
|
---|
154 | tolerance=getfieldvalue(options,'tolerance',1e-5);
|
---|
155 |
|
---|
156 | %recover edges:
|
---|
157 | edges=self.edges();
|
---|
158 |
|
---|
159 | if strcmpi(edges.Geometry,'Point'),
|
---|
160 | return;
|
---|
161 | else
|
---|
162 | %check we don't have two identical vertices:
|
---|
163 | x=edges.x; y=edges.y;
|
---|
164 | distance=sqrt( (x(1:end-1)-x(2:end)).^2+(y(1:end-1)-y(2:end)).^2);
|
---|
165 | dmax=max(distance);
|
---|
166 | isidentical=find(distance<dmax*tolerance);
|
---|
167 | if ~isempty(isidentical),
|
---|
168 | error(['boundary ' shpfilename ' has two vertices extremely close to one another']);
|
---|
169 | end
|
---|
170 | end
|
---|
171 | end % }}}
|
---|
172 | function plot3d(self,varargin) % {{{
|
---|
173 | %recover options
|
---|
174 |
|
---|
175 | options=pairoptions(varargin{:});
|
---|
176 |
|
---|
177 | %parse input:
|
---|
178 | figurenumber=getfieldvalue(options,'figure',1);
|
---|
179 | color=getfieldvalue(options,'color','r');
|
---|
180 | linewidth=getfieldvalue(options,'linewidth',1);
|
---|
181 | markersize=getfieldvalue(options,'markersize',1);
|
---|
182 | unitmultiplier=getfieldvalue(options,'unit',1);
|
---|
183 | radius=getfieldvalue(options,'radius',6371012);
|
---|
184 | aboveground=getfieldvalue(options,'aboveground',1000);
|
---|
185 | offset=getfieldvalue(options,'offset',.1);
|
---|
186 | fontsize=getfieldvalue(options,'fontsize',10);
|
---|
187 | label=getfieldvalue(options,'label','on');
|
---|
188 |
|
---|
189 | if exist(options,'epsg'),
|
---|
190 | if exist(options,'proj'),
|
---|
191 | error('Boundary plot error message: cannot specify epsg and proj at the same time for plot projection');
|
---|
192 | end
|
---|
193 | proj=epsg2proj(getfieldvalue(options,'epsg'));
|
---|
194 | elseif exist(options,'proj'),
|
---|
195 | proj=getfieldvalue(options,'proj');
|
---|
196 | else
|
---|
197 | proj=epsg2proj(4326);
|
---|
198 | end
|
---|
199 |
|
---|
200 | %read domain:
|
---|
201 | [path,name,ext]=fileparts(self.shpfilename);
|
---|
202 | if ~strcmpi(ext,'shp'),
|
---|
203 | ext='shp';
|
---|
204 | end
|
---|
205 | domain=shpread([self.shppath '/' name '.' ext]);
|
---|
206 |
|
---|
207 | for i=1:length(domain),
|
---|
208 | try,
|
---|
209 | [lat,long] = gdaltransform(domain(i).x,domain(i).y,self.proj,'EPSG:4326');
|
---|
210 | catch me
|
---|
211 | disp(me.message());
|
---|
212 | self.disp();
|
---|
213 | end
|
---|
214 | domain(i).x=long; domain(i).y=lat;
|
---|
215 | end
|
---|
216 |
|
---|
217 | for i=1:length(domain),
|
---|
218 |
|
---|
219 | %project on x,y,z reference frame.
|
---|
220 | [x,y,z]=AboveGround(domain(i).x,domain(i).y,radius,aboveground);
|
---|
221 | [xt,yt,zt]=AboveGround(domain(i).x+offset,domain(i).y+offset,radius,300000);
|
---|
222 | hold on;
|
---|
223 | if length(x)==1,
|
---|
224 | p=plot3(x,y,z,'k*');
|
---|
225 | set(p,'MarkerSize',markersize);
|
---|
226 | if strcmpi(label,'on'),
|
---|
227 | t=text(xt,yt,zt,self.shpfilename,'FontSize',fontsize);
|
---|
228 | end
|
---|
229 | else
|
---|
230 | p=plot3(x,y,z,'k-');
|
---|
231 | mid=floor(length(xt)/2);
|
---|
232 | if strcmpi(label,'on'),
|
---|
233 | text(xt(mid),yt(mid),zt(mid),self.shpfilename,'FontSize',fontsize);
|
---|
234 | end
|
---|
235 | end
|
---|
236 | set(p,'Color',color);
|
---|
237 | set(p,'LineWidth',linewidth);
|
---|
238 |
|
---|
239 | end
|
---|
240 |
|
---|
241 | end % }}}
|
---|
242 | end
|
---|
243 | end
|
---|