1 : /******************************************************************************
2 : * $Id: tigerpolygoneconomic.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: TIGER/Line Translator
5 : * Purpose: Implements TigerPolygonEconomic, providing access to .RTE files.
6 : * Author: Mark Phillips, mbp@geomtech.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2002, Frank Warmerdam, Mark Phillips
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: tigerpolygoneconomic.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
34 :
35 : #define FILE_CODE "E"
36 :
37 : /* I think this was the expected RTE format, but was never deployed, leaving
38 : it in the code in case I am missing something.
39 :
40 : static TigerFieldInfo rtE_fields[] = {
41 : // fieldname fmt type OFTType beg end len bDefine bSet bWrite
42 : { "MODULE", ' ', ' ', OFTString, 0, 0, 8, 1, 0, 0 },
43 : { "FILE", 'L', 'N', OFTInteger, 6, 10, 5, 1, 1, 1 },
44 : { "CENID", 'L', 'A', OFTString, 11, 15, 5, 1, 1, 1 },
45 : { "POLYID", 'R', 'N', OFTInteger, 16, 25, 10, 1, 1, 1 },
46 : { "STATEEC", 'L', 'N', OFTInteger, 26, 27, 2, 1, 1, 1 },
47 : { "COUNTYEC", 'L', 'N', OFTInteger, 28, 30, 3, 1, 1, 1 },
48 : { "CONCITEC", 'L', 'N', OFTInteger, 31, 35, 5, 1, 1, 1 },
49 : { "COUSUBEC", 'L', 'N', OFTInteger, 36, 40, 5, 1, 1, 1 },
50 : { "PLACEEC", 'L', 'N', OFTInteger, 41, 45, 5, 1, 1, 1 },
51 : { "AIANHHFPEC", 'L', 'N', OFTInteger, 46, 50, 5, 1, 1, 1 },
52 : { "AIANHHEC", 'L', 'N', OFTInteger, 51, 54, 4, 1, 1, 1 },
53 : { "AIAHHTLIEC", 'L', 'A', OFTString, 55, 55, 1, 1, 1, 1 },
54 : { "RS_E1", 'L', 'A', OFTString, 56, 73, 18, 1, 1, 1 }
55 : };
56 : */
57 :
58 : static TigerFieldInfo rtE_fields[] = {
59 : // fieldname fmt type OFTType beg end len bDefine bSet bWrite
60 : { "MODULE", ' ', ' ', OFTString, 0, 0, 8, 1, 0, 0 },
61 : { "FILE", 'L', 'N', OFTInteger, 6, 10, 5, 1, 1, 1 },
62 : { "CENID", 'L', 'A', OFTString, 11, 15, 5, 1, 1, 1 },
63 : { "POLYID", 'R', 'N', OFTInteger, 16, 25, 10, 1, 1, 1 },
64 : { "STATEEC", 'L', 'N', OFTInteger, 26, 27, 2, 1, 1, 1 },
65 : { "COUNTYEC", 'L', 'N', OFTInteger, 28, 30, 3, 1, 1, 1 },
66 : { "RS_E1", 'L', 'A', OFTString, 31, 35, 5, 1, 1, 1 },
67 : { "RS_E2", 'L', 'A', OFTString, 36, 40, 5, 1, 1, 1 },
68 : { "PLACEEC", 'L', 'N', OFTInteger, 41, 45, 5, 1, 1, 1 },
69 : { "RS-E3", 'L', 'A', OFTString, 46, 50, 5, 1, 1, 1 },
70 : { "RS-E4", 'L', 'A', OFTString, 51, 54, 4, 1, 1, 1 },
71 : { "RS-E5", 'L', 'A', OFTString, 55, 55, 1, 1, 1, 1 },
72 : { "COMMREGEC", 'L', 'N', OFTInteger, 56, 56, 1, 1, 1, 1 },
73 : { "RS_E6", 'L', 'A', OFTString, 57, 73, 17, 1, 1, 1 }
74 : };
75 : static TigerRecordInfo rtE_info =
76 : {
77 : rtE_fields,
78 : sizeof(rtE_fields) / sizeof(TigerFieldInfo),
79 : 73
80 : };
81 :
82 : /************************************************************************/
83 : /* TigerPolygonEconomic() */
84 : /************************************************************************/
85 :
86 : TigerPolygonEconomic::TigerPolygonEconomic( OGRTigerDataSource * poDSIn,
87 0 : const char * pszPrototypeModule )
88 :
89 : {
90 0 : OGRFieldDefn oField("",OFTInteger);
91 :
92 0 : poDS = poDSIn;
93 0 : poFeatureDefn = new OGRFeatureDefn( "PolygonEconomic" );
94 0 : poFeatureDefn->Reference();
95 0 : poFeatureDefn->SetGeomType( wkbNone );
96 :
97 0 : psRTEInfo = &rtE_info;
98 :
99 : /* -------------------------------------------------------------------- */
100 : /* Fields from type E record. */
101 : /* -------------------------------------------------------------------- */
102 :
103 0 : AddFieldDefns( psRTEInfo, poFeatureDefn );
104 0 : }
105 :
106 : /************************************************************************/
107 : /* ~TigerPolygonEconomic() */
108 : /************************************************************************/
109 :
110 0 : TigerPolygonEconomic::~TigerPolygonEconomic()
111 :
112 : {
113 0 : }
114 :
115 : /************************************************************************/
116 : /* SetModule() */
117 : /************************************************************************/
118 :
119 0 : int TigerPolygonEconomic::SetModule( const char * pszModule )
120 :
121 : {
122 0 : if( !OpenFile( pszModule, FILE_CODE ) )
123 0 : return FALSE;
124 :
125 0 : EstablishFeatureCount();
126 :
127 0 : return TRUE;
128 : }
129 :
130 : /************************************************************************/
131 : /* GetFeature() */
132 : /************************************************************************/
133 :
134 0 : OGRFeature *TigerPolygonEconomic::GetFeature( int nRecordId )
135 :
136 : {
137 : char achRecord[OGR_TIGER_RECBUF_LEN];
138 :
139 0 : if( nRecordId < 0 || nRecordId >= nFeatures )
140 : {
141 : CPLError( CE_Failure, CPLE_FileIO,
142 : "Request for out-of-range feature %d of %sZ",
143 0 : nRecordId, pszModule );
144 0 : return NULL;
145 : }
146 :
147 : /* -------------------------------------------------------------------- */
148 : /* Read the raw record data from the file. */
149 : /* -------------------------------------------------------------------- */
150 0 : if( fpPrimary == NULL )
151 0 : return NULL;
152 :
153 0 : if( VSIFSeek( fpPrimary, nRecordId * nRecordLength, SEEK_SET ) != 0 )
154 : {
155 : CPLError( CE_Failure, CPLE_FileIO,
156 : "Failed to seek to %d of %sZ",
157 0 : nRecordId * nRecordLength, pszModule );
158 0 : return NULL;
159 : }
160 :
161 0 : if( VSIFRead( achRecord, psRTEInfo->nRecordLength, 1, fpPrimary ) != 1 )
162 : {
163 : CPLError( CE_Failure, CPLE_FileIO,
164 : "Failed to read record %d of %sZ",
165 0 : nRecordId, pszModule );
166 0 : return NULL;
167 : }
168 :
169 : /* -------------------------------------------------------------------- */
170 : /* Set fields. */
171 : /* -------------------------------------------------------------------- */
172 0 : OGRFeature *poFeature = new OGRFeature( poFeatureDefn );
173 :
174 0 : SetFields( psRTEInfo, poFeature, achRecord );
175 :
176 0 : return poFeature;
177 : }
178 :
179 : /************************************************************************/
180 : /* CreateFeature() */
181 : /************************************************************************/
182 :
183 0 : OGRErr TigerPolygonEconomic::CreateFeature( OGRFeature *poFeature )
184 :
185 : {
186 : char szRecord[OGR_TIGER_RECBUF_LEN];
187 :
188 0 : if( !SetWriteModule( FILE_CODE, psRTEInfo->nRecordLength+2, poFeature ) )
189 0 : return OGRERR_FAILURE;
190 :
191 0 : memset( szRecord, ' ', psRTEInfo->nRecordLength );
192 :
193 0 : WriteFields( psRTEInfo, poFeature, szRecord);
194 :
195 0 : WriteRecord( szRecord, psRTEInfo->nRecordLength, FILE_CODE );
196 :
197 0 : return OGRERR_NONE;
198 : }
|