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