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