~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/terralib/functions/TeImportMIF.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
ImportĀ upstreamĀ versionĀ 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright ļæ½ 2001-2004 INPE and Tecgraf/PUC-Rio.
 
4
 
 
5
This code is part of the TerraLib library.
 
6
This library is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU Lesser General Public
 
8
License as published by the Free Software Foundation; either
 
9
version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
14
The authors reassure the license terms regarding the warranties.
 
15
They specifically disclaim any warranties, including, but not limited to,
 
16
the implied warranties of merchantability and fitness for a particular purpose.
 
17
The library provided hereunder is on an "as is" basis, and the authors have no
 
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
 
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
 
20
indirect, special, incidental, or consequential damages arising out of the use
 
21
of this library and its documentation.
 
22
*************************************************************************************/
 
23
 
 
24
#include <string>
 
25
#include <iostream>
 
26
#include <list>
 
27
 
 
28
#ifdef WIN32
 
29
#include <io.h>
 
30
#else
 
31
#include <unistd.h>
 
32
#endif
 
33
 
 
34
using namespace std;
 
35
 
 
36
#include "TeImportExport.h"
 
37
#include "TeTable.h"
 
38
#include "TeGeometry.h"
 
39
#include "TeAsciiFile.h"
 
40
#include "TeMIFProjection.h"
 
41
#include "TeException.h"
 
42
#include "TeProjection.h"
 
43
#include "TeAttribute.h"
 
44
#include "TeTable.h"
 
45
#include "TeAssertions.h"
 
46
#include "TeUtils.h"
 
47
#include "TeLayer.h"
 
48
#include "TeGeometryAlgorithms.h"
 
49
#include "TeDatabase.h"
 
50
 
 
51
void TeMIFRegionDecode ( TeAsciiFile& mifFile, TePolygonSet& temp, string& index);
 
52
 
 
53
void TeMIFLineDecode (TeAsciiFile& mifFile, TeLineSet& temp, string& index);
 
54
 
 
55
void TeMIFPlineDecode (TeAsciiFile& mifFile, TeLineSet& temp, string& index);
 
56
 
 
57
void TeMIFPointDecode (TeAsciiFile& mifFile, TePointSet& temp, string& index);
 
58
 
 
59
void TeMIFMultiPointDecode (TeAsciiFile& mifFile, TePointSet& temp, string& index);
 
60
 
 
61
void TeMIFTextDecode (TeAsciiFile& mifFile, TeTextSet& temp, string& index);
 
62
 
 
63
void TeMIFCollectionDecode(TeAsciiFile& mifFile, TePointSet& ps, TeLineSet& ls, TePolygonSet& pols, string& index);
 
64
 
 
65
void TeMIFRectDecode (TeAsciiFile& mifFile, TePolygonSet& temp, string& index);
 
66
 
 
67
void TeMIFOthersDecode    ( TeAsciiFile& );
 
68
 
 
69
void TeMIFTransformDecode ( TeAsciiFile& );
 
70
 
 
71
bool TeReadMIFAttributes(TeAsciiFile& midFile, TeTable& attTable, 
 
72
                                                 int ncolumns, int nrecords=-1, int delta = 0, bool createAuto=false);
 
73
 
 
74
bool TeImportMIFGeometries(TeLayer* layer,const string& mifFileName, 
 
75
                                                   vector<string> &indexes, unsigned int chunckSize);
 
76
 
 
77
char TeReadMIFSeparator(const string& mifFileName);
 
78
 
 
79
double glXmulti = 1., glYmulti = 1;
 
80
 
 
81
TeCoord2D glAdd (0., 0. );
 
82
 
 
83
 
 
84
TeLayer* TeImportMIF(const string& mifFileName, TeDatabase* db, const string& layerName)
 
