1 | function [bndry_lon,bndry_lat] = m_plotbndry(name,varargin) |
---|
2 | % M_PLOTBNDRY plots text files of Lat,Lon for political boundaries. |
---|
3 | % Text files (derived from the DCW) are obtained from |
---|
4 | % http://www.maproom.psu.edu/cgi-bin/ian/points/index.cgi |
---|
5 | % |
---|
6 | % M_PLOTBNDRY(NAME) plots the state or country specified in the |
---|
7 | % string NAME, which may include a path name. The routine will |
---|
8 | % then a) search the specified path for a mat-file of that name |
---|
9 | % b) search the specified path for an ascii *2pts.txt file |
---|
10 | % of that name, and, if found convert it to a mat-file. |
---|
11 | % c) failing that, it will open a file dialog box. |
---|
12 | % |
---|
13 | % M_PLOTBNDRY(NAME, ...line properties) will use the specified |
---|
14 | % line properties in drawing the boundary. |
---|
15 | % |
---|
16 | % [LON,LAT]=M_PLOTBNDRY(...) returns vectors of the boundary |
---|
17 | % points. |
---|
18 | % |
---|
19 | % Note: If errors occur when a file is first plotted, check that |
---|
20 | % the entire file was downloaded. It should end with two consecutive |
---|
21 | % END lines. |
---|
22 | |
---|
23 | |
---|
24 | % Original Author: Michael W. Mann |
---|
25 | % |
---|
26 | % Changes: R. Pawlowicz 21/12/98 - changed interface to read from |
---|
27 | % given directory, allow output, allow various line |
---|
28 | % properties to be specified. |
---|
29 | % 6/Nov/00 - eliminate returned stuff if ';' neglected (thx to D Byrne) |
---|
30 | % 19/Mar/04 - .mat files not being created because of a bug found by James Connor. |
---|
31 | |
---|
32 | |
---|
33 | % Set current projection to geographic |
---|
34 | Currentmap=m_coord('set'); |
---|
35 | m_coord('geographic'); |
---|
36 | |
---|
37 | |
---|
38 | targetfile = [name,'2pts.mat']; %try to find binary file |
---|
39 | targetpath = dir(targetfile); |
---|
40 | |
---|
41 | if ( length(targetpath)~=0 ) %then load file. |
---|
42 | load(targetfile); |
---|
43 | m_line(bndry_lon,bndry_lat,'tag','m_plotbndry',varargin{:}); |
---|
44 | |
---|
45 | else %Can't find binary file, load and process text file. |
---|
46 | % Create .mat file from data read. |
---|
47 | |
---|
48 | targetfile = [name,'2pts.txt']; |
---|
49 | targetpath = dir(targetfile); |
---|
50 | if ( length(targetpath)==0 ) %then |
---|
51 | [filename,pathname]=uigetfile('*2pts.txt',['Select ',targetfile,' file']); |
---|
52 | if ( filename == 0 ), error(['Could not find ',targetfile]), end |
---|
53 | targetpath = [pathname,filename]; |
---|
54 | end %if |
---|
55 | [fid,message] = fopen(targetfile,'r'); |
---|
56 | if ( fid < 0 ) %then |
---|
57 | fclose(fid); |
---|
58 | error(message); |
---|
59 | end %if |
---|
60 | |
---|
61 | namein = fgetl(fid); |
---|
62 | if ( ~findstr(lower(name),lower(namein)) ) %then |
---|
63 | fclose(fid); |
---|
64 | error(['File contains wrong state! ','Desired: ',name,' Found: ',namein]); |
---|
65 | end %if |
---|
66 | |
---|
67 | done = 0; |
---|
68 | polynum = 0; |
---|
69 | numpts = []; |
---|
70 | while ( ~done ) |
---|
71 | %read in polygon index |
---|
72 | line = fgetl(fid); |
---|
73 | field = sscanf(line,'%3c'); |
---|
74 | if ( strcmp(upper(field),'END') ) %then found EOF |
---|
75 | done = 1; |
---|
76 | else %start reading in polygon |
---|
77 | polynum = polynum + 1; |
---|
78 | polynumin = sscanf(line,'%u'); |
---|
79 | if ( polynum ~= polynumin ) %then |
---|
80 | fclose(fid); |
---|
81 | error(['Didn''t find polynum ',num2str(polynum),... |
---|
82 | ', found ',num2str(polynumin),' .']); |
---|
83 | end %if |
---|
84 | line = fgetl(fid); %skip polygon centroid |
---|
85 | line = fgetl(fid); %first point |
---|
86 | ptcount = 0; |
---|
87 | while ( ~strcmp(upper(line(1:3)),'END') ) |
---|
88 | ptcount = ptcount + 1; |
---|
89 | line = fgetl(fid); |
---|
90 | end %while |
---|
91 | numpts = [numpts,ptcount]; |
---|
92 | end %if |
---|
93 | end %while |
---|
94 | |
---|
95 | Npolygon = length(numpts); |
---|
96 | fclose(fid); %Close file. |
---|
97 | |
---|
98 | % Vectors for composite outline |
---|
99 | bndry_lat = zeros(1,sum(numpts)+Npolygon); |
---|
100 | bndry_lon = zeros(1,sum(numpts)+Npolygon); |
---|
101 | i1 = 0; %index to last filled entry in vectors |
---|
102 | |
---|
103 | fid = fopen(targetfile,'r'); %reopen file |
---|
104 | line = fgetl(fid); %ignore namefield |
---|
105 | |
---|
106 | for i = 1:Npolygon |
---|
107 | line = fgetl(fid); %ignore polygon index |
---|
108 | line = fgetl(fid); %ignore centroid lat,lon |
---|
109 | [poly,count] = fscanf(fid,'%g',[2,inf]); %read data |
---|
110 | line = fgetl(fid); %ignore END |
---|
111 | |
---|
112 | % Save data for reuse as .mat file |
---|
113 | i0 = i1 + 1; i1 = i0 + numpts(i); |
---|
114 | bndry_lon(i0:i1) = [poly(1,:),NaN]; %add NaN to lift pen |
---|
115 | bndry_lat(i0:i1) = [poly(2,:),NaN]; %add NaN to lift pen |
---|
116 | |
---|
117 | end %for |
---|
118 | |
---|
119 | fclose(fid); |
---|
120 | |
---|
121 | m_line(bndry_lon,bndry_lat,'tag','m_plotbndry',varargin{:}); |
---|
122 | nchar = length(targetfile); % Bug fix thanks to James Connor |
---|
123 | matfile = [targetfile(1:(nchar-4)),'.mat']; |
---|
124 | save(matfile,'bndry_lat','bndry_lon'); |
---|
125 | |
---|
126 | end %if |
---|
127 | |
---|
128 | m_coord(Currentmap.name); |
---|
129 | |
---|
130 | if nargout==0, |
---|
131 | clear bndry_lon bndry_lat |
---|
132 | end; |
---|