| 1 | function Struct=shpread(filename,varargin)
|
|---|
| 2 | %SHPREAD - read a shape file and build a Structure
|
|---|
| 3 | %
|
|---|
| 4 | % This routine reads a shape file .shp and builds a Structure containing the
|
|---|
| 5 | % fields x and y corresponding to the coordinates, one for the filename of
|
|---|
| 6 | % the shp file, for the density, for the nodes, and a field closed to
|
|---|
| 7 | % indicate if the domain is closed.
|
|---|
| 8 | % If this initial shapefile is point only, the fields closed and
|
|---|
| 9 | % points are ommited
|
|---|
| 10 | % The first argument is the .shp file to be read and the second one (optional)
|
|---|
| 11 | % indicates if the last point shall be read (1 to read it, 0 not to).
|
|---|
| 12 | %
|
|---|
| 13 | % Usage:
|
|---|
| 14 | % Struct=shpread(filename)
|
|---|
| 15 | %
|
|---|
| 16 | % Example:
|
|---|
| 17 | % Struct=shpread('domainoutline.shp')
|
|---|
| 18 | %
|
|---|
| 19 | % See also EXPDOC, EXPWRITEASVERTICES
|
|---|
| 20 |
|
|---|
| 21 | %recover options
|
|---|
| 22 | options=pairoptions(varargin{:});
|
|---|
| 23 |
|
|---|
| 24 | %some checks
|
|---|
| 25 | if ~exist(filename),
|
|---|
| 26 | error(['shpread error message: file ' filename ' not found!']);
|
|---|
| 27 | end
|
|---|
| 28 |
|
|---|
| 29 | %initialize number of profile
|
|---|
| 30 | count=0;
|
|---|
| 31 |
|
|---|
| 32 | %read shapefile
|
|---|
| 33 | shp=shaperead(filename);
|
|---|
| 34 |
|
|---|
| 35 | Struct=struct([]);
|
|---|
| 36 | fields=fieldnames(shp);
|
|---|
| 37 | for i=1:length(shp),
|
|---|
| 38 | if strcmpi(shp(i).Geometry,'Polygon'),
|
|---|
| 39 | x=shp(i).X'; y=shp(i).Y';
|
|---|
| 40 | ids=find(isnan(x));
|
|---|
| 41 | x(ids)=[]; y(ids)=[];
|
|---|
| 42 |
|
|---|
| 43 | Struct(end+1).x=x;
|
|---|
| 44 | Struct(end).y=y;
|
|---|
| 45 | Struct(end).nods=length(x);
|
|---|
| 46 | Struct(end).density=1;
|
|---|
| 47 | Struct(end).closed=1;
|
|---|
| 48 | if isfield(shp,'id'),
|
|---|
| 49 | Struct(end).name=num2str(shp(i).id);
|
|---|
| 50 | else
|
|---|
| 51 | Struct(end).name='';
|
|---|
| 52 | end
|
|---|
| 53 | for j=1:length(fields),
|
|---|
| 54 | field=fields{j};
|
|---|
| 55 | if ~(strcmpi(field,'X') | strcmpi(field,'Y') | strcmpi(field,'id')),
|
|---|
| 56 | Struct(end).(field)=shp(i).(field);
|
|---|
| 57 | end
|
|---|
| 58 | end
|
|---|
| 59 | end
|
|---|
| 60 |
|
|---|
| 61 | if strcmpi(shp(i).Geometry,'Line'),
|
|---|
| 62 | x=shp(i).X'; y=shp(i).Y';
|
|---|
| 63 | ids=find(isnan(x));
|
|---|
| 64 | x(ids)=[]; y(ids)=[];
|
|---|
| 65 |
|
|---|
| 66 | Struct(end+1).x=x;
|
|---|
| 67 | Struct(end).y=y;
|
|---|
| 68 | Struct(end).nods=length(x);
|
|---|
| 69 | Struct(end).density=1;
|
|---|
| 70 | Struct(end).closed=1;
|
|---|
| 71 | if isfield(shp,'id'),
|
|---|
| 72 | Struct(end).name=num2str(shp(i).id);
|
|---|
| 73 | else
|
|---|
| 74 | Struct(end).name='';
|
|---|
| 75 | end
|
|---|
| 76 | for j=1:length(fields),
|
|---|
| 77 | field=fields{j};
|
|---|
| 78 | if ~(strcmpi(field,'X') | strcmpi(field,'Y') | strcmpi(field,'id')),
|
|---|
| 79 | Struct(end).(field)=shp(i).(field);
|
|---|
| 80 | end
|
|---|
| 81 | end
|
|---|
| 82 | end
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | if strcmpi(shp(i).Geometry,'Point'),
|
|---|
| 86 | x=shp(i).X'; y=shp(i).Y';
|
|---|
| 87 | ids=find(isnan(x));
|
|---|
| 88 | x(ids)=[]; y(ids)=[];
|
|---|
| 89 |
|
|---|
| 90 | Struct(end+1).x=x;
|
|---|
| 91 | Struct(end).y=y;
|
|---|
| 92 | Struct(end).density=1;
|
|---|
| 93 | if isfield(shp,'id'),
|
|---|
| 94 | Struct(end).name=num2str(shp(i).id);
|
|---|
| 95 | else
|
|---|
| 96 | Struct(end).name='';
|
|---|
| 97 | end
|
|---|
| 98 | for j=1:length(fields),
|
|---|
| 99 | field=fields{j};
|
|---|
| 100 | if ~(strcmpi(field,'X') | strcmpi(field,'Y') | strcmpi(field,'id')),
|
|---|
| 101 | Struct(end).(field)=shp(i).(field);
|
|---|
| 102 | end
|
|---|
| 103 | end
|
|---|
| 104 | end
|
|---|
| 105 | end
|
|---|
| 106 |
|
|---|
| 107 | invert=getfieldvalue(options,'invert',0);
|
|---|
| 108 | if invert,
|
|---|
| 109 | for i=1:length(Struct),
|
|---|
| 110 | Struct(i).x=flipud(Struct(i).x);
|
|---|
| 111 | Struct(i).y=flipud(Struct(i).y);
|
|---|
| 112 | end
|
|---|
| 113 | end
|
|---|
| 114 |
|
|---|
| 115 | end
|
|---|