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