85
{
 
86
        if (!db || mifFileName.empty()) 
 
87
                return 0;
 
88
 
 
89
        // check if format is complete (MID and MIF files exist)
 
90
        string filePrefix = TeGetName(mifFileName.c_str());
 
91
        string ftest = filePrefix + ".mid";
 
92
        if (access(ftest.c_str(),04) == -1)
 
93
        {
 
94
                ftest = filePrefix + ".MID";
 
95
                if (access(ftest.c_str(),04) == -1)
 
96
                        return 0;
 
97
        }
 
98
 
 
99
        ftest = filePrefix + ".mif";
 
100
        if (access(ftest.c_str(),04) == -1)
 
101
        {
 
102
                ftest = filePrefix + ".MIF";
 
103
                if (access(ftest.c_str(),04) == -1)
 
104
                        return 0;
 
105
        }
 
106
 
 
107
        // found a valid layer name
 
108
        string lName;
 
109
        string baseName = TeGetBaseName(mifFileName.c_str());
 
110
        if (layerName.empty())
 
111
                lName = baseName;
 
112
        else
 
113
                lName = layerName;
 
114
 
 
115
        string newLayerName = lName;
 
116
        TeLayerMap& layerMap = db->layerMap();
 
117
        TeLayerMap::iterator it;
 
118
        bool flag = true;
 
119
        int n = 1;
 
120
        while (flag)
 
121
        {
 
122
                for (it = layerMap.begin(); it != layerMap.end(); ++it)
 
123
                {
 
124
                        if (TeStringCompare(it->second->name(),newLayerName))
 
125
                                break;
 
126
                }
 
127
                if (it == layerMap.end())
 
128
                        flag = 0;
 
129
                else
 
130
                        newLayerName = lName + "_" +Te2String(n);
 
131
                n++;    
 
132
        }
 
133
 
 
134
        // find projection
 
135
        TeProjection* mifProj = TeReadMIFProjection(mifFileName);
 
136
        TeLayer* newLayer = new TeLayer(newLayerName,db,mifProj);
 
137
        if (newLayer->id() <= 0 )
 
138
                return 0;                               // couldnļæ½t create new layer
 
139
 
 
140
        bool res = TeImportMIF(newLayer,mifFileName);
 
141
        delete mifProj;
 
142
        if (res)
 
143
                return newLayer;
 
144
        else
 
145
        {
 
146
                db->deleteLayer(newLayer->id());
 
147
                delete newLayer;
 
148
                return 0;
 
149
        }
 
150
}
 
151
 
 
152
bool 
 
153
TeImportMIF(TeLayer* layer, const string& mifFileName, string attrTableName, 
 
154
                        string objectIdAttr, unsigned int chunkSize)
 
