source: issm/trunk/src/c/modules/Exp2Kmlx/Exp2Kmlx.cpp@ 8747

Last change on this file since 8747 was 8747, checked in by jschierm, 14 years ago

Exp2Kmlx: Added styles for kml file.

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