| 1 | %BASIN class definition
|
|---|
| 2 | %
|
|---|
| 3 | % Usage:
|
|---|
| 4 | % basin=basin();
|
|---|
| 5 |
|
|---|
| 6 | classdef basin
|
|---|
| 7 | properties (SetAccess=public)
|
|---|
| 8 | boundaries = {};
|
|---|
| 9 | epsg = 3426;
|
|---|
| 10 | name = '';
|
|---|
| 11 | continent = '';
|
|---|
| 12 | end
|
|---|
| 13 | methods (Static)
|
|---|
| 14 | function self = loadobj(self) % {{{
|
|---|
| 15 | % This function is directly called by matlab when a model object is
|
|---|
| 16 | % loaded. Update old properties here
|
|---|
| 17 | end% }}}
|
|---|
| 18 | end
|
|---|
| 19 | methods
|
|---|
| 20 | function self = basin(varargin) % {{{
|
|---|
| 21 | switch nargin
|
|---|
| 22 | case 0
|
|---|
| 23 | self=setdefaultparameters(self);
|
|---|
| 24 | otherwise
|
|---|
| 25 |
|
|---|
| 26 | self=setdefaultparameters(self);
|
|---|
| 27 | options=pairoptions(varargin{:});
|
|---|
| 28 |
|
|---|
| 29 | %recover field values:
|
|---|
| 30 | self.boundaries=getfieldvalue(options,'boundaries',{});
|
|---|
| 31 | self.name=getfieldvalue(options,'name','');
|
|---|
| 32 | self.continent=getfieldvalue(options,'continent','');
|
|---|
| 33 | self.epsg=getfieldvalue(options,'epsg',3426);
|
|---|
| 34 | end
|
|---|
| 35 | end % }}}
|
|---|
| 36 | function self = setdefaultparameters(self) % {{{
|
|---|
| 37 | self.name='';
|
|---|
| 38 | self.continent='';
|
|---|
| 39 | self.epsg=3426;
|
|---|
| 40 | self.boundaries={};
|
|---|
| 41 |
|
|---|
| 42 | end % }}}
|
|---|
| 43 | function disp(self) % {{{
|
|---|
| 44 | disp(sprintf(' basin parameters:'));
|
|---|
| 45 | fielddisplay(self,'continent','continent name');
|
|---|
| 46 | fielddisplay(self,'name','basin name');
|
|---|
| 47 | fielddisplay(self,'epsg','epsg projection number for the entire basin');
|
|---|
| 48 | fielddisplay(self,'boundaries','list of boundary objects');
|
|---|
| 49 | for i=1:length(self.boundaries),
|
|---|
| 50 | disp(sprintf(' boundary #%i: %s',i,self.boundaries{i}.name));
|
|---|
| 51 | end
|
|---|
| 52 |
|
|---|
| 53 | end % }}}
|
|---|
| 54 | function boolean=isnameany(self,varargin) % {{{
|
|---|
| 55 | boolean=0;
|
|---|
| 56 | for i=1:length(varargin),
|
|---|
| 57 | if strcmpi(self.name,varargin{i}),
|
|---|
| 58 | boolean=1;
|
|---|
| 59 | break;
|
|---|
| 60 | end
|
|---|
| 61 | end
|
|---|
| 62 | end % }}}
|
|---|
| 63 | function boolean=iscontinentany(self,varargin) % {{{
|
|---|
| 64 | boolean=0;
|
|---|
| 65 | for i=1:length(varargin),
|
|---|
| 66 | if strcmpi(self.continent,varargin{i}),
|
|---|
| 67 | boolean=1;
|
|---|
| 68 | break;
|
|---|
| 69 | end
|
|---|
| 70 | end
|
|---|
| 71 | end % }}}
|
|---|
| 72 | function output=outputname(self,varargin) % {{{
|
|---|
| 73 |
|
|---|
| 74 | %recover options
|
|---|
| 75 | options=pairoptions(varargin{:});
|
|---|
| 76 | extension=getfieldvalue(options,'extension',1);
|
|---|
| 77 |
|
|---|
| 78 | [path,nme,ext]=fileparts(self.name);
|
|---|
| 79 | if extension,
|
|---|
| 80 | output=[nme ext];
|
|---|
| 81 | else
|
|---|
| 82 | output=nme;
|
|---|
| 83 | end
|
|---|
| 84 | end % }}}
|
|---|
| 85 | function plot(self,varargin) % {{{
|
|---|
| 86 |
|
|---|
| 87 | %add option:
|
|---|
| 88 | for i=1:length(self.boundaries),
|
|---|
| 89 | self.boundaries{i}.plot('epsg',self.epsg,varargin{:});
|
|---|
| 90 | end
|
|---|
| 91 |
|
|---|
| 92 | end % }}}
|
|---|
| 93 | function plot3d(self,varargin) % {{{
|
|---|
| 94 |
|
|---|
| 95 | %add option:
|
|---|
| 96 | for i=1:length(self.boundaries),
|
|---|
| 97 | self.boundaries{i}.plot3d(varargin{:});
|
|---|
| 98 | end
|
|---|
| 99 |
|
|---|
| 100 | end % }}}
|
|---|
| 101 | function out=contour(self,varargin) % {{{
|
|---|
| 102 |
|
|---|
| 103 | %recover options
|
|---|
| 104 | options=pairoptions(varargin{:});
|
|---|
| 105 | x=[]; y=[];
|
|---|
| 106 |
|
|---|
| 107 | %go through boundaries, recover edges and project them in the basin epsg reference frame:
|
|---|
| 108 | for i=1:length(self.boundaries),
|
|---|
| 109 | boundary=self.boundaries{i};
|
|---|
| 110 | contour=boundary.edges();
|
|---|
| 111 | [contour.x,contour.y]=gdaltransform(contour.x,contour.y,sprintf('EPSG:%i',boundary.epsg),sprintf('EPSG:%i',self.epsg));
|
|---|
| 112 | x=[x;contour.x];
|
|---|
| 113 | y=[y;contour.y];
|
|---|
| 114 | end
|
|---|
| 115 | %close the contour:
|
|---|
| 116 | if x(end)~=x(1) | y(end)~=y(1),
|
|---|
| 117 | x(end)=x(1); y(end)=y(1);
|
|---|
| 118 | end
|
|---|
| 119 |
|
|---|
| 120 | out.x=x;
|
|---|
| 121 | out.y=y;
|
|---|
| 122 | out.nods=length(x);
|
|---|
| 123 | end % }}}
|
|---|
| 124 | function output=shapefilecrop(self,varargin) % {{{
|
|---|
| 125 |
|
|---|
| 126 | %recover options
|
|---|
| 127 | options=pairoptions(varargin{:});
|
|---|
| 128 | threshold=getfieldvalue(options,'threshold',.65); %.65 degrees lat,long
|
|---|
| 129 | inshapefile=getfieldvalue(options,'shapefile');
|
|---|
| 130 | outputshapefile=getfieldvalue(options,'outputshapefile','');
|
|---|
| 131 | epsgshapefile=getfieldvalue(options,'epsgshapefile');
|
|---|
| 132 |
|
|---|
| 133 | %create list of contours that have critical length > threshold: (in lat,long)
|
|---|
| 134 | contours=shpread(inshapefile);
|
|---|
| 135 | llist=[];
|
|---|
| 136 | for i=1:length(contours),
|
|---|
| 137 | contour=contours(i);
|
|---|
| 138 | carea=polyarea(contour.x,contour.y);
|
|---|
| 139 | clength=sqrt(carea);
|
|---|
| 140 | if clength<threshold,
|
|---|
| 141 | llist=[llist;i];
|
|---|
| 142 | end
|
|---|
| 143 | end
|
|---|
| 144 | contours(llist)=[];
|
|---|
| 145 |
|
|---|
| 146 | %project onto reference frame:
|
|---|
| 147 | if self.epsg~=epsgshapefile,
|
|---|
| 148 | for i=1:length(contours),
|
|---|
| 149 | h=contours(i);
|
|---|
| 150 | [h.x,h.y]=gdaltransform(h.x,h.y,sprintf('EPSG:%i',epsgshapefile),sprintf('EPSG:%i',self.epsg));
|
|---|
| 151 | contours(i).x=h.x;
|
|---|
| 152 | contours(i).y=h.y;
|
|---|
| 153 | end
|
|---|
| 154 | end
|
|---|
| 155 |
|
|---|
| 156 | %only keep the contours that are inside the basin (take centroids):
|
|---|
| 157 | ba=self.contour();
|
|---|
| 158 | flags=zeros(length(contours),1);
|
|---|
| 159 | for i=1:length(contours),
|
|---|
| 160 | h=contours(i);
|
|---|
| 161 | in=inpolygon(h.x,h.y,ba.x,ba.y);
|
|---|
| 162 | if ~isempty(find(in==0)),
|
|---|
| 163 | flags(i)=1;
|
|---|
| 164 | end
|
|---|
| 165 | end
|
|---|
| 166 | pos=find(flags); contours(pos)=[];
|
|---|
| 167 |
|
|---|
| 168 | %Two options:
|
|---|
| 169 | if strcmpi(outputshapefile,''),
|
|---|
| 170 | output=contours;
|
|---|
| 171 | else
|
|---|
| 172 | shpwrite(contours,outputshapefile);
|
|---|
| 173 | end
|
|---|
| 174 |
|
|---|
| 175 | end % }}}
|
|---|
| 176 | end
|
|---|
| 177 | end
|
|---|