1 | /*!\file Exp2Kmlx
|
---|
2 | * \brief exp to kml conversion routines.
|
---|
3 | */
|
---|
4 |
|
---|
5 | #include "./Exp2Kmlx.h"
|
---|
6 | #include "../../shared/shared.h"
|
---|
7 | #include "../../include/include.h"
|
---|
8 | #include "../../io/io.h"
|
---|
9 | #include "../../toolkits/toolkits.h"
|
---|
10 | #include "../../EnumDefinitions/EnumDefinitions.h"
|
---|
11 | #include "../modules.h"
|
---|
12 |
|
---|
13 | int Exp2Kmlx(char* filexp,char* filkml,
|
---|
14 | int sgn,
|
---|
15 | bool holes){
|
---|
16 |
|
---|
17 | double cm,sp;
|
---|
18 |
|
---|
19 | Xy2lldef(&cm,&sp,sgn);
|
---|
20 |
|
---|
21 | return(Exp2Kmlx(filexp,filkml,
|
---|
22 | sgn,cm,sp,
|
---|
23 | holes));
|
---|
24 | }
|
---|
25 |
|
---|
26 | int Exp2Kmlx(char* filexp,char* filkml,
|
---|
27 | int sgn,double cm,double sp,
|
---|
28 | bool holes){
|
---|
29 |
|
---|
30 | int i,j,iret=0;
|
---|
31 | int lwidth=1;
|
---|
32 | double popac=0.50;
|
---|
33 | int nprof;
|
---|
34 | int *pnvert=NULL;
|
---|
35 | double **pprofx=NULL,**pprofy=NULL;
|
---|
36 | bool *closed=NULL;
|
---|
37 | double *lat=NULL,*lon=NULL;
|
---|
38 |
|
---|
39 | char indent[81]="";
|
---|
40 | KML_File* kfile =NULL;
|
---|
41 | KML_Document* kdoc =NULL;
|
---|
42 | KML_Style* kstyle=NULL;
|
---|
43 | KML_LineStyle* klsty =NULL;
|
---|
44 | KML_PolyStyle* kpsty =NULL;
|
---|
45 | KML_Folder* kfold =NULL;
|
---|
46 | KML_Placemark* kplace=NULL;
|
---|
47 | KML_Polygon* kpoly =NULL;
|
---|
48 | KML_LinearRing* kring =NULL;
|
---|
49 | KML_LineString* kline =NULL;
|
---|
50 |
|
---|
51 | FILE* fid=NULL;
|
---|
52 |
|
---|
53 | clock_t clock0,clock1;
|
---|
54 | time_t time0, time1;
|
---|
55 |
|
---|
56 | clock0=clock();
|
---|
57 | time0 =time(NULL);
|
---|
58 | _printf_(true,"\nExp2Kmlx Module -- %s",ctime(&time0));
|
---|
59 |
|
---|
60 | /* read exp file */
|
---|
61 |
|
---|
62 | if (!DomainOutlineRead(&nprof,&pnvert,&pprofx,&pprofy,&closed,filexp,false))
|
---|
63 | _error_("Error reading exp file.");
|
---|
64 |
|
---|
65 | /* construct kml file */
|
---|
66 |
|
---|
67 | kfile =new KML_File();
|
---|
68 | kfile->AddAttrib("xmlns","http://www.opengis.net/kml/2.2");
|
---|
69 |
|
---|
70 | /* construct kml document */
|
---|
71 |
|
---|
72 | kdoc =new KML_Document();
|
---|
73 | sprintf(kdoc->name ,"Exp2Kmlx Module -- %s",ctime(&time0));
|
---|
74 | kdoc->open =1;
|
---|
75 |
|
---|
76 | /* construct style templates for defaults */
|
---|
77 |
|
---|
78 | klsty =new KML_LineStyle();
|
---|
79 | sprintf(klsty->color ,"ff000000");
|
---|
80 | sprintf(klsty->colormode ,"normal");
|
---|
81 | klsty->width =lwidth;
|
---|
82 | kpsty =new KML_PolyStyle();
|
---|
83 | sprintf(kpsty->color ,"%02xffffff",(int)floor(popac*255+0.5));
|
---|
84 | sprintf(kpsty->colormode ,"random");
|
---|
85 | kstyle=new KML_Style();
|
---|
86 | kstyle->AddAttrib("id","BlackLineRandomPoly");
|
---|
87 | kstyle->line =klsty;
|
---|
88 | kstyle->poly =kpsty;
|
---|
89 | (kdoc->style )->AddObject((Object*)kstyle);
|
---|
90 |
|
---|
91 | klsty =new KML_LineStyle();
|
---|
92 | sprintf(klsty->color ,"ff000000");
|
---|
93 | sprintf(klsty->colormode ,"normal");
|
---|
94 | klsty->width ,lwidth;
|
---|
95 | kpsty =new KML_PolyStyle();
|
---|
96 | sprintf(kpsty->color ,"00ffffff");
|
---|
97 | sprintf(kpsty->colormode ,"random");
|
---|
98 | kstyle=new KML_Style();
|
---|
99 | kstyle->AddAttrib("id","BlackLineEmptyPoly");
|
---|
100 | kstyle->line =klsty;
|
---|
101 | kstyle->poly =kpsty;
|
---|
102 | (kdoc->style )->AddObject((Object*)kstyle);
|
---|
103 |
|
---|
104 | klsty =new KML_LineStyle();
|
---|
105 | sprintf(klsty->color ,"%02xffffff",(int)floor(popac*255+0.5));
|
---|
106 | sprintf(klsty->colormode ,"random");
|
---|
107 | klsty->width =lwidth*2;
|
---|
108 | kpsty =new KML_PolyStyle();
|
---|
109 | sprintf(kpsty->color ,"00ffffff");
|
---|
110 | sprintf(kpsty->colormode ,"random");
|
---|
111 | kstyle=new KML_Style();
|
---|
112 | kstyle->AddAttrib("id","RandomLineEmptyPoly");
|
---|
113 | kstyle->line =klsty;
|
---|
114 | kstyle->poly =kpsty;
|
---|
115 | (kdoc->style )->AddObject((Object*)kstyle);
|
---|
116 |
|
---|
117 | /* construct kml folder for polygons */
|
---|
118 |
|
---|
119 | kfold =new KML_Folder();
|
---|
120 | sprintf(kfold->name ,"Profiles translated from file \"%s\".",filexp);
|
---|
121 | kfold->open =1;
|
---|
122 |
|
---|
123 | /* polygon with multiple holes */
|
---|
124 |
|
---|
125 | if (holes && nprof && !closed[0]) {
|
---|
126 | _printf_(true,"Warning -- Outer profile is not closed, so \"holes\" option will be ignored.\n");
|
---|
127 | holes=false;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (holes) {
|
---|
131 | i=0;
|
---|
132 | kplace=new KML_Placemark();
|
---|
133 | sprintf(kplace->name ,"Polygon with Holes");
|
---|
134 | kplace->visibility=true;
|
---|
135 | sprintf(kplace->styleurl ,"#BlackLineRandomPoly");
|
---|
136 |
|
---|
137 | kpoly =new KML_Polygon();
|
---|
138 | kring =new KML_LinearRing();
|
---|
139 |
|
---|
140 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
141 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
142 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
143 |
|
---|
144 | kring->ncoord =pnvert[i];
|
---|
145 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
146 | for (j=0; j<pnvert[i]; j++) {
|
---|
147 | kring->coords[j][0]=lon[j];
|
---|
148 | kring->coords[j][1]=lat[j];
|
---|
149 | kring->coords[j][2]=0.;
|
---|
150 | }
|
---|
151 | xfree((void**)&lon);
|
---|
152 | xfree((void**)&lat);
|
---|
153 |
|
---|
154 | (kpoly ->outer )->AddObject((Object*)kring);
|
---|
155 | kring =NULL;
|
---|
156 |
|
---|
157 | for (i=1; i<nprof; i++) {
|
---|
158 | if (!closed[i]) {
|
---|
159 | _printf_(true,"Warning -- Inner profile %d is not closed with \"holes\" specified, so it will be ignored.\n",i+1);
|
---|
160 | continue;
|
---|
161 | }
|
---|
162 |
|
---|
163 | kring =new KML_LinearRing();
|
---|
164 |
|
---|
165 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
166 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
167 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
168 | kring->ncoord =pnvert[i];
|
---|
169 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
170 | for (j=0; j<pnvert[i]; j++) {
|
---|
171 | kring->coords[j][0]=lon[j];
|
---|
172 | kring->coords[j][1]=lat[j];
|
---|
173 | kring->coords[j][2]=0.;
|
---|
174 | }
|
---|
175 | xfree((void**)&lon);
|
---|
176 | xfree((void**)&lat);
|
---|
177 |
|
---|
178 | (kpoly ->inner )->AddObject((Object*)kring);
|
---|
179 | kring =NULL;
|
---|
180 | }
|
---|
181 |
|
---|
182 | (kplace->geometry )->AddObject((Object*)kpoly);
|
---|
183 | kpoly =NULL;
|
---|
184 | (kfold ->feature )->AddObject((Object*)kplace);
|
---|
185 | kplace=NULL;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* multiple polygons or linestrings */
|
---|
189 |
|
---|
190 | else {
|
---|
191 | for (i=0; i<nprof; i++) {
|
---|
192 | kplace=new KML_Placemark();
|
---|
193 |
|
---|
194 | if (closed[i]) {
|
---|
195 | sprintf(kplace->name ,"Polygon %d",i+1);
|
---|
196 | kplace->visibility=true;
|
---|
197 | sprintf(kplace->styleurl ,"#BlackLineRandomPoly");
|
---|
198 |
|
---|
199 | kpoly =new KML_Polygon();
|
---|
200 | kring =new KML_LinearRing();
|
---|
201 |
|
---|
202 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
203 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
204 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
205 |
|
---|
206 | kring->ncoord =pnvert[i];
|
---|
207 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
208 | for (j=0; j<pnvert[i]; j++) {
|
---|
209 | kring->coords[j][0]=lon[j];
|
---|
210 | kring->coords[j][1]=lat[j];
|
---|
211 | kring->coords[j][2]=0.;
|
---|
212 | }
|
---|
213 | xfree((void**)&lon);
|
---|
214 | xfree((void**)&lat);
|
---|
215 |
|
---|
216 | (kpoly ->outer )->AddObject((Object*)kring);
|
---|
217 | kring =NULL;
|
---|
218 |
|
---|
219 | (kplace->geometry )->AddObject((Object*)kpoly);
|
---|
220 | kpoly =NULL;
|
---|
221 | }
|
---|
222 |
|
---|
223 | else {
|
---|
224 | sprintf(kplace->name ,"LineString %d",i+1);
|
---|
225 | kplace->visibility=true;
|
---|
226 | sprintf(kplace->styleurl ,"#RandomLineEmptyPoly");
|
---|
227 |
|
---|
228 | kline =new KML_LineString();
|
---|
229 |
|
---|
230 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
231 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
232 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
233 |
|
---|
234 | kline->ncoord =pnvert[i];
|
---|
235 | kline->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
236 | for (j=0; j<pnvert[i]; j++) {
|
---|
237 | kline->coords[j][0]=lon[j];
|
---|
238 | kline->coords[j][1]=lat[j];
|
---|
239 | kline->coords[j][2]=0.;
|
---|
240 | }
|
---|
241 | xfree((void**)&lon);
|
---|
242 | xfree((void**)&lat);
|
---|
243 |
|
---|
244 | (kplace->geometry )->AddObject((Object*)kline);
|
---|
245 | kline =NULL;
|
---|
246 | }
|
---|
247 |
|
---|
248 | (kfold ->feature )->AddObject((Object*)kplace);
|
---|
249 | kplace=NULL;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /* assemble the rest of the kml hierarchy */
|
---|
254 |
|
---|
255 | (kdoc ->feature )->AddObject((Object*)kfold);
|
---|
256 | kfold=NULL;
|
---|
257 | (kfile->kmlobj )->AddObject((Object*)kdoc);
|
---|
258 | kdoc =NULL;
|
---|
259 |
|
---|
260 | /* write kml file */
|
---|
261 |
|
---|
262 | _printf_(true,"Writing kml document to file.\n");
|
---|
263 | fid=fopen(filkml,"w");
|
---|
264 | fprintf(fid,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
---|
265 | kfile->Write(fid,indent);
|
---|
266 | fclose(fid);
|
---|
267 |
|
---|
268 | delete kfile;
|
---|
269 | for (i=nprof-1; i>=0; i--) {
|
---|
270 | xfree((void**)&(pprofy[i]));
|
---|
271 | xfree((void**)&(pprofx[i]));
|
---|
272 | }
|
---|
273 | xfree((void**)&pnvert);
|
---|
274 |
|
---|
275 | clock1=clock();
|
---|
276 | time1 =time(NULL);
|
---|
277 | _printf_(true,"Exp2Kmlx Module -- %f CPU seconds; %f elapsed seconds.\n\n",
|
---|
278 | ((double)(clock1-clock0))/CLOCKS_PER_SEC,difftime(time1,time0));
|
---|
279 |
|
---|
280 | return(iret);
|
---|
281 | }
|
---|
282 |
|
---|