00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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,
00095 int type,
00096 int index,
00097 int size,
00098 struct Cell_head *region,
00099 SITE_XYZ *xyz
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
00109 if (fdsite == NULL || feof(fdsite)) {
00110 return EOF;
00111 }
00112
00113
00114 fdsave = ftell(fdsite);
00115 rewind(fdsite);
00116
00117
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
00125 fseek(fdsite, fdsave, SEEK_SET);
00126
00127
00128 index -= 1;
00129
00130 switch (type) {
00131 case SITE_COL_DIM:
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:
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:
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:
00156 break;
00157 default:
00158
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
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
00176 if(region && !G_site_in_region(s, region)) {
00177 i--;
00178 continue;
00179 }
00180
00181
00182 switch(type) {
00183 case SITE_COL_DIM:
00184 xyz[i].z = s->dim[index]; break;
00185 case SITE_COL_DBL:
00186 xyz[i].z = s->dbl_att[index]; break;
00187 case SITE_COL_STR:
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:
00195 break;
00196 default:
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:
00210 break;
00211 }
00212
00213 }
00214
00215 G_site_free_struct(s);
00216
00217 return i;
00218 }
00219
00220