1 | function shpcoarsen(newfile,oldfile,resolution)
|
---|
2 | %SHPCOARSEN - coarsen an shp polygon
|
---|
3 | %
|
---|
4 | % This routine reads a polygon shapefile and remove points with respect to
|
---|
5 | % the resolution (in meters) given in input.
|
---|
6 | %
|
---|
7 | % Usage:
|
---|
8 | % shpcoarsen(newfile,oldfile,resolution)
|
---|
9 | %
|
---|
10 | % Example:
|
---|
11 | % shpcoarsen('DomainOutline.shp','Antarctica.shp',4000)
|
---|
12 |
|
---|
13 | %Some checks
|
---|
14 | if nargin~=3 | nargout
|
---|
15 | error('shpcoarsen usage: shpcoarsen(newfile,oldfile,resolution)')
|
---|
16 | elseif ~exist(oldfile)
|
---|
17 | error(['shpcoarsen error message: the file ' oldfile ' does not exist'])
|
---|
18 | elseif exist(newfile),
|
---|
19 | %choice=input(['A file ' newfile ' already exists, do you want to modify it? (y/n)'],'s');
|
---|
20 | %if ~strcmpi(choice,'y'),
|
---|
21 | % disp('no modification done ... exiting');
|
---|
22 | % return;
|
---|
23 | %end
|
---|
24 | end
|
---|
25 |
|
---|
26 | %Get oldfile:
|
---|
27 | A=shpread(oldfile);
|
---|
28 | numprofiles=size(A,2);
|
---|
29 |
|
---|
30 | %Go through the profiles
|
---|
31 | count=1;
|
---|
32 | while count<=numprofiles,
|
---|
33 |
|
---|
34 | %get number of points and initialize j
|
---|
35 | numpoints=length(A(count).x);
|
---|
36 | j=1;
|
---|
37 |
|
---|
38 | %stop if we have reached end of profile (always keep the last point)
|
---|
39 | while j<numpoints,
|
---|
40 |
|
---|
41 | %See whether we keep this point or not
|
---|
42 | distance=sqrt((A(count).x(j)-A(count).x(j+1))^2+(A(count).y(j)-A(count).y(j+1))^2);
|
---|
43 | if distance<resolution & j<numpoints-1 %do not remove last point
|
---|
44 | A(count).x(j+1)=[];
|
---|
45 | A(count).y(j+1)=[];
|
---|
46 | numpoints=numpoints-1;
|
---|
47 | else
|
---|
48 | division=floor(distance/resolution)+1;
|
---|
49 | if division>=2,
|
---|
50 | x=linspace(A(count).x(j),A(count).x(j+1),division)';
|
---|
51 | y=linspace(A(count).y(j),A(count).y(j+1),division)';
|
---|
52 | A(count).x=[A(count).x(1:j);x(2:end-1); A(count).x(j+1:end)];
|
---|
53 | A(count).y=[A(count).y(1:j);y(2:end-1); A(count).y(j+1:end)];
|
---|
54 |
|
---|
55 | %update current point
|
---|
56 | j=j+1+division-2;
|
---|
57 | numpoints=numpoints+division-2;
|
---|
58 | else
|
---|
59 | %update current point
|
---|
60 | j=j+1;
|
---|
61 | end
|
---|
62 | end
|
---|
63 | end
|
---|
64 | if length(A(count).x)<=1,
|
---|
65 | A(count)=[];
|
---|
66 | numprofiles=numprofiles-1;
|
---|
67 | else
|
---|
68 | count=count+1;
|
---|
69 | end
|
---|
70 | end
|
---|
71 |
|
---|
72 | %write output
|
---|
73 | shpwrite(A,newfile);
|
---|