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 <grass/gis.h>
00038 #include <grass/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;
00207   long size;
00208   int n_cats, do_cats;
00209   int type;
00210   char rhead, nc;
00211   short field;
00212 
00213   G_debug (3, "Vect__Read_line_nat: offset = %ld", offset);
00214  
00215   Map->head.last_offset = offset;
00216   
00217   /* reads must set in_head, but writes use default */
00218   dig_set_cur_port (&(Map->head.port));
00219 
00220   dig_fseek ( &(Map->dig_fp), offset, 0);
00221 
00222   if (0 >= dig__fread_port_C (&rhead, 1, &(Map->dig_fp) ))
00223       return (-2);
00224 
00225   if ( !(rhead & 0x01) ) /* dead line */
00226       dead = 1;
00227   
00228   if ( rhead & 0x02 ) /* categories exists */
00229       do_cats = 1;    /* do not return here let file offset moves forward to next */
00230   else                /* line */
00231       do_cats = 0;
00232   
00233   rhead >>= 2;
00234   type = dig_type_from_store ( (int) rhead );  
00235  
00236   G_debug (3, "    type = %d, do_cats = %d dead = %d", type, do_cats, dead);
00237           
00238   if ( c != NULL )
00239       c->n_cats = 0;
00240   
00241   if ( do_cats ) { 
00242       if ( Map->head.Version_Minor == 1 ) { /* coor format 5.1 */
00243           if (0 >= dig__fread_port_I ( &n_cats, 1, &(Map->dig_fp) )) return (-2);
00244       } else { /* coor format 5.0 */
00245           if (0 >= dig__fread_port_C (&nc, 1, &(Map->dig_fp) )) return (-2);
00246           n_cats = (int) nc;
00247       }
00248       G_debug (3, "    n_cats = %d", n_cats);
00249       
00250       if ( c != NULL )
00251         {         
00252           c->n_cats = n_cats;
00253           if (n_cats > 0)
00254           {
00255             if (0 > dig_alloc_cats (c, (int) n_cats + 1))
00256               return (-1);
00257             
00258             if ( Map->head.Version_Minor == 1 ) { /* coor format 5.1 */
00259                 if (0 >= dig__fread_port_I (c->field, n_cats, &(Map->dig_fp) )) return (-2);
00260             } else { /* coor format 5.0 */
00261                 for (i = 0; i < n_cats; i++) { 
00262                     if (0 >= dig__fread_port_S (&field, 1, &(Map->dig_fp) )) return (-2);
00263                     c->field[i] = (int) field; 
00264                 }
00265             }
00266             if (0 >= dig__fread_port_I (c->cat, n_cats, &(Map->dig_fp) )) return (-2);
00267             
00268           }
00269         }
00270       else
00271         {
00272           if ( Map->head.Version_Minor == 1 ) { /* coor format 5.1 */
00273               size = ( 2 * PORT_INT ) * n_cats;
00274           } else { /* coor format 5.0 */
00275               size = ( PORT_SHORT + PORT_INT ) * n_cats;
00276           }
00277               
00278           dig_fseek ( &(Map->dig_fp), size, SEEK_CUR);
00279         }
00280   }
00281   
00282   if ( type & GV_POINTS ) {
00283       n_points = 1;
00284   } else {
00285       if (0 >= dig__fread_port_I (&n_points, 1, &(Map->dig_fp) )) return (-2);
00286   }
00287 
00288 #ifdef GDEBUG
00289   G_debug (3, "    n_points = %d", n_points);
00290 #endif
00291 
00292   if ( p != NULL ) {      
00293       if (0 > dig_alloc_points (p, n_points + 1))
00294           return (-1);
00295 
00296       p->n_points = n_points;
00297       if (0 >= dig__fread_port_D (p->x, n_points, &(Map->dig_fp) )) return (-2);
00298       if (0 >= dig__fread_port_D (p->y, n_points, &(Map->dig_fp) )) return (-2);
00299 
00300       if (Map->head.with_z) {
00301           if (0 >= dig__fread_port_D (p->z, n_points, &(Map->dig_fp) )) return (-2);
00302       } else {
00303           for ( i = 0; i <  n_points; i++ )
00304               p->z[i] = 0.0;
00305       }
00306   } else {
00307       if (Map->head.with_z) 
00308           size = n_points * 3 * PORT_DOUBLE;
00309       else 
00310           size = n_points * 2 * PORT_DOUBLE;
00311       
00312       dig_fseek ( &(Map->dig_fp), size, SEEK_CUR);
00313   }
00314   
00315   G_debug (3, "    off = %ld", dig_ftell( &(Map->dig_fp) ));
00316   
00317   if ( dead ) return 0;
00318   
00319   return (type);
00320 }
00321 

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5