| 1 | function Struct=shpread(filename)
|
|---|
| 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 | %some checks
|
|---|
| 22 | if ~exist(filename),
|
|---|
| 23 | error(['shpread error message: file ' filename ' not found!']);
|
|---|
| 24 | end
|
|---|
| 25 |
|
|---|
| 26 | %initialize number of profile
|
|---|
| 27 | count=0;
|
|---|
| 28 |
|
|---|
| 29 | %read shapefile
|
|---|
| 30 | shp=shaperead(filename);
|
|---|
| 31 |
|
|---|
| 32 | Struct=struct([]);
|
|---|
| 33 | fields=fieldnames(shp);
|
|---|
| 34 | for i=1:length(shp),
|
|---|
| 35 | if strcmpi(shp(i).Geometry,'Polygon'),
|
|---|
| 36 | x=shp(i).X'; y=shp(i).Y';
|
|---|
| 37 | ids=find(isnan(x));
|
|---|
| 38 | x(ids)=[]; y(ids)=[];
|
|---|
| 39 |
|
|---|
| 40 | Struct(end+1).x=x;
|
|---|
| 41 | Struct(end).y=y;
|
|---|
| 42 | Struct(end).nods=length(x);
|
|---|
| 43 | Struct(end).density=1;
|
|---|
| 44 | Struct(end).closed=1;
|
|---|
| 45 | if isfield(shp,'id'),
|
|---|
| 46 | Struct(end).name=num2str(shp(i).id);
|
|---|
| 47 | else
|
|---|
| 48 | Struct(end).name='';
|
|---|
| 49 | end
|
|---|
| 50 | for j=1:length(fields),
|
|---|
| 51 | field=fields{j};
|
|---|
| 52 | if ~(strcmpi(field,'X') | strcmpi(field,'Y') | strcmpi(field,'id')),
|
|---|
| 53 | Struct(end).(field)=shp(i).(field);
|
|---|
| 54 | end
|
|---|
| 55 | end
|
|---|
| 56 | end
|
|---|
| 57 |
|
|---|
| 58 | if strcmpi(shp(i).Geometry,'Line'),
|
|---|
| 59 | x=shp(i).X'; y=shp(i).Y';
|
|---|
| 60 | ids=find(isnan(x));
|
|---|
| 61 | x(ids)=[]; y(ids)=[];
|
|---|
| 62 |
|
|---|
| 63 | Struct(end+1).x=x;
|
|---|
| 64 | Struct(end).y=y;
|
|---|
| 65 | Struct(end).nods=length(x);
|
|---|
| 66 | Struct(end).density=1;
|
|---|
| 67 | Struct(end).closed=1;
|
|---|
| 68 | if isfield(shp,'id'),
|
|---|
| 69 | Struct(end).name=num2str(shp(i).id);
|
|---|
| 70 | else
|
|---|
| 71 | Struct(end).name='';
|
|---|
| 72 | end
|
|---|
| 73 | for j=1:length(fields),
|
|---|
| 74 | field=fields{j};
|
|---|
| 75 | if ~(strcmpi(field,'X') | strcmpi(field,'Y') | strcmpi(field,'id')),
|
|---|
| 76 | Struct(end).(field)=shp(i).(field);
|
|---|
| 77 | end
|
|---|
| 78 | end
|
|---|
| 79 | end
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | if strcmpi(shp(i).Geometry,'Point'),
|
|---|
| 83 | x=shp(i).X'; y=shp(i).Y';
|
|---|
| 84 | ids=find(isnan(x));
|
|---|
| 85 | x(ids)=[]; y(ids)=[];
|
|---|
| 86 |
|
|---|
| 87 | Struct(end+1).x=x;
|
|---|
| 88 | Struct(end).y=y;
|
|---|
| 89 | Struct(end).density=1;
|
|---|
| 90 | if isfield(shp,'id'),
|
|---|
| 91 | Struct(end).name=num2str(shp(i).id);
|
|---|
| 92 | else
|
|---|
| 93 | Struct(end).name='';
|
|---|
| 94 | end
|
|---|
| 95 | for j=1:length(fields),
|
|---|
| 96 | field=fields{j};
|
|---|
| 97 | if ~(strcmpi(field,'X') | strcmpi(field,'Y') | strcmpi(field,'id')),
|
|---|
| 98 | Struct(end).(field)=shp(i).(field);
|
|---|
| 99 | end
|
|---|
| 100 | end
|
|---|
| 101 | end
|
|---|
| 102 | end
|
|---|
| 103 |
|
|---|
| 104 | end
|
|---|