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