1 : /******************************************************************************
2 : * $Id: tigertlidrange.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: TIGER/Line Translator
5 : * Purpose: Implements TigerTLIDRange, providing access to .RTR files.
6 : * Author: Frank Warmerdam, warmerda@home.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: tigertlidrange.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
34 :
35 : #define FILE_CODE "R"
36 :
37 : static TigerFieldInfo rtR_2002_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', OFTInteger, 6, 10, 5, 1, 1, 1 },
41 : { "CENID", 'L', 'A', OFTString, 11, 15, 5, 1, 1, 1 },
42 : { "TLMAXID", 'R', 'N', OFTInteger, 16, 25, 10, 1, 1, 1 },
43 : { "TLMINID", 'R', 'N', OFTInteger, 26, 35, 10, 1, 1, 1 },
44 : { "TLIGHID", 'R', 'N', OFTInteger, 36, 45, 10, 1, 1, 1 },
45 : { "TZMAXID", 'R', 'N', OFTInteger, 46, 55, 10, 1, 1, 1 },
46 : { "TZMINID", 'R', 'N', OFTInteger, 56, 65, 10, 1, 1, 1 },
47 : { "TZHIGHID", 'R', 'N', OFTInteger, 66, 75, 10, 1, 1, 1 },
48 : { "FILLER", 'L', 'A', OFTString, 76, 76, 1, 1, 1, 1 },
49 : };
50 : static TigerRecordInfo rtR_2002_info =
51 : {
52 : rtR_2002_fields,
53 : sizeof(rtR_2002_fields) / sizeof(TigerFieldInfo),
54 : 76
55 : };
56 :
57 : static TigerFieldInfo rtR_fields[] = {
58 : // fieldname fmt type OFTType beg end len bDefine bSet bWrite
59 : { "MODULE", ' ', ' ', OFTString, 0, 0, 8, 1, 0, 0 },
60 : { "FILE", 'L', 'N', OFTString, 6, 10, 5, 1, 1, 1 },
61 : { "STATE", 'L', 'N', OFTInteger, 6, 7, 2, 1, 1, 1 },
62 : { "COUNTY", 'L', 'N', OFTInteger, 8, 10, 3, 1, 1, 1 },
63 : { "CENID", 'L', 'A', OFTString, 11, 15, 5, 1, 1, 1 },
64 : { "MAXID", 'R', 'N', OFTInteger, 16, 25, 10, 1, 1, 1 },
65 : { "MINID", 'R', 'N', OFTInteger, 26, 35, 10, 1, 1, 1 },
66 : { "HIGHID", 'R', 'N', OFTInteger, 36, 45, 10, 1, 1, 1 }
67 : };
68 :
69 : static TigerRecordInfo rtR_info =
70 : {
71 : rtR_fields,
72 : sizeof(rtR_fields) / sizeof(TigerFieldInfo),
73 : 46
74 : };
75 :
76 : /************************************************************************/
77 : /* TigerTLIDRange() */
78 : /************************************************************************/
79 :
80 : TigerTLIDRange::TigerTLIDRange( OGRTigerDataSource * poDSIn,
81 0 : const char * pszPrototypeModule )
82 :
83 : {
84 0 : OGRFieldDefn oField("",OFTInteger);
85 :
86 0 : poDS = poDSIn;
87 0 : poFeatureDefn = new OGRFeatureDefn( "TLIDRange" );
88 0 : poFeatureDefn->Reference();
89 0 : poFeatureDefn->SetGeomType( wkbNone );
90 :
91 0 : if (poDS->GetVersion() >= TIGER_2002) {
92 0 : psRTRInfo = &rtR_2002_info;
93 : } else {
94 0 : psRTRInfo = &rtR_info;
95 : }
96 :
97 : /* -------------------------------------------------------------------- */
98 : /* Fields from type R record. */
99 : /* -------------------------------------------------------------------- */
100 :
101 0 : AddFieldDefns( psRTRInfo, poFeatureDefn );
102 :
103 0 : }
104 :
105 : /************************************************************************/
106 : /* ~TigerTLIDRange() */
107 : /************************************************************************/
108 :
109 0 : TigerTLIDRange::~TigerTLIDRange()
110 :
111 : {
112 0 : }
113 :
114 : /************************************************************************/
115 : /* SetModule() */
116 : /************************************************************************/
117 :
118 0 : int TigerTLIDRange::SetModule( const char * pszModule )
119 :
120 : {
121 0 : if( !OpenFile( pszModule, FILE_CODE ) )
122 0 : return FALSE;
123 :
124 0 : EstablishFeatureCount();
125 :
126 0 : return TRUE;
127 : }
128 :
129 : /************************************************************************/
130 : /* GetFeature() */
131 : /************************************************************************/
132 :
133 0 : OGRFeature *TigerTLIDRange::GetFeature( int nRecordId )
134 :
135 : {
136 : char achRecord[OGR_TIGER_RECBUF_LEN];
137 :
138 0 : if( nRecordId < 0 || nRecordId >= nFeatures )
139 : {
140 : CPLError( CE_Failure, CPLE_FileIO,
141 : "Request for out-of-range feature %d of %sR",
142 0 : nRecordId, pszModule );
143 0 : return NULL;
144 : }
145 :
146 : /* -------------------------------------------------------------------- */
147 : /* Read the raw record data from the file. */
148 : /* -------------------------------------------------------------------- */
149 0 : if( fpPrimary == NULL )
150 0 : return NULL;
151 :
152 0 : if( VSIFSeek( fpPrimary, nRecordId * nRecordLength, SEEK_SET ) != 0 )
153 : {
154 : CPLError( CE_Failure, CPLE_FileIO,
155 : "Failed to seek to %d of %sR",
156 0 : nRecordId * nRecordLength, pszModule );
157 0 : return NULL;
158 : }
159 :
160 0 : if( VSIFRead( achRecord, psRTRInfo->nRecordLength, 1, fpPrimary ) != 1 )
161 : {
162 : CPLError( CE_Failure, CPLE_FileIO,
163 : "Failed to read record %d of %sR",
164 0 : nRecordId, pszModule );
165 0 : return NULL;
166 : }
167 :
168 : /* -------------------------------------------------------------------- */
169 : /* Set fields. */
170 : /* -------------------------------------------------------------------- */
171 :
172 0 : OGRFeature *poFeature = new OGRFeature( poFeatureDefn );
173 :
174 0 : SetFields( psRTRInfo, poFeature, achRecord );
175 :
176 0 : return poFeature;
177 : }
178 :
179 : /************************************************************************/
180 : /* CreateFeature() */
181 : /************************************************************************/
182 :
183 0 : OGRErr TigerTLIDRange::CreateFeature( OGRFeature *poFeature )
184 :
185 : {
186 : char szRecord[OGR_TIGER_RECBUF_LEN];
187 :
188 0 : if( !SetWriteModule( FILE_CODE, psRTRInfo->nRecordLength+2, poFeature ) )
189 0 : return OGRERR_FAILURE;
190 :
191 0 : memset( szRecord, ' ', psRTRInfo->nRecordLength );
192 :
193 0 : WriteFields( psRTRInfo, poFeature, szRecord );
194 :
195 0 : WriteRecord( szRecord, psRTRInfo->nRecordLength, FILE_CODE );
196 :
197 0 : return OGRERR_NONE;
198 : }
|