155
{
 
156
        // check if format is complete (MID and MIF files exist)
 
157
        string filePrefix = TeGetName(mifFileName.c_str());
 
158
        string ftest = filePrefix + ".mid";
 
159
        if (access(ftest.c_str(),04) == -1)
 
160
        {
 
161
                ftest = filePrefix + ".MID";
 
162
                if (access(ftest.c_str(),04) == -1)
 
163
                        return false;
 
164
        }
 
165
 
 
166
        ftest = filePrefix + ".mif";
 
167
        if (access(ftest.c_str(),04) == -1)
 
168
        {
 
169
                ftest = filePrefix + ".MIF";
 
170
                if (access(ftest.c_str(),04) == -1)
 
171
                        return false;
 
172
        }
 
173
        
 
174
 
 
175
        //-- Step 1: Read any adicional information (ex. projection) 
 
176
        if (!layer->projection())
 
177
        {
 
178
                TeProjection* mifProj = TeReadMIFProjection(mifFileName);
 
179
                if (mifProj)
 
180
                        layer->setProjection(mifProj);
 
181
        }
 
182
 
 
183
        char separator = TeReadMIFSeparator(mifFileName);
 
184
 
 
185
        //read the attribute list information 
 
186
        TeAttributeList attList;
 
187
        TeReadMIFAttributeList(mifFileName, attList);
 
188
 
 
189
        // define a TeAttributeTable
 
190
        if (attrTableName.empty())
 
191
        {
 
192
                if (layer->name().empty())
 
193
                        return false;
 
194
                else
 
195
                        attrTableName = layer->name();
 
196
        }
 
197
        string aux = attrTableName;
 
198
        int c = 1;
 
199
        while (layer->database()->tableExist(aux))
 
200
        {
 
201
                aux = attrTableName + "_" + Te2String(c);
 
202
                ++c;
 
203
        }
 
204
        attrTableName = aux;
 
205
        
 
206
        int linkCol=-1, j=0;
 
207
        bool autoIndex = false;
 
208
 
 
209
        // if no geometry link name is given, creates one called 'object_id'
 
210
        if (objectIdAttr.empty())
 
211
        {
 
212
                objectIdAttr = "object_id_"+Te2String(layer->id());
 
213
                TeAttribute at;
 
214
                at.rep_.type_ = TeSTRING;
 
215
                at.rep_.numChar_ = 16;
 
216
                at.rep_.name_ = objectIdAttr;
 
217
                at.rep_.isPrimaryKey_ = true;
 
218
                attList.push_back(at);
 
219
                autoIndex = true;
 
220
        }
 
221
        else
 
222
        {
 
223
                // check if given index is valid
 
224
                TeAttributeList::iterator it = attList.begin();
 
225
                while (it != attList.end())
 
226
                {
 
227
                        if (TeConvertToUpperCase((*it).rep_.name_) == TeConvertToUpperCase(objectIdAttr))               // index found
 
228
                        {
 
229
                                if ((*it).rep_.type_ != TeSTRING)       // make sure it is a String type
 
230
                                {
 
231
                                        (*it).rep_.type_ = TeSTRING;
 
232
                                        (*it).rep_.numChar_ = 16;
 
233
                                }
 
234
                                linkCol=j;
 
235
                                (*it).rep_.isPrimaryKey_ = true;
 
236
                                break;
 
237
                        }
 
238
                        it++;
 
239
                        j++;
 
240
                }
 
241
                if (it == attList.end())                        // index not found
 
242
                {
 
243
                        TeAttribute at;
 
244
                        at.rep_.type_ = TeSTRING;
 
245
                        at.rep_.numChar_ = 16;
 
246
                        at.rep_.name_ = objectIdAttr;
 
247
                        at.rep_.isPrimaryKey_ = true;
 
248
                        attList.push_back(at);
 
249
                        autoIndex = true;
 
250
                }
 
251
        }
 
252
 
 
253
        if (autoIndex)
 
254
                linkCol = attList.size()-1;
 
255
 
 
256
        TeTable attTable (attrTableName,attList,objectIdAttr,objectIdAttr,TeAttrStatic);
 
257
        attTable.setSeparator(separator);
 
258
 
 
259
        // insert the table into the database
 
260
        if (!layer->createAttributeTable(attTable))
 
261
                return false;
 
262
 
 
263
        vector<string> objectIds;
 
264
 
 
265
        string midFileName = filePrefix + ".mid";
 
266
 
 
267
        int ncol = attList.size();
 
268
        if (autoIndex)
 
269
                ncol--;
 
270
 
 
271
        TeAsciiFile midFile(midFileName);
 
272
        int delta = 0;
 
273
        while (TeReadMIFAttributes(midFile,attTable,ncol,chunkSize,delta, autoIndex))
 
274
        {
 
275
                for (unsigned int n=0; n<attTable.size();++n)
 
276
                        objectIds.push_back(attTable.operator ()(n,linkCol));
 
277
 
 
278
                // save table
 
279
                if (!layer->saveAttributeTable( attTable ))
 
280
                {
 
281
                        attTable.clear();
 
282
                        break;
 
283
                }
 
284
                delta += attTable.size();
 
285
                attTable.clear();
 
286
        }
 
287
 
 
288
        // Import the geometries
 
289
        bool res = TeImportMIFGeometries(layer,mifFileName,objectIds,chunkSize);
 
290
        return res;
 
291
}
 
292
 
 
293
bool TeReadMIFAttributes(TeAsciiFile& midFile, TeTable& attTable, 
 
294
                                                 int ncolumns, int nrecords, int delta, bool createAuto )
 
295
{
 
296
        if (!midFile.isNotAtEOF() || nrecords <=0 || ncolumns <= 0)
 
297
                return false;
 
298
        char separator = attTable.separator();
 
299
 
 
300
        int count;
 
301
        for (count=0; count<nrecords; count++)
 
302
        {
 
303
                if (!midFile.isNotAtEOF())
 
304
                         break;
 
305
 
 
306
                TeTableRow row;
 
307
                for (int n=0; n<ncolumns; n++)
 
308
                {
 
309
                        string value;
 
310
                        try 
 
311
                        {
 
312
                                value = midFile.readStringCSVNoQuote(separator);
 
313
                        }
 
314
                        catch(...)
 
315
                        {
 
316
                                if (count > 0)
 
317
                                        return true;
 
318
                                else
 
319
                                        return false;
 
320
                        }
 
321
                        row.push_back ( value ); 
 
322
                }
 
323
 
 
324
                if (createAuto)
 
325
                        row.push_back(Te2String(count+delta));
 
326
 
 
327
                attTable.add(row);
 
328
                midFile.findNewLine();
 
329
        }
 
330
        return true;
 
331
}
 
