]> www.wagner.pp.ru Git - oss/fgis.git/blob - epu/extents.c
First checked in version
[oss/fgis.git] / epu / extents.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "epp.h"
4 #include "eppl_ut.h"
5 typedef struct Contour {int Class,XMin,YMin,XMax,YMax,Count;} CONTOUR;
6 /* ÏÐÉÓÁÎÉÅ ÏÄÎÏÇÏ ËÏÎÔÕÒÁ */
7 CONTOUR *a; 
8 EPP *epp;
9 EPPHEADER *h;
10 int first_class; /* ÄÌÑ ÐÅÒÅÓÞÅÔÁ */
11
12 int sort_area(const void *p1, const void *p2)
13 /* óÒÁ×ÎÅÎÉÅ ÐÏ ÐÌÏÝÁÄÉ */
14 {
15 int t1,t2;
16 t1=(((CONTOUR *)p1))->Count;
17 t2=(((CONTOUR *)p2))->Count;
18 if (t1>t2) return 1;
19 if (t1==t2) return 0;
20 return -1;
21 }
22
23 int sort_y_top(const void *p1, const void *p2)
24 /* óÒÁ×ÎÅÎÉÅ ÐÏ ×ÅÒÈÎÅÊ ÇÒÁÎÉÃÅ */
25 {
26 int t1, t2;
27 t1=((CONTOUR *)p1)->YMin;
28 t2=((CONTOUR *)p2)->YMin;
29 if (t1>t2) return 1;
30 if (t1==t2) return 0;
31 return -1;
32 }
33
34 int sort_y_bottom(const void *p1, const void *p2)
35 {
36 int t1, t2;
37 t1=((CONTOUR *)p1)->YMax;
38 t2=((CONTOUR *)p2)->YMax;
39 if (t1>t2) return 1;
40 if (t1==t2) return 0;
41 return -1;
42 }
43 int sort_x_right(const void *p1, const void *p2)
44 {
45 int t1, t2;
46 t1=((CONTOUR *)p1)->XMax;
47 t2=((CONTOUR *)p2)->XMax;
48 if (t1>t2) return 1;
49 if (t1==t2) return 0;
50 return -1;
51 }
52 int sort_x_left(const void *p1, const void *p2)
53 {
54 int t1, t2;
55 t1=((CONTOUR *)p1)->XMin;
56 t2=((CONTOUR *)p2)->XMin;
57 if (t1>t2) return 1;
58 if (t1==t2) return 0;
59 return -1;
60 }
61
62 int sort_x(const void *p1, const void *p2)
63 /* óÒÁ×ÎÅÎÉÅ ÐÏ ÓÒÅÄÎÅÍÕ X */
64 {
65 int t1, t2;
66 t1=((CONTOUR *)p1)->XMin+((CONTOUR *)p1)->XMax;
67 t2=((CONTOUR *)p2)->XMin+((CONTOUR *)p2)->XMax;
68 if (t1>t2) return 1;
69 if (t1==t2) return 0;
70 return -1;
71 }
72
73 int sort_y(const void *p1, const void *p2)
74 /* óÒÁ×ÎÅÎÉÅ ÐÏ ÓÒÅÄÎÅÍÕ Y */
75 {
76 int t1, t2;
77 t1=((CONTOUR *)p1)->YMin+((CONTOUR *)p1)->YMax;
78 t2=((CONTOUR *)p2)->YMin+((CONTOUR *)p2)->YMax;
79 if (t1>t2) return 1;
80 if (t1==t2) return 0;
81 return -1;
82 }
83
84 /*int sort_s(const void *p1, const void *p2)*/
85 /* óÒÁ×ÎÅÎÉÅ ÐÏ ÏÔÎÏÛÅÎÉÀ count/(W*H) */
86 /*{
87 }*/
88
89
90
91 int process_cell(int col,int row, int value)
92 {
93 CONTOUR *b=a+value;  
94 if (b->XMin>col) b->XMin=col;
95 if (b->XMax<col) b->XMax=col;
96 if (b->YMin>row) b->YMin=row;
97 if (b->YMax<row) b->YMax=row;
98 b->Count++;
99 return 0;
100 }
101
102 int main(int argc,char **argv)
103 {
104 int (*sort_function)(const void *,const void *)=NULL;
105 /*òÁÚÂÏÒ ÐÁÒÁÍÅÔÒÏ× ËÏÍÁÎÄÎÏÊ ÓÔÒÏËÉ*/
106 struct option longoptions[]={
107 {"help",0,0,1},
108 {"version",0,0,2},
109 {"verbose",0,0,'%'},
110 {"alt-coords",0,0,'a'},
111 {"sort-left",0,0,'l'},
112 {"sort-right",0,0,'r'},
113 {"sort-top",0,0,'t'},
114 {"sort-bottom",0,0,'b'},
115 {"sort-area",0,0,'A'},
116 {"sort-x-center",0,0,'x'},
117 {"sort-y-center",0,0,'y'},
118 {"output-file",1,0,'o'},
119 {"header",0,0,'h'},
120 {NULL,0,0,0}
121 };
122 int c,index,i;
123 int verbose=0;
124 int use_alt=0;
125 int print_header=0;
126 CONTOUR *b;
127 FILE *f=stdout;
128 while((c=getopt_long(argc,argv,"alrtbxyA%ho:",longoptions,&index))!=-1)
129  switch(c)
130   { case 2:show_version("extents","$Revision: 1.1 $");
131     case '%': verbose=1; break; 
132     case 'a': use_alt=1;break;
133     case 'o': if (!(f=fopen(optarg,"w"))) 
134                  {fprintf(stderr,"Cannot open file %s\n",optarg); return 2;}
135               break;
136     case 'l':sort_function=sort_x_left; break;
137     case 'r':sort_function=sort_x_right; break;
138     case 't':sort_function=sort_y_top; break;
139     case 'b':sort_function=sort_y_bottom; break;
140     case 'x':sort_function=sort_x; break;
141     case 'y':sort_function=sort_y; break;
142     case 'A':sort_function=sort_area; break;
143     case 'h':print_header=1;break;
144     case 1:
145     case '?':
146     default: printf("Usage: extents [-%%alrtbxtA] [-o file] file.epp\n");
147              return c!=1;
148    }
149             
150   
151 /*ïÔËÒÙÔØ epp-ÛÎÉË, ÐÒÏÞÉÔÁÔØ ÚÁÇÏÌÏ×ÏË, ÓÏÚÄÁÔØ ÍÁÓÓÉ× CONTOUR ÄÌÉÎÙ 
152 */
153 epp=open_epp(default_ext(argv[optind],".epp"));
154 if(epp==NULL) {fprintf(stderr,"Can not open file %s\n",
155            default_ext(argv[optind],".epp"));
156                return 2;} 
157 a=calloc(sizeof(CONTOUR),epp->max+1);
158 for(i=0,b=a;i<=epp->max;i++,b++)
159 { b->XMin=epp->lc;
160   b->XMax=epp->fc;
161   b->YMin=epp->lr;
162   b->YMax=epp->fr;
163   b->Class=i;
164 }
165 install_progress_indicator(verbose?show_percent:check_int);
166 if (clear_progress(for_each_cell(epp,process_cell))) return 3;
167 if (sort_function) qsort(a,epp->max+1,sizeof(CONTOUR),sort_function);
168 if (print_header) 
169     fprintf(f,use_alt?
170 " Class        X Left       X Right         Y Top      Y Bottom           Area\n":
171 " Class Min Col Max Col Min Row Max Row     Count\n");
172 for(i=0;i<=epp->max;i++)
173   if (a[i].Count) 
174     { if (use_alt) 
175         fprintf(f,"%6d %13g %13g %13g %13g %14g\n",
176                 a[i].Class,alt_x(epp,a[i].XMin),alt_x(epp,a[i].XMax+1),
177                 alt_y(epp,a[i].YMin),alt_y(epp,a[i].YMax+1),
178                 a[i].Count*epp->cell_area);
179       else
180         fprintf(f,"%6d %7d %7d %7d %7d %9d\n",  
181                  a[i].Class,a[i].XMin,a[i].XMax,a[i].YMin,a[i].YMax,a[i].Count);
182      }
183          
184 close_epp(epp);
185 if (f!=stdout) fclose(f);
186 return 0;
187 }