00001 /* $Id: plmap.c 9477 2009-02-08 22:34:26Z andrewross $ 00002 00003 Continental Outline and Political Boundary Backgrounds 00004 00005 Some plots need a geographical background such as the global 00006 surface temperatures or the population density. The routine 00007 plmap() will draw one of the following backgrounds: continental 00008 outlines, political boundaries, the United States, and the United 00009 States with the continental outlines. The routine plmeridians() 00010 will add the latitudes and longitudes to the background. After 00011 the background has been drawn, one can use a contour routine or a 00012 symbol plotter to finish off the plot. 00013 00014 Copyright (C) 1991, 1993, 1994 Wesley Ebisuzaki 00015 Copyright (C) 1994, 2000, 2001 Maurice LeBrun 00016 Copyright (C) 1999 Geoffrey Furnish 00017 Copyright (C) 2000, 2001, 2002 Alan W. Irwin 00018 Copyright (C) 2001 Andrew Roach 00019 Copyright (C) 2001, 2004 Rafael Laboissiere 00020 Copyright (C) 2002 Vincent Darley 00021 Copyright (C) 2003 Joao Cardoso 00022 00023 This file is part of PLplot. 00024 00025 PLplot is free software; you can redistribute it and/or modify 00026 it under the terms of the GNU Library General Public License 00027 as published by the Free Software Foundation; version 2 of the 00028 License. 00029 00030 PLplot is distributed in the hope that it will be useful, but 00031 WITHOUT ANY WARRANTY; without even the implied warranty of 00032 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00033 GNU Library General Public License for more details. 00034 00035 You should have received a copy of the GNU Library General Public 00036 License along with this library; if not, write to the Free Software 00037 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 00038 USA 00039 00040 */ 00041 00042 #include "plplotP.h" 00043 00044 /*----------------------------------------------------------------------*\ 00045 * void plmap(void (*mapform)(PLINT, PLFLT *, PLFLT *), const char *type, 00046 * PLFLT minlong, PLFLT maxlong, PLFLT minlat, PLFLT maxlat); 00047 * 00048 * plot continental outline in world coordinates 00049 * 00050 * v1.4: machine independant version 00051 * v1.3: replaced plcontinent by plmap, added plmeridians 00052 * v1.2: 2 arguments: mapform, type of plot 00053 * 00054 * mapform(PLINT n, PLFLT *x, PLFLT *y) is a routine to transform the 00055 * coordinate longitudes and latitudes to a plot coordinate system. By 00056 * using this transform, we can change from a longitude, latitude 00057 * coordinate to a polar stereographic project, for example. Initially, 00058 * x[0]..[n-1] are the longitudes and y[0]..y[n-1] are the corresponding 00059 * latitudes. After the call to mapform(), x[] and y[] should be replaced 00060 * by the corresponding plot coordinates. If no transform is desired, 00061 * mapform can be replaced by NULL. 00062 * 00063 * type is a character string. The value of this parameter determines the 00064 * type of background. The possible values are, 00065 * 00066 * "globe" continental outlines 00067 * "usa" USA and state boundaries 00068 * "cglobe" continental outlines and countries 00069 * "usaglobe" USA, state boundaries and continental outlines 00070 * 00071 * minlong, maxlong are the values of the longitude on the left and right 00072 * side of the plot, respectively. The value of minlong must be less than 00073 * the values of maxlong, and the values of maxlong-minlong must be less 00074 * or equal to 360. 00075 * 00076 * minlat, maxlat are the minimum and maximum latitudes to be plotted on 00077 * the background. One can always use -90.0 and 90.0 as the boundary 00078 * outside the plot window will be automatically eliminated. However, the 00079 * program will be faster if one can reduce the size of the background 00080 * plotted. 00081 \*----------------------------------------------------------------------*/ 00082 00083 #define MAP_FILE ".map" 00084 #define OFFSET (180*100) 00085 #define SCALE 100.0 00086 #define W_BUFSIZ (32*1024) 00087 00088 void 00089 plmap( void (*mapform)(PLINT, PLFLT *, PLFLT *), const char *type, 00090 PLFLT minlong, PLFLT maxlong, PLFLT minlat, PLFLT maxlat ) 00091 { 00092 PLINT wrap, sign; 00093 int i, j; 00094 PLFLT bufx[200], bufy[200], x[2], y[2]; 00095 short int test[400]; 00096 register PDFstrm *in; 00097 char filename[100]; 00098 00099 unsigned char n_buff[2], buff[800]; 00100 int n; 00101 long int t; 00102 00103 /* 00104 * read map outline 00105 */ 00106 strncpy(filename,type,99); 00107 filename[99] = '\0'; 00108 strncat(filename,MAP_FILE,99-strlen(filename)); 00109 00110 if ((in = plLibOpenPdfstrm(filename)) == NULL) 00111 return; 00112 00113 for (;;) { 00114 /* read in # points in segment */ 00115 if (pdf_rdx(n_buff, sizeof (unsigned char)* 2, in) == 0) break; 00116 n = (n_buff[0] << 8) + n_buff[1]; 00117 if (n == 0) break; 00118 00119 pdf_rdx(buff, sizeof (unsigned char)*4*n, in); 00120 if (n == 1) continue; 00121 00122 for (j = i = 0; i < n; i++, j += 2) { 00123 t = (buff[j] << 8) + buff[j+1]; 00124 bufx[i] = (t - OFFSET) / SCALE; 00125 } 00126 for (i = 0; i < n; i++, j += 2) { 00127 t = (buff[j] << 8) + buff[j+1]; 00128 bufy[i] = (t - OFFSET) / SCALE; 00129 } 00130 00131 for (i = 0; i < n; i++) { 00132 while (bufx[i] < minlong) { 00133 bufx[i] += 360.0; 00134 } 00135 while (bufx[i] > maxlong) { 00136 bufx[i] -= 360.0; 00137 } 00138 } 00139 00140 /* remove last 2 points if both outside of domain and won't plot */ 00141 00142 /* AR: 18/11/01 00143 * I have commented out the next section which supposedly 00144 * removes points that do not plot within the domain. 00145 * 00146 * This code appears at any rate to be superseded by the next 00147 * block of code that checks for wrapping problems. Removing 00148 * this code seems to have fixed up the problems with mapping 00149 * function, but I do not wish to delete it outright just now in 00150 * case I have managed to miss something. 00151 */ 00152 00153 /* while (n > 1) { 00154 if ((bufx[n-1] < minlong && bufx[n-2] < minlong) || 00155 (bufx[n-1] > maxlong && bufx[n-2] > maxlong) || 00156 (bufy[n-1] < minlat && bufy[n-2] < minlat) || 00157 (bufy[n-1] > maxlat && bufy[n-2] > maxlat)) { 00158 n--; 00159 } 00160 else { 00161 break; 00162 } 00163 } 00164 if (n <= 1) continue; 00165 */ 00166 00167 if (mapform != NULL) (*mapform)(n,bufx,bufy); /* moved transformation to here */ 00168 /* so bound-checking worked right */ 00169 00170 wrap = 0; 00171 /* check for wrap around problems */ 00172 for (i = 0; i < n-1; i++) { 00173 00174 /* jc: this code is wrong, as the bufx/y are floats that are 00175 converted to ints before abs() is called. Thus, small 00176 differences are masked off. The proof that it is wrong is 00177 that when replacing abs() with fabs(), as it should be, 00178 the code works the wrong way. What a proof :-) 00179 00180 test[i] = abs((bufx[i]-bufx[i+1])) > abs(bufy[i]/3); 00181 00182 If the intended behaviour is *really* that, than an 00183 explicit cast should be used to help other programmers, as 00184 is done bellow!!! 00185 */ 00186 00187 test[i] = abs((int)(bufx[i]-bufx[i+1])) > abs((int)bufy[i]/3); /* Changed this from 30 degrees so it is now "polar sensitive" */ 00188 if (test[i]) wrap = 1; 00189 } 00190 00191 if (wrap == 0) { 00192 plline(n,bufx,bufy); 00193 } 00194 else { 00195 for (i = 0; i < n-1; i++) { 00196 x[0] = bufx[i]; 00197 x[1] = bufx[i+1]; 00198 y[0] = bufy[i]; 00199 y[1] = bufy[i+1]; 00200 if (test[i] == 0) { 00201 plline(2,x,y); 00202 } 00203 else { /* this code seems to supercede the block commented out above */ 00204 /* segment goes off the edge */ 00205 sign = (x[1] > x[0]) ? 1 : -1; 00206 x[1] -= sign * 360.0; 00207 plline(2,x,y); 00208 x[0] = bufx[i]; 00209 x[1] = bufx[i+1]; 00210 y[0] = bufy[i]; 00211 y[1] = bufy[i+1]; 00212 x[0] += sign * 360.0; 00213 plline(2,x,y); 00214 } 00215 } 00216 } 00217 } 00218 /* Close map file */ 00219 pdf_close(in); 00220 } 00221 00222 /*----------------------------------------------------------------------*\ 00223 * void plmeridians(void (*mapform)(PLINT, PLFLT *, PLFLT *), 00224 * PLFLT dlong, PLFLT dlat, PLFLT minlong, PLFLT maxlong, 00225 * PLFLT minlat, PLFLT maxlat); 00226 * 00227 * Plot the latitudes and longitudes on the background. The lines 00228 * are plotted in the current color and line style. 00229 * 00230 * mapform(PLINT n, PLFLT *x, PLFLT *y) is a routine to transform the 00231 * coordinate longitudes and latitudes to a plot coordinate system. By 00232 * using this transform, we can change from a longitude, latitude 00233 * coordinate to a polar stereographic project, for example. Initially, 00234 * x[0]..x[n-1] are the longitudes and y[0]..y[n-1] are the corresponding 00235 * latitudes. After the call to mapform(), x[] and y[] should be replaced 00236 * by the corresponding plot coordinates. If no transform is desired, 00237 * mapform can be replaced by NULL. 00238 * 00239 * dlat, dlong are the interval in degrees that the latitude and longitude 00240 * lines are to be plotted. 00241 * 00242 * minlong, maxlong are the values of the longitude on the left and right 00243 * side of the plot, respectively. The value of minlong must be less than 00244 * the values of maxlong, and the values of maxlong-minlong must be less 00245 * or equal to 360. 00246 * 00247 * minlat, maxlat are the minimum and maximum latitudes to be plotted on 00248 * the background. One can always use -90.0 and 90.0 as the boundary 00249 * outside the plot window will be automatically eliminated. However, the 00250 * program will be faster if one can reduce the size of the background 00251 * plotted. 00252 \*----------------------------------------------------------------------*/ 00253 00254 #define NSEG 100 00255 00256 void 00257 plmeridians( void (*mapform)(PLINT, PLFLT *, PLFLT *), 00258 PLFLT dlong, PLFLT dlat, 00259 PLFLT minlong, PLFLT maxlong, PLFLT minlat, PLFLT maxlat ) 00260 { 00261 PLFLT yy, xx, temp, x[2], y[2], dx, dy; 00262 00263 if (minlong > maxlong) { 00264 temp = minlong; 00265 minlong = maxlong; 00266 maxlong = temp; 00267 } 00268 if (minlat > maxlat) { 00269 temp = minlat; 00270 minlat = maxlat; 00271 maxlat = temp; 00272 } 00273 dx = (maxlong - minlong) / NSEG; 00274 dy = (maxlat - minlat) / NSEG; 00275 00276 /* latitudes */ 00277 00278 for (yy = dlat * ceil(minlat/dlat); yy <= maxlat; yy += dlat) { 00279 if (mapform == NULL) { 00280 y[0] = y[1] = yy; 00281 x[0] = minlong; 00282 x[1] = maxlong; 00283 plline(2,x,y); 00284 } 00285 else { 00286 for (xx = minlong; xx < maxlong; xx += dx) { 00287 y[0] = y[1] = yy; 00288 x[0] = xx; 00289 x[1] = xx + dx; 00290 (*mapform)(2,x,y); 00291 plline(2,x,y); 00292 } 00293 } 00294 } 00295 00296 /* longitudes */ 00297 00298 for (xx = dlong * ceil(minlong/dlong); xx <= maxlong; xx += dlong) { 00299 if (mapform == NULL) { 00300 x[0] = x[1] = xx; 00301 y[0] = minlat; 00302 y[1] = maxlat; 00303 plline(2,x,y); 00304 } 00305 else { 00306 for (yy = minlat; yy < maxlat; yy += dy) { 00307 x[0] = x[1] = xx; 00308 y[0] = yy; 00309 y[1] = yy + dy; 00310 (*mapform)(2,x,y); 00311 plline(2,x,y); 00312 } 00313 } 00314 } 00315 }
1.6.1