332
 
 
333
char 
 
334
TeReadMIFSeparator(const string& mifFileName)
 
335
{
 
336
        string filePrefix = TeGetName (mifFileName.c_str()) ;
 
337
        string fileName = filePrefix + ".mif";
 
338
 
 
339
        TeAsciiFile mifFile(fileName);
 
340
        string name = mifFile.readString ();
 
341
 
 
342
        while ( mifFile.isNotAtEOF() && !TeStringCompare(name,"Delimiter"))
 
343
                name = mifFile.readString ();
 
344
 
 
345
        if (mifFile.isNotAtEOF())
 
346
                return mifFile.readQuotedChar();
 
347
        else 
 
348
                return '\t';
 
349
}
 
350
 
 
351
 
 
352
TeProjection* 
 
353
TeReadMIFProjection(const string& mifFileName)
 
354
{
 
355
        TeProjection* proj = 0;
 
356
 
 
357
        string filePrefix = TeGetName (mifFileName.c_str()) ;
 
358
        string fileName = filePrefix + ".mif";
 
359
 
 
360
        TeAsciiFile mifFile(fileName);
 
361
        string name = mifFile.readString ();
 
362
 
 
363
        while ( mifFile.isNotAtEOF() && !TeStringCompare(name,"CoordSys")
 
364
                                         && !TeStringCompare(name,"Columns"))
 
365
                name = mifFile.readString ();
 
366
        
 
367
        if (mifFile.isNotAtEOF() && TeStringCompare(name,"CoordSys"))  
 
368
        {
 
369
                string option = mifFile.readString();
 
370
 
 
371
                vector<string> argList;
 
372
 
 
373
                if (TeStringCompare(option,"Earth"))    // expect Projection clause
 
374
                {
 
375
                        string projRef =  mifFile.readString ();
 
376
                        if (!TeStringCompare(projRef,"Projection"))
 
377
                        {
 
378
                                proj = new TeNoProjection();
 
379
                                return proj;
 
380
                        }
 
381
                        mifFile.readStringListCSV(argList,',');
 
382
                        proj = TeMIFProjectionFactory::make ( argList );        // create projection from parameters
 
383
                }
 
384
                else if (TeStringCompare(option,"Nonearth"))    // non earth
 
385
                {
 
386
                        // look for units specification
 
387
                        while (mifFile.isNotAtEOF() && !TeStringCompare(name,"Units")
 
388
                                                && !TeStringCompare(name,"Columns"))
 
389
                                name = mifFile.readString ();
 
390
                        if (TeStringCompare(name,"Units"))
 
391
                        {
 
392
                                name = mifFile.readString();
 
393
                                proj = new TeNoProjection(name);
 
394
                        }
 
395
                        else
 
396
                                proj = new TeNoProjection(name);
 
397
                }
 
398
        }
 
399
        else    // no CoordSys clause specified 
 
400
        {               // it should be assumed that the data is stored in Lat Long
 
401
                TeDatum  datum = TeDatumFactory::make("Spherical");
 
402
                proj = new TeLatLong(datum);
 
403
        }
 
404
        return proj;
 
405
}
 
406
 
 
407
void
 
408
TeReadMIFAttributeList ( const string& mifFileName, TeAttributeList& attList )
 
