readsites.c

Go to the documentation of this file.
00001 /*
00002  * int G_readsites 
00003  *     (FILE *fdsite, int all, int verbose, int dec_field,
00004  *      struct Cell_head *window, Z **xyz)
00005  *   reads in a sites list into site struct
00006  *   returning the number of sites found
00007  *   select decimal field (table column) by dec_field parameter
00008  *   closes fdsite on exit
00009  *
00010  * Markus Neteler
00011  * neteler@geog.uni-hannover.de
00012  *
00013  * $Id: readsites.c,v 2.0 2004/11/09 12:21:33 bernhard Exp $
00014  *
00015  * based on support.c by Job Spijker (v.bubble)
00016  *
00017  * TODO:
00018  *   - no string support yet
00019  *   - problems, if double entries missing in second row
00020  */
00021 
00022 
00023 #include <stdlib.h>
00024 #include "gis.h"
00025 #include "glocale.h"
00026 #include "readsites.h"
00027 
00028 int G_readsites (FILE *fdsite, int all, int verbose, int field,
00029                  struct Cell_head *window, Z **xyz)
00030 
00031 /* Reads a sites list into {\tt xyz}, returning the number of sites found.  */
00032 {
00033   int i, strs, dims,map_type,dbls,allocated=1000;
00034   Site *s;
00035 
00036   G_sleep_on_error (0);
00037 
00038   field -= 1;  /* field number -> array index */
00039 
00040   if (verbose)
00041     fprintf (stderr, _("Reading sites list ...                  "));
00042 
00043   /* check structure from first row in sites list */
00044   if (G_site_describe (fdsite, &dims, &map_type, &strs, &dbls)!=0)
00045     G_fatal_error(_("failed to guess format"));
00046   s = G_site_new_struct (map_type, dims, strs, dbls);
00047 
00048   if(field >= dbls){
00049       G_fatal_error(_("decimal field %i not present in sites file"), field + 1);
00050   }
00051 
00052   if (dbls==0)
00053   {
00054     fprintf(stderr,"\n");
00055     G_warning(_("I'm finding records that do not have a floating point attributes (fields prefixed with '%')."));
00056   }
00057 
00058   /* allocate chunk of memory */
00059   (*xyz) = (Z *) G_malloc (allocated * sizeof (Z));
00060   if ((*xyz)==NULL) G_fatal_error(_("cannot allocate memory"));
00061 
00062   i = 0;
00063   while (G_site_get (fdsite, s) == 0) 
00064   {
00065     if (i == allocated)
00066     {
00067       allocated+=1000;
00068       (*xyz) = (Z *) G_realloc ((*xyz), allocated * sizeof (Z));
00069       if ((*xyz)==NULL) G_fatal_error(_("cannot allocate memory"));
00070     }
00071     if (all || (s->east >= window->west && s->east <= window->east &&
00072                 s->north <= window->north && s->north >= window->south))
00073     {
00074       (*xyz)[i].z=s->dbl_att[field];
00075       (*xyz)[i].x=s->east;
00076       (*xyz)[i++].y=s->north;
00077     }
00078   }
00079 
00080   fclose (fdsite);
00081   G_sleep_on_error (1);
00082   if (verbose)
00083     G_percent (1, 1, 1);
00084   return i;
00085 }

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