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