409
{
 
410
        string filePrefix = TeGetName (mifFileName.c_str()) ;
 
411
        string fileName = filePrefix + ".mif";
 
412
 
 
413
        TeAsciiFile mifFile(fileName);
 
414
        string name = mifFile.readString ();
 
415
        
 
416
        while ( mifFile.isNotAtEOF() && !TeStringCompare(name,"Columns"))
 
417
                name = mifFile.readString ();
 
418
 
 
419
        if (mifFile.isNotAtEOF()) 
 
420
        {
 
421
                int ncol = mifFile.readInt();
 
422
                mifFile.findNewLine();
 
423
                TeAttribute attribute;
 
424
 
 
425
                for ( int i = 0; i< ncol; i++ )
 
426
                {
 
427
                        attribute.rep_.name_  = mifFile.readString();
 
428
                        string attType = mifFile.readStringCSVNoSpace ('(');
 
429
                        if ( TeStringCompare(attType,"Integer") || TeStringCompare(attType,"Smallint") 
 
430
                                 || TeStringCompare(attType,"Logical") )
 
431
                        {
 
432
                                attribute.rep_.type_ = TeINT;
 
433
                        }
 
434
                        else if (TeStringCompare(attType,"Float"))
 
435
                        {
 
436
                                attribute.rep_.type_ = TeREAL;
 
437
                                attribute.rep_.numChar_ = 20;
 
438
                                attribute.rep_.decimals_ = 1;
 
439
                        }
 
440
                        else if (TeStringCompare(attType,"Date"))
 
441
                        {
 
442
                                attribute.rep_.type_ = TeDATETIME;
 
443
                                attribute.dateTimeFormat_="YYYYMMDD";
 
444
                        }
 
445
                        else if (TeStringCompare(attType,"Decimal"))
 
446
                        {
 
447
                                attribute.rep_.type_  = TeREAL;
 
448
                                attribute.rep_.numChar_ = mifFile.readIntCSV();
 
449
                                attribute.rep_.decimals_ = mifFile.readIntCSV(')');
 
450
                        }
 
451
                        else
 
452
                        {
 
453
                                attribute.rep_.type_  = TeSTRING;
 
454
                                attribute.rep_.numChar_  = mifFile.readIntCSV(')');
 
455
                        }
 
456
                        attList.push_back ( attribute );
 
457
                        mifFile.findNewLine();
 
458
                }
 
459
        }
 
460
}
 
461
 
 
462
bool
 
463
TeImportMIFGeometries(TeLayer* layer,const string& mifFileName, 
 
464
                                          vector<string> &indexes, unsigned int chunckSize)
 
465
{
 
466
 
 
467
        string filePrefix = TeGetName (mifFileName.c_str()) ;
 
468
        string fileName = filePrefix + ".mif";
 
469
 
 
470
        TePointSet pointSet;
 
471
        TeLineSet lineSet;
 
472
        TePolygonSet polySet;
 
473
        TeTextSet textSet;
 
474
        string textTableName = layer->name() + "Texto";
 
475
 
 
476
        try
 
477
        {
 
478
                TeAsciiFile mifFile(fileName);
 
479
                int n = 0;
 
480
                while ( mifFile.isNotAtEOF()  )
 
481
                {
 
482
                        string name = mifFile.readString ();
 
483
                        if (TeStringCompare(name,"Region"))
 
484
                        {
 
485
                                TeMIFRegionDecode ( mifFile, polySet, indexes[n]);
 
486
                                n++;
 
487
                                if ( polySet.size() == chunckSize )
 
488
                                {
 
489
                                        layer->addPolygons( polySet ); 
 
490
                                        polySet.clear();
 
491
                                }
 
492
                        } 
 
493
                        else if (TeStringCompare(name,"Pline"))
 
494
                        {
 
495
                                TeMIFPlineDecode ( mifFile, lineSet, indexes[n] );
 
496
                                n++;
 
497
                                if ( lineSet.size() == chunckSize )
 
498
                                {
 
499
                                        layer->addLines( lineSet ); 
 
500
                                        lineSet.clear();
 
501
                                }
 
502
                        }
 
503
                        else if (TeStringCompare(name,"Line"))
 
504
                        {
 
505
                                TeMIFLineDecode ( mifFile, lineSet, indexes[n] );
 
506
                                n++;
 
507
                                if ( lineSet.size() == chunckSize )
 
508
                                {
 
509
                                        layer->addLines( lineSet ); 
 
510
                                        lineSet.clear();
 
511
                                }
 
512
                        }
 
513
                        else if (TeStringCompare(name,"Point"))
 
514
                        {
 
515
                                TeMIFPointDecode ( mifFile, pointSet, indexes[n] );
 
516
                                n++;
 
517
                                if ( pointSet.size() == chunckSize )
 
518
                                {
 
519
                                        layer->addPoints( pointSet ); 
 
520
                                        pointSet.clear();
 
521
                                }
 
522
                        }
 
523
                        else if (TeStringCompare(name,"Multipoint"))
 
524
                        {
 
525
                                TeMIFMultiPointDecode ( mifFile, pointSet, indexes[n] );
 
526
                                n++;
 
527
                                if ( pointSet.size() == chunckSize )
 
528
                                {
 
529
                                        layer->addPoints( pointSet ); 
 
530
                                        pointSet.clear();
 
531
                                }
 
532
                        }
 
533
                        else if (TeStringCompare(name,"Text"))
 
534
                        {
 
535
                                TeMIFTextDecode ( mifFile, textSet,indexes[n]);
 
536
                                n++;
 
537
                                if ( textSet.size() == chunckSize )
 
538
                                {
 
539
                                        layer->addText( textSet,textTableName ); 
 
540
                                        textSet.clear();
 
541
                                }
 
542
                        }
 
543
                        else if (TeStringCompare(name,"Rect"))
 
544
                        {
 
545
                                TeMIFRectDecode(mifFile,polySet,indexes[n]);
 
546
                                n++;
 
547
                                if (polySet.size() == chunckSize )
 
548
                                {
 
549
                                        layer->addPolygons(polySet); 
 
550
                                        polySet.clear();
 
551
                                }
 
552
                        }
 
553
                        else if (TeStringCompare(name,"Collection"))
 
554
                        {
 
555
                                TeMIFCollectionDecode(mifFile,pointSet,lineSet, polySet,indexes[n]);
 
556
                                n++;
 
557
                                if (lineSet.size() == chunckSize )
 
558
                                {
 
559
                                        layer->addLines(lineSet); 
 
560
                                        lineSet.clear();
 
561
                                }
 
562
                                if (polySet.size() == chunckSize )
 
563
                                {
 
564
                                        layer->addPolygons(polySet); 
 
565
                                        polySet.clear();
 
566
                                }
 
567
                                if (pointSet.size() == chunckSize )
 
568
                                {
 
569
                                        layer->addPoints(pointSet); 
 
570
                                        pointSet.clear();
 
571
                                }
 
572
                        }
 
573
                        else if (TeStringCompare(name,"Transform"))
 
574
                                TeMIFTransformDecode ( mifFile );                       
 
575
                        else if (TeStringCompare(name,"NONE"))
 
576
                                n++;
 
577
                        else
 
578
                                TeMIFOthersDecode ( mifFile );
 
579
                }
 
580
                
 
581
                // save the remaining geometries
 
582
                if (pointSet.size() > 0 )
 
583
                {
 
584
                        layer->addPoints( pointSet ); 
 
585
                        pointSet.clear();
 
586
                }
 
587
 
 
588
                if (lineSet.size() > 0 )
 
589
                {
 
590
                        layer->addLines( lineSet ); 
 
591
                        lineSet.clear();
 
592
                }
 
593
 
 
594
                if (polySet.size() > 0 )
 
595
                {
 
596
                        layer->addPolygons( polySet ); 
 
597
                        polySet.clear();
 
598
                }
 
599
                
 
600
                if (textSet.size() > 0)
 
601
                {
 
602
                        layer->addText( textSet,textTableName ); 
 
603
                        textSet.clear();
 
604
                }
 
605
        }
 
606
        catch(...)
 
607
        {
 
608
                return false;
 
609
        }
 
610
        return true;
 
611
}
 
