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