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 |
|
---|
12 | int 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 |
|
---|
25 | int Exp2Kmlx(char* filexp,char* filkml,
|
---|
26 | int sgn,double cm,double sp,
|
---|
27 | bool holes){
|
---|
28 |
|
---|
29 | int i,j,iret=0;
|
---|
30 | int nprof;
|
---|
31 | int *pnvert=NULL;
|
---|
32 | double **pprofx=NULL,**pprofy=NULL;
|
---|
33 | bool *closed=NULL;
|
---|
34 | double *lat=NULL,*lon=NULL;
|
---|
35 |
|
---|
36 | char indent[81]="";
|
---|
37 | KML_File* kfile =NULL;
|
---|
38 | KML_Document* kdoc =NULL;
|
---|
39 | KML_Folder* kfold =NULL;
|
---|
40 | KML_Placemark* kplace=NULL;
|
---|
41 | KML_Polygon* kpoly =NULL;
|
---|
42 | KML_LinearRing* kring =NULL;
|
---|
43 | KML_LineString* kline =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 | _printf_(true,"\nExp2Kmlx Module -- %s",ctime(&time0));
|
---|
53 |
|
---|
54 | /* read exp file */
|
---|
55 |
|
---|
56 | if (!DomainOutlineRead(&nprof,&pnvert,&pprofx,&pprofy,&closed,filexp,false))
|
---|
57 | _error_("Error reading exp file.");
|
---|
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 kml folder for polygons */
|
---|
71 |
|
---|
72 | kfold =new KML_Folder();
|
---|
73 | sprintf(kfold->name ,"Profiles translated from file \"%s\".",filexp);
|
---|
74 | kfold->open =1;
|
---|
75 |
|
---|
76 | /* polygon with multiple holes */
|
---|
77 |
|
---|
78 | if (holes && nprof && !closed[0]) {
|
---|
79 | _printf_(true,"Warning -- Outer profile is not closed, so \"holes\" option will be ignored.\n");
|
---|
80 | holes=false;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (holes) {
|
---|
84 | i=0;
|
---|
85 | kplace=new KML_Placemark();
|
---|
86 | sprintf(kplace->name ,"Polygon with Holes");
|
---|
87 | kplace->visibility=true;
|
---|
88 |
|
---|
89 | kpoly =new KML_Polygon();
|
---|
90 | kring =new KML_LinearRing();
|
---|
91 |
|
---|
92 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
93 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
94 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
95 |
|
---|
96 | kring->ncoord =pnvert[i];
|
---|
97 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
98 | for (j=0; j<pnvert[i]; j++) {
|
---|
99 | kring->coords[j][0]=lon[j];
|
---|
100 | kring->coords[j][1]=lat[j];
|
---|
101 | kring->coords[j][2]=0.;
|
---|
102 | }
|
---|
103 | xfree((void**)&lon);
|
---|
104 | xfree((void**)&lat);
|
---|
105 |
|
---|
106 | (kpoly ->outer )->AddObject((Object*)kring);
|
---|
107 | kring =NULL;
|
---|
108 |
|
---|
109 | for (i=1; i<nprof; i++) {
|
---|
110 | if (!closed[i]) {
|
---|
111 | _printf_(true,"Warning -- Inner profile %d is not closed with \"holes\" specified, so it will be ignored.\n",i+1);
|
---|
112 | continue;
|
---|
113 | }
|
---|
114 |
|
---|
115 | kring =new KML_LinearRing();
|
---|
116 |
|
---|
117 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
118 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
119 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
120 | kring->ncoord =pnvert[i];
|
---|
121 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
122 | for (j=0; j<pnvert[i]; j++) {
|
---|
123 | kring->coords[j][0]=lon[j];
|
---|
124 | kring->coords[j][1]=lat[j];
|
---|
125 | kring->coords[j][2]=0.;
|
---|
126 | }
|
---|
127 | xfree((void**)&lon);
|
---|
128 | xfree((void**)&lat);
|
---|
129 |
|
---|
130 | (kpoly ->inner )->AddObject((Object*)kring);
|
---|
131 | kring =NULL;
|
---|
132 | }
|
---|
133 |
|
---|
134 | (kplace->geometry )->AddObject((Object*)kpoly);
|
---|
135 | kpoly =NULL;
|
---|
136 | (kfold ->feature )->AddObject((Object*)kplace);
|
---|
137 | kplace=NULL;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* multiple polygons or linestrings */
|
---|
141 |
|
---|
142 | else {
|
---|
143 | for (i=0; i<nprof; i++) {
|
---|
144 | kplace=new KML_Placemark();
|
---|
145 |
|
---|
146 | if (closed[i]) {
|
---|
147 | sprintf(kplace->name ,"Polygon %d",i+1);
|
---|
148 | kplace->visibility=true;
|
---|
149 |
|
---|
150 | kpoly =new KML_Polygon();
|
---|
151 | kring =new KML_LinearRing();
|
---|
152 |
|
---|
153 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
154 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
155 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
156 |
|
---|
157 | kring->ncoord =pnvert[i];
|
---|
158 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
159 | for (j=0; j<pnvert[i]; j++) {
|
---|
160 | kring->coords[j][0]=lon[j];
|
---|
161 | kring->coords[j][1]=lat[j];
|
---|
162 | kring->coords[j][2]=0.;
|
---|
163 | }
|
---|
164 | xfree((void**)&lon);
|
---|
165 | xfree((void**)&lat);
|
---|
166 |
|
---|
167 | (kpoly ->outer )->AddObject((Object*)kring);
|
---|
168 | kring =NULL;
|
---|
169 |
|
---|
170 | (kplace->geometry )->AddObject((Object*)kpoly);
|
---|
171 | kpoly =NULL;
|
---|
172 | }
|
---|
173 |
|
---|
174 | else {
|
---|
175 | sprintf(kplace->name ,"LineString %d",i+1);
|
---|
176 | kplace->visibility=true;
|
---|
177 |
|
---|
178 | kline =new KML_LineString();
|
---|
179 |
|
---|
180 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
181 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
182 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
183 |
|
---|
184 | kline->ncoord =pnvert[i];
|
---|
185 | kline->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
186 | for (j=0; j<pnvert[i]; j++) {
|
---|
187 | kline->coords[j][0]=lon[j];
|
---|
188 | kline->coords[j][1]=lat[j];
|
---|
189 | kline->coords[j][2]=0.;
|
---|
190 | }
|
---|
191 | xfree((void**)&lon);
|
---|
192 | xfree((void**)&lat);
|
---|
193 |
|
---|
194 | (kplace->geometry )->AddObject((Object*)kline);
|
---|
195 | kline =NULL;
|
---|
196 | }
|
---|
197 |
|
---|
198 | (kfold ->feature )->AddObject((Object*)kplace);
|
---|
199 | kplace=NULL;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /* assemble the rest of the kml hierarchy */
|
---|
204 |
|
---|
205 | (kdoc ->feature )->AddObject((Object*)kfold);
|
---|
206 | kfold=NULL;
|
---|
207 | (kfile->kmlobj )->AddObject((Object*)kdoc);
|
---|
208 | kdoc =NULL;
|
---|
209 |
|
---|
210 | /* write kml file */
|
---|
211 |
|
---|
212 | _printf_(true,"Writing kml document to file.\n");
|
---|
213 | fid=fopen(filkml,"w");
|
---|
214 | fprintf(fid,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
---|
215 | kfile->Write(fid,indent);
|
---|
216 | fclose(fid);
|
---|
217 |
|
---|
218 | delete kfile;
|
---|
219 | for (i=nprof-1; i>=0; i--) {
|
---|
220 | xfree((void**)&(pprofy[i]));
|
---|
221 | xfree((void**)&(pprofx[i]));
|
---|
222 | }
|
---|
223 | xfree((void**)&pnvert);
|
---|
224 |
|
---|
225 | clock1=clock();
|
---|
226 | time1 =time(NULL);
|
---|
227 | _printf_(true,"Exp2Kmlx Module -- %f CPU seconds; %f elapsed seconds.\n\n",
|
---|
228 | ((double)(clock1-clock0))/CLOCKS_PER_SEC,difftime(time1,time0));
|
---|
229 |
|
---|
230 | return(iret);
|
---|
231 | }
|
---|
232 |
|
---|