|
GRASS Programmer's Manual
6.5.svn(2012)-r51648
|
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 Vect_set_constraint_region(&map, wind.north, wind.south, wind.east, 00129 wind.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX); 00130 00131 /* get ndim */ 00132 ndim = 2; 00133 if (Vect_is_3d(&map)) { 00134 ndim = 3; 00135 } 00136 00137 /* set rtype */ 00138 rtype = CELL_TYPE; 00139 00140 while (eof == 0) { 00141 ltype = Vect_read_next_line(&map, Points, Cats); 00142 switch (ltype) { 00143 case -1: 00144 { 00145 G_warning(_("Unable to read vector map <%s>"), 00146 G_fully_qualified_name(grassname, mapset)); 00147 return (NULL); 00148 } 00149 case -2: /* EOF */ 00150 { 00151 eof = 1; 00152 continue; 00153 } 00154 } 00155 if ((ltype & GV_POINTS)) { 00156 np++; 00157 gpt->p3[X] = Points->x[0]; 00158 gpt->p3[Y] = Points->y[0]; 00159 00160 if (ndim > 2) { 00161 *has_z = 1; 00162 gpt->dims = 3; 00163 gpt->p3[Z] = Points->z[0]; 00164 } 00165 else { 00166 gpt->dims = 2; 00167 *has_z = 0; 00168 } 00169 00170 if (Cats->n_cats > 0) { 00171 *has_att = 1; 00172 gpt->fattr = Cats->field[0]; /* Is this correct? */ 00173 /* gpt->cat = ; ??** */ 00174 gpt->highlight_color = gpt->highlight_size = 00175 gpt->highlight_marker = FALSE; 00176 } 00177 else { 00178 gpt->fattr = 0; 00179 *has_att = 0; 00180 } 00181 00182 gpt->iattr = gpt->fattr; 00183 gpt->cattr = NULL; 00184 00185 G_debug(3, "loading vector point %d %f %f -- %d", 00186 np, Points->x[0], Points->y[0], Cats->n_cats); 00187 00188 gpt->next = (geopoint *) G_malloc(sizeof(geopoint)); /* G_fatal_error */ 00189 if (!gpt->next) { 00190 return (NULL); 00191 } 00192 00193 prev = gpt; 00194 gpt = gpt->next; 00195 } 00196 00197 } 00198 if (np > 0) { 00199 prev->next = NULL; 00200 G_free(gpt); 00201 } 00202 00203 Vect_close(&map); 00204 00205 if (!np) { 00206 G_warning(_("No points from vector map <%s> fall within current region"), 00207 G_fully_qualified_name(grassname, mapset)); 00208 return (NULL); 00209 } 00210 else { 00211 G_message(_("Vector map <%s> loaded (%d points)"), 00212 G_fully_qualified_name(grassname, mapset), np); 00213 } 00214 00215 *nsites = np; 00216 00217 return (top); 00218 }