1 : /*-*-C++-*-*/
2 : /******************************************************************************
3 : * $Id: ogr_tiger.h 23423 2011-11-26 18:40:30Z rouault $
4 : *
5 : * Project: TIGER/Line Translator
6 : * Purpose: Main declarations for Tiger translator.
7 : * Author: Frank Warmerdam, warmerdam@pobox.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 1999, Frank Warmerdam
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #ifndef _OGR_TIGER_H_INCLUDED
32 : #define _OGR_TIGER_H_INCLUDED
33 :
34 : #include "cpl_conv.h"
35 : #include "ogrsf_frmts.h"
36 :
37 : class OGRTigerDataSource;
38 :
39 : /*
40 : ** TIGER Versions
41 : **
42 : ** 0000 TIGER/Line Precensus Files, 1990
43 : ** 0002 TIGER/Line Initial Voting District Codes Files, 1990
44 : ** 0003 TIGER/Line Files, 1990
45 : ** 0005 TIGER/Line Files, 1992
46 : ** 0021 TIGER/Line Files, 1994
47 : ** 0024 TIGER/Line Files, 1995
48 : ** 0697 to 1098 TIGER/Line Files, 1997
49 : ** 1298 to 0499 TIGER/Line Files, 1998
50 : ** 0600 to 0800 TIGER/Line Files, 1999
51 : ** 1000 to 1100 TIGER/Line Files, Redistricting Census 2000
52 : ** 0301 to 0801 TIGER/Line Files, Census 2000
53 : **
54 : ** 0302 to 0502 TIGER/Line Files, UA 2000
55 : ** ???? ????
56 : **
57 : ** 0602 & higher TIGER/Line Files, 2002
58 : ** ???? ????
59 : */
60 :
61 : typedef enum {
62 : TIGER_1990_Precensus = 0,
63 : TIGER_1990 = 1,
64 : TIGER_1992 = 2,
65 : TIGER_1994 = 3,
66 : TIGER_1995 = 4,
67 : TIGER_1997 = 5,
68 : TIGER_1998 = 6,
69 : TIGER_1999 = 7,
70 : TIGER_2000_Redistricting = 8,
71 : TIGER_2000_Census = 9,
72 : TIGER_UA2000 = 10,
73 : TIGER_2002 = 11,
74 : TIGER_2003 = 12,
75 : TIGER_2004 = 13,
76 : TIGER_Unknown
77 : } TigerVersion;
78 :
79 : TigerVersion TigerClassifyVersion( int );
80 : const char * TigerVersionString( TigerVersion );
81 :
82 : /*****************************************************************************/
83 : /* The TigerFieldInfo and TigerRecordInfo structures hold information about */
84 : /* the schema of a TIGER record type. In each layer implementation file */
85 : /* there are statically initalized variables of these types that describe */
86 : /* the record types associated with that layer. In the case where different */
87 : /* TIGER versions have different schemas, there is a */
88 : /* TigerFieldInfo/TigerRecordInfo for each version, and the constructor */
89 : /* for the layer chooses a pointer to the correct set based on the version. */
90 : /*****************************************************************************/
91 :
92 : typedef struct TigerFieldInfo {
93 : char pszFieldName[11]; // name of the field
94 : char cFmt; // format of the field ('L' or 'R')
95 : char cType; // type of the field ('A' or 'N')
96 : char OGRtype; // OFTType of the field (OFTInteger, OFTString, ...?)
97 : unsigned char nBeg; // beginning column number for field
98 : unsigned char nEnd; // ending column number for field
99 : unsigned char nLen; // length of field
100 :
101 : int bDefine:1; // whether to add this field to the FeatureDefn
102 : int bSet:1; // whether to set this field in GetFeature()
103 : int bWrite:1; // whether to write this field in CreateFeature()
104 : } TigerFieldInfo;
105 :
106 : typedef struct TigerRecordInfo {
107 : const TigerFieldInfo *pasFields;
108 : unsigned char nFieldCount;
109 : unsigned char nRecordLength;
110 : } TigerRecordInfo;
111 :
112 : // OGR_TIGER_RECBUF_LEN should be a number that is larger than the
113 : // longest possible record length for any record type; it's used to
114 : // create arrays to hold the records. At the time of this writing the
115 : // longest record (RT1) has length 228, but I'm choosing 500 because
116 : // it's a good round number and will allow for growth without having
117 : // to modify this file. The code never holds more than a few records
118 : // in memory at a time, so having OGR_TIGER_RECBUF_LEN be much larger
119 : // than is really necessary won't affect the amount of memory required
120 : // in a substantial way.
121 : // mbp Fri Dec 20 19:19:59 2002
122 : #define OGR_TIGER_RECBUF_LEN 500
123 :
124 : /************************************************************************/
125 : /* TigerFileBase */
126 : /************************************************************************/
127 :
128 : class TigerFileBase
129 : {
130 : protected:
131 : OGRTigerDataSource *poDS;
132 :
133 : char *pszModule;
134 : char *pszShortModule;
135 : FILE *fpPrimary;
136 :
137 : OGRFeatureDefn *poFeatureDefn;
138 :
139 : int nFeatures;
140 : int nRecordLength;
141 :
142 : int OpenFile( const char *, const char * );
143 : void EstablishFeatureCount();
144 :
145 : static int EstablishRecordLength( FILE * );
146 :
147 : void SetupVersion();
148 :
149 : int nVersionCode;
150 : TigerVersion nVersion;
151 :
152 : public:
153 : TigerFileBase( const TigerRecordInfo *psRTInfoIn = NULL,
154 : const char *m_pszFileCodeIn = NULL );
155 : virtual ~TigerFileBase();
156 :
157 : TigerVersion GetVersion() { return nVersion; }
158 : int GetVersionCode() { return nVersionCode; }
159 :
160 868994 : virtual const char *GetShortModule() { return pszShortModule; }
161 0 : virtual const char *GetModule() { return pszModule; }
162 : virtual int SetWriteModule( const char *, int, OGRFeature * );
163 :
164 212314 : virtual int GetFeatureCount() { return nFeatures; }
165 :
166 126811 : OGRFeatureDefn *GetFeatureDefn() { return poFeatureDefn; }
167 :
168 : static const char* GetField( const char *, int, int );
169 : static void SetField( OGRFeature *, const char *, const char *,
170 : int, int );
171 :
172 : int WriteField( OGRFeature *, const char *, char *,
173 : int, int, char, char );
174 : int WriteRecord( char *pachRecord, int nRecLen,
175 : const char *pszType, FILE *fp = NULL );
176 : int WritePoint( char *pachRecord, int nStart,
177 : double dfX, double dfY );
178 :
179 : virtual int SetModule( const char * pszModule );
180 : virtual OGRFeature *GetFeature( int nRecordId );
181 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
182 :
183 : protected:
184 : void WriteFields(const TigerRecordInfo *psRTInfo,
185 : OGRFeature *poFeature,
186 : char *szRecord);
187 :
188 : void AddFieldDefns(const TigerRecordInfo *psRTInfo,
189 : OGRFeatureDefn *poFeatureDefn);
190 :
191 :
192 : void SetFields(const TigerRecordInfo *psRTInfo,
193 : OGRFeature *poFeature,
194 : char *achRecord);
195 :
196 :
197 : const TigerRecordInfo *psRTInfo;
198 : const char *m_pszFileCode;
199 : };
200 :
201 : /************************************************************************/
202 : /* TigerCompleteChain */
203 : /************************************************************************/
204 :
205 : class TigerCompleteChain : public TigerFileBase
206 : {
207 : FILE *fpShape;
208 : int *panShapeRecordId;
209 :
210 : FILE *fpRT3;
211 : int bUsingRT3;
212 : int nRT1RecOffset;
213 :
214 : int GetShapeRecordId( int, int );
215 : int AddShapePoints( int, int, OGRLineString *, int );
216 :
217 : void AddFieldDefnsPre2002();
218 : OGRFeature *GetFeaturePre2002( int );
219 : OGRErr WriteRecordsPre2002( OGRFeature *, OGRLineString * );
220 :
221 : OGRErr WriteRecords2002( OGRFeature *, OGRLineString * );
222 : OGRFeature *GetFeature2002( int );
223 : void AddFieldDefns2002();
224 :
225 : const TigerRecordInfo *psRT1Info;
226 : const TigerRecordInfo *psRT2Info;
227 : const TigerRecordInfo *psRT3Info;
228 :
229 : public:
230 : TigerCompleteChain( OGRTigerDataSource *,
231 : const char * );
232 : virtual ~TigerCompleteChain();
233 :
234 : virtual int SetModule( const char * );
235 :
236 : virtual OGRFeature *GetFeature( int );
237 :
238 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
239 :
240 : virtual int SetWriteModule( const char *, int, OGRFeature * );
241 : };
242 :
243 : /************************************************************************/
244 : /* TigerAltName (Type 4 records) */
245 : /************************************************************************/
246 :
247 : class TigerAltName : public TigerFileBase
248 7 : {
249 : public:
250 : TigerAltName( OGRTigerDataSource *,
251 : const char * );
252 :
253 : virtual OGRFeature *GetFeature( int );
254 :
255 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
256 : };
257 :
258 : /************************************************************************/
259 : /* TigerFeatureIds (Type 5 records) */
260 : /************************************************************************/
261 :
262 : class TigerFeatureIds : public TigerFileBase
263 7 : {
264 : public:
265 : TigerFeatureIds( OGRTigerDataSource *,
266 : const char * );
267 : };
268 :
269 : /************************************************************************/
270 : /* TigerZipCodes (Type 6 records) */
271 : /************************************************************************/
272 :
273 : class TigerZipCodes : public TigerFileBase
274 7 : {
275 : public:
276 : TigerZipCodes( OGRTigerDataSource *, const char * );
277 : };
278 :
279 : /************************************************************************/
280 : /* TigerPoint */
281 : /* This is an abstract base class for TIGER layers with point geometry. */
282 : /* Since much of the implementation of these layers is similar, I've */
283 : /* put it into this base class to avoid duplication in the actual */
284 : /* layer classes. mbp Sat Jan 4 16:41:19 2003. */
285 : /************************************************************************/
286 :
287 : class TigerPoint : public TigerFileBase
288 21 : {
289 : protected:
290 : TigerPoint(int bRequireGeom,
291 : const TigerRecordInfo *psRTInfoIn = NULL,
292 : const char *m_pszFileCodeIn = NULL);
293 :
294 : // The boolean bRequireGeom indicates whether
295 : // the layer requires each feature to actual
296 : // have a geom. It's used in CreateFeature() to
297 : // decide whether to report an error when a
298 : // missing geom is detected.
299 :
300 : private:
301 : int bRequireGeom;
302 :
303 : public:
304 0 : virtual OGRFeature *GetFeature( int nFID) { return TigerFileBase::GetFeature(nFID); } /* to avoid -Woverloaded-virtual warnings */
305 : OGRFeature *GetFeature( int nRecordId,
306 : int nX0, int nX1,
307 : int nY0, int nY1 );
308 :
309 :
310 0 : virtual OGRErr CreateFeature( OGRFeature *poFeature) { return TigerFileBase::CreateFeature(poFeature); } /* to avoid -Woverloaded-virtual warnings */
311 : OGRErr CreateFeature( OGRFeature *poFeature,
312 : int nIndex );
313 :
314 : };
315 :
316 : /************************************************************************/
317 : /* TigerLandmarks (Type 7 records) */
318 : /************************************************************************/
319 :
320 : class TigerLandmarks : public TigerPoint
321 7 : {
322 : public:
323 : TigerLandmarks( OGRTigerDataSource *, const char * );
324 :
325 : virtual OGRFeature *GetFeature( int );
326 :
327 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
328 : };
329 :
330 : /************************************************************************/
331 : /* TigerAreaLandmarks (Type 8 records) */
332 : /************************************************************************/
333 :
334 : class TigerAreaLandmarks : public TigerFileBase
335 7 : {
336 : public:
337 : TigerAreaLandmarks( OGRTigerDataSource *, const char * );
338 : };
339 :
340 : /************************************************************************/
341 : /* TigerKeyFeatures (Type 9 records) */
342 : /************************************************************************/
343 :
344 : class TigerKeyFeatures : public TigerFileBase
345 0 : {
346 : public:
347 : TigerKeyFeatures( OGRTigerDataSource *, const char * );
348 : };
349 :
350 : /************************************************************************/
351 : /* TigerPolygon (Type A&S records) */
352 : /************************************************************************/
353 :
354 : class TigerPolygon : public TigerFileBase
355 : {
356 : private:
357 : const TigerRecordInfo *psRTAInfo;
358 : const TigerRecordInfo *psRTSInfo;
359 :
360 : FILE *fpRTS;
361 : int bUsingRTS;
362 : int nRTSRecLen;
363 :
364 : public:
365 : TigerPolygon( OGRTigerDataSource *, const char * );
366 : virtual ~TigerPolygon();
367 :
368 : virtual int SetModule( const char * );
369 :
370 : virtual OGRFeature *GetFeature( int );
371 :
372 : virtual int SetWriteModule( const char *, int, OGRFeature * );
373 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
374 : };
375 :
376 : /************************************************************************/
377 : /* TigerPolygonCorrections (Type B records) */
378 : /************************************************************************/
379 :
380 : class TigerPolygonCorrections : public TigerFileBase
381 7 : {
382 : public:
383 : TigerPolygonCorrections( OGRTigerDataSource *, const char * );
384 : };
385 :
386 : /************************************************************************/
387 : /* TigerEntityNames (Type C records) */
388 : /************************************************************************/
389 :
390 : class TigerEntityNames : public TigerFileBase
391 7 : {
392 : public:
393 : TigerEntityNames( OGRTigerDataSource *, const char * );
394 : };
395 :
396 : /************************************************************************/
397 : /* TigerPolygonEconomic (Type E records) */
398 : /************************************************************************/
399 :
400 : class TigerPolygonEconomic : public TigerFileBase
401 7 : {
402 : public:
403 : TigerPolygonEconomic( OGRTigerDataSource *, const char * );
404 :
405 : };
406 :
407 : /************************************************************************/
408 : /* TigerIDHistory (Type H records) */
409 : /************************************************************************/
410 :
411 : class TigerIDHistory : public TigerFileBase
412 7 : {
413 : public:
414 : TigerIDHistory( OGRTigerDataSource *, const char * );
415 : };
416 :
417 : /************************************************************************/
418 : /* TigerPolyChainLink (Type I records) */
419 : /************************************************************************/
420 :
421 : class TigerPolyChainLink : public TigerFileBase
422 7 : {
423 : public:
424 : TigerPolyChainLink( OGRTigerDataSource *, const char * );
425 : };
426 :
427 : /************************************************************************/
428 : /* TigerSpatialMetadata (Type M records) */
429 : /************************************************************************/
430 :
431 : class TigerSpatialMetadata : public TigerFileBase
432 7 : {
433 : public:
434 : TigerSpatialMetadata( OGRTigerDataSource *, const char * );
435 : };
436 :
437 : /************************************************************************/
438 : /* TigerPIP (Type P records) */
439 : /************************************************************************/
440 :
441 : class TigerPIP : public TigerPoint
442 7 : {
443 : public:
444 : TigerPIP( OGRTigerDataSource *, const char * );
445 :
446 : virtual OGRFeature *GetFeature( int );
447 :
448 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
449 : };
450 :
451 : /************************************************************************/
452 : /* TigerTLIDRange (Type R records) */
453 : /************************************************************************/
454 :
455 : class TigerTLIDRange : public TigerFileBase
456 7 : {
457 : public:
458 : TigerTLIDRange( OGRTigerDataSource *, const char * );
459 : };
460 :
461 : /************************************************************************/
462 : /* TigerZeroCellID (Type T records) */
463 : /************************************************************************/
464 :
465 : class TigerZeroCellID : public TigerFileBase
466 7 : {
467 : public:
468 : TigerZeroCellID( OGRTigerDataSource *, const char * );
469 : };
470 :
471 : /************************************************************************/
472 : /* TigerOverUnder (Type U records) */
473 : /************************************************************************/
474 :
475 : class TigerOverUnder : public TigerPoint
476 7 : {
477 : public:
478 : TigerOverUnder( OGRTigerDataSource *, const char * );
479 :
480 : virtual OGRFeature *GetFeature( int );
481 :
482 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
483 : };
484 :
485 : /************************************************************************/
486 : /* TigerZipPlus4 (Type Z records) */
487 : /************************************************************************/
488 :
489 : class TigerZipPlus4 : public TigerFileBase
490 7 : {
491 : public:
492 : TigerZipPlus4( OGRTigerDataSource *, const char * );
493 : };
494 :
495 : /************************************************************************/
496 : /* OGRTigerLayer */
497 : /************************************************************************/
498 :
499 : class OGRTigerLayer : public OGRLayer
500 : {
501 : TigerFileBase *poReader;
502 :
503 : OGRTigerDataSource *poDS;
504 :
505 : int nFeatureCount;
506 : int *panModuleFCount;
507 : int *panModuleOffset;
508 :
509 : int iLastFeatureId;
510 : int iLastModule;
511 :
512 : public:
513 : OGRTigerLayer( OGRTigerDataSource * poDS,
514 : TigerFileBase * );
515 : virtual ~OGRTigerLayer();
516 :
517 : void ResetReading();
518 : OGRFeature * GetNextFeature();
519 : OGRFeature *GetFeature( long nFeatureId );
520 :
521 : OGRFeatureDefn * GetLayerDefn();
522 :
523 : int GetFeatureCount( int );
524 :
525 : int TestCapability( const char * );
526 :
527 : virtual OGRSpatialReference *GetSpatialRef();
528 :
529 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
530 : virtual OGRErr CreateField( OGRFieldDefn *poField,
531 : int bApproxOK = TRUE );
532 : };
533 :
534 : /************************************************************************/
535 : /* OGRTigerDataSource */
536 : /************************************************************************/
537 :
538 : class OGRTigerDataSource : public OGRDataSource
539 : {
540 : char *pszName;
541 :
542 : int nLayers;
543 : OGRTigerLayer **papoLayers;
544 :
545 : OGRSpatialReference *poSpatialRef;
546 :
547 : char **papszOptions;
548 :
549 : char *pszPath;
550 :
551 : int nModules;
552 : char **papszModules;
553 :
554 : int nVersionCode;
555 : TigerVersion nVersion;
556 :
557 : int bWriteMode;
558 :
559 : TigerVersion TigerCheckVersion( TigerVersion, const char * );
560 :
561 : public:
562 : OGRTigerDataSource();
563 : ~OGRTigerDataSource();
564 :
565 144 : int GetWriteMode() { return bWriteMode; }
566 :
567 160162 : TigerVersion GetVersion() { return nVersion; }
568 160092 : int GetVersionCode() { return nVersionCode; }
569 :
570 : void SetOptionList( char ** );
571 : const char *GetOption( const char * );
572 :
573 : int Open( const char * pszName, int bTestOpen = FALSE,
574 : char ** papszFileList = NULL );
575 :
576 : int Create( const char *pszName, char **papszOptions );
577 :
578 2 : const char *GetName() { return pszName; }
579 : int GetLayerCount();
580 : OGRLayer *GetLayer( int );
581 : OGRLayer *GetLayer( const char *pszLayerName );
582 :
583 : void AddLayer( OGRTigerLayer * );
584 : int TestCapability( const char * );
585 :
586 262708 : OGRSpatialReference *GetSpatialRef() { return poSpatialRef; }
587 :
588 73477 : const char *GetDirPath() { return pszPath; }
589 : char *BuildFilename( const char * pszModule,
590 : const char * pszExtension );
591 :
592 :
593 723 : int GetModuleCount() { return nModules; }
594 : const char *GetModule( int );
595 : int CheckModule( const char *pszModule );
596 : void AddModule( const char *pszModule );
597 :
598 : void DeleteModuleFiles( const char *pszModule );
599 :
600 : virtual OGRLayer *CreateLayer( const char *,
601 : OGRSpatialReference * = NULL,
602 : OGRwkbGeometryType = wkbUnknown,
603 : char ** = NULL );
604 : };
605 :
606 : /************************************************************************/
607 : /* OGRTigerDriver */
608 : /************************************************************************/
609 :
610 : class OGRTigerDriver : public OGRSFDriver
611 178 : {
612 : public:
613 : ~OGRTigerDriver();
614 :
615 : const char *GetName();
616 :
617 : OGRDataSource *Open( const char *, int );
618 :
619 : virtual OGRDataSource *CreateDataSource( const char *pszName,
620 : char ** = NULL );
621 :
622 : int TestCapability( const char * );
623 : };
624 :
625 : #endif /* ndef _OGR_TIGER_H_INCLUDED */
|