GRASS Programmer's Manual  6.4.2(2012)
Gp3.c
Go to the documentation of this file.
00001 
00019 #include <stdlib.h>
00020 
00021 #include <grass/gis.h>
00022 #include <grass/site.h>
00023 #include <grass/Vect.h>
00024 #include <grass/glocale.h>
00025 #include <grass/gstypes.h>
00026 
00041 int Gp_set_color(const char *grassname, geopoint * gp)
00042 {
00043     const char *col_map;
00044     struct Colors sc;
00045     CELL cat;
00046     geopoint *tp;
00047     int r, g, b, color;
00048 
00049     /* TODO: handle error messages */
00050 
00051     if (grassname) {
00052         col_map = G_find_cell2(grassname, "");
00053         if (!col_map) {
00054             G_warning(_("Raster map <%s> not found"), grassname);
00055             return 0;
00056         }
00057 
00058         G_read_colors(grassname, col_map, &sc);
00059 
00060         for (tp = gp; tp; tp = tp->next) {
00061             cat = (int)tp->fattr;
00062             color = NULL_COLOR;
00063 
00064             if (G_get_color(cat, &r, &g, &b, &sc)) {
00065                 color = (r & 0xff) | ((g & 0xff) << 8) | ((b & 0xff) << 16);
00066             }
00067 
00068             tp->iattr = color;
00069         }
00070 
00071         return (1);
00072     }
00073 
00074     return (0);
00075 }
00076 
00090 geopoint *Gp_load_sites(const char *grassname, int *nsites, int *has_z,
00091                         int *has_att)
00092 {
00093     struct Map_info map;
00094     static struct line_pnts *Points = NULL;
00095     static struct line_cats *Cats = NULL;
00096     geopoint *top, *gpt, *prev;
00097     int np, ltype, eof;
00098     struct Cell_head wind;
00099     RASTER_MAP_TYPE rtype;
00100     int ndim;
00101     const char *mapset;
00102 
00103     np = 0;
00104     eof = 0;
00105     *has_z = *has_att = 0;
00106 
00107     mapset = G_find_vector2(grassname, "");
00108     if (!mapset) {
00109         G_warning(_("Vector map <%s> not found"), grassname);
00110         return NULL;
00111     }
00112 
00113     Vect_set_open_level(2);
00114     if (Vect_open_old(&map, grassname, "") == -1) {
00115         G_fatal_error(_("Unable to open vector map <%s>"),
00116                       G_fully_qualified_name(grassname, mapset));
00117     }
00118 
00119     Points = Vect_new_line_struct();
00120     Cats = Vect_new_cats_struct();
00121 
00122     top = gpt = (geopoint *) G_malloc(sizeof(geopoint));
00123     if (!top) {
00124         return (NULL);
00125     }
00126 
00127     G_get_set_window(&wind);
00128 
00129     /* get ndim */
00130     ndim = 2;
00131     if (Vect_is_3d(&map)) {
00132         ndim = 3;
00133     }
00134 
00135     /* set rtype */
00136     rtype = CELL_TYPE;
00137 
00138     while (eof == 0) {
00139         ltype = Vect_read_next_line(&map, Points, Cats);
00140         switch (ltype) {
00141         case -1:
00142             {
00143                 G_warning(_("Unable to read vector map <%s>"),
00144                           G_fully_qualified_name(grassname, mapset));
00145                 return (NULL);
00146             }
00147         case -2:                /* EOF */
00148             {
00149                 eof = 1;
00150                 continue;
00151             }
00152         }
00153         if ((ltype & GV_POINTS)) {
00154             np++;
00155             gpt->p3[X] = Points->x[0];
00156             gpt->p3[Y] = Points->y[0];
00157 
00158             if (ndim > 2) {
00159                 *has_z = 1;
00160                 gpt->dims = 3;
00161                 gpt->p3[Z] = Points->z[0];
00162             }
00163             else {
00164                 gpt->dims = 2;
00165                 *has_z = 0;
00166             }
00167 
00168             if (Cats->n_cats > 0) {
00169                 *has_att = 1;
00170                 gpt->fattr = Cats->field[0];    /* Is this correct? */
00171                 /* gpt->cat = ; ??** */
00172                 gpt->highlight_color = gpt->highlight_size =
00173                     gpt->highlight_marker = FALSE;
00174             }
00175             else {
00176                 gpt->fattr = 0;
00177                 *has_att = 0;
00178             }
00179 
00180             gpt->iattr = gpt->fattr;
00181             gpt->cattr = NULL;
00182 
00183             G_debug(3, "loading vector point %d %f %f -- %d",
00184                     np, Points->x[0], Points->y[0], Cats->n_cats);
00185 
00186             gpt->next = (geopoint *) G_malloc(sizeof(geopoint));        /* G_fatal_error */
00187             if (!gpt->next) {
00188                 return (NULL);
00189             }
00190 
00191             prev = gpt;
00192             gpt = gpt->next;
00193         }
00194 
00195     }
00196     if (np > 0) {
00197         prev->next = NULL;
00198         G_free(gpt);
00199     }
00200 
00201     Vect_close(&map);
00202 
00203     if (!np) {
00204         G_warning(_("No points from vector map <%s> fall within current region"),
00205                   G_fully_qualified_name(grassname, mapset));
00206         return (NULL);
00207     }
00208     else {
00209         G_message(_("Vector map <%s> loaded (%d points)"),
00210                   G_fully_qualified_name(grassname, mapset), np);
00211     }
00212 
00213     *nsites = np;
00214 
00215     return (top);
00216 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines