read_nat.c

Go to the documentation of this file.
00001 /*
00002 ****************************************************************************
00003 *
00004 * MODULE:       Vector library 
00005 *               
00006 * AUTHOR(S):    Original author CERL, probably Dave Gerdes or Mike Higgins.
00007 *               Update to GRASS 5.7 Radim Blazek and David D. Gray.
00008 *
00009 * PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
00010 *
00011 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00012 *
00013 *               This program is free software under the GNU General Public
00014 *               License (>=v2). Read the file COPYING that comes with GRASS
00015 *               for details.
00016 *
00017 *****************************************************************************/
00018 /*
00019  *V1_read_next_line (Map, line_p)
00020  *              reads thru digit file looking for next line within window,
00021  *              stores info in struct instead of args.
00022  *      NOTE:  line_p->alloc_points better be set to 0 for the first call.
00023  * 
00024  * returns:  -1 on error
00025  *           -2 EOF
00026  *           line type (positive value) if it found a line
00027 
00028  **
00029  **
00030  **  The action of this routine can be modified by:
00031  **    Vect_read_constraint_region ()
00032  **    Vect_read_constraint_type   ()
00033  **    Vect_remove_constraints     ()
00034  **
00035  */
00036 
00037 #include "gis.h"
00038 #include "Vect.h"
00039 
00040 int Vect__Read_line_nat (struct Map_info *, struct line_pnts *, struct line_cats *, long);
00041 
00042 /*
00043 *  Read line from coor file on given offset.
00044 *
00045 *  Returns  line type
00046 *           -2 End of table (last row)
00047 *           -1 Out of memory
00048 */
00049 int 
00050 V1_read_line_nat (
00051                struct Map_info *Map,
00052                struct line_pnts *Points,
00053                struct line_cats *Cats,
00054                long offset)
00055 {
00056   return Vect__Read_line_nat (Map, Points, Cats, offset);
00057 }
00058 
00059 /*
00060 *  Read next line from coor file.
00061 *
00062 *  Returns  line type
00063 *           -2 End of table (last row)
00064 *           -1 Out of memory
00065 */
00066 int 
00067 V1_read_next_line_nat (
00068                     struct Map_info *Map,
00069                     struct line_pnts *line_p,
00070                     struct line_cats *line_c)
00071 {
00072   int itype;
00073   long offset;
00074   BOUND_BOX lbox, mbox;
00075 
00076   G_debug (3, "V1_read_next_line_nat()" );
00077 
00078   if (Map->Constraint_region_flag)
00079       Vect_get_constraint_box ( Map, &mbox );
00080 
00081   while (1)
00082     {
00083       offset = dig_ftell ( &(Map->dig_fp) );
00084       itype = Vect__Read_line_nat (Map, line_p, line_c, offset);
00085       if (itype < 0)
00086         return (itype);
00087 
00088       if (itype == 0)   /* is it DEAD? */
00089         continue;
00090 
00091       /* Constraint on Type of line 
00092        * Default is all of  Point, Line, Area and whatever else comes along
00093        */
00094       if (Map->Constraint_type_flag)
00095         {
00096           if (!(itype & Map->Constraint_type))
00097             continue;
00098         }
00099 
00100       /* Constraint on specified region */
00101       if (Map->Constraint_region_flag) {
00102           Vect_line_box ( line_p, &lbox );
00103 
00104           if ( !Vect_box_overlap (&lbox, &mbox) )
00105             continue;
00106       }
00107        
00108       return (itype);
00109     }
00110   /* NOTREACHED */
00111 
00112 }                               /*  dig_read_line_struct_in_box()  */
00113 
00114 /*
00115    ** reads any specified line   This is NOT affected by constraints
00116  */
00117 int 
00118 V2_read_line_nat (
00119                struct Map_info *Map,
00120                struct line_pnts *line_p,
00121                struct line_cats *line_c,
00122                int line)
00123 {
00124     P_LINE *Line;
00125 
00126     G_debug (3, "V2_read_line_nat(): line = %d", line); 
00127     
00128     
00129     Line = Map->plus.Line[line]; 
00130 
00131     if ( Line == NULL )
00132         G_fatal_error ( "Attempt to read dead line %d", line );
00133             
00134     return Vect__Read_line_nat (Map, line_p, line_c, Line->offset);
00135 }
00136 
00137 /* reads next unread line each time called.  use Vect_rewind to reset */
00138 /* returns -2 on end of lines */
00139 
00140 int 
00141 V2_read_next_line_nat (
00142                     struct Map_info *Map,
00143                     struct line_pnts *line_p,
00144                     struct line_cats *line_c)
00145 {
00146   register int line;
00147   register P_LINE *Line;
00148   BOUND_BOX lbox, mbox;
00149 
00150   G_debug (3, "V2_read_next_line_nat()"); 
00151   
00152   if (Map->Constraint_region_flag)
00153       Vect_get_constraint_box ( Map, &mbox );
00154 
00155   while (1)
00156     {
00157       line = Map->next_line;
00158 
00159       if (line > Map->plus.n_lines)
00160         return (-2);
00161 
00162       Line = Map->plus.Line[line];
00163       if ( Line == NULL) {           /* Dead line */
00164           Map->next_line++;
00165           continue;
00166       }
00167 
00168       if ((Map->Constraint_type_flag && !(Line->type & Map->Constraint_type)))
00169         {
00170           Map->next_line++;
00171           continue;
00172         }
00173 
00174       if (Map->Constraint_region_flag) {
00175         Vect_get_line_box ( Map, line, &lbox );   
00176         if ( !Vect_box_overlap (&lbox, &mbox) )
00177           {
00178             Map->next_line++;
00179             continue;
00180           }
00181       }
00182 
00183       return V2_read_line_nat (Map, line_p, line_c, Map->next_line++);
00184     }
00185 
00186   /* NOTREACHED */
00187 }
00188 
00189 
00190 /*  
00191 *  read line from coor file 
00192 *
00193 *  Returns:  line type ( > 0 )
00194 *            0 Dead line
00195 *           -1 Out of memory
00196 *           -2 End of file
00197 */
00198 int
00199 Vect__Read_line_nat (
00200                     struct Map_info *Map,
00201                     struct line_pnts *p,
00202                     struct line_cats *c,
00203                     long offset)
00204 {
00205   int i, dead = 0;   
00206   int n_points, size;
00207   int n_cats, do_cats;
00208   int type;
00209   char rhead, nc;
00210   short field;
00211 
00212   G_debug (3, "Vect__Read_line_nat: offset = %ld", offset);
00213  
00214   Map->head.last_offset = offset;
00215   
00216   /* reads must set in_head, but writes use default */
00217   dig_set_cur_port (&(Map->head.port));
00218 
00219   dig_fseek ( &(Map->dig_fp), offset, 0);
00220 
00221   if (0 >= dig__fread_port_C (&rhead, 1, &(Map->dig_fp) ))
00222       return (-2);
00223 
00224   if ( !(rhead & 0x01) ) /* dead line */
00225       dead = 1;
00226   
00227   if ( rhead & 0x02 ) /* categories exists */
00228       do_cats = 1;    /* do not return here let file offset moves forward to next */
00229   else                /* line */
00230       do_cats = 0;
00231   
00232   rhead >>= 2;
00233   type = dig_type_from_store ( (int) rhead );  
00234  
00235   G_debug (3, "    type = %d, do_cats = %d dead = %d", type, do_cats, dead);
00236           
00237   if ( c != NULL )
00238       c->n_cats = 0;
00239   
00240   if ( do_cats ) { 
00241       if ( Map->head.Version_Minor == 1 ) { /* coor format 5.1 */
00242           if (0 >= dig__fread_port_I ( &n_cats, 1, &(Map->dig_fp) )) return (-2);
00243       } else { /* coor format 5.0 */
00244           if (0 >= dig__fread_port_C (&nc, 1, &(Map->dig_fp) )) return (-2);
00245           n_cats = (int) nc;
00246       }
00247       G_debug (3, "    n_cats = %d", n_cats);
00248       
00249       if ( c != NULL )
00250         {         
00251           c->n_cats = n_cats;
00252           if (n_cats > 0)
00253           {
00254             if (0 > dig_alloc_cats (c, (int) n_cats + 1))
00255               return (-1);
00256             
00257             if ( Map->head.Version_Minor == 1 ) { /* coor format 5.1 */
00258                 if (0 >= dig__fread_port_I (c->field, n_cats, &(Map->dig_fp) )) return (-2);
00259             } else { /* coor format 5.0 */
00260                 for (i = 0; i < n_cats; i++) { 
00261                     if (0 >= dig__fread_port_S (&field, 1, &(Map->dig_fp) )) return (-2);
00262                     c->field[i] = (int) field; 
00263                 }
00264             }
00265             if (0 >= dig__fread_port_I (c->cat, n_cats, &(Map->dig_fp) )) return (-2);
00266             
00267           }
00268         }
00269       else
00270         {
00271           if ( Map->head.Version_Minor == 1 ) { /* coor format 5.1 */
00272               size = ( 2 * PORT_INT ) * n_cats;
00273           } else { /* coor format 5.0 */
00274               size = ( PORT_SHORT + PORT_INT ) * n_cats;
00275           }
00276               
00277           dig_fseek ( &(Map->dig_fp), size, SEEK_CUR);
00278         }
00279   }
00280   
00281   if ( type & GV_POINTS ) {
00282       n_points = 1;
00283   } else {
00284       if (0 >= dig__fread_port_I (&n_points, 1, &(Map->dig_fp) )) return (-2);
00285   }
00286 
00287 #ifdef GDEBUG
00288   G_debug (3, "    n_points = %d", n_points);
00289 #endif
00290 
00291   if ( p != NULL ) {      
00292       if (0 > dig_alloc_points (p, n_points + 1))
00293           return (-1);
00294 
00295       p->n_points = n_points;
00296       if (0 >= dig__fread_port_D (p->x, n_points, &(Map->dig_fp) )) return (-2);
00297       if (0 >= dig__fread_port_D (p->y, n_points, &(Map->dig_fp) )) return (-2);
00298 
00299       if (Map->head.with_z) {
00300           if (0 >= dig__fread_port_D (p->z, n_points, &(Map->dig_fp) )) return (-2);
00301       } else {
00302           for ( i = 0; i <  n_points; i++ )
00303               p->z[i] = 0.0;
00304       }
00305   } else {
00306       if (Map->head.with_z) 
00307           size = n_points * 3 * PORT_DOUBLE;
00308       else 
00309           size = n_points * 2 * PORT_DOUBLE;
00310       
00311       dig_fseek ( &(Map->dig_fp), size, SEEK_CUR);
00312   }
00313   
00314   G_debug (3, "    off = %ld", dig_ftell( &(Map->dig_fp) ));
00315   
00316   if ( dead ) return 0;
00317   
00318   return (type);
00319 }
00320 

Generated on Mon Jan 1 19:49:16 2007 for GRASS by  doxygen 1.5.1