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

Last change on this file since 16560 was 16560, checked in by Mathieu Morlighem, 11 years ago

merged trunk-jpl and trunk for revision 16554

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