1 : /******************************************************************************
2 : * $Id: tigerentitynames.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: TIGER/Line Translator
5 : * Purpose: Implements TigerEntityNames, providing access to .RTC 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: tigerentitynames.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
34 :
35 : #define FILE_CODE "C"
36 :
37 : static TigerFieldInfo rtC_2002_fields[] = {
38 : // fieldname fmt type OFTType beg end len bDefine bSet bWrite
39 : { "MODULE", ' ', ' ', OFTString, 0, 0, 8, 1, 0, 0 },
40 : { "STATE", 'L', 'N', OFTInteger, 6, 7, 2, 1, 1, 1 },
41 : { "COUNTY", 'L', 'N', OFTInteger, 8, 10, 3, 1, 1, 1 },
42 : { "DATAYR", 'L', 'A', OFTString, 11, 14, 4, 1, 1, 1 },
43 : { "FIPS", 'L', 'N', OFTInteger, 15, 19, 5, 1, 1, 1 },
44 : { "FIPSCC", 'L', 'A', OFTString, 20, 21, 2, 1, 1, 1 },
45 : { "PLACEDC", 'L', 'A', OFTString, 22, 22, 1, 1, 1, 1 },
46 : { "LSADC", 'L', 'A', OFTString, 23, 24, 2, 1, 1, 1 },
47 : { "ENTITY", 'L', 'A', OFTString, 25, 25, 1, 1, 1, 1 },
48 : { "MA", 'L', 'N', OFTInteger, 26, 29, 4, 1, 1, 1 },
49 : { "SD", 'L', 'N', OFTInteger, 30, 34, 5, 1, 1, 1 },
50 : { "AIANHH", 'L', 'N', OFTInteger, 35, 38, 4, 1, 1, 1 },
51 : { "VTDTRACT", 'R', 'A', OFTString, 39, 44, 6, 1, 1, 1 },
52 : { "UAUGA", 'L', 'N', OFTInteger, 45, 49, 5, 1, 1, 1 },
53 : { "AITSCE", 'L', 'N', OFTInteger, 50, 52, 3, 1, 1, 1 },
54 : { "RS_C1", 'L', 'N', OFTInteger, 53, 54, 2, 1, 1, 1 },
55 : { "RS_C2", 'L', 'N', OFTInteger, 55, 62, 8, 1, 1, 1 },
56 : { "NAME", 'L', 'A', OFTString, 63, 122, 60, 1, 1, 1 },
57 : };
58 : static TigerRecordInfo rtC_2002_info =
59 : {
60 : rtC_2002_fields,
61 : sizeof(rtC_2002_fields) / sizeof(TigerFieldInfo),
62 : 122
63 : };
64 :
65 : static TigerFieldInfo rtC_2000_Redistricting_fields[] = {
66 : // fieldname fmt type OFTType beg end len bDefine bSet bWrite
67 : { "MODULE", ' ', ' ', OFTString, 0, 0, 8, 1, 0, 0 },
68 : { "STATE", 'L', 'N', OFTInteger, 6, 7, 2, 1, 1, 1 },
69 : { "COUNTY", 'L', 'N', OFTInteger, 8, 10, 3, 1, 1, 1 },
70 : { "FIPSYR", 'L', 'N', OFTString, 11, 14, 4, 1, 1, 1 },
71 : { "FIPS", 'L', 'N', OFTInteger, 15, 19, 5, 1, 1, 1 },
72 : { "FIPSCC", 'L', 'A', OFTString, 20, 21, 2, 1, 1, 1 },
73 : { "PDC", 'L', 'A', OFTString, 22, 22, 1, 1, 1, 1 },
74 : { "LASAD", 'L', 'A', OFTString, 23, 24, 2, 1, 1, 1 },
75 : { "ENTITY", 'L', 'A', OFTString, 25, 25, 1, 1, 1, 1 },
76 : { "MA", 'L', 'N', OFTInteger, 26, 29, 4, 1, 1, 1 },
77 : { "SD", 'L', 'N', OFTInteger, 30, 34, 5, 1, 1, 1 },
78 : { "AIR", 'L', 'N', OFTInteger, 35, 38, 4, 1, 1, 1 },
79 : { "VTD", 'R', 'A', OFTString, 39, 44, 6, 1, 1, 1 },
80 : { "UA", 'L', 'N', OFTInteger, 45, 49, 5, 1, 1, 1 },
81 : { "AITSCE", 'L', 'N', OFTInteger, 50, 52, 3, 1, 1, 1 },
82 : { "NAME", 'L', 'A', OFTString, 53, 112, 66, 1, 1, 1 }
83 : };
84 : static TigerRecordInfo rtC_2000_Redistricting_info =
85 : {
86 : rtC_2000_Redistricting_fields,
87 : sizeof(rtC_2000_Redistricting_fields) / sizeof(TigerFieldInfo),
88 : 112
89 : };
90 :
91 : static TigerFieldInfo rtC_fields[] = {
92 : // fieldname fmt type OFTType beg end len bDefine bSet bWrite
93 : { "MODULE", ' ', ' ', OFTString, 0, 0, 8, 1, 0, 0 },
94 : { "STATE", 'L', 'N', OFTInteger, 6, 7, 2, 1, 1, 1 },
95 : { "COUNTY", 'L', 'N', OFTInteger, 8, 10, 3, 1, 1, 1 },
96 : { "FIPSYR", 'L', 'N', OFTString, 11, 12, 4, 1, 1, 1 },
97 : { "FIPS", 'L', 'N', OFTInteger, 13, 17, 5, 1, 1, 1 },
98 : { "FIPSCC", 'L', 'A', OFTString, 18, 19, 2, 1, 1, 1 },
99 : { "PDC", 'L', 'A', OFTString, 20, 20, 1, 1, 1, 1 },
100 : { "LASAD", 'L', 'A', OFTString, 21, 22, 2, 1, 1, 1 },
101 : { "ENTITY", 'L', 'A', OFTString, 23, 23, 1, 1, 1, 1 },
102 : { "MA", 'L', 'N', OFTInteger, 24, 27, 4, 1, 1, 1 },
103 : { "SD", 'L', 'N', OFTInteger, 28, 32, 5, 1, 1, 1 },
104 : { "AIR", 'L', 'N', OFTInteger, 33, 36, 4, 1, 1, 1 },
105 : { "VTD", 'R', 'A', OFTString, 37, 42, 6, 1, 1, 1 },
106 : { "UA", 'L', 'N', OFTInteger, 43, 46, 4, 1, 1, 1 },
107 : { "NAME", 'L', 'A', OFTString, 47, 112, 66, 1, 1, 1 }
108 : };
109 : static TigerRecordInfo rtC_info =
110 : {
111 : rtC_fields,
112 : sizeof(rtC_fields) / sizeof(TigerFieldInfo),
113 : 112
114 : };
115 :
116 :
117 : /************************************************************************/
118 : /* TigerEntityNames() */
119 : /************************************************************************/
120 :
121 : TigerEntityNames::TigerEntityNames( OGRTigerDataSource * poDSIn,
122 0 : const char * pszPrototypeModule )
123 :
124 : {
125 0 : poDS = poDSIn;
126 0 : poFeatureDefn = new OGRFeatureDefn( "EntityNames" );
127 0 : poFeatureDefn->Reference();
128 0 : poFeatureDefn->SetGeomType( wkbPoint );
129 :
130 0 : if( poDS->GetVersion() >= TIGER_2002 ) {
131 0 : psRTCInfo = &rtC_2002_info;
132 0 : } else if( poDS->GetVersion() >= TIGER_2000_Redistricting ) {
133 0 : psRTCInfo = &rtC_2000_Redistricting_info;
134 : } else {
135 0 : psRTCInfo = &rtC_info;
136 : }
137 :
138 0 : AddFieldDefns( psRTCInfo, poFeatureDefn );
139 0 : }
140 :
141 : /************************************************************************/
142 : /* ~TigerEntityNames() */
143 : /************************************************************************/
144 :
145 0 : TigerEntityNames::~TigerEntityNames()
146 :
147 : {
148 0 : }
149 :
150 : /************************************************************************/
151 : /* SetModule() */
152 : /************************************************************************/
153 :
154 0 : int TigerEntityNames::SetModule( const char * pszModule )
155 :
156 : {
157 0 : if( !OpenFile( pszModule, "C" ) )
158 0 : return FALSE;
159 :
160 0 : EstablishFeatureCount();
161 :
162 0 : return TRUE;
163 : }
164 :
165 : /************************************************************************/
166 : /* GetFeature() */
167 : /************************************************************************/
168 :
169 0 : OGRFeature *TigerEntityNames::GetFeature( int nRecordId )
170 :
171 : {
172 : char achRecord[OGR_TIGER_RECBUF_LEN];
173 :
174 0 : if( nRecordId < 0 || nRecordId >= nFeatures )
175 : {
176 : CPLError( CE_Failure, CPLE_FileIO,
177 : "Request for out-of-range feature %d of %sC",
178 0 : nRecordId, pszModule );
179 0 : return NULL;
180 : }
181 :
182 : /* -------------------------------------------------------------------- */
183 : /* Read the raw record data from the file. */
184 : /* -------------------------------------------------------------------- */
185 :
186 0 : if( fpPrimary == NULL )
187 0 : return NULL;
188 :
189 0 : if( VSIFSeek( fpPrimary, nRecordId * nRecordLength, SEEK_SET ) != 0 )
190 : {
191 : CPLError( CE_Failure, CPLE_FileIO,
192 : "Failed to seek to %d of %sC",
193 0 : nRecordId * nRecordLength, pszModule );
194 0 : return NULL;
195 : }
196 :
197 0 : if( VSIFRead( achRecord, psRTCInfo->nRecordLength, 1, fpPrimary ) != 1 )
198 : {
199 : CPLError( CE_Failure, CPLE_FileIO,
200 : "Failed to read record %d of %sC",
201 0 : nRecordId, pszModule );
202 0 : return NULL;
203 : }
204 :
205 : /* -------------------------------------------------------------------- */
206 : /* Set fields. */
207 : /* -------------------------------------------------------------------- */
208 :
209 0 : OGRFeature *poFeature = new OGRFeature( poFeatureDefn );
210 :
211 0 : SetFields( psRTCInfo, poFeature, achRecord );
212 :
213 0 : return poFeature;
214 : }
215 :
216 : /************************************************************************/
217 : /* CreateFeature() */
218 : /************************************************************************/
219 :
220 0 : OGRErr TigerEntityNames::CreateFeature( OGRFeature *poFeature )
221 :
222 : {
223 : char szRecord[OGR_TIGER_RECBUF_LEN];
224 :
225 0 : if( !SetWriteModule( FILE_CODE, psRTCInfo->nRecordLength+2, poFeature ) )
226 0 : return OGRERR_FAILURE;
227 :
228 0 : memset( szRecord, ' ', psRTCInfo->nRecordLength );
229 :
230 0 : WriteFields( psRTCInfo, poFeature, szRecord );
231 :
232 0 : WriteRecord( szRecord, psRTCInfo->nRecordLength, FILE_CODE );
233 :
234 0 : return OGRERR_NONE;
235 : }
|