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