612
 
 
613
void 
 
614
TeMIFRectDecode (TeAsciiFile& mifFile, TePolygonSet& temp, string& index)
 
615
{
 
616
        if (index.empty())
 
617
                index = "te_nulo";
 
618
        TeCoord2D ll = mifFile.readCoord2D();
 
619
        TeCoord2D ur = mifFile.readCoord2D();
 
620
 
 
621
        TeBox rec(ll.x_,ll.y_,ur.x_,ur.y_);
 
622
        TePolygon pol = polygonFromBox(rec);
 
623
        pol.objectId(index);
 
624
        temp.add(pol);
 
625
}
 
626
 
 
627
void
 
628
TeMIFRegionDecode ( TeAsciiFile& mifFile, TePolygonSet& temp, string& index)
 
629
{
 
630
        if (index.empty())
 
631
                index = "te_nulo";
 
632
 
 
633
        int npolyg = mifFile.readInt();
 
634
        mifFile.findNewLine();
 
635
 
 
636
        TeCoord2D pt;
 
637
        TePolygon pol; 
 
638
        typedef list<TePolygon> PolyList;
 
639
        PolyList pList;
 
640
        for ( int i = 0; i < npolyg; i++ )
 
641
        {
 
642
                TeLine2D line;
 
643
 
 
644
                int ncoord = mifFile.readInt();
 
645
                mifFile.findNewLine();
 
646
 
 
647
                for ( int j = 0; j < ncoord; j++ )
 
648
                {
 
649
                        pt = mifFile.readCoord2D();
 
650
                        pt.scale( glXmulti, glYmulti);
 
651
                        pt += glAdd;
 
652
                        mifFile.findNewLine();
 
653
                        line.add ( pt );
 
654
 
 
655
                }
 
656
                if ( line.size() <= 3 ) continue; // to avoid dangling rings
 
657
                if ( !line.isRing() )           //close the ring
 
658
                {
 
659
                        line.add(line[0]);
 
660
//                      throw TeException ( MIF_REGION_CLOSE );
 
661
                }
 
662
 
 
663
                TeLinearRing ring (line);
 
664
                
 
665
                bool inside = false;
 
666
 
 
667
                PolyList::iterator it = pList.begin();
 
668
 
 
669
                while ( it != pList.end() )
 
670
                {
 
671
                        TePolygon p1;
 
672
                        p1.add(ring);
 
673
                        TePolygon p2 = (*it);
 
674
                        if (TeWithin(p1,p2))
 
675
                        {
 
676
                                inside = true;
 
677
                                ring.objectId(p2[0].objectId());
 
678
                                p2.add ( ring );// add a closed region
 
679
                        }
 
680
                        ++it;
 
681
                }
 
682
                if ( !inside )
 
683
                {
 
684
                        TePolygon poly;
 
685
                        ring.objectId(index);
 
686
                        poly.add ( ring ); // add an outer region
 
687
                        poly.objectId(index);
 
688
                        pList.push_back ( poly );
 
689
                }
 
690
        }
 
691
 
 
692
        PolyList::iterator it = pList.begin();
 
693
        while ( it != pList.end() )
 
694
        {
 
695
                temp.add ( *it ); // add a polygon to a layer
 
696
                ++it;
 
697
        }
 
698
}       
 
