4 int read_item(DGT* dgt)
5 /* read next item of dgt file opened for reading */
11 if(fread(dgt->buffer,1,14,(FILE *)dgt->F)!=14) return 1;
13 if(fread(&tmp,1,14,(FILE *)(dgt->F))!=14) return 1;
14 dgt->buffer->ID=swaplong(tmp.ID);
15 swab(&(tmp.x1),&(dgt->buffer->x1),10);
17 if (dgt->buffer->npoints)
18 { if(dgt->buffer->npoints>MAX_LINE_LEN) {to_skip=(dgt->buffer->npoints-MAX_LINE_LEN)*4;
19 dgt->buffer->npoints=MAX_LINE_LEN;}
21 if (fread(&(dgt->buffer->s),4,dgt->buffer->npoints,
22 (FILE *)dgt->F)!=dgt->buffer->npoints)
23 { map_error = ME_READ_ERROR; return 1;}
25 if(fread(&(tmp.s),4,dgt->buffer->npoints,
26 (FILE *)dgt->F)!=dgt->buffer->npoints)
27 {map_error=ME_READ_ERROR;return 1;}
28 swab(&(tmp.s),&(dgt->buffer->s),dgt->buffer->npoints*4);
30 if (to_skip) fseek((FILE*)dgt->F,to_skip,SEEK_CUR);
33 return dgt->eof=(dgt->buffer->ID==0&&dgt->buffer->npoints==0);
36 void go_first(DGT *dgt)
38 fseek((FILE *)dgt->F,128L,SEEK_SET);
40 dgt->eof=dgt->next_item(dgt);
43 DGT* open_dgt(char *filename)
44 /*opens existing file, given by name*/
46 if (!(f=fopen(filename,"r")))
50 typedef void (*close_func)(void *f);
51 DGT* fopen_dgt(FILE *f)
52 /* open existing file given by file variable */
55 if(!(tmp=malloc(sizeof(DGT)))) {map_error=ME_OUT_OF_MEMORY;return NULL;}
56 if(fread(&h,1,sizeof(h),f)!=sizeof(h)) {map_error=ME_READ_ERROR;free(tmp);return NULL;}
57 if(!(tmp->buffer=malloc(sizeof(DGT_ITEM)))) {map_error=ME_OUT_OF_MEMORY;free(tmp);return NULL;}
58 tmp->XLeft=swapdouble(h.xleft);
59 tmp->XRight=swapdouble(h.xright);
60 tmp->YTop=swapdouble(h.ytop);
61 tmp->YBottom=swapdouble(h.ybottom);
62 tmp->projection=h.coord_sys;
65 tmp->next_item=read_item;
68 tmp->dispose=(close_func)fclose;
73 void close_dgt(DGT* dgt)
74 /* Destroys DGT object */
80 DGT_ITEM* dgt_get(DGT* dgt)
81 /* allocates current item in memory and returns pointer to it*/
83 if (!(dgt->mode&MAP_INPUT)) {map_error=ME_INVALID_MODE;return NULL;}
84 if (!dgt->eof) {item= alloc_item(dgt->buffer);dgt->next_item(dgt);return item;}
87 int dgt_x(DGT* dgt,double x)
89 return ((x-dgt->XLeft)/(dgt->XRight-dgt->XLeft)*65535)-32767;
91 int dgt_y(DGT* dgt,double y)
93 return ((y-dgt->YBottom)/(dgt->YTop-dgt->YBottom)*65535)-32767;
95 double real_x(DGT* dgt,int x)
96 { return (x+32767)/65535.0*(dgt->XRight-dgt->XLeft)+dgt->XLeft;
98 double real_y(DGT* dgt,int y)
100 return (y+32767)/65535.0*(dgt->YTop-dgt->YBottom)+dgt->YBottom;
103 DGT_ITEM* alloc_item(DGT_ITEM *item)
105 new_item=malloc(14+item->npoints*4);
106 return memcpy(new_item,item,14+item->npoints*4);
108 void reset_dgt(DGT* dgt)
109 /* rewinds dgt file for first item, reopens for reading,if nessecary */
111 dgt->eof=dgt->next_item(dgt);