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

Last change on this file since 13395 was 13395, checked in by Mathieu Morlighem, 12 years ago

merged trunk-jpl and trunk for revision 13393

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