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,int sgn,bool holes){
|
---|
14 |
|
---|
15 | double cm,sp;
|
---|
16 |
|
---|
17 | Xy2lldef(&cm,&sp,sgn);
|
---|
18 | return(Exp2Kmlx(filexp,filkml,sgn,cm,sp,holes));
|
---|
19 | }
|
---|
20 |
|
---|
21 | int Exp2Kmlx(char* filexp,char* filkml,int sgn,double cm,double sp,bool holes){
|
---|
22 |
|
---|
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;
|
---|
31 |
|
---|
32 | char indent[81]="";
|
---|
33 | KML_File* kfile =NULL;
|
---|
34 | KML_Document* kdoc =NULL;
|
---|
35 | KML_Style* kstyle=NULL;
|
---|
36 | KML_LineStyle* klsty =NULL;
|
---|
37 | KML_PolyStyle* kpsty =NULL;
|
---|
38 | KML_Folder* kfold =NULL;
|
---|
39 | KML_Placemark* kplace=NULL;
|
---|
40 | KML_Polygon* kpoly =NULL;
|
---|
41 | KML_LinearRing* kring =NULL;
|
---|
42 | KML_LineString* kline =NULL;
|
---|
43 | KML_Point* kpoint=NULL;
|
---|
44 |
|
---|
45 | FILE* fid=NULL;
|
---|
46 |
|
---|
47 | clock_t clock0,clock1;
|
---|
48 | time_t time0, time1;
|
---|
49 |
|
---|
50 | clock0=clock();
|
---|
51 | time0 =time(NULL);
|
---|
52 | _pprintString_("\nExp2Kmlx Module -- " << ctime(&time0));
|
---|
53 |
|
---|
54 | /*read exp file */
|
---|
55 |
|
---|
56 | if (!DomainOutlineRead(&nprof,&pnvert,&pprofx,&pprofy,&closed,filexp))
|
---|
57 | _error2_("Error reading exp file.");
|
---|
58 | _pprintLine_("Exp2Kmlx -- Reading " << nprof << " exp profiles from file \"" << filexp << "\".");
|
---|
59 | // for (i=0; i<nprof; i++)
|
---|
60 | // _printLine_("i=" << i << "; nvert=" << pnvert[i] << ", closed=" << closed[i]);
|
---|
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 |
|
---|
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 |
|
---|
114 | /* construct kml folder for polygons */
|
---|
115 |
|
---|
116 | kfold =new KML_Folder();
|
---|
117 | sprintf(kfold->name ,"Profiles translated from file \"%s\".",filexp);
|
---|
118 | kfold->open =1;
|
---|
119 |
|
---|
120 | /* polygon with multiple holes */
|
---|
121 |
|
---|
122 | if (holes && nprof && (pnvert[0] <= 1 || pprofx[0][pnvert[0]-1] != pprofx[0][0] || pprofy[0][pnvert[0]-1] != pprofy[0][0])) {
|
---|
123 | _pprintLine_("Warning -- Outer profile is not closed, so \"holes\" option will be ignored.");
|
---|
124 | holes=false;
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (holes) {
|
---|
128 | i=0;
|
---|
129 | kplace=new KML_Placemark();
|
---|
130 | sprintf(kplace->name ,"Polygon with Holes");
|
---|
131 | kplace->visibility=true;
|
---|
132 | sprintf(kplace->styleurl ,"#BlackLineRandomPoly");
|
---|
133 |
|
---|
134 | kpoly =new KML_Polygon();
|
---|
135 | kring =new KML_LinearRing();
|
---|
136 |
|
---|
137 | kring->ncoord =pnvert[i]-1;
|
---|
138 | lat=xNew<double>(kring->ncoord);
|
---|
139 | lon=xNew<double>(kring->ncoord);
|
---|
140 | Xy2llx(lat,lon,pprofx[i],pprofy[i],kring->ncoord,sgn,cm,sp);
|
---|
141 | kring->coords=xNew<double>(kring->ncoord*3);
|
---|
142 | for (j=0; j<kring->ncoord; j++) {
|
---|
143 | kring->coords[3*j+0]=lon[j];
|
---|
144 | kring->coords[3*j+1]=lat[j];
|
---|
145 | kring->coords[3*j+2]=0.;
|
---|
146 | }
|
---|
147 | xDelete<double>(lon);
|
---|
148 | xDelete<double>(lat);
|
---|
149 |
|
---|
150 | (kpoly ->outer )->AddObject((Object*)kring);
|
---|
151 | kring =NULL;
|
---|
152 |
|
---|
153 | for (i=1; i<nprof; i++) {
|
---|
154 | if (pnvert[i] <= 1 || pprofx[i][pnvert[i]-1] != pprofx[i][0] || pprofy[i][pnvert[i]-1] != pprofy[i][0]) {
|
---|
155 | _pprintLine_("Warning -- Inner profile " << i+1 << " is not closed with \"holes\" specified, so it will be ignored.");
|
---|
156 | continue;
|
---|
157 | }
|
---|
158 |
|
---|
159 | kring =new KML_LinearRing();
|
---|
160 |
|
---|
161 | kring->ncoord =pnvert[i]-1;
|
---|
162 | lat=xNew<double>(kring->ncoord);
|
---|
163 | lon=xNew<double>(kring->ncoord);
|
---|
164 | Xy2llx(lat,lon,pprofx[i],pprofy[i],kring->ncoord,sgn,cm,sp);
|
---|
165 | kring->coords =xNew<double>(kring->ncoord*3);
|
---|
166 | for (j=0; j<kring->ncoord; j++) {
|
---|
167 | kring->coords[3*j+0]=lon[j];
|
---|
168 | kring->coords[3*j+1]=lat[j];
|
---|
169 | kring->coords[3*j+2]=0.;
|
---|
170 | }
|
---|
171 | xDelete<double>(lon);
|
---|
172 | xDelete<double>(lat);
|
---|
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 |
|
---|
184 | /* multiple polygons or linestrings */
|
---|
185 |
|
---|
186 | else {
|
---|
187 | for (i=0; i<nprof; i++) {
|
---|
188 | kplace=new KML_Placemark();
|
---|
189 |
|
---|
190 | if (pnvert[i] > 1 && pprofx[i][pnvert[i]-1] == pprofx[i][0] && pprofy[i][pnvert[i]-1] == pprofy[i][0]) {
|
---|
191 | sprintf(kplace->name ,"Polygon %d",i+1);
|
---|
192 | kplace->visibility=true;
|
---|
193 | sprintf(kplace->styleurl ,"#BlackLineRandomPoly");
|
---|
194 |
|
---|
195 | kpoly =new KML_Polygon();
|
---|
196 | kring =new KML_LinearRing();
|
---|
197 |
|
---|
198 | kring->ncoord =pnvert[i]-1;
|
---|
199 | lat=xNew<double>(kring->ncoord);
|
---|
200 | lon=xNew<double>(kring->ncoord);
|
---|
201 | Xy2llx(lat,lon,pprofx[i],pprofy[i],kring->ncoord,sgn,cm,sp);
|
---|
202 | kring->coords =xNew<double>(kring->ncoord*3);
|
---|
203 | for (j=0; j<kring->ncoord; j++) {
|
---|
204 | kring->coords[3*j+0]=lon[j];
|
---|
205 | kring->coords[3*j+1]=lat[j];
|
---|
206 | kring->coords[3*j+2]=0.;
|
---|
207 | }
|
---|
208 | xDelete<double>(lon);
|
---|
209 | xDelete<double>(lat);
|
---|
210 |
|
---|
211 | (kpoly ->outer )->AddObject((Object*)kring);
|
---|
212 | kring =NULL;
|
---|
213 |
|
---|
214 | (kplace->geometry )->AddObject((Object*)kpoly);
|
---|
215 | kpoly =NULL;
|
---|
216 | }
|
---|
217 |
|
---|
218 | else if (pnvert[i] > 1) {
|
---|
219 | sprintf(kplace->name ,"LineString %d",i+1);
|
---|
220 | kplace->visibility=true;
|
---|
221 | sprintf(kplace->styleurl ,"#RandomLineEmptyPoly");
|
---|
222 |
|
---|
223 | kline =new KML_LineString();
|
---|
224 |
|
---|
225 | kline->ncoord =pnvert[i];
|
---|
226 | lat=xNew<double>(kline->ncoord);
|
---|
227 | lon=xNew<double>(kline->ncoord);
|
---|
228 | Xy2llx(lat,lon,pprofx[i],pprofy[i],kline->ncoord,sgn,cm,sp);
|
---|
229 | kline->coords =xNew<double>(kline->ncoord*3);
|
---|
230 | for (j=0; j<kline->ncoord; j++) {
|
---|
231 | kline->coords[3*j+0]=lon[j];
|
---|
232 | kline->coords[3*j+1]=lat[j];
|
---|
233 | kline->coords[3*j+2]=0.;
|
---|
234 | }
|
---|
235 | xDelete<double>(lon);
|
---|
236 | xDelete<double>(lat);
|
---|
237 |
|
---|
238 | (kplace->geometry )->AddObject((Object*)kline);
|
---|
239 | kline =NULL;
|
---|
240 | }
|
---|
241 |
|
---|
242 | else if (pnvert[i]) {
|
---|
243 | sprintf(kplace->name ,"Point %d",i+1);
|
---|
244 | kplace->visibility=true;
|
---|
245 | sprintf(kplace->styleurl ,"#RandomLineEmptyPoly");
|
---|
246 | int one=1;
|
---|
247 |
|
---|
248 | kpoint=new KML_Point();
|
---|
249 |
|
---|
250 | lat=xNew<double>(one);
|
---|
251 | lon=xNew<double>(one);
|
---|
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.;
|
---|
256 | xDelete<double>(lon);
|
---|
257 | xDelete<double>(lat);
|
---|
258 |
|
---|
259 | (kplace->geometry )->AddObject((Object*)kpoint);
|
---|
260 | kpoint =NULL;
|
---|
261 | }
|
---|
262 |
|
---|
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 |
|
---|
277 | _pprintLine_("Exp2Kmlx -- Writing kml document to file \"" << filkml << "\".");
|
---|
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--) {
|
---|
285 | xDelete<double>(pprofy[i]);
|
---|
286 | xDelete<double>(pprofx[i]);
|
---|
287 | }
|
---|
288 | xDelete<int>(pnvert);
|
---|
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 |
|
---|