Ice Sheet System Model  4.18
Code documentation
KML_LinearRing.cpp
Go to the documentation of this file.
1 
5 /*Headers:*/
6 /*{{{*/
7 #ifdef HAVE_CONFIG_H
8  #include <config.h>
9 #else
10 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
11 #endif
12 
13 #include "./KML_LinearRing.h"
14 #include "./KMLFileReadUtils.h"
15 #include "../shared/shared.h"
16 /*}}}*/
17 
18 /*Constructors/destructor/copy*/
20 
21  extrude =false;
22  tessellate=false;
23  memcpy(altmode,"clampToGround",(strlen("clampToGround")+1)*sizeof(char));
24 
25  ncoord =0;
26  coords =NULL;
27 
28 }
29 /*}}}*/
31 
32  if (coords) xDelete<double>(coords);
33 
34  coords =NULL;
35  ncoord =0;
36 
37 }
38 /*}}}*/
39 
40 /*Other*/
41 void KML_LinearRing::Echo(){/*{{{*/
42 
43  bool flag=true;
44 
45  if(flag) _printf0_("KML_LinearRing:\n");
47 
48  if(flag) _printf0_(" extrude: " << (extrude ? "true" : "false") << "\n");
49  if(flag) _printf0_(" tessellate: " << (tessellate ? "true" : "false") << "\n");
50  if(flag) _printf0_(" altmode: \"" << altmode << "\"\n");
51  if(flag) _printf0_(" coords: (ncoord=" << ncoord << ")\n");
52 
53  return;
54 }
55 /*}}}*/
57 
58  char indent[81]="";
59 
61 
62  return;
63 }
64 /*}}}*/
65 void KML_LinearRing::DeepEcho(const char* indent){/*{{{*/
66 
67  int i;
68  bool flag=true;
69 
70  if(flag) _printf0_(indent << "KML_LinearRing:\n");
71  KML_Geometry::DeepEcho(indent);
72 
73  if(flag) _printf0_(indent << " extrude: " << (extrude ? "true" : "false") << "\n");
74  if(flag) _printf0_(indent << " tessellate: " << (tessellate ? "true" : "false") << "\n");
75  if(flag) _printf0_(indent << " altmode: \"" << altmode << "\"\n");
76  if(flag) _printf0_(indent << " coords: (ncoord=" << ncoord << ")\n");
77  for (i=0; i<ncoord; i++)
78  if(flag)_printf_(indent << " (" <<coords[3*i+0] << "," <<coords[3*i+1] << "," <<coords[3*i+2] << ")\n\n");
79 
80  return;
81 }
82 /*}}}*/
83 void KML_LinearRing::Write(FILE* filout,const char* indent){/*{{{*/
84 
85  int i;
86 
87  fprintf(filout,"%s<LinearRing",indent);
88  WriteAttrib(filout," ");
89  fprintf(filout,">\n");
90  WriteCommnt(filout,indent);
91 
92  KML_Geometry::Write(filout,indent);
93 
94  fprintf(filout,"%s <extrude>%d</extrude>\n",indent,(extrude ? 1 : 0));
95  fprintf(filout,"%s <tessellate>%d</tessellate>\n",indent,(tessellate ? 1 : 0));
96  fprintf(filout,"%s <altitudeMode>%s</altitudeMode>\n",indent,altmode);
97  fprintf(filout,"%s <coordinates>\n",indent);
98 
99 /* loop over the coordinates for the linearring */
100 
101  for (i=0; i<ncoord; i++)
102  fprintf(filout,"%s %0.16g,%0.16g,%0.16g\n",indent,coords[3*i+0],coords[3*i+1],coords[3*i+2]);
103 
104  fprintf(filout,"%s </coordinates>\n",indent);
105  fprintf(filout,"%s</LinearRing>\n",indent);
106 
107  return;
108 }
109 /*}}}*/
110 void KML_LinearRing::Read(FILE* fid,char* kstr){/*{{{*/
111 
112  char *kstri = NULL;
113  int ncom = 0;
114  char **pcom = NULL;
115 
116 /* get object attributes and check for solo tag */
117 
118  if (KMLFileTagAttrib(this,kstr)) return;
119 
120 /* loop over and process fields within opening and closing tags */
121 
122  while((kstri=KMLFileToken(fid,&ncom,&pcom))){
123  if (!strncmp(kstri,"</LinearRing",12)){
124  xDelete<char>(kstri);
125  break;
126  }
127  else if (!strncmp(kstri,"</",2))
128  {_error_("KML_LinearRing::Read -- Unexpected closing tag " << kstri << ".\n");}
129  else if (strncmp(kstri,"<",1))
130  {_error_("KML_LinearRing::Read -- Unexpected field \"" << kstri << "\".\n");}
131 
132  else if (!strcmp(kstri,"<extrude>"))
133  KMLFileTokenParse(&extrude,kstri,fid);
134  else if (!strcmp(kstri,"<tessellate>"))
135  KMLFileTokenParse(&tessellate,kstri,fid);
136  else if (!strcmp(kstri,"<altitudeMode>"))
138  else if (!strcmp(kstri,"<coordinates>"))
139  KMLFileTokenParse(&coords,&ncoord,3,0,kstri,fid);
140  else if (!strncmp(kstri,"<",1))
141  KML_Geometry::Read(fid,kstri);
142 
143  xDelete<char>(kstri);
144  }
145 
146  this->AddCommnt(ncom,pcom);
147 
148  for(ncom=ncom; ncom>0; ncom--)
149  xDelete<char>(pcom[ncom-1]);
150  xDelete<char*>(pcom);
151 
152  return;
153 }
154 /*}}}*/
155 void KML_LinearRing::WriteExp(FILE* fid,const char* nstr,int sgn,double cm,double sp){/*{{{*/
156 
157  int i;
158  double *lat,*lon,*x,*y;
159  char nstr2[81];
160 
161 /* extract latitude and longitude into vectors */
162 
163  lat=xNew<IssmPDouble>(ncoord);
164  lon=xNew<IssmPDouble>(ncoord);
165  for (i=0; i<ncoord; i++) {
166  lon[i]=coords[3*i+0];
167  lat[i]=coords[3*i+1];
168  }
169 
170 /* convert latitude and longitude to x and y */
171 
172  x =xNew<IssmPDouble>(ncoord);
173  y =xNew<IssmPDouble>(ncoord);
174  if (sgn) {
175  Ll2xyx(x,y,lat,lon,ncoord,sgn,cm,sp);
176  }
177  else {
178  memcpy(x,lon,ncoord*sizeof(IssmDouble));
179  memcpy(y,lat,ncoord*sizeof(IssmDouble));
180  }
181 
182 /* write header */
183 
184  memcpy(nstr2,nstr,(strlen(nstr)+1)*sizeof(char));
185 
186  for (i=0; i<strlen(nstr2); i++)
187  if ((nstr2[i] == ' ') || (nstr2[i] == '\t'))
188  nstr2[i]='_';
189  fprintf(fid,"## Name:%s\n",nstr2);
190  fprintf(fid,"## Icon:0\n");
191  fprintf(fid,"# Points Count Value\n");
192  if ((lat[ncoord-1] != lat[0]) || (lon[ncoord-1] != lon[0]))
193  fprintf(fid,"%u %s\n",ncoord+1,"1.");
194  else
195  fprintf(fid,"%u %s\n",ncoord ,"1.");
196  fprintf(fid,"# X pos Y pos\n");
197 
198 /* write vertices, making sure ring is closed */
199 
200  for (i=0; i<ncoord; i++)
201  fprintf(fid,"%lf\t%lf\n",x[i],y[i]);
202  if ((lat[ncoord-1] != lat[0]) || (lon[ncoord-1] != lon[0]))
203  fprintf(fid,"%lf\t%lf\n",x[0],y[0]);
204 
205 /* write blank line */
206 
207  fprintf(fid,"\n");
208 
209  xDelete<double>(y);
210  xDelete<double>(x);
211  xDelete<double>(lon);
212  xDelete<double>(lat);
213 
214  return;
215 }
216 /*}}}*/
Ll2xyx
int Ll2xyx(double *x, double *y, double *lat, double *lon, int ncoord, int sgn)
Definition: Ll2xyx.cpp:8
KML_LinearRing::Echo
void Echo()
Definition: KML_LinearRing.cpp:41
IssmDouble
double IssmDouble
Definition: types.h:37
_printf0_
#define _printf0_(StreamArgs)
Definition: Print.h:29
KML_Object::WriteAttrib
virtual void WriteAttrib(FILE *fid, const char *indent)
Definition: KML_Object.cpp:288
KML_Geometry::Write
void Write(FILE *fid, const char *indent)
Definition: KML_Geometry.cpp:56
KML_LinearRing::coords
double * coords
Definition: KML_LinearRing.h:24
_printf_
#define _printf_(StreamArgs)
Definition: Print.h:22
KML_LinearRing::WriteExp
void WriteExp(FILE *fid, const char *nstr, int sgn, double cm, double sp)
Definition: KML_LinearRing.cpp:155
KML_LinearRing::altmode
char altmode[KML_LINEARRING_ALTMODE_LENGTH+1]
Definition: KML_LinearRing.h:22
KMLFileReadUtils.h
: header file for kml file reading utilities.
KMLFileTagAttrib
int KMLFileTagAttrib(KML_Object *kobj, char *ktag)
Definition: KMLFileReadUtils.cpp:287
KML_LinearRing::Write
void Write(FILE *fid, const char *indent)
Definition: KML_LinearRing.cpp:83
KML_LinearRing::Read
void Read(FILE *fid, char *kstr)
Definition: KML_LinearRing.cpp:110
KMLFileToken
char * KMLFileToken(FILE *fid, int *pncom=NULL, char ***ppcom=NULL)
Definition: KMLFileReadUtils.cpp:18
KML_LinearRing::KML_LinearRing
KML_LinearRing()
Definition: KML_LinearRing.cpp:19
KML_LinearRing::tessellate
bool tessellate
Definition: KML_LinearRing.h:21
KML_Geometry::DeepEcho
void DeepEcho()
Definition: KML_Geometry.cpp:40
KML_LinearRing::extrude
bool extrude
Definition: KML_LinearRing.h:20
KML_LinearRing::ncoord
int ncoord
Definition: KML_LinearRing.h:23
KMLFileTokenParse
int KMLFileTokenParse(int *pival, char *ktag, FILE *fid)
Definition: KMLFileReadUtils.cpp:335
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
KML_LinearRing.h
: header file for kml_linearring object
KML_LinearRing::DeepEcho
void DeepEcho()
Definition: KML_LinearRing.cpp:56
KML_LINEARRING_ALTMODE_LENGTH
#define KML_LINEARRING_ALTMODE_LENGTH
Definition: KML_LinearRing.h:8
KML_Object::AddCommnt
virtual void AddCommnt(int ncom, char **pcom)
Definition: KML_Object.cpp:301
KML_Object::WriteCommnt
virtual void WriteCommnt(FILE *fid, const char *indent)
Definition: KML_Object.cpp:326
KML_LinearRing::~KML_LinearRing
~KML_LinearRing()
Definition: KML_LinearRing.cpp:30
KML_Geometry::Echo
void Echo()
Definition: KML_Geometry.cpp:33
KML_Geometry::Read
void Read(FILE *fid, char *kstr)
Definition: KML_Geometry.cpp:63