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 |
|
---|
44 | FILE* fid=NULL;
|
---|
45 |
|
---|
46 | clock_t clock0,clock1;
|
---|
47 | time_t time0, time1;
|
---|
48 |
|
---|
49 | clock0=clock();
|
---|
50 | time0 =time(NULL);
|
---|
51 | _printf_(true,"\nExp2Kmlx Module -- %s",ctime(&time0));
|
---|
52 |
|
---|
53 | /* read exp file */
|
---|
54 |
|
---|
55 | if (!DomainOutlineRead(&nprof,&pnvert,&pprofx,&pprofy,&closed,filexp))
|
---|
56 | _error_("Error reading exp file.");
|
---|
57 |
|
---|
58 | /* construct kml file */
|
---|
59 |
|
---|
60 | kfile =new KML_File();
|
---|
61 | kfile->AddAttrib("xmlns","http://www.opengis.net/kml/2.2");
|
---|
62 |
|
---|
63 | /* construct kml document */
|
---|
64 |
|
---|
65 | kdoc =new KML_Document();
|
---|
66 | sprintf(kdoc->name ,"Exp2Kmlx Module -- %s",ctime(&time0));
|
---|
67 | kdoc->open =1;
|
---|
68 |
|
---|
69 | /* construct kml folder for polygons */
|
---|
70 |
|
---|
71 | kfold =new KML_Folder();
|
---|
72 | sprintf(kfold->name ,"Polygons translated from file \"%s\".",filexp);
|
---|
73 | kfold->open =1;
|
---|
74 |
|
---|
75 | /* polygon with multiple holes */
|
---|
76 |
|
---|
77 | if (holes) {
|
---|
78 | i=0;
|
---|
79 | kplace=new KML_Placemark();
|
---|
80 | sprintf(kplace->name ,"Polygon with Holes");
|
---|
81 | kplace->visibility=true;
|
---|
82 |
|
---|
83 | kpoly =new KML_Polygon();
|
---|
84 | kring =new KML_LinearRing();
|
---|
85 |
|
---|
86 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
87 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
88 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
89 |
|
---|
90 | kring->ncoord =pnvert[i];
|
---|
91 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
92 | for (j=0; j<pnvert[i]; j++) {
|
---|
93 | kring->coords[j][0]=lon[j];
|
---|
94 | kring->coords[j][1]=lat[j];
|
---|
95 | kring->coords[j][2]=0.;
|
---|
96 | }
|
---|
97 | xfree((void**)&lon);
|
---|
98 | xfree((void**)&lat);
|
---|
99 |
|
---|
100 | (kpoly ->outer )->AddObject((Object*)kring);
|
---|
101 | kring =NULL;
|
---|
102 |
|
---|
103 | for (i=1; i<nprof; i++) {
|
---|
104 | kring =new KML_LinearRing();
|
---|
105 |
|
---|
106 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
107 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
108 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
109 | kring->ncoord =pnvert[i];
|
---|
110 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
111 | for (j=0; j<pnvert[i]; j++) {
|
---|
112 | kring->coords[j][0]=lon[j];
|
---|
113 | kring->coords[j][1]=lat[j];
|
---|
114 | kring->coords[j][2]=0.;
|
---|
115 | }
|
---|
116 | xfree((void**)&lon);
|
---|
117 | xfree((void**)&lat);
|
---|
118 |
|
---|
119 | (kpoly ->inner )->AddObject((Object*)kring);
|
---|
120 | kring =NULL;
|
---|
121 | }
|
---|
122 |
|
---|
123 | (kplace->geometry )->AddObject((Object*)kpoly);
|
---|
124 | kpoly =NULL;
|
---|
125 | (kfold ->feature )->AddObject((Object*)kplace);
|
---|
126 | kplace=NULL;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* multiple polygons */
|
---|
130 |
|
---|
131 | else {
|
---|
132 | for (i=0; i<nprof; i++) {
|
---|
133 | kplace=new KML_Placemark();
|
---|
134 | sprintf(kplace->name ,"Polygon %d",i);
|
---|
135 | kplace->visibility=true;
|
---|
136 |
|
---|
137 | kpoly =new KML_Polygon();
|
---|
138 | kring =new KML_LinearRing();
|
---|
139 |
|
---|
140 | lat=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
141 | lon=(double *) xmalloc(pnvert[i]*sizeof(double));
|
---|
142 | Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
|
---|
143 |
|
---|
144 | kring->ncoord =pnvert[i];
|
---|
145 | kring->coords =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
|
---|
146 | for (j=0; j<pnvert[i]; j++) {
|
---|
147 | kring->coords[j][0]=lon[j];
|
---|
148 | kring->coords[j][1]=lat[j];
|
---|
149 | kring->coords[j][2]=0.;
|
---|
150 | }
|
---|
151 | xfree((void**)&lon);
|
---|
152 | xfree((void**)&lat);
|
---|
153 |
|
---|
154 | (kpoly ->outer )->AddObject((Object*)kring);
|
---|
155 | kring =NULL;
|
---|
156 |
|
---|
157 | (kplace->geometry )->AddObject((Object*)kpoly);
|
---|
158 | kpoly =NULL;
|
---|
159 | (kfold ->feature )->AddObject((Object*)kplace);
|
---|
160 | kplace=NULL;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* assemble the rest of the kml hierarchy */
|
---|
165 |
|
---|
166 | (kdoc ->feature )->AddObject((Object*)kfold);
|
---|
167 | kfold=NULL;
|
---|
168 | (kfile->kmlobj )->AddObject((Object*)kdoc);
|
---|
169 | kdoc =NULL;
|
---|
170 |
|
---|
171 | /* write kml file */
|
---|
172 |
|
---|
173 | _printf_(true,"Writing kml document to file.\n");
|
---|
174 | fid=fopen(filkml,"w");
|
---|
175 | fprintf(fid,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
---|
176 | kfile->Write(fid,indent);
|
---|
177 | fclose(fid);
|
---|
178 |
|
---|
179 | delete kfile;
|
---|
180 | for (i=nprof-1; i>=0; i--) {
|
---|
181 | xfree((void**)&(pprofy[i]));
|
---|
182 | xfree((void**)&(pprofx[i]));
|
---|
183 | }
|
---|
184 | xfree((void**)&pnvert);
|
---|
185 |
|
---|
186 | clock1=clock();
|
---|
187 | time1 =time(NULL);
|
---|
188 | _printf_(true,"Exp2Kmlx Module -- %f CPU seconds; %f elapsed seconds.\n\n",
|
---|
189 | ((double)(clock1-clock0))/CLOCKS_PER_SEC,difftime(time1,time0));
|
---|
190 |
|
---|
191 | return(iret);
|
---|
192 | }
|
---|
193 |
|
---|