]> www.wagner.pp.ru Git - oss/fgis.git/blob - lib/dgt_output.c
First checked in version
[oss/fgis.git] / lib / dgt_output.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "dgt.h"
5 typedef void (*close_func)(void *f); 
6
7
8 DGT* creat_dgt(char *filename,double x_left,double y_bottom,
9                double x_right,double y_top, unsigned char proj)
10 /* creates file with given limits with given name */           
11 {FILE *f;
12  f=fopen(filename,"w+");
13  if (!f) return NULL;
14  return fcreat_dgt(f,x_left,y_bottom,x_right,y_top,proj);
15 }    
16 int write_header(FILE *f ,double x_left,double y_bottom,
17                double x_right,double y_top,unsigned char proj)
18 { DGTHEADER h;
19   memset(&h,0,sizeof(h));
20   h.xleft=swapdouble(x_left);
21   h.ybottom=swapdouble(y_bottom);
22   h.xright=swapdouble(x_right);
23   h.ytop=swapdouble(y_top);
24   h.coord_sys=proj;
25   return ( fwrite(&h,1,128,f)==128); 
26 }               
27 int write_buf(DGT *dgt)
28 {
29 #ifdef LSB_FIRST
30 chk_mask(dgt->buffer);
31 dgt->modified=0;dgt->item_no++;
32 return fwrite(dgt->buffer,1,14+4*dgt->buffer->npoints,(FILE *)dgt->F)==
33   14+4*dgt->buffer->npoints;
34 #else
35 DGT_ITEM tmp;
36 chk_mask(dgt->buffer);
37 tmp.ID=swaplong(dgt->buffer->ID);
38 swab(&(dgt->buffer->x1),&(tmp.x1),10+4*dgt->buffer->npoints);
39 dgt->modified=0;dgt->item_no++;
40 return fwrite(&tmp,14+4*dgt->buffer->npoints,1,(FILE *)dgt->F)==
41   14+4*dgt->buffer->npoints;
42 #endif
43 }
44 void write_eof(DGT* dgt)
45 {short int eof_marker[7]={0,0,0,0,0,0,0};
46  if (dgt->modified) write_buf(dgt);
47  fwrite(&eof_marker,14,1,(FILE *)dgt->F);
48 }
49 DGT* fcreat_dgt(FILE *f ,double x_left,double y_bottom,
50                double x_right,double y_top,unsigned char proj)
51 /* creates dgt file with given limits in given stream */
52 { DGT* tmp;
53   if (!write_header(f,x_left,y_bottom,x_right,y_top,proj)) { map_error=ME_WRITE_ERROR; return NULL;}
54   if (!(tmp=malloc(sizeof(DGT)))) {map_error=ME_OUT_OF_MEMORY;return NULL;}
55   tmp->XLeft=x_left;
56   tmp->YBottom=y_bottom;
57   tmp->XRight=x_right;
58   tmp->YTop=y_top;
59   tmp->mode=MAP_OUTPUT;
60   tmp->item_no=0;
61   tmp->projection=proj;
62   tmp->F=f;
63   tmp->eof=1;
64   tmp->modified=0;
65   tmp->next_item=write_buf;
66   tmp->rewind=write_eof;
67   tmp->dispose=(close_func) fclose;
68   tmp->buffer=malloc(sizeof(DGT_ITEM));
69   return tmp;
70 }
71 DGT* creat_dgt_as(char *filename,DGT* pattern)
72 {return creat_dgt(filename,pattern->XLeft,pattern->YBottom,pattern->XRight,
73    pattern->YTop,pattern->projection);
74 }
75 DGT* fcreat_dgt_as(FILE *f,DGT *pattern)
76 {return fcreat_dgt(f,pattern->XLeft,pattern->YBottom,pattern->XRight,
77    pattern->YTop,pattern->projection);
78 }
79 void chk_mask(DGT_ITEM* item)
80 /*set correct values of bounding rectangle*/
81 {int i,x1,y1,x2,y2;
82  POINT* p;
83  
84  if (!item->npoints) return;
85  x1=32767;
86  y1=32767;
87  x2=-32767;
88  y2=-32767;
89  for(i=0,p=item->s;i<item->npoints;i++,p++)
90  { if(p->x<x1) x1=p->x;
91    if(p->x>x2) x2=p->x;
92    if(p->y<y1) y1=p->y;
93    if(p->y>y2) y2=p->y;
94  }
95  item->x1=x1;
96  item->y1=y1;
97  item->x2=x2;
98  item->y2=y2;  
99 }
100 void dgt_put(DGT *dgt, DGT_ITEM* item)
101 /* inserts item in writable file*/
102 { if (!dgt->mode&MAP_OUTPUT) {map_error=ME_INVALID_MODE;}
103   if (dgt->modified) dgt->next_item(dgt);
104   memcpy(dgt->buffer,item,14+4*item->npoints);
105   dgt->modified=1;
106   dgt->next_item(dgt);
107 }
108 void item_set_id(DGT_ITEM *item,long int newID)
109 {item->ID=newID?newID:1;
110 }
111 void dgt_set_point(DGT *dgt,int x,int y)
112 { dgt->modified=1;
113   dgt->buffer->npoints=0;
114   if (x!=KEEP_OLD_VALUE) dgt->buffer->x1=x;
115   if (y!=KEEP_OLD_VALUE) dgt->buffer->y1=y;
116 }
117 int dgt_add_node(DGT* dgt,int x,int y)
118 { DGT_ITEM *buf=dgt->buffer;
119   POINT *p=dgt->buffer->s+dgt->buffer->npoints-1;
120   
121   if (buf->npoints==MAX_LINE_LEN) return DGT_LINE_TOO_LONG;
122   if (buf->npoints==0||p->x!=x||p->y!=y)
123   { p++;
124     p->x=x;p->y=y;
125     buf->npoints++;
126     dgt->modified=1;
127     return 0;
128   } else return DGT_ZERO_SEGMENT;
129 }
130 int dgt_ins_node(DGT *dgt,int index,int x,int y)
131 { DGT_ITEM *buf=dgt->buffer;
132   if (index<0) return 3;
133   if (buf->npoints==MAX_LINE_LEN) return DGT_LINE_TOO_LONG;
134   if (index>=buf->npoints)
135    if (index==buf->npoints) return dgt_add_node(dgt,x,y);
136     else return DGT_INVALID_NODE;
137   if ((index==0||buf->s[index-1].x!=x||buf->s[index-1].y!=y)
138       &&(buf->s[index].x!=x||buf->s[index].y!=y))
139    {int i; POINT *s,*s1;
140     s=buf->s+buf->npoints;
141     s1=s-1;
142     for(i=buf->npoints;i>index;i--,*(s--)=*(s1--));
143     s->x=x;
144     s->y=y;
145     buf->npoints++;
146     dgt->modified=1;
147     return 0;
148    }
149   else
150    return DGT_ZERO_SEGMENT;
151
152 }  
153
154 int item_mv_node(DGT_ITEM *item,int index,int x,int y)
155 { POINT *s=item->s+index,*s1;
156   if (index>=item->npoints||index<0) return DGT_INVALID_NODE;
157   
158   if (x==KEEP_OLD_VALUE) x=s->x;
159   if (y==KEEP_OLD_VALUE) y=s->y;
160   if (index>0) {s1=s-1;if (s1->x==x&&s1->y==y) return DGT_ZERO_SEGMENT;}
161   if (index<item->npoints-1)
162    { s1=s+1;if (s1->x==x&&s1->y==y) return DGT_ZERO_SEGMENT;}
163   s->x=x;
164   s->y=y;
165   return 0;
166 }
167 int dgt_rm_node(DGT *dgt,int index)
168 { DGT_ITEM *buf=dgt->buffer;int i;POINT *s,*s1,*s2;
169   if (buf->npoints==2) return DGT_LINE_TOO_SHORT;
170   if (index<0||index>=buf->npoints) return DGT_INVALID_NODE;
171   s=buf->s+index;
172   s1=s+1;s2=s-1;
173   if(!index&&index<buf->npoints-1&&s1->x==s2->x&&s1->y==s2->y) 
174    return DGT_ZERO_SEGMENT;
175   for(i=index+1;i<buf->npoints;i++,*(s++)=*(s1++));
176   dgt->modified=1;
177   return 0;
178 }
179 int dgt_split(DGT *dgt,int index)
180 {DGT_ITEM new_line,*buf=dgt->buffer;
181  POINT *s,*s1;
182  int i;
183  if (index<=0||index>=buf->npoints-1) return DGT_BAD_SPLIT_POINT;
184  s=buf->s+index;
185  s1=new_line.s;
186  for(i=index;i<buf->npoints;i++,*(s1++)=*(s++));
187  new_line.npoints=buf->npoints-index;
188  buf->npoints=index+1;
189  dgt_next(dgt);
190  memcpy(buf,&new_line,14+4*new_line.npoints);
191  dgt->modified=1;
192  return 0;
193 }
194
195 int dgt_cut_segment(DGT *dgt,int index)
196 { POINT *s,*s1;
197   DGT_ITEM new_line,*buf=dgt->buffer;
198   int i;
199  if (index<=1||index>=buf->npoints-1) return DGT_BAD_SPLIT_POINT;
200  s=buf->s+index;
201  s1=new_line.s;
202  for(i=index;i<buf->npoints;i++,*(s1++)=*(s++));
203  new_line.npoints=buf->npoints-index;
204  buf->npoints=index;
205  dgt_next(dgt);
206  memcpy(buf,&new_line,14+4*new_line.npoints);
207  dgt->modified=1;
208  return 0;
209 }
210
211