1 : /******************************************************************************
2 : * $Id: tigeraltname.cpp 23871 2012-02-02 03:24:07Z 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 23871 2012-02-02 03:24:07Z warmerdam $");
34 :
35 : #define FILE_CODE "4"
36 :
37 : static const 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 const TigerRecordInfo rt4_info =
49 : {
50 : rt4_fields,
51 : sizeof(rt4_fields) / sizeof(TigerFieldInfo),
52 : 58
53 : };
54 :
55 :
56 : /************************************************************************/
57 : /* TigerAltName() */
58 : /************************************************************************/
59 :
60 10 : TigerAltName::TigerAltName( OGRTigerDataSource * poDSIn,
61 10 : const char * pszPrototypeModule ) : TigerFileBase(&rt4_info, FILE_CODE)
62 :
63 : {
64 10 : OGRFieldDefn oField("",OFTInteger);
65 :
66 10 : poDS = poDSIn;
67 10 : poFeatureDefn = new OGRFeatureDefn( "AltName" );
68 10 : poFeatureDefn->Reference();
69 10 : poFeatureDefn->SetGeomType( wkbNone );
70 :
71 : /* -------------------------------------------------------------------- */
72 : /* Fields from type 4 record. */
73 : /* -------------------------------------------------------------------- */
74 :
75 10 : AddFieldDefns( psRTInfo, poFeatureDefn );
76 10 : }
77 :
78 : /************************************************************************/
79 : /* GetFeature() */
80 : /************************************************************************/
81 :
82 22510 : OGRFeature *TigerAltName::GetFeature( int nRecordId )
83 :
84 : {
85 : char achRecord[OGR_TIGER_RECBUF_LEN];
86 :
87 22510 : if( nRecordId < 0 || nRecordId >= nFeatures )
88 : {
89 : CPLError( CE_Failure, CPLE_FileIO,
90 : "Request for out-of-range feature %d of %s4",
91 0 : nRecordId, pszModule );
92 0 : return NULL;
93 : }
94 :
95 : /* -------------------------------------------------------------------- */
96 : /* Read the raw record data from the file. */
97 : /* -------------------------------------------------------------------- */
98 22510 : if( fpPrimary == NULL )
99 0 : return NULL;
100 :
101 22510 : if( VSIFSeekL( fpPrimary, nRecordId * nRecordLength, SEEK_SET ) != 0 )
102 : {
103 : CPLError( CE_Failure, CPLE_FileIO,
104 : "Failed to seek to %d of %s4",
105 0 : nRecordId * nRecordLength, pszModule );
106 0 : return NULL;
107 : }
108 :
109 22510 : if( VSIFReadL( achRecord, psRTInfo->nRecordLength, 1, fpPrimary ) != 1 )
110 : {
111 : CPLError( CE_Failure, CPLE_FileIO,
112 : "Failed to read record %d of %s4",
113 0 : nRecordId, pszModule );
114 0 : return NULL;
115 : }
116 :
117 : /* -------------------------------------------------------------------- */
118 : /* Set fields. */
119 : /* -------------------------------------------------------------------- */
120 :
121 22510 : OGRFeature *poFeature = new OGRFeature( poFeatureDefn );
122 : int anFeatList[5];
123 22510 : int nFeatCount=0;
124 :
125 22510 : SetFields( psRTInfo, poFeature, achRecord );
126 :
127 135060 : for( int iFeat = 0; iFeat < 5; iFeat++ )
128 : {
129 : const char * pszFieldText;
130 :
131 112550 : pszFieldText = GetField( achRecord, 19 + iFeat*8, 26 + iFeat*8 );
132 :
133 112550 : if( *pszFieldText != '\0' )
134 25726 : anFeatList[nFeatCount++] = atoi(pszFieldText);
135 : }
136 :
137 22510 : poFeature->SetField( "FEAT", nFeatCount, anFeatList );
138 :
139 22510 : return poFeature;
140 : }
141 :
142 : /************************************************************************/
143 : /* CreateFeature() */
144 : /************************************************************************/
145 :
146 2812 : OGRErr TigerAltName::CreateFeature( OGRFeature *poFeature )
147 :
148 : {
149 : char szRecord[OGR_TIGER_RECBUF_LEN];
150 : const int *panValue;
151 2812 : int nValueCount = 0;
152 :
153 2812 : if( !SetWriteModule( FILE_CODE, psRTInfo->nRecordLength+2, poFeature ) )
154 0 : return OGRERR_FAILURE;
155 :
156 2812 : memset( szRecord, ' ', psRTInfo->nRecordLength );
157 :
158 2812 : WriteFields( psRTInfo, poFeature, szRecord );
159 :
160 2812 : panValue = poFeature->GetFieldAsIntegerList( "FEAT", &nValueCount );
161 6026 : for( int i = 0; i < nValueCount; i++ )
162 : {
163 : char szWork[9];
164 :
165 3214 : sprintf( szWork, "%8d", panValue[i] );
166 3214 : strncpy( szRecord + 18 + 8 * i, szWork, 8 );
167 : }
168 :
169 2812 : WriteRecord( szRecord, psRTInfo->nRecordLength, FILE_CODE );
170 :
171 2812 : return OGRERR_NONE;
172 : }
|