699
 
 
700
void
 
701
TeMIFLineDecode (TeAsciiFile& mifFile, TeLineSet& temp, string& index)
 
702
{
 
703
        if (index.empty())
 
704
                index = "te_nulo";
 
705
 
 
706
        TeLine2D line;
 
707
        TeCoord2D pt;
 
708
 
 
709
        for ( int i= 0; i < 2; i++ )
 
710
        {
 
711
                pt = mifFile.readCoord2D();
 
712
                pt.scale( glXmulti, glYmulti);
 
713
                pt += glAdd;
 
714
                line.add ( pt );
 
715
        }
 
716
 
 
717
        line.objectId( index );
 
718
        temp.add ( line );
 
719
        mifFile.findNewLine();
 
720
}
 
721
 
 
722
void
 
723
TeMIFPlineDecode (TeAsciiFile& mifFile, TeLineSet& temp, string& index)
 
724
{
 
725
        if (index.empty())
 
726
                index = "te_nulo";
 
727
 
 
728
        int numSections = 1; 
 
729
        int ncoord = 0;
 
730
 
 
731
        // Read the Pline parameters
 
732
        vector<string>  strList;
 
733
        mifFile.readStringList ( strList );
 
734
        
 
735
        // Are we dealing with MULTIPLE sections ??
 
736
        // If there are two parameters for the pline,
 
737
        // the first MUST be a "Multiple" keyword
 
738
        
 
739
        bool hasMultiple = false;
 
740
 
 
741
        if ( strList.size() == 2 )
 
742
        {
 
743
                hasMultiple = true;
 
744
                ensure (TeStringCompare(strList[0],"Multiple"));
 
745
                numSections = atoi ( strList[1].c_str() );
 
746
        }
 
747
        else
 
748
                ncoord = atoi ( strList[0].c_str());
 
749
 
 
750
        mifFile.findNewLine(); // go to the next line
 
751
        // If there is a single PLine, there is a single
 
752
        // section ( numSections = 1 ) and will read
 
753
        // the number of points within the loop
 
754
 
 
755
 
 
756
        for ( int i= 0;  i < numSections; i++ )
 
757
        {
 
758
                TeLine2D line;  
 
759
                TeCoord2D pt;
 
760
                if ( hasMultiple == true )
 
761
                {
 
762
                        ncoord = mifFile.readInt();
 
763
                        mifFile.findNewLine(); // go to the next line
 
764
                }
 
765
 
 
766
                for ( int j = 0; j < ncoord; j++ )
 
767
                {
 
768
                        pt = mifFile.readCoord2D();
 
769
                        pt.scale( glXmulti, glYmulti);
 
770
                        pt += glAdd;
 
771
                        mifFile.findNewLine();
 
772
                        line.add ( pt );
 
773
                }
 
774
                line.objectId( index );
 
775
                temp.add ( line );
 
776
        }
 
777
}
 
