GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <sys/types.h> 00004 #include <unistd.h> 00005 #include <rpc/types.h> 00006 #include <rpc/xdr.h> 00007 #include <grass/gis.h> 00008 #include "G3d_intern.h" 00009 00010 /*---------------------------------------------------------------------------*/ 00011 00012 00026 int G3d_writeCats(const char *name, struct Categories *cats) 00027 /* adapted from G_write_cats */ 00028 { 00029 FILE *fd; 00030 int i; 00031 const char *descr; 00032 DCELL val1, val2; 00033 char str1[100], str2[100], buf[200], buf2[200], xname[GNAME_MAX], 00034 xmapset[GMAPSET_MAX]; 00035 00036 if (G__name_is_fully_qualified(name, xname, xmapset)) { 00037 sprintf(buf, "%s/%s", G3D_DIRECTORY, xname); 00038 sprintf(buf2, "%s@%s", G3D_CATS_ELEMENT, xmapset); /* == cats@mapset */ 00039 } 00040 else { 00041 sprintf(buf, "%s/%s", G3D_DIRECTORY, name); 00042 sprintf(buf2, "%s", G3D_CATS_ELEMENT); 00043 } 00044 00045 if (!(fd = G_fopen_new(buf, buf2))) 00046 return -1; 00047 00048 /* write # cats - note # indicate 3.0 or later */ 00049 fprintf(fd, "# %ld categories\n", (long)cats->num); 00050 00051 /* title */ 00052 fprintf(fd, "%s\n", cats->title != NULL ? cats->title : ""); 00053 00054 /* write format and coefficients */ 00055 fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : ""); 00056 fprintf(fd, "%.2f %.2f %.2f %.2f\n", 00057 cats->m1, cats->a1, cats->m2, cats->a2); 00058 00059 /* write the cat numbers:label */ 00060 for (i = 0; i < G_quant_nof_rules(&cats->q); i++) { 00061 descr = G_get_ith_d_raster_cat(cats, i, &val1, &val2); 00062 if ((cats->fmt && cats->fmt[0]) || (descr && descr[0])) { 00063 if (val1 == val2) { 00064 sprintf(str1, "%.10f", val1); 00065 G_trim_decimal(str1); 00066 fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : ""); 00067 } 00068 else { 00069 sprintf(str1, "%.10f", val1); 00070 G_trim_decimal(str1); 00071 sprintf(str2, "%.10f", val2); 00072 G_trim_decimal(str2); 00073 fprintf(fd, "%s:%s:%s\n", str1, str2, 00074 descr != NULL ? descr : ""); 00075 } 00076 } 00077 } 00078 fclose(fd); 00079 return 1; 00080 } 00081 00082 /*---------------------------------------------------------------------------*/ 00083 00084 static int 00085 read_cats(const char *name, const char *mapset, struct Categories *pcats) 00086 /* adapted from G__read_cats */ 00087 { 00088 FILE *fd; 00089 char buff[1024], buf2[200], xname[512], xmapset[512]; 00090 CELL cat; 00091 DCELL val1, val2; 00092 int old; 00093 long num = -1; 00094 00095 if (G__name_is_fully_qualified(name, xname, xmapset)) { 00096 sprintf(buff, "%s/%s", G3D_DIRECTORY, xname); 00097 sprintf(buf2, "%s@%s", G3D_CATS_ELEMENT, xmapset); /* == cats@mapset */ 00098 } 00099 else { 00100 sprintf(buff, "%s/%s", G3D_DIRECTORY, name); 00101 sprintf(buf2, "%s", G3D_CATS_ELEMENT); 00102 } 00103 00104 if (!(fd = G_fopen_old(buff, buf2, mapset))) 00105 return -2; 00106 00107 /* Read the number of categories */ 00108 if (G_getl(buff, sizeof(buff), fd) == 0) 00109 goto error; 00110 00111 if (sscanf(buff, "# %ld", &num) == 1) 00112 old = 0; 00113 else if (sscanf(buff, "%ld", &num) == 1) 00114 old = 1; 00115 00116 /* Read the title for the file */ 00117 if (G_getl(buff, sizeof(buff), fd) == 0) 00118 goto error; 00119 G_strip(buff); 00120 00121 G_init_raster_cats(buff, pcats); 00122 if (num >= 0) 00123 pcats->num = num; 00124 00125 if (!old) { 00126 char fmt[256]; 00127 float m1, a1, m2, a2; 00128 00129 if (G_getl(fmt, sizeof(fmt), fd) == 0) 00130 goto error; 00131 /* next line contains equation coefficients */ 00132 if (G_getl(buff, sizeof(buff), fd) == 0) 00133 goto error; 00134 if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4) 00135 goto error; 00136 G_set_raster_cats_fmt(fmt, m1, a1, m2, a2, pcats); 00137 } 00138 00139 /* Read all category names */ 00140 for (cat = 0;; cat++) { 00141 char label[1024]; 00142 00143 if (G_getl(buff, sizeof(buff), fd) == 0) 00144 break; 00145 00146 if (old) 00147 G_set_cat(cat, buff, pcats); 00148 else { 00149 *label = 0; 00150 if (sscanf(buff, "%1s", label) != 1) 00151 continue; 00152 if (*label == '#') 00153 continue; 00154 *label = 0; 00155 00156 /* try to read a range of data */ 00157 if (sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3) 00158 G_set_raster_cat(&val1, &val2, label, pcats, DCELL_TYPE); 00159 else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1) 00160 G_set_raster_cat(&cat, &cat, label, pcats, CELL_TYPE); 00161 else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1) 00162 G_set_raster_cat(&val1, &val1, label, pcats, DCELL_TYPE); 00163 else 00164 goto error; 00165 } 00166 } 00167 00168 fclose(fd); 00169 return 0; 00170 00171 error: 00172 fclose(fd); 00173 return -1; 00174 } 00175 00176 /*---------------------------------------------------------------------------*/ 00177 00178 00192 int 00193 G3d_readCats(const char *name, const char *mapset, struct Categories *pcats) 00194 /* adapted from G_read_cats */ 00195 { 00196 const char *type; 00197 00198 switch (read_cats(name, mapset, pcats)) { 00199 case -2: 00200 type = "missing"; 00201 break; 00202 case -1: 00203 type = "invalid"; 00204 break; 00205 default: 00206 return 0; 00207 } 00208 00209 G_warning("category support for [%s] in mapset [%s] %s", 00210 name, mapset, type); 00211 return -1; 00212 }