1 : /******************************************************************************
2 : * $Id: tigeraltname.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: TIGER/Line Translator
5 : * Purpose: Implements TigerAltName, providing access to RT4 files.
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 : #include "ogr_tiger.h"
31 : #include "cpl_conv.h"
32 :
33 : CPL_CVSID("$Id: tigeraltname.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
34 :
35 : #define FILE_CODE "4"
36 :
37 : static TigerFieldInfo rt4_fields[] = {
38 : // fieldname fmt type OFTType beg end len bDefine bSet bWrite
39 : { "MODULE", ' ', ' ', OFTString, 0, 0, 8, 1, 0, 0 },
40 : { "TLID", 'R', 'N', OFTInteger, 6, 15, 10, 1, 1, 1 },
41 : { "RTSQ", 'R', 'N', OFTInteger, 16, 18, 3, 1, 1, 1 },
42 : { "FEAT", ' ', ' ', OFTIntegerList, 0, 0, 8, 1, 0, 0 }
43 : // Note: we don't mention the FEAT1, FEAT2, FEAT3, FEAT4, FEAT5 fields
44 : // here because they're handled separately in the code below; they correspond
45 : // to the FEAT array field here.
46 : };
47 :
48 : static TigerRecordInfo rt4_info =
49 : {
50 : rt4_fields,
51 : sizeof(rt4_fields) / sizeof(TigerFieldInfo),
52 : 58
53 : };
54 :
55 :
56 : /************************************************************************/
57 : /* TigerAltName() */
58 : /************************************************************************/
59 :
60 : TigerAltName::TigerAltName( OGRTigerDataSource * poDSIn,
61 0 : const char * pszPrototypeModule )
62 :
63 : {
64 0 : OGRFieldDefn oField("",OFTInteger);
65 :
66 0 : psRT4Info = &rt4_info;
67 :
68 0 : poDS = poDSIn;
69 0 : poFeatureDefn = new OGRFeatureDefn( "AltName" );
70 0 : poFeatureDefn->Reference();
71 0 : poFeatureDefn->SetGeomType( wkbNone );
72 :
73 : /* -------------------------------------------------------------------- */
74 : /* Fields from type 4 record. */
75 : /* -------------------------------------------------------------------- */
76 :
77 0 : AddFieldDefns( psRT4Info, poFeatureDefn );
78 0 : }
79 :
80 : /************************************************************************/
81 : /* ~TigerAltName() */
82 : /************************************************************************/
83 :
84 0 : TigerAltName::~TigerAltName()
85 :
86 : {
87 0 : }
88 :
89 : /************************************************************************/
90 : /* SetModule() */
91 : /************************************************************************/
92 :
93 0 : int TigerAltName::SetModule( const char * pszModule )
94 :
95 : {
96 0 : if( !OpenFile( pszModule, FILE_CODE ) )
97 0 : return FALSE;
98 :
99 0 : EstablishFeatureCount();
100 :
101 0 : return TRUE;
102 : }
103 :
104 : /************************************************************************/
105 : /* GetFeature() */
106 : /************************************************************************/
107 :
108 0 : OGRFeature *TigerAltName::GetFeature( int nRecordId )
109 :
110 : {
111 : char achRecord[OGR_TIGER_RECBUF_LEN];
112 :
113 0 : if( nRecordId < 0 || nRecordId >= nFeatures )
114 : {
115 : CPLError( CE_Failure, CPLE_FileIO,
116 : "Request for out-of-range feature %d of %s4",
117 0 : nRecordId, pszModule );
118 0 : return NULL;
119 : }
120 :
121 : /* -------------------------------------------------------------------- */
122 : /* Read the raw record data from the file. */
123 : /* -------------------------------------------------------------------- */
124 0 : if( fpPrimary == NULL )
125 0 : return NULL;
126 :
127 0 : if( VSIFSeek( fpPrimary, nRecordId * nRecordLength, SEEK_SET ) != 0 )
128 : {
129 : CPLError( CE_Failure, CPLE_FileIO,
130 : "Failed to seek to %d of %s4",
131 0 : nRecordId * nRecordLength, pszModule );
132 0 : return NULL;
133 : }
134 :
135 0 : if( VSIFRead( achRecord, psRT4Info->nRecordLength, 1, fpPrimary ) != 1 )
136 : {
137 : CPLError( CE_Failure, CPLE_FileIO,
138 : "Failed to read record %d of %s4",
139 0 : nRecordId, pszModule );
140 0 : return NULL;
141 : }
142 :
143 : /* -------------------------------------------------------------------- */
144 : /* Set fields. */
145 : /* -------------------------------------------------------------------- */
146 :
147 0 : OGRFeature *poFeature = new OGRFeature( poFeatureDefn );
148 : int anFeatList[5];
149 0 : int nFeatCount=0;
150 :
151 0 : SetFields( psRT4Info, poFeature, achRecord );
152 :
153 0 : for( int iFeat = 0; iFeat < 5; iFeat++ )
154 : {
155 : const char * pszFieldText;
156 :
157 0 : pszFieldText = GetField( achRecord, 19 + iFeat*8, 26 + iFeat*8 );
158 :
159 0 : if( *pszFieldText != '\0' )
160 0 : anFeatList[nFeatCount++] = atoi(pszFieldText);
161 : }
162 :
163 0 : poFeature->SetField( "FEAT", nFeatCount, anFeatList );
164 :
165 0 : return poFeature;
166 : }
167 :
168 : /************************************************************************/
169 : /* CreateFeature() */
170 : /************************************************************************/
171 :
172 0 : OGRErr TigerAltName::CreateFeature( OGRFeature *poFeature )
173 :
174 : {
175 : char szRecord[OGR_TIGER_RECBUF_LEN];
176 : const int *panValue;
177 0 : int nValueCount = 0;
178 :
179 0 : if( !SetWriteModule( FILE_CODE, psRT4Info->nRecordLength+2, poFeature ) )
180 0 : return OGRERR_FAILURE;
181 :
182 0 : memset( szRecord, ' ', psRT4Info->nRecordLength );
183 :
184 0 : WriteFields( psRT4Info, poFeature, szRecord );
185 :
186 0 : panValue = poFeature->GetFieldAsIntegerList( "FEAT", &nValueCount );
187 0 : for( int i = 0; i < nValueCount; i++ )
188 : {
189 : char szWork[9];
190 :
191 0 : sprintf( szWork, "%8d", panValue[i] );
192 0 : strncpy( szRecord + 18 + 8 * i, szWork, 8 );
193 : }
194 :
195 0 : WriteRecord( szRecord, psRT4Info->nRecordLength, FILE_CODE );
196 :
197 0 : return OGRERR_NONE;
198 : }
|