778
 
 
779
void
 
780
TeMIFPointDecode (TeAsciiFile& mifFile, TePointSet& temp, string& index)
 
781
{
 
782
        if (index.empty())
 
783
                index = "te_nulo";
 
784
 
 
785
        TePoint point;
 
786
        
 
787
        // Read the coordinates
 
788
 
 
789
        TeCoord2D pt = mifFile.readCoord2D();
 
790
        pt.scale( glXmulti, glYmulti);
 
791
        pt += glAdd;
 
792
        point.add ( pt );
 
793
        point.objectId( index );
 
794
 
 
795
        // Add a point to the Point Set
 
796
                        
 
797
        temp.add ( point );
 
798
 
 
799
        mifFile.findNewLine();
 
800
}
 
801
 
 
802
void
 
803
TeMIFMultiPointDecode (TeAsciiFile& mifFile, TePointSet& temp, string& index)
 
804
{
 
805
        if (index.empty())
 
806
                index = "te_nulo";
 
807
 
 
808
        int     npts = mifFile.readInt();
 
809
        mifFile.findNewLine();
 
810
        for (int i=0; i<npts;i++)
 
811
        {
 
812
                // Read points
 
813
                TePoint point;
 
814
                TeCoord2D pt = mifFile.readCoord2D();
 
815
                pt.scale( glXmulti, glYmulti);
 
816
                pt += glAdd;
 
817
                point.add ( pt );
 
818
                point.objectId( index );
 
819
 
 
820
                // Add a point to the Point Set         
 
821
                temp.add ( point );
 
822
        }
 
823
        mifFile.findNewLine();
 
824
}
 
825
 
 
826
void
 
827
TeMIFTransformDecode ( TeAsciiFile& mifFile )
 
828
{
 
829
        // read the tansformation params
 
830
 
 
831
        double param = mifFile.readFloatCSV();
 
832
 
 
833
        if ( param != 0. )
 
834
                glXmulti = param;
 
835
 
 
836
        param = mifFile.readFloatCSV();
 
837
 
 
838
        if ( param != 0. )
 
839
                glYmulti = param;
 
840
 
 
841
        double x = mifFile.readFloatCSV();
 
842
 
 
843
        double y  = mifFile.readFloatCSV();
 
844
 
 
845
        TeCoord2D pt ( x, y );
 
846
 
 
847
        glAdd =  pt;
 
848
        
 
849
        // Go to the next line
 
850
        mifFile.findNewLine ();
 
851
        
 
852
}
 
853
 
 
854
void
 
855
TeMIFTextDecode (TeAsciiFile& mifFile, TeTextSet& temp, string& index)
 
856
{
 
857
        string txt = mifFile.readQuotedString(); // read the text string
 
858
        mifFile.findNewLine ();
 
859
        TeBox box = mifFile.readBox();                  // read the text box 
 
860
 
 
861
        TeCoord2D lowerLeft = box.lowerLeft();
 
862
        TeText text(lowerLeft,txt);
 
863
        text.setBox(box);
 
864
        text.objectId(index);
 
865
        text.setHeight(box.height());
 
866
        temp.add(text);
 
867
        mifFile.findNewLine ();                                 // go to the next line
 
868
}
 
869
 
 
870
void
 
871
TeMIFOthersDecode ( TeAsciiFile& mifFile )
 
872
{
 
873
        // Simply go to the next line
 
874
        mifFile.findNewLine ();
 
875
}
 
876
 
 
877
void 
 
878
TeMIFCollectionDecode(TeAsciiFile& mifFile, TePointSet& ps, TeLineSet& ls, TePolygonSet& pols, string& index)
 
879
{
 
880
        int     nparts = mifFile.readInt();
 
881
        if (nparts == 0)
 
882
                nparts = 3;
 
883
        mifFile.findNewLine();
 
884
        for (int i=0; i<nparts; i++)
 
885
        {
 
886
                string part = mifFile.readString ();
 
887
                if (TeStringCompare(part,"Region"))
 
888
                        TeMIFRegionDecode (mifFile, pols, index);
 
889
                else if(TeStringCompare(part,"Pline"))
 
890
                        TeMIFPlineDecode (mifFile, ls, index);
 
891
                else if (TeStringCompare(part,"Multipoint"))
 
892
                        TeMIFMultiPointDecode (mifFile,ps, index);
 
893
        }
 
894
}