Line data Source code
1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: SDTS Translator
5 : * Purpose: Include file for entire SDTS Abstraction Layer functions.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef SDTS_AL_H_INCLUDED
31 : #define SDTS_AL_H_INCLUDED
32 :
33 : #include "cpl_conv.h"
34 : #include "iso8211.h"
35 :
36 : class SDTS_IREF;
37 : class SDTSModId;
38 : class SDTSTransfer;
39 :
40 : #define SDTS_SIZEOF_SADR 8
41 :
42 : char **SDTSScanModuleReferences(DDFModule *, const char *);
43 :
44 : /************************************************************************/
45 : /* SDTS_IREF */
46 : /************************************************************************/
47 :
48 : /**
49 : Class holding SDTS IREF (internal reference) information, internal
50 : coordinate system format, scaling and resolution. This object isn't
51 : normally needed by applications.
52 : */
53 : class SDTS_IREF
54 : {
55 : int nDefaultSADRFormat;
56 :
57 : public:
58 : SDTS_IREF();
59 : ~SDTS_IREF();
60 :
61 : int Read(const char *pszFilename);
62 :
63 : char *pszXAxisName; /* XLBL */
64 : char *pszYAxisName; /* YLBL */
65 :
66 : double dfXScale; /* SFAX */
67 : double dfYScale; /* SFAY */
68 :
69 : double dfXOffset; /* XORG */
70 : double dfYOffset; /* YORG */
71 :
72 : double dfXRes; /* XHRS */
73 : double dfYRes; /* YHRS */
74 :
75 : char *pszCoordinateFormat; /* HFMT */
76 :
77 : int GetSADRCount(DDFField *) const;
78 : int GetSADR(DDFField *, int, double *, double *, double *);
79 : };
80 :
81 : /************************************************************************/
82 : /* SDTS_XREF */
83 : /************************************************************************/
84 :
85 : /**
86 : Class for reading the XREF (external reference) module containing the
87 : data projection definition.
88 : */
89 :
90 : class SDTS_XREF
91 : {
92 : public:
93 : SDTS_XREF();
94 : ~SDTS_XREF();
95 :
96 : int Read(const char *pszFilename);
97 :
98 : /** Projection system name, from the RSNM field. One of GEO, SPCS, UTM,
99 : UPS, OTHR, UNSP. */
100 : char *pszSystemName;
101 :
102 : /** Horizontal datum name, from the HDAT field. One of NAS, NAX, WGA,
103 : WGB, WGC, WGE. */
104 : char *pszDatum;
105 :
106 : /** Zone number for UTM and SPCS projections, from the ZONE field. */
107 : int nZone;
108 : };
109 :
110 : /************************************************************************/
111 : /* SDTS_CATD */
112 : /************************************************************************/
113 : class SDTS_CATDEntry;
114 :
115 : /**
116 : List of feature layer types. See SDTSTransfer::GetLayerType().
117 : */
118 :
119 : typedef enum
120 : {
121 : SLTUnknown,
122 : SLTPoint,
123 : SLTLine,
124 : SLTAttr,
125 : SLTPoly,
126 : SLTRaster
127 : } SDTSLayerType;
128 :
129 : /**
130 : Class for accessing the CATD (Catalog Directory) file containing a list of
131 : all other files (modules) in the transfer.
132 : */
133 : class SDTS_CATD
134 : {
135 : char *pszPrefixPath;
136 :
137 : int nEntries;
138 : SDTS_CATDEntry **papoEntries;
139 :
140 : public:
141 : SDTS_CATD();
142 : ~SDTS_CATD();
143 :
144 : int Read(const char *pszFilename);
145 :
146 : const char *GetModuleFilePath(const char *pszModule) const;
147 :
148 69 : int GetEntryCount() const
149 : {
150 69 : return nEntries;
151 : }
152 :
153 : const char *GetEntryModule(int) const;
154 : const char *GetEntryTypeDesc(int) const;
155 : const char *GetEntryFilePath(int) const;
156 : SDTSLayerType GetEntryType(int) const;
157 : void SetEntryTypeUnknown(int);
158 : };
159 :
160 : /************************************************************************/
161 : /* SDTSModId */
162 : /************************************************************************/
163 :
164 : /**
165 : Object representing a unique module/record identifier within an SDTS
166 : transfer.
167 : */
168 : class SDTSModId
169 : {
170 : public:
171 1290 : SDTSModId()
172 1290 : {
173 1290 : szModule[0] = '\0';
174 1290 : nRecord = -1;
175 1290 : szOBRP[0] = '\0';
176 1290 : szName[0] = '\0';
177 1290 : }
178 :
179 : int Set(DDFField *);
180 :
181 : const char *GetName();
182 :
183 : /** The module name, such as PC01, containing the indicated record. */
184 : char szModule[8];
185 :
186 : /** The record within the module referred to. This is -1 for unused
187 : SDTSModIds. */
188 : int nRecord;
189 :
190 : /** The "role" of this record within the module. This is normally empty
191 : for references, but set in the oModId member of a feature. */
192 : char szOBRP[8];
193 :
194 : /** String "szModule:nRecord" */
195 : char szName[20];
196 : };
197 :
198 : /************************************************************************/
199 : /* SDTSFeature */
200 : /************************************************************************/
201 :
202 : /**
203 : Base class for various SDTS features classes, providing a generic
204 : module identifier, and list of attribute references.
205 : */
206 : class SDTSFeature
207 : {
208 : public:
209 : SDTSFeature();
210 : virtual ~SDTSFeature();
211 :
212 : /** Unique identifier for this record/feature within transfer. */
213 : SDTSModId oModId;
214 :
215 : /** Number of attribute links (aoATID[]) on this feature. */
216 : int nAttributes;
217 :
218 : /** List of nAttributes attribute record identifiers related to this
219 : feature. */
220 : SDTSModId *paoATID;
221 :
222 : void ApplyATID(DDFField *);
223 :
224 : /** Dump readable description of feature to indicated stream. */
225 : virtual void Dump(FILE *) = 0;
226 : };
227 :
228 : /************************************************************************/
229 : /* SDTSIndexedReader */
230 : /************************************************************************/
231 :
232 : /**
233 : Base class for all the SDTSFeature type readers. Provides feature
234 : caching semantics and fetching based on a record number.
235 : */
236 :
237 : class SDTSIndexedReader
238 : {
239 : int nIndexSize;
240 : SDTSFeature **papoFeatures;
241 :
242 : int iCurrentFeature;
243 :
244 : protected:
245 : DDFModule oDDFModule;
246 :
247 : public:
248 : SDTSIndexedReader();
249 : virtual ~SDTSIndexedReader();
250 :
251 : virtual SDTSFeature *GetNextRawFeature() = 0;
252 :
253 : SDTSFeature *GetNextFeature();
254 :
255 : virtual void Rewind();
256 :
257 : void FillIndex();
258 : void ClearIndex();
259 : int IsIndexed() const;
260 :
261 : SDTSFeature *GetIndexedFeatureRef(int);
262 : char **ScanModuleReferences(const char * = "ATID");
263 :
264 4 : DDFModule *GetModule()
265 : {
266 4 : return &oDDFModule;
267 : }
268 : };
269 :
270 : /************************************************************************/
271 : /* SDTSRawLine */
272 : /************************************************************************/
273 :
274 : /** SDTS line feature, as read from LE* modules by SDTSLineReader. */
275 :
276 : class SDTSRawLine : public SDTSFeature
277 : {
278 : public:
279 : SDTSRawLine();
280 : virtual ~SDTSRawLine();
281 :
282 : int Read(SDTS_IREF *, DDFRecord *);
283 :
284 : /** Number of vertices in the padfX, padfY and padfZ arrays. */
285 : int nVertices;
286 :
287 : /** List of nVertices X coordinates. */
288 : double *padfX;
289 : /** List of nVertices Y coordinates. */
290 : double *padfY;
291 : /** List of nVertices Z coordinates - currently always zero. */
292 : double *padfZ;
293 :
294 : /** Identifier of polygon to left of this line. This is the SDTS PIDL
295 : subfield. */
296 : SDTSModId oLeftPoly;
297 :
298 : /** Identifier of polygon to right of this line. This is the SDTS PIDR
299 : subfield. */
300 : SDTSModId oRightPoly;
301 :
302 : /** Identifier for the start node of this line. This is the SDTS SNID
303 : subfield. */
304 : SDTSModId oStartNode; /* SNID */
305 :
306 : /** Identifier for the end node of this line. This is the SDTS ENID
307 : subfield. */
308 : SDTSModId oEndNode; /* ENID */
309 :
310 : void Dump(FILE *) override;
311 : };
312 :
313 : /************************************************************************/
314 : /* SDTSLineReader */
315 : /* */
316 : /* Class for reading any of the files lines. */
317 : /************************************************************************/
318 :
319 : /**
320 : Reader for SDTS line modules.
321 :
322 : Returns SDTSRawLine features. Normally readers are instantiated with
323 : the SDTSTransfer::GetIndexedReader() method.
324 :
325 : */
326 :
327 : class SDTSLineReader : public SDTSIndexedReader
328 : {
329 : SDTS_IREF *poIREF;
330 :
331 : public:
332 : explicit SDTSLineReader(SDTS_IREF *);
333 : ~SDTSLineReader();
334 :
335 : int Open(const char *);
336 : SDTSRawLine *GetNextLine();
337 : void Close();
338 :
339 57 : SDTSFeature *GetNextRawFeature() override
340 : {
341 57 : return GetNextLine();
342 : }
343 :
344 : void AttachToPolygons(SDTSTransfer *, int iPolyLayer);
345 : };
346 :
347 : /************************************************************************/
348 : /* SDTSAttrRecord */
349 : /************************************************************************/
350 :
351 : /**
352 : SDTS attribute record feature, as read from A* modules by
353 : SDTSAttrReader.
354 :
355 : Note that even though SDTSAttrRecord is derived from SDTSFeature, there
356 : are never any attribute records associated with attribute records using
357 : the aoATID[] mechanism. SDTSFeature::nAttributes will always be zero.
358 : */
359 :
360 : class SDTSAttrRecord : public SDTSFeature
361 : {
362 : public:
363 : SDTSAttrRecord();
364 : virtual ~SDTSAttrRecord();
365 :
366 : /** The entire DDFRecord read from the file. */
367 : DDFRecord *poWholeRecord;
368 :
369 : /** The ATTR DDFField with the user attribute. Each subfield is a
370 : attribute value. */
371 :
372 : DDFField *poATTR;
373 :
374 : virtual void Dump(FILE *) override;
375 : };
376 :
377 : /************************************************************************/
378 : /* SDTSAttrReader */
379 : /************************************************************************/
380 :
381 : /**
382 : Class for reading SDTSAttrRecord features from a primary or secondary
383 : attribute module.
384 : */
385 :
386 : class SDTSAttrReader : public SDTSIndexedReader
387 : {
388 : int bIsSecondary;
389 :
390 : public:
391 : SDTSAttrReader();
392 : virtual ~SDTSAttrReader();
393 :
394 : int Open(const char *);
395 : DDFField *GetNextRecord(SDTSModId * = nullptr, DDFRecord ** = nullptr,
396 : int bDuplicate = FALSE);
397 : SDTSAttrRecord *GetNextAttrRecord();
398 : void Close();
399 :
400 : /**
401 : Returns TRUE if this is a Attribute Secondary layer rather than
402 : an Attribute Primary layer.
403 : */
404 : int IsSecondary() const
405 : {
406 : return bIsSecondary;
407 : }
408 :
409 357 : SDTSFeature *GetNextRawFeature() override
410 : {
411 357 : return GetNextAttrRecord();
412 : }
413 : };
414 :
415 : /************************************************************************/
416 : /* SDTSRawPoint */
417 : /************************************************************************/
418 :
419 : /**
420 : Object containing a point feature (type NA, NO or NP).
421 : */
422 : class SDTSRawPoint : public SDTSFeature
423 : {
424 : public:
425 : SDTSRawPoint();
426 : virtual ~SDTSRawPoint();
427 :
428 : int Read(SDTS_IREF *, DDFRecord *);
429 :
430 : /** X coordinate of point. */
431 : double dfX;
432 : /** Y coordinate of point. */
433 : double dfY;
434 : /** Z coordinate of point. */
435 : double dfZ;
436 :
437 : /** Optional identifier of area marked by this point (i.e. PC01:27). */
438 : SDTSModId oAreaId; /* ARID */
439 :
440 : virtual void Dump(FILE *) override;
441 : };
442 :
443 : /************************************************************************/
444 : /* SDTSPointReader */
445 : /************************************************************************/
446 :
447 : /**
448 : Class for reading SDTSRawPoint features from a point module (type NA, NO
449 : or NP).
450 : */
451 :
452 : class SDTSPointReader : public SDTSIndexedReader
453 : {
454 : SDTS_IREF *poIREF;
455 :
456 : public:
457 : explicit SDTSPointReader(SDTS_IREF *);
458 : virtual ~SDTSPointReader();
459 :
460 : int Open(const char *);
461 : SDTSRawPoint *GetNextPoint();
462 : void Close();
463 :
464 132 : SDTSFeature *GetNextRawFeature() override
465 : {
466 132 : return GetNextPoint();
467 : }
468 : };
469 :
470 : /************************************************************************/
471 : /* SDTSRawPolygon */
472 : /************************************************************************/
473 :
474 : /**
475 : Class for holding information about a polygon feature.
476 :
477 : When directly read from a polygon module, the polygon has no concept
478 : of its geometry. Just its ID, and references to attribute records.
479 : However, if the SDTSLineReader::AttachToPolygons() method is called on
480 : the module containing the lines forming the polygon boundaries, then the
481 : nEdges/papoEdges information on the SDTSRawPolygon will be filled in.
482 :
483 : Once this is complete the AssembleRings() method can be used to fill in the
484 : nRings/nVertices/panRingStart/padfX/padfY/padfZ information defining the
485 : ring geometry.
486 :
487 : Note that the rings may not appear in any particular order, nor with any
488 : meaningful direction (clockwise or counterclockwise).
489 : */
490 :
491 : class SDTSRawPolygon : public SDTSFeature
492 : {
493 : void AddEdgeToRing(int, double *, double *, double *, int, int);
494 :
495 : public:
496 : SDTSRawPolygon();
497 : virtual ~SDTSRawPolygon();
498 :
499 : int Read(DDFRecord *);
500 :
501 : int nEdges;
502 : SDTSRawLine **papoEdges;
503 :
504 : void AddEdge(SDTSRawLine *);
505 :
506 : /** This method will assemble the edges associated with a polygon into
507 : rings, returning FALSE if problems are encountered during assembly. */
508 : int AssembleRings();
509 :
510 : /** Number of rings in assembled polygon. */
511 : int nRings;
512 : /** Total number of vertices in all rings of assembled polygon. */
513 : int nVertices;
514 : /** Offsets into padfX/padfY/padfZ for the beginning of each ring in the
515 : polygon. This array is nRings long. */
516 : int *panRingStart;
517 :
518 : /** List of nVertices X coordinates for the polygon (split over multiple
519 : rings via panRingStart. */
520 : double *padfX;
521 : /** List of nVertices Y coordinates for the polygon (split over multiple
522 : rings via panRingStart. */
523 : double *padfY;
524 : /** List of nVertices Z coordinates for the polygon (split over multiple
525 : rings via panRingStart. The values are almost always zero. */
526 : double *padfZ;
527 :
528 : virtual void Dump(FILE *) override;
529 : };
530 :
531 : /************************************************************************/
532 : /* SDTSPolygonReader */
533 : /************************************************************************/
534 :
535 : /** Class for reading SDTSRawPolygon features from a polygon (PC*) module. */
536 :
537 : class SDTSPolygonReader : public SDTSIndexedReader
538 : {
539 : int bRingsAssembled;
540 :
541 : public:
542 : SDTSPolygonReader();
543 : virtual ~SDTSPolygonReader();
544 :
545 : int Open(const char *);
546 : SDTSRawPolygon *GetNextPolygon();
547 : void Close();
548 :
549 36 : SDTSFeature *GetNextRawFeature() override
550 : {
551 36 : return GetNextPolygon();
552 : }
553 :
554 : void AssembleRings(SDTSTransfer *, int iPolyLayer);
555 : };
556 :
557 : /************************************************************************/
558 : /* SDTSRasterReader */
559 : /************************************************************************/
560 :
561 : /**
562 : Class for reading raster data from a raster layer.
563 :
564 : This class is somewhat unique among the reader classes in that it isn't
565 : derived from SDTSIndexedFeature, and it doesn't return "features". Instead
566 : it is used to read raster blocks, in the natural block size of the dataset.
567 : */
568 :
569 : class SDTSRasterReader
570 : {
571 : DDFModule oDDFModule;
572 :
573 : char szModule[20];
574 :
575 : int nXSize;
576 : int nYSize;
577 : int nXBlockSize;
578 : int nYBlockSize;
579 :
580 : int nXStart; /* SOCI */
581 : int nYStart; /* SORI */
582 :
583 : double adfTransform[6];
584 :
585 : public:
586 : char szINTR[4]; /* CE is center, TL is top left */
587 : char szFMT[32];
588 : char szUNITS[64];
589 : char szLabel[64];
590 :
591 : SDTSRasterReader();
592 : ~SDTSRasterReader();
593 :
594 : int Open(SDTS_CATD *poCATD, SDTS_IREF *, const char *pszModule);
595 : void Close();
596 :
597 : int GetRasterType(); /* 1 = int16, see GDAL types */
598 : #define SDTS_RT_INT16 1
599 : #define SDTS_RT_FLOAT32 6
600 :
601 : int GetTransform(double *);
602 :
603 : int GetMinMax(double *pdfMin, double *pdfMax, double dfNoData);
604 :
605 : /**
606 : Fetch the raster width.
607 :
608 : @return the width in pixels.
609 : */
610 2 : int GetXSize() const
611 : {
612 2 : return nXSize;
613 : }
614 :
615 : /**
616 : Fetch the raster height.
617 :
618 : @return the height in pixels.
619 : */
620 2 : int GetYSize() const
621 : {
622 2 : return nYSize;
623 : }
624 :
625 : /** Fetch the width of a source block (usually same as raster width). */
626 2 : int GetBlockXSize() const
627 : {
628 2 : return nXBlockSize;
629 : }
630 :
631 : /** Fetch the height of a source block (usually one). */
632 2 : int GetBlockYSize() const
633 : {
634 2 : return nYBlockSize;
635 : }
636 :
637 : int GetBlock(int nXOffset, int nYOffset, void *pData);
638 : };
639 :
640 : /************************************************************************/
641 : /* SDTSTransfer */
642 : /************************************************************************/
643 :
644 : /**
645 : Master class representing an entire SDTS transfer.
646 :
647 : This class is used to open the transfer, to get a list of available
648 : feature layers, and to instantiate readers for those layers.
649 :
650 : */
651 :
652 : class SDTSTransfer
653 : {
654 : public:
655 : SDTSTransfer();
656 : ~SDTSTransfer();
657 :
658 : int Open(const char *);
659 : void Close();
660 :
661 : int FindLayer(const char *);
662 :
663 20 : int GetLayerCount() const
664 : {
665 20 : return nLayers;
666 : }
667 :
668 : SDTSLayerType GetLayerType(int) const;
669 : int GetLayerCATDEntry(int) const;
670 :
671 : SDTSLineReader *GetLayerLineReader(int);
672 : SDTSPointReader *GetLayerPointReader(int);
673 : SDTSPolygonReader *GetLayerPolygonReader(int);
674 : SDTSAttrReader *GetLayerAttrReader(int);
675 : SDTSRasterReader *GetLayerRasterReader(int);
676 : DDFModule *GetLayerModuleReader(int);
677 :
678 : SDTSIndexedReader *GetLayerIndexedReader(int);
679 :
680 : /**
681 : Fetch the catalog object for this transfer.
682 :
683 : @return pointer to the internally managed SDTS_CATD for the transfer.
684 : */
685 13 : SDTS_CATD *GetCATD()
686 : {
687 13 : return &oCATD;
688 : }
689 :
690 : SDTS_IREF *GetIREF()
691 : {
692 : return &oIREF;
693 : }
694 :
695 : /**
696 : Fetch the external reference object for this transfer.
697 :
698 : @return pointer to the internally managed SDTS_XREF for the transfer.
699 : */
700 3 : SDTS_XREF *GetXREF()
701 : {
702 3 : return &oXREF;
703 : }
704 :
705 : SDTSFeature *GetIndexedFeatureRef(SDTSModId *,
706 : SDTSLayerType *peType = nullptr);
707 :
708 : DDFField *GetAttr(SDTSModId *);
709 :
710 : int GetBounds(double *pdfMinX, double *pdfMinY, double *pdfMaxX,
711 : double *pdfMaxY);
712 :
713 : private:
714 : SDTS_CATD oCATD;
715 : SDTS_IREF oIREF;
716 : SDTS_XREF oXREF;
717 :
718 : int nLayers;
719 : int *panLayerCATDEntry;
720 : SDTSIndexedReader **papoLayerReader;
721 : };
722 :
723 : #endif /* ifndef SDTS_AL_H_INCLUDED */
|