1 : /******************************************************************************
2 : * $Id: tigeridhistory.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: TIGER/Line Translator
5 : * Purpose: Implements TigerIDHistory, providing access to .RTH 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: tigeridhistory.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
34 :
35 : #define FILE_CODE "H"
36 :
37 : static TigerFieldInfo rtH_fields[] = {
38 : // fieldname fmt type OFTType beg end len bDefine bSet bWrite
39 : { "MODULE", ' ', ' ', OFTString, 0, 0, 8, 1, 0, 0 },
40 : { "FILE", 'L', 'N', OFTString, 6, 10, 5, 1, 1, 1 },
41 : { "STATE", 'L', 'N', OFTInteger, 6, 7, 2, 1, 1, 1 },
42 : { "COUNTY", 'L', 'N', OFTInteger, 8, 10, 3, 1, 1, 1 },
43 : { "TLID", 'R', 'N', OFTInteger, 11, 20, 10, 1, 1, 1 },
44 : { "HIST", 'L', 'A', OFTString, 21, 21, 1, 1, 1, 1 },
45 : { "SOURCE", 'L', 'A', OFTString, 22, 22, 1, 1, 1, 1 },
46 : { "TLIDFR1", 'R', 'N', OFTInteger, 23, 32, 10, 1, 1, 1 },
47 : { "TLIDFR2", 'R', 'N', OFTInteger, 33, 42, 10, 1, 1, 1 },
48 : { "TLIDTO1", 'R', 'N', OFTInteger, 43, 52, 10, 1, 1, 1 },
49 : { "TLIDTO2", 'R', 'N', OFTInteger, 53, 62, 10, 1, 1, 1 }
50 : };
51 : static TigerRecordInfo rtH_info =
52 : {
53 : rtH_fields,
54 : sizeof(rtH_fields) / sizeof(TigerFieldInfo),
55 : 62
56 : };
57 :
58 : /************************************************************************/
59 : /* TigerIDHistory() */
60 : /************************************************************************/
61 :
62 : TigerIDHistory::TigerIDHistory( OGRTigerDataSource * poDSIn,
63 0 : const char * pszPrototypeModule )
64 :
65 : {
66 0 : OGRFieldDefn oField("",OFTInteger);
67 :
68 0 : poDS = poDSIn;
69 0 : poFeatureDefn = new OGRFeatureDefn( "IDHistory" );
70 0 : poFeatureDefn->Reference();
71 0 : poFeatureDefn->SetGeomType( wkbNone );
72 :
73 0 : psRTHInfo = &rtH_info;
74 :
75 : /* -------------------------------------------------------------------- */
76 : /* Fields from record type H */
77 : /* -------------------------------------------------------------------- */
78 :
79 0 : AddFieldDefns(psRTHInfo, poFeatureDefn);
80 0 : }
81 :
82 : /************************************************************************/
83 : /* ~TigerIDHistory() */
84 : /************************************************************************/
85 :
86 0 : TigerIDHistory::~TigerIDHistory()
87 :
88 : {
89 0 : }
90 :
91 : /************************************************************************/
92 : /* SetModule() */
93 : /************************************************************************/
94 :
95 0 : int TigerIDHistory::SetModule( const char * pszModule )
96 :
97 : {
98 0 : if( !OpenFile( pszModule, FILE_CODE ) )
99 0 : return FALSE;
100 :
101 0 : EstablishFeatureCount();
102 :
103 0 : return TRUE;
104 : }
105 :
106 : /************************************************************************/
107 : /* GetFeature() */
108 : /************************************************************************/
109 :
110 0 : OGRFeature *TigerIDHistory::GetFeature( int nRecordId )
111 :
112 : {
113 : char achRecord[OGR_TIGER_RECBUF_LEN];
114 :
115 0 : if( nRecordId < 0 || nRecordId >= nFeatures )
116 : {
117 : CPLError( CE_Failure, CPLE_FileIO,
118 : "Request for out-of-range feature %d of %sH",
119 0 : nRecordId, pszModule );
120 0 : return NULL;
121 : }
122 :
123 : /* -------------------------------------------------------------------- */
124 : /* Read the raw record data from the file. */
125 : /* -------------------------------------------------------------------- */
126 0 : if( fpPrimary == NULL )
127 0 : return NULL;
128 :
129 0 : if( VSIFSeek( fpPrimary, nRecordId * nRecordLength, SEEK_SET ) != 0 )
130 : {
131 : CPLError( CE_Failure, CPLE_FileIO,
132 : "Failed to seek to %d of %sH",
133 0 : nRecordId * nRecordLength, pszModule );
134 0 : return NULL;
135 : }
136 :
137 0 : if( VSIFRead( achRecord, psRTHInfo->nRecordLength, 1, fpPrimary ) != 1 )
138 : {
139 : CPLError( CE_Failure, CPLE_FileIO,
140 : "Failed to read record %d of %sH",
141 0 : nRecordId, pszModule );
142 0 : return NULL;
143 : }
144 :
145 : /* -------------------------------------------------------------------- */
146 : /* Set fields. */
147 : /* -------------------------------------------------------------------- */
148 0 : OGRFeature *poFeature = new OGRFeature( poFeatureDefn );
149 :
150 0 : SetFields( psRTHInfo, poFeature, achRecord );
151 :
152 0 : return poFeature;
153 : }
154 :
155 : /************************************************************************/
156 : /* CreateFeature() */
157 : /************************************************************************/
158 :
159 0 : OGRErr TigerIDHistory::CreateFeature( OGRFeature *poFeature )
160 :
161 : {
162 : char szRecord[OGR_TIGER_RECBUF_LEN];
163 :
164 0 : if( !SetWriteModule( FILE_CODE, psRTHInfo->nRecordLength+2, poFeature ) )
165 0 : return OGRERR_FAILURE;
166 :
167 0 : memset( szRecord, ' ', psRTHInfo->nRecordLength );
168 :
169 0 : WriteFields( psRTHInfo, poFeature, szRecord );
170 :
171 0 : WriteRecord( szRecord, psRTHInfo->nRecordLength, FILE_CODE );
172 :
173 0 : return OGRERR_NONE;
174 : }
|