wind_overlap.c

Go to the documentation of this file.
00001 #include <grass/gis.h>
00002 
00003 /*
00004  * given a map window, and a box of N,S,E,W
00005  * does the box overlap the map window?
00006  * Returns 1 yes, 0 no.
00007  *
00008  * Note: knows about global wrap-around for lat-long
00009  */
00010 
00011 int G_window_overlap ( struct Cell_head *window,
00012     double N,double S,double E,double W)
00013 {
00014     if (window->north <= S) return 0;
00015     if (window->south >= N) return 0;
00016 
00017     if (window->proj == PROJECTION_LL)
00018     {
00019         while (E < window->west)
00020         {
00021             E += 360.0;
00022             W += 360.0;
00023         }
00024         while (W > window->east)
00025         {
00026             E -= 360.0;
00027             W -= 360.0;
00028         }
00029     }
00030 
00031     if (window->east <= W) return 0;
00032     if (window->west >= E) return 0;
00033 
00034     return 1;
00035 }
00036 
00037 /*
00038  * This version returns the percentage (from 0 to 1) of the box 
00039  * contained in the window. This feature can be used during vector
00040  * plotting to decide if it is more efficient to do a level-one
00041  * read of the whole vector file, or to pay the price of a
00042  * level-two startup so only those arcs that enter the window are
00043  * actually read.
00044  *
00045  */
00046 
00047 double G_window_percentage_overlap ( struct Cell_head *window,
00048     double N,double S,double E,double W)
00049 {
00050     double V,H;
00051     double n,s,e,w;
00052     double shift;
00053 
00054 /* vertical height of the box that overlaps the window */
00055     if ((n = window->north) > N) n = N;
00056     if ((s = window->south) < S) s = S;
00057     V = n - s;
00058     if(V <= 0.0) return 0.0 ;
00059 
00060 /* global wrap-around, part 1 */
00061     if (window->proj == PROJECTION_LL)
00062     {
00063         shift = 0.0;
00064         while (E+shift > window->east)
00065             shift -= 360.0;
00066         while (E+shift < window->west)
00067             shift += 360.0;
00068         E += shift;
00069         W += shift;
00070     }
00071 
00072 /* horizontal width of the box that overlaps the window */
00073     if ((e = window->east) > E) e = E;
00074     if ((w = window->west) < W) w = W;
00075     H = e - w;
00076     if(H <= 0.0) return 0.0 ;
00077 
00078 /* global wrap-around, part 2 */
00079     if (window->proj == PROJECTION_LL)
00080     {
00081         shift = 0.0;
00082         while (W+shift < window->west)
00083             shift += 360.0;
00084         while (W+shift > window->east)
00085             shift -= 360.0;
00086         if (shift)
00087         {
00088             E += shift;
00089             W += shift;
00090             if ((e = window->east) > E) e = E;
00091             if ((w = window->west) < W) w = W;
00092             H += e - w;
00093         }
00094     }
00095 
00096     return (H*V)/((N-S)*(E-W));
00097 }

Generated on Fri Nov 21 11:02:18 2008 for GRASS by  doxygen 1.5.1