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

Last change on this file since 8716 was 8716, checked in by jschierm, 14 years ago

Implement Xy2lldef into Exp2Kmlx (among other changes).

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