readsites_xyz.c

Go to the documentation of this file.
00001 /* ========================================================================== *
00002  * G_readsites_xyz(): New implementation of the readsites() library           *
00003  * function to read an array of xyz and cat values.                           *
00004  * ========================================================================== *
00005  * Copyright (c) 2000 Eric G. Miller <egm2@jps.net>                           *
00006  * -------------------------------------------------------------------------- *
00007  * This program is free software; you can redistribute it and/or modify       *
00008  * it under the terms of the GNU General Public License as published by       *
00009  * the Free Software Foundation; either version 2 of the License, or          *
00010  * (at your option) any later version.                                        *
00011  *                                                                            *
00012  * This program is distributed in the hope that it will be useful,            *
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00015  * GNU General Public License for more details.                               *
00016  *                                                                            *
00017  * You should have received a copy of the GNU General Public License          *
00018  * along with this program; if not, write to the Free Software                *
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  *
00020  * -------------------------------------------------------------------------- *
00021  */
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <errno.h>
00027 #include "gis.h"
00028 #include "glocale.h"
00029 #include "site.h"
00030 
00031 
00041 SITE_XYZ *G_alloc_site_xyz(size_t num)
00042 {
00043         SITE_XYZ *xyz;
00044 
00045         xyz = (SITE_XYZ *) G_malloc(sizeof(SITE_XYZ) * num);
00046         if (xyz == NULL)
00047                 return NULL;
00048         memset(xyz, 0, sizeof(SITE_XYZ) * num);
00049         return xyz;
00050 }
00051 
00052 
00062 void G_free_site_xyz(SITE_XYZ *theSites)
00063 {
00064         G_free(theSites);
00065 }
00066 
00067 
00093 int G_readsites_xyz( 
00094         FILE * fdsite,   /* The FILE stream to the sites file               */
00095         int    type,     /* Attribute type: SITE_COL_DIM, etc...            */
00096         int    index,    /* The field index (1 based) for the attribute     */
00097         int    size,     /* Size of the array                               */
00098         struct Cell_head *region,   /* Respect region if not NULL */
00099         SITE_XYZ *xyz    /* The site array of size 'size'                   */
00100         )
00101 {
00102         int i, strs, dims, dbls;
00103         RASTER_MAP_TYPE map_type;
00104         Site *s;
00105         long fdsave;
00106         char *end_ptr;
00107 
00108         /* If fdsite is EOF or NULL return EOF */
00109         if (fdsite == NULL || feof(fdsite)) {
00110                 return EOF;
00111         }
00112 
00113         /* Save file position for seek after G_site_describe */
00114         fdsave = ftell(fdsite);
00115         rewind(fdsite);
00116         
00117         /* note: G_site_describe only reads first record to guess format */
00118         if (G_site_describe (fdsite, &dims, &map_type, &strs, &dbls) !=0) {
00119                 G_fatal_error(_("Unable to guess site format!"));
00120         }
00121         s = G_site_new_struct (map_type, dims, strs, dbls);
00122         dims = dims - 2;
00123 
00124         /* Restore file position */
00125         fseek(fdsite, fdsave, SEEK_SET);
00126         
00127         /* Check 'type' and 'index' */
00128         index -= 1;
00129         
00130         switch (type) {
00131                 case SITE_COL_DIM: /* Use n-dimensions */
00132                         if (dims == 0) {
00133                                 G_fatal_error(_("No n-dims in site_list"));
00134                         }
00135                         else if (index >= dims) {
00136                                 G_fatal_error(_("Dimension index out of range"));
00137                         }
00138                         break;
00139                 case SITE_COL_DBL: /* Use double attribute */
00140                         if (dbls == 0) {
00141                                 G_fatal_error(_("No double attributes in site_list"));
00142                         }
00143                         else if (index >= dbls) {
00144                                 G_fatal_error(_("Double attribute index out of range"));
00145                         }
00146                         break;
00147                 case SITE_COL_STR: /* Use string attribute */
00148                         if (strs == 0) {
00149                                 G_fatal_error(_("No string attributes in site_list"));
00150                         }
00151                         else if (index >= strs) {
00152                                 G_fatal_error(_("String attribute index out of range"));
00153                         }
00154                         break;
00155                 case SITE_COL_NUL: /* Doesn't want a z-dim */
00156                         break;
00157                 default:
00158                         /* Die miserable death due to bad call */
00159                         G_fatal_error(_("Unknown attribute type in call to "
00160                                 "G_readsites_xyz()!\n"));
00161         }
00162 
00163         for (i = 0; i < size; ++i){
00164                 /* Read next site */
00165                 if (G_site_get (fdsite, s) != 0) {
00166                         if (i == 0) {
00167                                 G_site_free_struct(s);
00168                                 return EOF;
00169                         }
00170                         else {
00171                                 G_site_free_struct(s);
00172                                 return i;
00173                         }
00174                 }
00175                 /* Check if in region */
00176                 if(region && !G_site_in_region(s, region)) {
00177                         i--;
00178                         continue;
00179                 }
00180 
00181                 /* Do 'z' based on 'type' and 'index' */
00182                 switch(type) {
00183                         case SITE_COL_DIM: /* Z-dim */
00184                                 xyz[i].z = s->dim[index]; break;
00185                         case SITE_COL_DBL: /* Dbl attribute */
00186                                 xyz[i].z = s->dbl_att[index]; break;
00187                         case SITE_COL_STR: /* String Attribute */
00188                                 errno = 0;
00189                                 xyz[i].z = strtod(s->str_att[index], &end_ptr);
00190                                 if (end_ptr == s->str_att[index] || errno == ERANGE) {
00191                                         G_fatal_error(_("Failed to convert string attribute."));
00192                                 }
00193                                 break;
00194                         case SITE_COL_NUL: /* No z-dim requested */
00195                                 break;
00196                         default: /* Programming error, die miserably */
00197                                 G_fatal_error("G_readsites_xyz(): fatal programmer error!\n");
00198                 }
00199                 xyz[i].x = s->east;
00200                 xyz[i].y = s->north;
00201                 xyz[i].cattype = s->cattype;
00202                 switch(s->cattype) {
00203                         case CELL_TYPE:
00204                                 xyz[i].cat.c = s->ccat; break;
00205                         case FCELL_TYPE:
00206                                 xyz[i].cat.f = s->fcat; break;
00207                         case DCELL_TYPE:
00208                                 xyz[i].cat.d = s->dcat; break;
00209                         default: /* No cat */
00210                                 break;
00211                 }
00212            
00213         }
00214 
00215         G_site_free_struct(s);
00216 
00217         return i;
00218 }
00219 
00220 

Generated on Sat Jul 22 22:06:15 2006 for GRASS by  doxygen 1.4.7