1 /******************************************************************************
2  *
3  * Project:  SDTS Translator
4  * Purpose:  Implementation of SDTSPointReader and SDTSRawPoint classes.
5  * Author:   Frank Warmerdam, warmerdam@pobox.com
6  *
7  ******************************************************************************
8  * Copyright (c) 1999, Frank Warmerdam
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  ****************************************************************************/
28 
29 #include "sdts_al.h"
30 
31 CPL_CVSID("$Id: sdtspointreader.cpp 7e07230bbff24eb333608de4dbd460b7312839d0 2017-12-11 19:08:47Z Even Rouault $")
32 
33 /************************************************************************/
34 /* ==================================================================== */
35 /*                            SDTSRawPoint                              */
36 /*                                                                      */
37 /*      This is a simple class for holding the data related with a      */
38 /*      point feature.                                                  */
39 /* ==================================================================== */
40 /************************************************************************/
41 
42 /************************************************************************/
43 /*                            SDTSRawPoint()                            */
44 /************************************************************************/
45 
SDTSRawPoint()46 SDTSRawPoint::SDTSRawPoint() :
47     dfX(0.0),
48     dfY(0.0),
49     dfZ(0.0)
50 {
51     nAttributes = 0;
52 }
53 
54 /************************************************************************/
55 /*                           ~STDSRawPoint()                            */
56 /************************************************************************/
57 
~SDTSRawPoint()58 SDTSRawPoint::~SDTSRawPoint() {}
59 
60 /************************************************************************/
61 /*                                Read()                                */
62 /*                                                                      */
63 /*      Read a record from the passed SDTSPointReader, and assign the   */
64 /*      values from that record to this point.  This is the bulk of     */
65 /*      the work in this whole file.                                    */
66 /************************************************************************/
67 
Read(SDTS_IREF * poIREF,DDFRecord * poRecord)68 int SDTSRawPoint::Read( SDTS_IREF * poIREF, DDFRecord * poRecord )
69 
70 {
71 /* ==================================================================== */
72 /*      Loop over fields in this record, looking for those we           */
73 /*      recognise, and need.                                            */
74 /* ==================================================================== */
75     for( int iField = 0; iField < poRecord->GetFieldCount(); iField++ )
76     {
77         DDFField        *poField = poRecord->GetField( iField );
78         if( poField == nullptr )
79             return FALSE;
80         DDFFieldDefn* poFieldDefn = poField->GetFieldDefn();
81         if( poFieldDefn == nullptr )
82             return FALSE;
83 
84         const char *pszFieldName = poFieldDefn->GetName();
85 
86         if( EQUAL(pszFieldName,"PNTS") )
87             oModId.Set( poField );
88 
89         else if( EQUAL(pszFieldName,"ATID") )
90             ApplyATID( poField );
91 
92         else if( EQUAL(pszFieldName,"ARID") )
93         {
94             oAreaId.Set( poField );
95         }
96         else if( EQUAL(pszFieldName,"SADR") )
97         {
98             poIREF->GetSADR( poField, 1, &dfX, &dfY, &dfZ );
99         }
100     }
101 
102     return TRUE;
103 }
104 
105 /************************************************************************/
106 /*                                Dump()                                */
107 /************************************************************************/
108 
Dump(FILE * fp)109 void SDTSRawPoint::Dump( FILE * fp )
110 
111 {
112     fprintf( fp, "SDTSRawPoint %s: ", oModId.GetName() );
113 
114     if( oAreaId.nRecord != -1 )
115         fprintf( fp, " AreaId=%s", oAreaId.GetName() );
116 
117     for( int i = 0; i < nAttributes; i++ )
118         fprintf( fp, "  ATID[%d]=%s", i, paoATID[i].GetName() );
119 
120     fprintf( fp, "  Vertex = (%.2f,%.2f,%.2f)\n", dfX, dfY, dfZ );
121 }
122 
123 /************************************************************************/
124 /* ==================================================================== */
125 /*                             SDTSPointReader                          */
126 /*                                                                      */
127 /*      This is the class used to read a point module.                  */
128 /* ==================================================================== */
129 /************************************************************************/
130 
131 /************************************************************************/
132 /*                           SDTSPointReader()                          */
133 /************************************************************************/
134 
SDTSPointReader(SDTS_IREF * poIREFIn)135 SDTSPointReader::SDTSPointReader( SDTS_IREF * poIREFIn ) :
136     poIREF(poIREFIn)
137 {}
138 
139 /************************************************************************/
140 /*                             ~SDTSLineReader()                        */
141 /************************************************************************/
142 
~SDTSPointReader()143 SDTSPointReader::~SDTSPointReader() {}
144 
145 /************************************************************************/
146 /*                               Close()                                */
147 /************************************************************************/
148 
Close()149 void SDTSPointReader::Close()
150 
151 {
152     oDDFModule.Close();
153 }
154 
155 /************************************************************************/
156 /*                                Open()                                */
157 /*                                                                      */
158 /*      Open the requested line file, and prepare to start reading      */
159 /*      data records.                                                   */
160 /************************************************************************/
161 
Open(const char * pszFilename)162 int SDTSPointReader::Open( const char * pszFilename )
163 
164 {
165     return oDDFModule.Open( pszFilename );
166 }
167 
168 /************************************************************************/
169 /*                            GetNextPoint()                            */
170 /*                                                                      */
171 /*      Fetch the next feature as an STDSRawPoint.                      */
172 /************************************************************************/
173 
GetNextPoint()174 SDTSRawPoint * SDTSPointReader::GetNextPoint()
175 
176 {
177 /* -------------------------------------------------------------------- */
178 /*      Read a record.                                                  */
179 /* -------------------------------------------------------------------- */
180     if( oDDFModule.GetFP() == nullptr )
181         return nullptr;
182 
183     DDFRecord *poRecord = oDDFModule.ReadRecord();
184 
185     if( poRecord == nullptr )
186         return nullptr;
187 
188 /* -------------------------------------------------------------------- */
189 /*      Transform into a point feature.                                 */
190 /* -------------------------------------------------------------------- */
191     SDTSRawPoint *poRawPoint = new SDTSRawPoint();
192 
193     if( poRawPoint->Read( poIREF, poRecord ) )
194     {
195         return poRawPoint;
196     }
197 
198     delete poRawPoint;
199     return nullptr;
200 }
201