00001
00026 #include <grass/gis.h>
00027 #include <grass/Vect.h>
00028 #include <grass/glocale.h>
00029
00030 static int
00031 Vect__Read_line_nat(struct Map_info *,
00032 struct line_pnts *, struct line_cats *, long);
00033
00047 int
00048 V1_read_line_nat(struct Map_info *Map,
00049 struct line_pnts *Points,
00050 struct line_cats *Cats, long offset)
00051 {
00052 return Vect__Read_line_nat(Map, Points, Cats, offset);
00053 }
00054
00067 int
00068 V1_read_next_line_nat(struct Map_info *Map,
00069 struct line_pnts *line_p, struct line_cats *line_c)
00070 {
00071 int itype;
00072 long offset;
00073 BOUND_BOX lbox, mbox;
00074
00075 G_debug(3, "V1_read_next_line_nat()");
00076
00077 if (Map->Constraint_region_flag)
00078 Vect_get_constraint_box(Map, &mbox);
00079
00080 while (1) {
00081 offset = dig_ftell(&(Map->dig_fp));
00082 itype = Vect__Read_line_nat(Map, line_p, line_c, offset);
00083 if (itype < 0)
00084 return (itype);
00085
00086 if (itype == 0)
00087 continue;
00088
00089
00090
00091
00092 if (Map->Constraint_type_flag) {
00093 if (!(itype & Map->Constraint_type))
00094 continue;
00095 }
00096
00097
00098 if (Map->Constraint_region_flag) {
00099 Vect_line_box(line_p, &lbox);
00100
00101 if (!Vect_box_overlap(&lbox, &mbox))
00102 continue;
00103 }
00104
00105 return (itype);
00106 }
00107
00108 }
00109
00123 int
00124 V2_read_line_nat(struct Map_info *Map,
00125 struct line_pnts *line_p, struct line_cats *line_c, int line)
00126 {
00127 P_LINE *Line;
00128
00129 G_debug(3, "V2_read_line_nat(): line = %d", line);
00130
00131
00132 Line = Map->plus.Line[line];
00133
00134 if (Line == NULL)
00135 G_fatal_error("V2_read_line_nat(): %s %d",
00136 _("Attempt to read dead line"), line);
00137
00138 return Vect__Read_line_nat(Map, line_p, line_c, Line->offset);
00139 }
00140
00153 int
00154 V2_read_next_line_nat(struct Map_info *Map,
00155 struct line_pnts *line_p, struct line_cats *line_c)
00156 {
00157 register int line;
00158 register P_LINE *Line;
00159 BOUND_BOX lbox, mbox;
00160
00161 G_debug(3, "V2_read_next_line_nat()");
00162
00163 if (Map->Constraint_region_flag)
00164 Vect_get_constraint_box(Map, &mbox);
00165
00166 while (1) {
00167 line = Map->next_line;
00168
00169 if (line > Map->plus.n_lines)
00170 return (-2);
00171
00172 Line = Map->plus.Line[line];
00173 if (Line == NULL) {
00174 Map->next_line++;
00175 continue;
00176 }
00177
00178 if ((Map->Constraint_type_flag &&
00179 !(Line->type & Map->Constraint_type))) {
00180 Map->next_line++;
00181 continue;
00182 }
00183
00184 if (Map->Constraint_region_flag) {
00185 Vect_get_line_box(Map, line, &lbox);
00186 if (!Vect_box_overlap(&lbox, &mbox)) {
00187 Map->next_line++;
00188 continue;
00189 }
00190 }
00191
00192 return V2_read_line_nat(Map, line_p, line_c, Map->next_line++);
00193 }
00194
00195 }
00196
00197
00211 int
00212 Vect__Read_line_nat(struct Map_info *Map,
00213 struct line_pnts *p, struct line_cats *c, long offset)
00214 {
00215 int i, dead = 0;
00216 int n_points;
00217 long size;
00218 int n_cats, do_cats;
00219 int type;
00220 char rhead, nc;
00221 short field;
00222
00223 G_debug(3, "Vect__Read_line_nat: offset = %ld", offset);
00224
00225 Map->head.last_offset = offset;
00226
00227
00228 dig_set_cur_port(&(Map->head.port));
00229
00230 dig_fseek(&(Map->dig_fp), offset, 0);
00231
00232 if (0 >= dig__fread_port_C(&rhead, 1, &(Map->dig_fp)))
00233 return (-2);
00234
00235 if (!(rhead & 0x01))
00236 dead = 1;
00237
00238 if (rhead & 0x02)
00239 do_cats = 1;
00240 else
00241 do_cats = 0;
00242
00243 rhead >>= 2;
00244 type = dig_type_from_store((int)rhead);
00245
00246 G_debug(3, " type = %d, do_cats = %d dead = %d", type, do_cats, dead);
00247
00248 if (c != NULL)
00249 c->n_cats = 0;
00250
00251 if (do_cats) {
00252 if (Map->head.Version_Minor == 1) {
00253 if (0 >= dig__fread_port_I(&n_cats, 1, &(Map->dig_fp)))
00254 return (-2);
00255 }
00256 else {
00257 if (0 >= dig__fread_port_C(&nc, 1, &(Map->dig_fp)))
00258 return (-2);
00259 n_cats = (int)nc;
00260 }
00261 G_debug(3, " n_cats = %d", n_cats);
00262
00263 if (c != NULL) {
00264 c->n_cats = n_cats;
00265 if (n_cats > 0) {
00266 if (0 > dig_alloc_cats(c, (int)n_cats + 1))
00267 return (-1);
00268
00269 if (Map->head.Version_Minor == 1) {
00270 if (0 >=
00271 dig__fread_port_I(c->field, n_cats, &(Map->dig_fp)))
00272 return (-2);
00273 }
00274 else {
00275 for (i = 0; i < n_cats; i++) {
00276 if (0 >= dig__fread_port_S(&field, 1, &(Map->dig_fp)))
00277 return (-2);
00278 c->field[i] = (int)field;
00279 }
00280 }
00281 if (0 >= dig__fread_port_I(c->cat, n_cats, &(Map->dig_fp)))
00282 return (-2);
00283
00284 }
00285 }
00286 else {
00287 if (Map->head.Version_Minor == 1) {
00288 size = (2 * PORT_INT) * n_cats;
00289 }
00290 else {
00291 size = (PORT_SHORT + PORT_INT) * n_cats;
00292 }
00293
00294 dig_fseek(&(Map->dig_fp), size, SEEK_CUR);
00295 }
00296 }
00297
00298 if (type & GV_POINTS) {
00299 n_points = 1;
00300 }
00301 else {
00302 if (0 >= dig__fread_port_I(&n_points, 1, &(Map->dig_fp)))
00303 return (-2);
00304 }
00305
00306 G_debug(3, " n_points = %d", n_points);
00307
00308 if (p != NULL) {
00309 if (0 > dig_alloc_points(p, n_points + 1))
00310 return (-1);
00311
00312 p->n_points = n_points;
00313 if (0 >= dig__fread_port_D(p->x, n_points, &(Map->dig_fp)))
00314 return (-2);
00315 if (0 >= dig__fread_port_D(p->y, n_points, &(Map->dig_fp)))
00316 return (-2);
00317
00318 if (Map->head.with_z) {
00319 if (0 >= dig__fread_port_D(p->z, n_points, &(Map->dig_fp)))
00320 return (-2);
00321 }
00322 else {
00323 for (i = 0; i < n_points; i++)
00324 p->z[i] = 0.0;
00325 }
00326 }
00327 else {
00328 if (Map->head.with_z)
00329 size = n_points * 3 * PORT_DOUBLE;
00330 else
00331 size = n_points * 2 * PORT_DOUBLE;
00332
00333 dig_fseek(&(Map->dig_fp), size, SEEK_CUR);
00334 }
00335
00336 G_debug(3, " off = %ld", dig_ftell(&(Map->dig_fp)));
00337
00338 if (dead)
00339 return 0;
00340
00341 return (type);
00342 }